Class: AbideDevUtils::CEM::Benchmark

Inherits:
Object
  • Object
show all
Defined in:
lib/abide_dev_utils/cem/benchmark.rb

Overview

Repesents a benchmark based on resource and mapping data

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(osname, major_version, hiera_conf, module_name, framework: 'cis') ⇒ Benchmark

Returns a new instance of Benchmark.



317
318
319
320
321
322
323
324
325
326
327
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 317

def initialize(osname, major_version, hiera_conf, module_name, framework: 'cis')
  @osname = osname
  @major_version = major_version
  @os_facts = AbideDevUtils::Ppt::FacterUtils.recursive_facts_for_os(@osname, @major_version)
  @osfamily = @os_facts['os']['family']
  @hiera_conf = hiera_conf
  @module_name = module_name
  @framework = framework
  @map_cache = {}
  @rules_in_map = {}
end

Instance Attribute Details

#frameworkObject (readonly)

Returns the value of attribute framework.



315
316
317
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 315

def framework
  @framework
end

#hiera_confObject (readonly)

Returns the value of attribute hiera_conf.



315
316
317
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 315

def hiera_conf
  @hiera_conf
end

#major_versionObject (readonly)

Returns the value of attribute major_version.



315
316
317
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 315

def major_version
  @major_version
end

#module_nameObject (readonly)

Returns the value of attribute module_name.



315
316
317
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 315

def module_name
  @module_name
end

#os_factsObject (readonly)

Returns the value of attribute os_facts.



315
316
317
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 315

def os_facts
  @os_facts
end

#osfamilyObject (readonly)

Returns the value of attribute osfamily.



315
316
317
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 315

def osfamily
  @osfamily
end

#osnameObject (readonly)

Returns the value of attribute osname.



315
316
317
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 315

def osname
  @osname
end

Class Method Details

.benchmarks_from_puppet_module(pupmod, ignore_all_errors: false, ignore_framework_mismatch: true) ⇒ Array<AbideDevUtils::CEM::Benchmark>

Creates Benchmark objects from a Puppet module

Parameters:

  • pupmod (AbideDevUtils::Ppt::PuppetModule)

    A PuppetModule instance

  • skip_errors (Boolean)

    True skips errors and loads non-erroring benchmarks, false raises the error.

Returns:



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 333

def self.benchmarks_from_puppet_module(pupmod, ignore_all_errors: false, ignore_framework_mismatch: true)
  frameworks = pupmod.hiera_conf.local_hiera_files(hierarchy_name: 'Mapping Data').each_with_object([]) do |hf, ary|
    parts = hf.path.split(pupmod.hiera_conf.default_datadir)[-1].split('/')
    ary << parts[2] unless ary.include?(parts[2])
  end
  pupmod.supported_os.each_with_object([]) do |supp_os, ary|
    osname, majver = supp_os.split('::')
    if majver.is_a?(Array)
      majver.sort.each do |v|
        frameworks.each do |fw|
          benchmark = Benchmark.new(osname,
                                    v,
                                    pupmod.hiera_conf,
                                    pupmod.name(strip_namespace: true),
                                    framework: fw)
          benchmark.controls
          ary << benchmark
        rescue AbideDevUtils::Errors::MappingDataFrameworkMismatchError => e
          raise e unless ignore_all_errors || ignore_framework_mismatch
        rescue StandardError => e
          raise e unless ignore_all_errors
        end
      end
    else
      frameworks.each do |fw|
        benchmark = Benchmark.new(osname,
                                  majver,
                                  pupmod.hiera_conf,
                                  pupmod.name(strip_namespace: true),
                                  framework: fw)
        benchmark.controls
        ary << benchmark
      rescue AbideDevUtils::Errors::MappingDataFrameworkMismatchError => e
        raise e unless ignore_all_errors || ignore_framework_mismatch
      rescue StandardError => e
        raise e unless ignore_all_errors
      end
    end
  end
end

Instance Method Details

#add_rule(rule_hash) ⇒ Object



408
409
410
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 408

def add_rule(rule_hash)
  @rules << rule_hash
end

#controlsObject



380
381
382
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 380

def controls
  @controls ||= resources.map(&:controls).flatten.sort
end

#map(control_id, level: nil, profile: nil) ⇒ Object



432
433
434
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 432

def map(control_id, level: nil, profile: nil)
  mapper.get(control_id, level: level, profile: profile)
end

#map_dataObject



388
389
390
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 388

def map_data
  mapper.map_data
end

#map_type(control_id) ⇒ Object



436
437
438
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 436

def map_type(control_id)
  mapper.map_type(control_id)
end

#mapperObject



384
385
386
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 384

def mapper
  @mapper ||= AbideDevUtils::CEM::Mapping::Mapper.new(module_name, framework, load_mapping_data)
end

#resource_dataObject



392
393
394
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 392

def resource_data
  @resource_data ||= load_resource_data
end

#resourcesObject



374
375
376
377
378
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 374

def resources
  @resources ||= resource_data["#{module_name}::resources"].each_with_object([]) do |(rtitle, rdata), arr|
    arr << Resource.new(rtitle, rdata, framework, mapper)
  end
end

#rules_in_map(mtype, level: nil, profile: nil) ⇒ Object



412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 412

def rules_in_map(mtype, level: nil, profile: nil)
  real_mtype = map_type(mtype)
  cache_key = [real_mtype, level, profile].compact.join('-')
  return @rules_in_map[cache_key] if @rules_in_map.key?(cache_key)

  all_rim = mapper.each_with_array_like(real_mtype) do |(lvl, profs), arr|
    next if lvl == 'benchmark' || (!level.nil? && lvl != level)

    profs.each do |prof, maps|
      next if !profile.nil? && prof != profile

      # CIS and STIG differ in that STIG does not have profiles
      control_ids = maps.respond_to?(:keys) ? maps.keys : prof
      arr << control_ids
    end
  end
  @rules_in_map[cache_key] = all_rim.flatten.uniq
  @rules_in_map[cache_key]
end

#titleObject



396
397
398
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 396

def title
  mapper.title
end

#title_keyObject



404
405
406
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 404

def title_key
  @title_key ||= "#{title} #{version}"
end

#versionObject



400
401
402
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 400

def version
  mapper.version
end