Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions lib/openscap_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'openscap_parser/profiles'
require 'openscap_parser/rule'
require 'openscap_parser/rule_result'
require 'openscap_parser/rule_results'
require 'openscap_parser/rules'
require 'openscap_parser/version'
require 'openscap_parser/xml_report'
Expand All @@ -16,6 +17,7 @@ class Base
include OpenscapParser::XMLReport
include OpenscapParser::Profiles
include OpenscapParser::Rules
include OpenscapParser::RuleResults

def initialize(report)
report_xml(report)
Expand All @@ -32,14 +34,5 @@ def start_time
def end_time
@end_time ||= DateTime.parse(test_result_node['end-time'])
end

def rule_results
@rule_results ||= test_result_node.search('rule-result').map do |rr|
rule_result_oscap = RuleResult.new
rule_result_oscap.id = rr.attributes['idref'].value
rule_result_oscap.result = rr.search('result').first.text
rule_result_oscap
end
end
end
end
7 changes: 3 additions & 4 deletions lib/openscap_parser/profiles.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# frozen_string_literal: true
require 'openscap_parser/test_result'

module OpenscapParser
# Methods related to saving profiles and finding which hosts
# they belong to
module Profiles
include TestResult

def self.included(base)
base.class_eval do
def profiles
Expand All @@ -18,10 +21,6 @@ def profile_node
@report_xml.at_xpath(".//Profile\
[contains('#{test_result_node['id']}', @id)]")
end

def test_result_node
@test_result_node ||= @report_xml.at_css('TestResult')
end
end
end
end
Expand Down
17 changes: 13 additions & 4 deletions lib/openscap_parser/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ def title
end

def description
@description ||= @rule_xml.at_css('description').text.delete("\n")
rule_node ||= @rule_xml.at_css('description')
@description ||= newline_to_whitespace(rule_node.text) if rule_node && rule_node.text
end

def rationale
@rationale ||= @rule_xml.at_css('rationale').children.text.delete("\n")
rationale_node ||= @rule_xml.at_css('rationale')
@rationale ||= newline_to_whitespace(rationale_node.children.text) if rationale_node &&
rationale_node.children &&
rationale_node.children.text
end

def references
Expand All @@ -35,10 +39,15 @@ def references

def identifier
@identifier ||= {
label: @rule_xml.at_css('ident')&.text,
label: @rule_xml.at_css('ident') && @rule_xml.at_css('ident').text,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change? You don't like the new &. syntax?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps the proxy has to run on an older version of ruby...?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah - I'm not blocking this PR on it, just curious.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly, proxies may have older versions of ruby.

system: (ident = @rule_xml.at_css('ident')) && ident['system']
}
end

private

def newline_to_whitespace(string)
string.gsub(/ *\n+/, " ").strip
end
end
end

21 changes: 21 additions & 0 deletions lib/openscap_parser/rule_results.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true
require 'openscap_parser/test_result'

module OpenscapParser
module RuleResults
include TestResult

def self.included(base)
base.class_eval do
def rule_results
@rule_results ||= test_result_node.search('rule-result').map do |rr|
rule_result_oscap = RuleResult.new
rule_result_oscap.id = rr.attributes['idref'].value
rule_result_oscap.result = rr.search('result').first.text
rule_result_oscap
end
end
end
end
end
end
15 changes: 15 additions & 0 deletions lib/openscap_parser/test_result.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module OpenscapParser
Comment thread
akofink marked this conversation as resolved.
module TestResult
def self.included(base)
base.class_eval do
private

def test_result_node
@test_result_node ||= @report_xml.at_css('TestResult')
end
end
end
end
end
1 change: 1 addition & 0 deletions openscap_parser.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "minitest", "~> 5.0"
spec.add_development_dependency "shoulda-context"
spec.add_development_dependency "pry"
spec.add_development_dependency "pry-byebug"
end
18 changes: 18 additions & 0 deletions test/openscap_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,23 @@ def setup

assert_equal references, rule.references
end

should 'parse rule description without newlines' do
rule = @report_parser.rule_objects.find do |rule_obj|
rule_obj.id == 'xccdf_org.ssgproject.content_rule_service_atd_disabled'
end

desc = <<~DESC.gsub("\n", ' ').strip
The at and batch commands can be used to
schedule tasks that are meant to be executed only once. This allows delayed
execution in a manner similar to cron, except that it is not
recurring. The daemon atd keeps track of tasks scheduled via
at and batch, and executes them at the specified time.
The atd service can be disabled with the following command:
$ sudo systemctl disable atd.service
DESC

assert_equal desc, rule.description
end
end
end