Class: AbideDevUtils::XCCDF::Benchmark
- Inherits:
-
Object
- Object
- AbideDevUtils::XCCDF::Benchmark
show all
- Includes:
- Common
- Defined in:
- lib/abide_dev_utils/xccdf.rb
Overview
Class representation of an XCCDF benchmark
Constant Summary
collapse
- MAP_INDICES =
%w[title hiera_title hiera_title_num number].freeze
Constants included
from Common
Common::CIS_CONTROL_NUMBER, Common::CIS_CONTROL_PARTS, Common::CIS_LEVEL_CODE, Common::CIS_NEXT_GEN_WINDOWS, Common::CIS_PROFILE_PARTS, Common::CONTROL_PREFIX, Common::UNDERSCORED, Common::XPATHS
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#controls ⇒ Object
-
#controls_by_profile_level(level_code) ⇒ Object
-
#controls_by_profile_title(profile_title) ⇒ Object
-
#facter_platform ⇒ Object
-
#find_cis_recommendation(name, number_format: false) ⇒ Object
-
#gen_map(dir: nil, type: 'cis', parent_key_prefix: '', version_output_dir: false, **_) ⇒ Object
-
#initialize(path) ⇒ Benchmark
constructor
A new instance of Benchmark.
-
#map_indexed(index: 'title', framework: 'cis', key_prefix: '') ⇒ Object
-
#normalized_title ⇒ Object
-
#profile_levels ⇒ Object
-
#profile_titles ⇒ Object
-
#profiles ⇒ Object
-
#resolve_control_reference(control) ⇒ Object
-
#to_h ⇒ Object
-
#to_hiera(parent_key_prefix: nil, num: false, levels: [], titles: [], **_kwargs) ⇒ String
Converts object to Hiera-formatted YAML.
Methods included from Common
#==, #abide_object?, #control_parts, #control_profile_text, #name_normalize_control, #normalize_control_name, #normalize_profile_name, #normalize_string, #number_normalize_control, #profile_parts, #sorted_control_classes, #text_normalize, #validate_xccdf, #xpath
Constructor Details
#initialize(path) ⇒ Benchmark
Returns a new instance of Benchmark.
176
177
178
179
180
181
|
# File 'lib/abide_dev_utils/xccdf.rb', line 176
def initialize(path)
@xml = parse(path)
@title = xpath('xccdf:Benchmark/xccdf:title').text
@version = xpath('xccdf:Benchmark/xccdf:version').text
@diff_properties = %i[title version profiles]
end
|
Instance Attribute Details
#diff_properties ⇒ Object
Returns the value of attribute diff_properties.
174
175
176
|
# File 'lib/abide_dev_utils/xccdf.rb', line 174
def diff_properties
@diff_properties
end
|
#title ⇒ Object
Returns the value of attribute title.
174
175
176
|
# File 'lib/abide_dev_utils/xccdf.rb', line 174
def title
@title
end
|
#version ⇒ Object
Returns the value of attribute version.
174
175
176
|
# File 'lib/abide_dev_utils/xccdf.rb', line 174
def version
@version
end
|
#xml ⇒ Object
Returns the value of attribute xml.
174
175
176
|
# File 'lib/abide_dev_utils/xccdf.rb', line 174
def xml
@xml
end
|
Instance Method Details
#controls ⇒ Object
199
200
201
|
# File 'lib/abide_dev_utils/xccdf.rb', line 199
def controls
@controls ||= Controls.new(xpath('//xccdf:select'))
end
|
#controls_by_profile_level(level_code) ⇒ Object
203
204
205
|
# File 'lib/abide_dev_utils/xccdf.rb', line 203
def controls_by_profile_level(level_code)
profiles.select { |x| x.level == level_code }.map(&:controls).flatten.uniq
end
|
#controls_by_profile_title(profile_title) ⇒ Object
207
208
209
|
# File 'lib/abide_dev_utils/xccdf.rb', line 207
def controls_by_profile_title(profile_title)
profiles.select { |x| x.title == profile_title }.controls
end
|
252
253
254
255
256
257
258
259
260
261
262
263
264
|
# File 'lib/abide_dev_utils/xccdf.rb', line 252
def facter_platform
cpe = xpath('xccdf:Benchmark/xccdf:platform')[0]['idref'].split(':')
if cpe.length > 4
product_name = cpe[4].split('_')
product_version = cpe[5].split('.') unless cpe[5].nil?
return [product_name[0], product_version[0]] unless product_version[0] == '-'
return [product_name[0], product_name[-1]] if product_version[0] == '-'
end
product = cpe[3].split('_')
[product[0], product[-1]] end
|
#find_cis_recommendation(name, number_format: false) ⇒ Object
224
225
226
227
228
229
230
|
# File 'lib/abide_dev_utils/xccdf.rb', line 224
def find_cis_recommendation(name, number_format: false)
profiles.each do |profile|
profile.controls.each do |ctrl|
return [profile, ctrl] if normalize_control_name(ctrl, number_format: number_format) == name
end
end
end
|
#gen_map(dir: nil, type: 'cis', parent_key_prefix: '', version_output_dir: false, **_) ⇒ Object
211
212
213
214
215
216
217
218
219
220
221
222
|
# File 'lib/abide_dev_utils/xccdf.rb', line 211
def gen_map(dir: nil, type: 'cis', parent_key_prefix: '', version_output_dir: false, **_)
os, ver = facter_platform
output_path = [type, os, ver]
output_path.unshift(File.expand_path(dir)) if dir
output_path << version if version_output_dir
mapping_dir = File.expand_path(File.join(output_path))
parent_key_prefix = '' if parent_key_prefix.nil?
MAP_INDICES.each_with_object({}) do |idx, h|
map_file_path = "#{mapping_dir}/#{idx}.yaml"
h[map_file_path] = map_indexed(index: idx, framework: type, key_prefix: parent_key_prefix)
end
end
|
#map_indexed(index: 'title', framework: 'cis', key_prefix: '') ⇒ Object
240
241
242
243
244
245
246
247
248
249
250
|
# File 'lib/abide_dev_utils/xccdf.rb', line 240
def map_indexed(index: 'title', framework: 'cis', key_prefix: '')
c_map = profiles.each_with_object({}) do |profile, obj|
obj[profile.level.downcase] = {} unless obj[profile.level.downcase].is_a?(Hash)
obj[profile.level.downcase][profile.title.downcase] = map_controls_hash(profile, index).sort_by { |k, _| k }.to_h
end
c_map['benchmark'] = { 'title' => title, 'version' => version }
mappings = [framework, index]
mappings.unshift(key_prefix) unless key_prefix.empty?
{ mappings.join('::') => c_map }.to_yaml
end
|
#normalized_title ⇒ Object
183
184
185
|
# File 'lib/abide_dev_utils/xccdf.rb', line 183
def normalized_title
normalize_string(title)
end
|
#profile_levels ⇒ Object
191
192
193
|
# File 'lib/abide_dev_utils/xccdf.rb', line 191
def profile_levels
@profiles.levels
end
|
#profile_titles ⇒ Object
195
196
197
|
# File 'lib/abide_dev_utils/xccdf.rb', line 195
def profile_titles
@profiles.titles
end
|
#profiles ⇒ Object
187
188
189
|
# File 'lib/abide_dev_utils/xccdf.rb', line 187
def profiles
@profiles ||= Profiles.new(xpath('xccdf:Benchmark/xccdf:Profile'))
end
|
#resolve_control_reference(control) ⇒ Object
283
284
285
|
# File 'lib/abide_dev_utils/xccdf.rb', line 283
def resolve_control_reference(control)
xpath("//xccdf:Rule[@id='#{control.reference}']")
end
|
#to_h ⇒ Object
232
233
234
235
236
237
238
|
# File 'lib/abide_dev_utils/xccdf.rb', line 232
def to_h
{
title: title,
version: version,
profiles: profiles.to_h
}
end
|
#to_hiera(parent_key_prefix: nil, num: false, levels: [], titles: [], **_kwargs) ⇒ String
Converts object to Hiera-formatted YAML
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
# File 'lib/abide_dev_utils/xccdf.rb', line 268
def to_hiera(parent_key_prefix: nil, num: false, levels: [], titles: [], **_kwargs)
hash = { 'title' => title, 'version' => version }
key_prefix = hiera_parent_key(parent_key_prefix)
profiles.each do |profile|
next unless levels.empty? || levels.include?(profile.level)
next unless titles.empty? || titles.include?(profile.title)
hash[profile.hiera_title] = hiera_controls_for_profile(profile, num)
end
hash.transform_keys! do |k|
[key_prefix, k].join('::').strip
end
hash.to_yaml
end
|