【发布时间】:2017-07-15 15:17:24
【问题描述】:
我正在做一个需要比较两个哈希值的项目。哈希键 'title' 有一个数组作为其值,其中包含两个问题的标题。 我打算实现的是说'如果 issue_yaml 中的标题存在于 fwparse_issues 中,则将标题保留在 issue_yaml 中(哈希内的数组),否则将其删除。
示例代码:
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
我最终需要的是:
{"ABC-1234"=>
**{"title"=>["No Post Logon Banner Message"],**
"desc"=>"some text",
"rec"=>"recommendations go here",
"ref"=>"references"}}
但粗体线最终变成:
{"ABC-1234"=>
{"title"=>["No Pre-Logon Banner Message", "No Post Logon Banner Message"],
"desc"=>"some text",
"rec"=>"recommendations go here",
"ref"=>"references"},
本质上,除非位不起作用。我花了这么长时间看它的那些情况之一,我什至无法再思考了。如果我将“除非”更改为“如果”,它会从 issue_yaml 中删除“无登录后横幅消息”,因此相反的情况似乎有效!
EDIT更正了预期的输出!
【问题讨论】:
-
如果标题
SSH Protocol Version 1 Supported在 fwparse_issues 中不存在,为什么还会保留它? -
它不会,这就是脚本的目的。它采用用户定义的问题列表,然后在 fwparse_issues 哈希数组中查找它们。所以在这个用例中,SSH 协议版本 1 不会成功,“没有登录前横幅消息”也不会。由用户为这些问题添加映射。对不起,我不清楚:)