Class: AbideDevUtils::Sce::Benchmark

Inherits:
Object
  • Object
show all
Defined in:
lib/abide_dev_utils/sce/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.



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/abide_dev_utils/sce/benchmark.rb', line 320

def initialize(osname, major_version, hiera_conf, module_name, framework: 'cis')
  @osname = osname
  @major_version = major_version
  @os_facts = AbideDevUtils::Ppt::FacterUtils::FactSets.new.find_by_fact_value_tuples(['os.name', @osname],
                                                                                      ['os.release.major',
                                                                                       @major_version])
  @osfamily = @os_facts['os']['family']
  @hiera_conf = hiera_conf
  @module_name = module_name
  @framework = framework
  @map_cache = {}
  @rules_in_map = {}
  @mapper = AbideDevUtils::Sce::Mapping::Mapper.new(@module_name, @framework, load_mapping_data)
  @resource_data = load_resource_data
  @resources = @resource_data["#{module_name}::resources"].each_with_object([]) do |(rtitle, rdata), arr|
    arr << Resource.new(rtitle, rdata, framework, mapper)
  end
  @controls = resources.map(&:controls).flatten.sort
end

Instance Attribute Details

#controlsObject (readonly) Also known as: rules

Returns the value of attribute controls.



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

def controls
  @controls
end

#frameworkObject (readonly)

Returns the value of attribute framework.



315
316
317
# File 'lib/abide_dev_utils/sce/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/sce/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/sce/benchmark.rb', line 315

def major_version
  @major_version
end

#mapperObject (readonly)

Returns the value of attribute mapper.



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

def mapper
  @mapper
end

#module_nameObject (readonly)

Returns the value of attribute module_name.



315
316
317
# File 'lib/abide_dev_utils/sce/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/sce/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/sce/benchmark.rb', line 315

def osfamily
  @osfamily
end

#osnameObject (readonly)

Returns the value of attribute osname.



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

def osname
  @osname
end

#resource_dataObject (readonly)

Returns the value of attribute resource_data.



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

def resource_data
  @resource_data
end

#resourcesObject (readonly)

Returns the value of attribute resources.



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

def resources
  @resources
end

Class Method Details

.benchmarks_from_puppet_module(pupmod, ignore_all_errors: false, ignore_framework_mismatch: true) ⇒ Array<AbideDevUtils::Sce::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:



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
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/abide_dev_utils/sce/benchmark.rb', line 344

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



401
402
403
# File 'lib/abide_dev_utils/sce/benchmark.rb', line 401

def add_rule(rule_hash)
  @rules << rule_hash
end

#inspectObject



437
438
439
# File 'lib/abide_dev_utils/sce/benchmark.rb', line 437

def inspect
  "#<#{self.class.name}:#{object_id} title: #{title}, version: #{version}, module_name: #{module_name}, framework: #{framework}>"
end

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



425
426
427
# File 'lib/abide_dev_utils/sce/benchmark.rb', line 425

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

#map_dataObject



385
386
387
# File 'lib/abide_dev_utils/sce/benchmark.rb', line 385

def map_data
  mapper.map_data
end

#map_type(control_id) ⇒ Object



429
430
431
# File 'lib/abide_dev_utils/sce/benchmark.rb', line 429

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

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



405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/abide_dev_utils/sce/benchmark.rb', line 405

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



389
390
391
# File 'lib/abide_dev_utils/sce/benchmark.rb', line 389

def title
  mapper.title
end

#title_keyObject



397
398
399
# File 'lib/abide_dev_utils/sce/benchmark.rb', line 397

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

#to_sObject



433
434
435
# File 'lib/abide_dev_utils/sce/benchmark.rb', line 433

def to_s
  title
end

#versionObject



393
394
395
# File 'lib/abide_dev_utils/sce/benchmark.rb', line 393

def version
  mapper.version
end