Im working on a project that requires the comparison of values within two hashes. The hash key 'title' has an array as its value, with the title of two issues within it. What I intend to achieve, is to say 'if the title in issue_yaml exists in fwparse_issues, keep the title in issue_yaml (the array inside the hash), else delete it'.
Example code:
fwparse_issues = [
{:section_title=>"Security Audit",
:ref=>"FILTER.LOG.DROP",
:title=>"Filter Drop Rules Were Configured Without Logging"},
{:section_title=>"Security Audit",
:ref=>"LOGGING.SYSLOG.NO.ENCRYPTION",
:title=>"Syslog Logging Configured With No Encryption"},
{:section_title=>"Security Audit",
:ref=>"FILTER.RULE.EENE",
:title=>"Filter Rules Allow Packets To A Network Destination"},
{:section_title=>"Security Audit",
:ref=>"FILTER.RULE.EEER",
:title=>"Filter Rules Allow Packets To A Port Range"},
{:section_title=>"Security Audit",
:ref=>"BANNER.NO.POST.LOGON.MESSAGE",
:title=>"No Post Logon Banner Message"},
{:section_title=>"Security Audit",
:ref=>"LOGGING.SYSLOG.SEVERITY",
:title=>"Weak Syslog Severity Level Configured"},
{:section_title=>"Security Audit",
:ref=>"FILTER.RULE.NEEE",
:title=>"Filter Rules Allow Packets From A Network Source"}]
issue_yaml = {"ABC-1234"=>
{"title"=>["No Pre-Logon Banner Message", "No Post Logon Banner Message"],
"desc"=>"some text",
"rec"=>"recommendations go here",
"ref"=>"references"},
"ABC-5678"=>
{"title"=>"SSH Protocol Version 1 Supported",
"desc"=>"some text\nwhich spans\nmultiple lines\n",
"rec"=>"recommendations go here",
"ref"=>"references"}}
fwparse_issues.each do |issue|
issue_yaml.keys.each do |key|
if issue_yaml[key]["title"].is_a?(Array)
unless issue_yaml[key]["title"].include?(issue[:title])
issue_yaml[key]["title"].delete(issue[:title])
end
end
end
end
What I needed to end up with was:
{"ABC-1234"=>
**{"title"=>["No Post Logon Banner Message"],**
"desc"=>"some text",
"rec"=>"recommendations go here",
"ref"=>"references"}}
But instead the bold line ends up being:
{"ABC-1234"=>
{"title"=>["No Pre-Logon Banner Message", "No Post Logon Banner Message"],
"desc"=>"some text",
"rec"=>"recommendations go here",
"ref"=>"references"},
In essence, the unless bit isn't working. One of those situations where I have spent so long looking at it I can't even think straight anymore. If I change the 'unless' to an 'if' it deletes "No Post Logon Banner Message" from issue_yaml so the reverse seems to work!
EDIT Corrected the expected output!
SSH Protocol Version 1 Supportedstill remain if it doesn't exist in fwparse_issues? - Ho Man