Class: AbideDevUtils::CEM::Benchmark

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

Overview

Repesents a benchmark for purposes of organizing data for markdown representation

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.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 13

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
  @rules = {}
  @map_cache = {}
  @rules_in_map = {}
  load_rules
end

Instance Attribute Details

#frameworkObject (readonly)

Returns the value of attribute framework.



11
12
13
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 11

def framework
  @framework
end

#hiera_confObject (readonly)

Returns the value of attribute hiera_conf.



11
12
13
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 11

def hiera_conf
  @hiera_conf
end

#major_versionObject (readonly)

Returns the value of attribute major_version.



11
12
13
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 11

def major_version
  @major_version
end

#module_nameObject (readonly)

Returns the value of attribute module_name.



11
12
13
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 11

def module_name
  @module_name
end

#os_factsObject (readonly)

Returns the value of attribute os_facts.



11
12
13
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 11

def os_facts
  @os_facts
end

#osfamilyObject (readonly)

Returns the value of attribute osfamily.



11
12
13
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 11

def osfamily
  @osfamily
end

#osnameObject (readonly)

Returns the value of attribute osname.



11
12
13
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 11

def osname
  @osname
end

#rulesObject (readonly)

Returns the value of attribute rules.



11
12
13
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 11

def rules
  @rules
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:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 31

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)
          ary << benchmark
        rescue StandardError => e
          raise e unless ignore_all_errors || (e.instance_of?(AbideDevUtils::Errors::MappingDataFrameworkMismatchError) && ignore_framework_mismatch)
        end
      end
    else
      frameworks.each do |fw|
        benchmark = Benchmark.new(osname, majver, pupmod.hiera_conf, pupmod.name(strip_namespace: true), framework: fw)
        ary << benchmark
      rescue StandardError => e
        raise e unless ignore_all_errors || (e.instance_of?(AbideDevUtils::Errors::MappingDataFrameworkMismatchError) && ignore_framework_mismatch)
      end
    end
  end
end

Instance Method Details

#add_rule(rule_hash) ⇒ Object



82
83
84
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 82

def add_rule(rule_hash)
  @rules << rule_hash
end

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



106
107
108
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 106

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

#map_dataObject



62
63
64
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 62

def map_data
  mapper.map_data
end

#map_type(control_id) ⇒ Object



110
111
112
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 110

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

#mapperObject



58
59
60
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 58

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

#resource_dataObject



66
67
68
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 66

def resource_data
  @resource_data ||= load_resource_data
end

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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 86

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



70
71
72
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 70

def title
  mapper.title
end

#title_keyObject



78
79
80
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 78

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

#versionObject



74
75
76
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 74

def version
  mapper.version
end