Skip to content

Generalize scap parsing#15

Merged
akofink merged 2 commits intoOpenSCAP:masterfrom
akofink:generalize-scap-parsing
Sep 19, 2019
Merged

Generalize scap parsing#15
akofink merged 2 commits intoOpenSCAP:masterfrom
akofink:generalize-scap-parsing

Conversation

@akofink
Copy link
Copy Markdown
Collaborator

@akofink akofink commented Sep 13, 2019

We initially wrote this openscap_parser in the context of parsing SCAP scan results. Now we want to use it for parsing datastreams without results. This PR is only a refactoring to generalize some of the code and move things around to be specific to results parsing or not. Behavior should not change!

}
@report_parser = ::OpenscapParser::Base.new(fake_report)
assert_equal 1, @report_parser.profiles.count
assert_equal 1, @report_parser.test_result_profiles.count
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.

If you want profiles associated with the test result only, you've got to ask for it. :)


def xpath_nodes(xpath)
@parsed_xml.xpath(xpath)
end
Copy link
Copy Markdown
Collaborator Author

@akofink akofink Sep 13, 2019

Choose a reason for hiding this comment

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

These are nice helpers. We could do the same for css_node and css_nodes, but we should be able to do everything with xpath.

@akofink akofink marked this pull request as ready for review September 16, 2019 18:55
@akofink
Copy link
Copy Markdown
Collaborator Author

akofink commented Sep 16, 2019

Notice the two commits to ease reviewing

Comment thread lib/openscap_parser.rb

def end_time
@end_time ||= DateTime.parse(test_result_node['end-time'])
end
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.

Moved to TestResult, as they come from that node

class Profile
attr_acessor :id, :title, :description
def initialize(profile_xml: nil)
@profile_xml = profile_xml
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.

I like this architecture of passing in the Nokogiri::Element like we do for OpenscapParser::Rule objects

# Methods related to saving profiles and finding which hosts
# they belong to
module Profiles
include TestResult
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.

No need for these mixins to be coupled so tightly IMO

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.

👍

base.class_eval do
def profiles
@profiles ||= {
profile_node['id'] => profile_node.at_css('title').text
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.

This method is now test_result_profiles from the TestResult module. It returns the list of (one) profiles that this scan was run against.

@profiles ||= profile_nodes.inject({}) do |profiles, profile_node|
profiles[profile_node['id']] = profile_node.at_css('title').text

private
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.

Nothing in here needs to be private IMO. It's useful to retrieve the raw Nokogiri::NodeSet or an array of OpenscapParser::Profile objects.

Comment thread lib/openscap_parser/rule.rb
Comment thread lib/openscap_parser/rule.rb
Comment thread lib/openscap_parser/rules.rb
Comment thread lib/openscap_parser/test_result.rb
@xprazak2
Copy link
Copy Markdown
Collaborator

Looking good. It will need a rebase and replacing safe navigation operator to be compatible with older version of ruby.

We're using this module for any SCAP xml content, not just reports

Signed-off-by: Andrew Kofink <akofink@redhat.com>
@akofink
Copy link
Copy Markdown
Collaborator Author

akofink commented Sep 17, 2019

Updated to remove the safe navigation operator

Signed-off-by: Andrew Kofink <akofink@redhat.com>
DateTime.parse(test_result_node['end-time'])
end

def test_result_profiles
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.

@bastilian funnily enough, MiniTest::Test only has a run method, but it looks for methods which start with test_. That creates the scenario you predicted would happen! 😝 So I've had to wrap the profiles_test tests in a dummy test class like you suggested.

@akofink
Copy link
Copy Markdown
Collaborator Author

akofink commented Sep 17, 2019

Tests are green now, and all comments addressed

Copy link
Copy Markdown
Collaborator

@xprazak2 xprazak2 left a comment

Choose a reason for hiding this comment

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

Works for me, I think we can merge if there are no additional comments.

@akofink
Copy link
Copy Markdown
Collaborator Author

akofink commented Sep 18, 2019

It's good to merge IMO - it'd be good to test it with any apps that use this gem (I've tested with compliance-backend, and it works fine).

@akofink akofink merged commit 2285213 into OpenSCAP:master Sep 19, 2019
@akofink akofink deleted the generalize-scap-parsing branch September 19, 2019 14:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants