openscap_parser 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5bbc310b2adc402a398f0b9e8524199a91c06e0b74c2f2757a9fdae292c645c5
4
- data.tar.gz: 1d8314602c86e916aff3b19b09d88a858892635beaee738bd79177bf38434abb
3
+ metadata.gz: 159fcf26b6fe71666a9d057d89d772589ac8436d783b7773556121b6b5c11654
4
+ data.tar.gz: f8181135885961c284b2dabfd4de0408d8888e7cf4271da699f0e42fc0e30bf6
5
5
  SHA512:
6
- metadata.gz: f2d316e18f0d82b9597b24b306dbae8615a044648102c085170005d1eaa9737263c11ec6a3c4e8af4e355cc59a9e57f78f560fe46f11c0e2cabfd134220153d7
7
- data.tar.gz: 59f11993820161a4ece0d23df623ea3412cfb90d1895a5dd7a11fd1fa519e8eb3afbd0812c9a4a78ba1d254c74bcb58578f849ec773ace989ca91b07cffa4fab
6
+ metadata.gz: be89da681f6a355591027ce73b0dd3a68bcbd73bcc442a2d4e21c2a51485a9e841d493bafef8757aff5f85025e0a5251d3b1519801b2fdb1343fe070b165fe6b
7
+ data.tar.gz: 8949839744c203ec9074e3069097a3d98739eb65c7a76841c117f456eb02c55c3eee1576cf3b550501012b52f549d8aaaac0f601350d5f92e37a79e2a6602117
@@ -5,6 +5,7 @@ require 'openscap_parser/xml_file'
5
5
  require 'openscap_parser/rules'
6
6
  require 'openscap_parser/profiles'
7
7
  require 'openscap_parser/rule_references'
8
+ require 'openscap_parser/groups'
8
9
 
9
10
  # Mimics openscap-ruby Benchmark interface
10
11
  module OpenscapParser
@@ -13,6 +14,7 @@ module OpenscapParser
13
14
  include OpenscapParser::Rules
14
15
  include OpenscapParser::RuleReferences
15
16
  include OpenscapParser::Profiles
17
+ include OpenscapParser::Groups
16
18
 
17
19
  def id
18
20
  @id ||= @parsed_xml['id']
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+ require 'openscap_parser/xml_node'
3
+ require 'openscap_parser/subs'
4
+
5
+ module OpenscapParser
6
+ class Fix < XmlNode
7
+ include OpenscapParser::Subs
8
+
9
+ def id
10
+ @id ||= @parsed_xml['id']
11
+ end
12
+
13
+ def system
14
+ @system ||= @parsed_xml['system']
15
+ end
16
+
17
+ def complexity
18
+ @complexity ||= @parsed_xml['complexity']
19
+ end
20
+
21
+ def disruption
22
+ @disruption ||= @parsed_xml['disruption']
23
+ end
24
+
25
+ def strategy
26
+ @strategy ||= @parsed_xml['strategy']
27
+ end
28
+
29
+ def full_text(set_values)
30
+ full_text_lines(set_values).join('')
31
+ end
32
+
33
+ def full_text_lines(set_values)
34
+ map_child_nodes(set_values).map do |text_node|
35
+ text_node.respond_to?(:text) ? text_node.text : ''
36
+ end
37
+ end
38
+
39
+ def map_child_nodes(set_values = [])
40
+ map_sub_nodes @parsed_xml.children, set_values
41
+ end
42
+
43
+ def to_h
44
+ {
45
+ :id => id,
46
+ :system => system,
47
+ :complexity => complexity,
48
+ :disruption => disruption,
49
+ :strategy => strategy,
50
+ :text => text,
51
+ :subs => subs.map(&:to_h)
52
+ }
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'openscap_parser/fix'
4
+
5
+ module OpenscapParser
6
+ module Fixes
7
+ def self.included(base)
8
+ base.class_eval do
9
+ def fixes
10
+ @fixes ||= fix_nodes.map do |fix_node|
11
+ OpenscapParser::Fix.new(parsed_xml: fix_node)
12
+ end
13
+ end
14
+
15
+ def fix_nodes(xpath = ".//fix")
16
+ xpath_nodes(xpath)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+ module OpenscapParser
3
+ class Group < XmlNode
4
+ include OpenscapParser::Util
5
+
6
+ def id
7
+ @id ||= parsed_xml['id']
8
+ end
9
+
10
+ def title
11
+ @title ||= parsed_xml.at_css('title') &&
12
+ parsed_xml.at_css('title').text
13
+ end
14
+
15
+ def description
16
+ @description ||= newline_to_whitespace(
17
+ parsed_xml.at_css('description') &&
18
+ parsed_xml.at_css('description').text || ''
19
+ )
20
+ end
21
+
22
+ def rationale
23
+ @rationale ||= newline_to_whitespace(
24
+ parsed_xml.at_css('rationale') &&
25
+ parsed_xml.at_css('rationale').text || ''
26
+ )
27
+ end
28
+
29
+ def requires
30
+ @requires ||= parsed_xml.xpath('./requires') &&
31
+ parsed_xml.xpath('./requires/@idref').flat_map do |r|
32
+ r.to_s&.split
33
+ end
34
+ end
35
+
36
+ def conflicts
37
+ @conflicts ||= parsed_xml.xpath('./conflicts') &&
38
+ parsed_xml.xpath('./conflicts/@idref').flat_map do |c|
39
+ c.to_s&.split
40
+ end
41
+ end
42
+
43
+ def selected
44
+ @selected ||= parsed_xml['selected']
45
+ end
46
+
47
+ def parent_id
48
+ @parent_id = parsed_xml.xpath('../@id').to_s
49
+ end
50
+
51
+ def parent_type
52
+ if parsed_xml.xpath("name(..)='Group'")
53
+ @parent_type = 'Group'
54
+ else
55
+ @parent_type = 'Benchmark'
56
+ end
57
+ end
58
+
59
+ def to_h
60
+ {
61
+ :id => id,
62
+ :title => title,
63
+ :description => description,
64
+ :requires => requires,
65
+ :conflicts => conflicts,
66
+ :rationale => rationale,
67
+ :selected => selected,
68
+ :parent_id => parent_id,
69
+ :parent_type => parent_type
70
+ }
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'openscap_parser/group'
4
+
5
+ module OpenscapParser
6
+ # Methods related to finding and saving rule references
7
+ module Groups
8
+ def self.included(base)
9
+ base.class_eval do
10
+ def groups
11
+ @groups ||= group_nodes.map do |group_node|
12
+ OpenscapParser::Group.new(parsed_xml: group_node)
13
+ end
14
+ end
15
+
16
+ def group_nodes(xpath = './/Group')
17
+ xpath_nodes(xpath)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+ require 'openscap_parser/xml_file'
3
+ require 'oval/definition_result'
4
+ require 'oval/definition'
5
+
6
+ module OpenscapParser
7
+ class OvalReport < XmlFile
8
+ def definition_results
9
+ @definition_results ||= definition_result_nodes.map { |node| ::Oval::DefinitionResult.new parsed_xml: node }
10
+ end
11
+
12
+ def definition_result_nodes(xpath = "./oval_results/results/system/definitions/definition")
13
+ xpath_nodes(xpath)
14
+ end
15
+
16
+ def definitions
17
+ @definitions ||= definition_nodes.map { |node| Oval::Definition.new parsed_xml: node }
18
+ end
19
+
20
+ def definition_nodes(xpath = "./oval_results/oval_definitions/definitions/definition")
21
+ xpath_nodes(xpath)
22
+ end
23
+ end
24
+ end
@@ -24,13 +24,6 @@ module OpenscapParser
24
24
  @parsed_xml.xpath("select[@selected='true']/@idref").map(&:text)
25
25
  end
26
26
 
27
- def set_values
28
- @set_values ||= @parsed_xml.xpath("set-value") &&
29
- @parsed_xml.xpath("set-value").map do |set_value|
30
- [set_value['idref'], set_value.text]
31
- end.to_h
32
- end
33
-
34
27
  def to_h
35
28
  { :id => id, :title => title, :description => description }
36
29
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'openscap_parser/rule_identifier'
4
4
  require 'openscap_parser/rule_references'
5
+ require 'openscap_parser/fixes'
5
6
  require 'openscap_parser/xml_file'
6
7
 
7
8
  # Mimics openscap-ruby Rule interface
@@ -9,6 +10,7 @@ module OpenscapParser
9
10
  class Rule < XmlNode
10
11
  include OpenscapParser::Util
11
12
  include OpenscapParser::RuleReferences
13
+ include OpenscapParser::Fixes
12
14
 
13
15
  def id
14
16
  @id ||= parsed_xml['id']
@@ -27,6 +29,20 @@ module OpenscapParser
27
29
  parsed_xml.at_css('title').text
28
30
  end
29
31
 
32
+ def requires
33
+ @requires ||= parsed_xml.xpath('./requires') &&
34
+ parsed_xml.xpath('./requires/@idref').flat_map do |r|
35
+ r.to_s&.split
36
+ end
37
+ end
38
+
39
+ def conflicts
40
+ @conflicts ||= parsed_xml.xpath('./conflicts') &&
41
+ parsed_xml.xpath('./conflicts/@idref').flat_map do |c|
42
+ c.to_s&.split
43
+ end
44
+ end
45
+
30
46
  def description
31
47
  @description ||= newline_to_whitespace(
32
48
  parsed_xml.at_css('description') &&
@@ -54,5 +70,33 @@ module OpenscapParser
54
70
  def identifier_node
55
71
  @identifier_node ||= parsed_xml.at_xpath('ident')
56
72
  end
73
+
74
+ def parent_id
75
+ parsed_xml.xpath('../@id').to_s
76
+ end
77
+
78
+ def parent_type
79
+ if parsed_xml.xpath("name(..)='Group'")
80
+ @parent_type = 'Group'
81
+ else
82
+ @parent_type = 'Benchmark'
83
+ end
84
+ end
85
+
86
+ def to_h
87
+ {
88
+ :id => id,
89
+ :selected => selected,
90
+ :severity => severity,
91
+ :title => title,
92
+ :requires => requires,
93
+ :conflicts => conflicts,
94
+ :description => description,
95
+ :rationale => rationale,
96
+ :identifier => rule_identifier.to_h,
97
+ :parent_id => parent_id,
98
+ :parent_type => parent_type
99
+ }
100
+ end
57
101
  end
58
102
  end
@@ -10,5 +10,12 @@ module OpenscapParser
10
10
  def system
11
11
  @system ||= @parsed_xml && @parsed_xml['system']
12
12
  end
13
+
14
+ def to_h
15
+ {
16
+ :label => label,
17
+ :system => system
18
+ }
19
+ end
13
20
  end
14
21
  end
@@ -22,6 +22,16 @@ module OpenscapParser
22
22
  @result ||= parsed_xml.at_xpath('result') &&
23
23
  parsed_xml.at_xpath('result').text || ''
24
24
  end
25
+
26
+ def to_h
27
+ {
28
+ :id => id,
29
+ :time => time,
30
+ :severity => severity,
31
+ :weight => weight,
32
+ :result => result
33
+ }
34
+ end
25
35
  end
26
36
  end
27
37
 
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'openscap_parser/set_values'
4
+
5
+ module OpenscapParser
6
+ module Selectors
7
+ include OpenscapParser::SetValues
8
+ end
9
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+ require 'openscap_parser/xml_node'
3
+
4
+ module OpenscapParser
5
+ class SetValue < XmlNode
6
+ def id
7
+ @id ||= @parsed_xml['idref']
8
+ end
9
+
10
+ def text
11
+ @text ||= @parsed_xml.text
12
+ end
13
+
14
+ def to_h
15
+ { :id => id, :text => text }
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'openscap_parser/set_value'
4
+
5
+ module OpenscapParser
6
+ module SetValues
7
+ def self.included(base)
8
+ base.class_eval do
9
+ def set_values
10
+ @set_values ||= set_value_nodes.map do |set_value_node|
11
+ OpenscapParser::SetValue.new(parsed_xml: set_value_node)
12
+ end
13
+ end
14
+
15
+ def set_value_nodes(xpath = ".//set-value")
16
+ xpath_nodes(xpath)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+ require 'openscap_parser/xml_node'
3
+
4
+ module OpenscapParser
5
+ class Sub < XmlNode
6
+ def id
7
+ @id ||= @parsed_xml['idref']
8
+ end
9
+
10
+ def use
11
+ @use ||= @parsed_xml['use']
12
+ end
13
+
14
+ def to_h
15
+ { :id => id, :text => text, :use => use }
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'openscap_parser/sub'
4
+
5
+ module OpenscapParser
6
+ module Subs
7
+ def self.included(base)
8
+ base.class_eval do
9
+ def subs
10
+ return [] unless sub_nodes
11
+ @subs ||= sub_nodes.map do |xml|
12
+ Sub.new(parsed_xml: xml)
13
+ end
14
+ end
15
+
16
+ def sub_nodes(xpath = './/sub')
17
+ @sub_nodes ||= xpath_nodes(xpath)
18
+ end
19
+
20
+ def map_sub_nodes(children, set_values)
21
+ children.map do |child|
22
+ next child if child.name == 'text'
23
+ next replace_sub(Sub.new(parsed_xml: child), set_values) if child.name == 'sub'
24
+ child
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def replace_sub(sub, set_values)
31
+ set_value = set_values.find { |set_value| set_value.id == sub.id }
32
+ return unless set_value
33
+ set_value.parsed_xml.children.first
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'openscap_parser/tailorings'
4
+
3
5
  module OpenscapParser
4
6
  # A class to represent a tailoring XmlFile
5
7
  class TailoringFile < XmlFile
@@ -1,10 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'openscap_parser/rule_results'
4
+ require 'openscap_parser/selectors'
4
5
 
5
6
  module OpenscapParser
6
7
  class TestResult < XmlNode
7
8
  include OpenscapParser::RuleResults
9
+ include OpenscapParser::Selectors
8
10
 
9
11
  def target
10
12
  @target ||= parsed_xml.at_xpath('target') &&
@@ -1,4 +1,7 @@
1
1
  # frozen_string_literal: true
2
+ require 'openscap_parser/xml_file'
3
+ require 'openscap_parser/benchmarks'
4
+ require 'openscap_parser/test_results'
2
5
 
3
6
  module OpenscapParser
4
7
  # A class to represent an XmlFile which contains a <TestResult /> Xccdf type
@@ -1,3 +1,3 @@
1
1
  module OpenscapParser
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -19,6 +19,10 @@ module OpenscapParser
19
19
  @parsed_xml.remove_namespaces!
20
20
  end
21
21
 
22
+ def text
23
+ @parsed_xml.text
24
+ end
25
+
22
26
  def xpath_node(xpath)
23
27
  parsed_xml && parsed_xml.at_xpath(xpath)
24
28
  end
@@ -6,6 +6,7 @@ require 'openscap_parser/benchmarks'
6
6
  require 'openscap_parser/test_results'
7
7
  require 'openscap_parser/profiles'
8
8
  require 'openscap_parser/rules'
9
+ require 'openscap_parser/groups'
9
10
  require 'openscap_parser/rule_results'
10
11
  require 'openscap_parser/tailorings'
11
12
 
@@ -13,6 +14,7 @@ require 'openscap_parser/xml_file'
13
14
  require 'openscap_parser/datastream_file'
14
15
  require 'openscap_parser/test_result_file'
15
16
  require 'openscap_parser/tailoring_file'
17
+ require 'openscap_parser/oval_report'
16
18
 
17
19
  require 'date'
18
20
  require 'railtie' if defined?(Rails)
@@ -0,0 +1,47 @@
1
+ require "openscap_parser/xml_node"
2
+ require "oval/reference"
3
+
4
+ module Oval
5
+ class Definition < ::OpenscapParser::XmlNode
6
+ def id
7
+ @id ||= @parsed_xml['id']
8
+ end
9
+
10
+ def version
11
+ @version ||= @parsed_xml['version']
12
+ end
13
+
14
+ def klass
15
+ @klass ||= @parsed_xml['class']
16
+ end
17
+
18
+ def title
19
+ xml = @parsed_xml.at_xpath("./metadata/title")
20
+ @title ||= xml && xml.text
21
+ end
22
+
23
+ def description
24
+ xml = @parsed_xml.at_xpath("./metadata/description")
25
+ @description ||= xml && xml.text
26
+ end
27
+
28
+ def reference_nodes
29
+ @reference_nodes ||= @parsed_xml.xpath("./metadata/reference")
30
+ end
31
+
32
+ def references
33
+ @references ||= reference_nodes.map { |node| Reference.new parsed_xml: node }
34
+ end
35
+
36
+ def to_h
37
+ {
38
+ :id => id,
39
+ :version => version,
40
+ :klass => klass,
41
+ :title => title,
42
+ :description => description,
43
+ :references => references.map(&:to_h)
44
+ }
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,17 @@
1
+ require 'openscap_parser/xml_node'
2
+
3
+ module Oval
4
+ class DefinitionResult < ::OpenscapParser::XmlNode
5
+ def definition_id
6
+ @definition_id ||= @parsed_xml['definition_id']
7
+ end
8
+
9
+ def result
10
+ @result ||= @parsed_xml['result']
11
+ end
12
+
13
+ def to_h
14
+ { :id => definition_id, :result => result }
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,21 @@
1
+ require "openscap_parser/xml_node"
2
+
3
+ module Oval
4
+ class Reference < ::OpenscapParser::XmlNode
5
+ def source
6
+ @source ||= @parsed_xml['source']
7
+ end
8
+
9
+ def ref_id
10
+ @ref_id ||= @parsed_xml['ref_id']
11
+ end
12
+
13
+ def ref_url
14
+ @ref_url ||= @parsed_xml['ref_url']
15
+ end
16
+
17
+ def to_h
18
+ { :source => source, :ref_id => ref_id, :ref_url => ref_url }
19
+ end
20
+ end
21
+ end
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
11
11
 
12
12
  spec.summary = %q{Parse OpenSCAP content}
13
13
  spec.description = %q{This gem is a Ruby interface into SCAP content. It can parse SCAP datastream files (i.e. ssg-rhel7-ds.xml), scan result files output by oscap eval, and tailoring files.}
14
- spec.homepage = 'https://github.com/dLobatog/openscap_parser'
14
+ spec.homepage = 'https://github.com/OpenSCAP/openscap_parser'
15
15
  spec.license = 'MIT'
16
16
 
17
17
  # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openscap_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Lobato Garcia
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-10-15 00:00:00.000000000 Z
12
+ date: 2022-03-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
@@ -149,6 +149,11 @@ files:
149
149
  - lib/openscap_parser/benchmark.rb
150
150
  - lib/openscap_parser/benchmarks.rb
151
151
  - lib/openscap_parser/datastream_file.rb
152
+ - lib/openscap_parser/fix.rb
153
+ - lib/openscap_parser/fixes.rb
154
+ - lib/openscap_parser/group.rb
155
+ - lib/openscap_parser/groups.rb
156
+ - lib/openscap_parser/oval_report.rb
152
157
  - lib/openscap_parser/profile.rb
153
158
  - lib/openscap_parser/profiles.rb
154
159
  - lib/openscap_parser/rule.rb
@@ -158,6 +163,11 @@ files:
158
163
  - lib/openscap_parser/rule_result.rb
159
164
  - lib/openscap_parser/rule_results.rb
160
165
  - lib/openscap_parser/rules.rb
166
+ - lib/openscap_parser/selectors.rb
167
+ - lib/openscap_parser/set_value.rb
168
+ - lib/openscap_parser/set_values.rb
169
+ - lib/openscap_parser/sub.rb
170
+ - lib/openscap_parser/subs.rb
161
171
  - lib/openscap_parser/tailoring.rb
162
172
  - lib/openscap_parser/tailoring_file.rb
163
173
  - lib/openscap_parser/tailorings.rb
@@ -168,13 +178,16 @@ files:
168
178
  - lib/openscap_parser/version.rb
169
179
  - lib/openscap_parser/xml_file.rb
170
180
  - lib/openscap_parser/xml_node.rb
181
+ - lib/oval/definition.rb
182
+ - lib/oval/definition_result.rb
183
+ - lib/oval/reference.rb
171
184
  - lib/railtie.rb
172
185
  - lib/ssg.rb
173
186
  - lib/ssg/downloader.rb
174
187
  - lib/ssg/unarchiver.rb
175
188
  - lib/tasks/ssg.rake
176
189
  - openscap_parser.gemspec
177
- homepage: https://github.com/dLobatog/openscap_parser
190
+ homepage: https://github.com/OpenSCAP/openscap_parser
178
191
  licenses:
179
192
  - MIT
180
193
  metadata: {}
@@ -193,7 +206,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
193
206
  - !ruby/object:Gem::Version
194
207
  version: '0'
195
208
  requirements: []
196
- rubygems_version: 3.0.6
209
+ rubygems_version: 3.2.16
197
210
  signing_key:
198
211
  specification_version: 4
199
212
  summary: Parse OpenSCAP content