Class: AbideDevUtils::XCCDF::Benchmark

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/abide_dev_utils/xccdf.rb

Overview

Class representation of an XCCDF benchmark

Constant Summary collapse

CIS_MAP_INDICES =
%w[title hiera_title hiera_title_num number].freeze
STIG_MAP_INDICES =
%w[vulnid ruleid].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::CIS_TITLE_MARKER, Common::CONTROL_PARTS, Common::CONTROL_PREFIX, Common::PROFILE_PARTS, Common::STIG_CONTROL_PARTS, Common::STIG_PROFILE_PARTS, Common::STIG_TITLE_MARKER, Common::UNDERSCORED, Common::XPATHS

Instance Attribute Summary collapse

Instance Method Summary collapse

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, #text_normalize, #validate_xccdf, #xpath

Constructor Details

#initialize(path) ⇒ Benchmark

Returns a new instance of Benchmark.



198
199
200
201
202
203
204
205
# File 'lib/abide_dev_utils/xccdf.rb', line 198

def initialize(path)
  @xml = parse(path)
  @xml.remove_namespaces!
  @benchmark = xpath('Benchmark')
  @title = xpath('Benchmark/title').text
  @version = xpath('Benchmark/version').text
  @diff_properties = %i[title version profiles]
end

Instance Attribute Details

#benchmarkObject (readonly)

Returns the value of attribute benchmark.



196
197
198
# File 'lib/abide_dev_utils/xccdf.rb', line 196

def benchmark
  @benchmark
end

#diff_propertiesObject (readonly)

Returns the value of attribute diff_properties.



196
197
198
# File 'lib/abide_dev_utils/xccdf.rb', line 196

def diff_properties
  @diff_properties
end

#titleObject (readonly)

Returns the value of attribute title.



196
197
198
# File 'lib/abide_dev_utils/xccdf.rb', line 196

def title
  @title
end

#versionObject (readonly)

Returns the value of attribute version.



196
197
198
# File 'lib/abide_dev_utils/xccdf.rb', line 196

def version
  @version
end

#xmlObject (readonly)

Returns the value of attribute xml.



196
197
198
# File 'lib/abide_dev_utils/xccdf.rb', line 196

def xml
  @xml
end

Instance Method Details

#controlsObject



223
224
225
# File 'lib/abide_dev_utils/xccdf.rb', line 223

def controls
  @controls ||= Controls.new(xpath('//select'))
end

#controls_by_profile_level(level_code) ⇒ Object



227
228
229
# File 'lib/abide_dev_utils/xccdf.rb', line 227

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



231
232
233
# File 'lib/abide_dev_utils/xccdf.rb', line 231

def controls_by_profile_title(profile_title)
  profiles.select { |x| x.title == profile_title }.controls
end

#facter_benchmarkObject



283
284
285
286
# File 'lib/abide_dev_utils/xccdf.rb', line 283

def facter_benchmark
  id = xpath('Benchmark/@id').text
  id.split('_')[0..-2]
end

#facter_platformObject



288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/abide_dev_utils/xccdf.rb', line 288

def facter_platform
  cpe = xpath('Benchmark/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]] # This should wrap the name i.e 'Windows' and the version '10'
end

#find_cis_recommendation(name, number_format: false) ⇒ Object



255
256
257
258
259
260
261
# File 'lib/abide_dev_utils/xccdf.rb', line 255

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



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/abide_dev_utils/xccdf.rb', line 235

def gen_map(dir: nil, type: 'cis', parent_key_prefix: '', version_output_dir: false, **_)
  case type
  when 'cis'
    os, ver = facter_platform
    indicies = CIS_MAP_INDICES
  when 'stig'
    os, ver = facter_benchmark
    indicies = STIG_MAP_INDICES
  end
  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?
  indicies.each_with_object({}) do |idx, h|
    map_file_path = "#{mapping_dir}/#{idx}.yaml"
    h[map_file_path] = map_indexed(indicies: indicies, index: idx, framework: type, key_prefix: parent_key_prefix)
  end
end

#map_indexed(indicies: [], index: 'title', framework: 'cis', key_prefix: '') ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
# File 'lib/abide_dev_utils/xccdf.rb', line 271

def map_indexed(indicies: [], 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, indicies, 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_titleObject



207
208
209
# File 'lib/abide_dev_utils/xccdf.rb', line 207

def normalized_title
  normalize_string(title)
end

#profile_levelsObject



215
216
217
# File 'lib/abide_dev_utils/xccdf.rb', line 215

def profile_levels
  @profiles.levels
end

#profile_titlesObject



219
220
221
# File 'lib/abide_dev_utils/xccdf.rb', line 219

def profile_titles
  @profiles.titles
end

#profilesObject



211
212
213
# File 'lib/abide_dev_utils/xccdf.rb', line 211

def profiles
  @profiles ||= Profiles.new(xpath('Benchmark/Profile'), @benchmark)
end

#resolve_cis_control_reference(control) ⇒ Object



319
320
321
# File 'lib/abide_dev_utils/xccdf.rb', line 319

def resolve_cis_control_reference(control)
  xpath("//Rule[@id='#{control.reference}']")
end

#to_hObject



263
264
265
266
267
268
269
# File 'lib/abide_dev_utils/xccdf.rb', line 263

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

Returns:

  • (String)

    YAML-formatted string



304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/abide_dev_utils/xccdf.rb', line 304

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