Module: CemSpecHelper::MappingDataSpec

Defined in:
lib/cem_spec_helper/mapping_data_spec.rb

Defined Under Namespace

Classes: Mapping

Constant Summary collapse

MAP_ROOT =

The root directory for mapping data fixtures

File.join(Dir.pwd, 'spec', 'fixtures', 'data', 'mapping').freeze
SYNTHETIC_MAP_ROOT =

The root directory for synthetic mapping data

File.join(Dir.pwd, 'spec', 'fixtures', 'unit', 'puppet_x', 'puppetlabs', 'sce', 'data_processor', 'mapping').freeze
MAPPINGS_KEY =

The key prefix for the mappings

"#{CemSpecHelper::MODULE_NAME}::mappings"
TOP_KEY_TYPES =

The top key types for each framework.

{
  'cis' => ['hiera_title', 'hiera_title_num', 'number', 'title'].freeze,
  'stig' => ['vulnid', 'ruleid'].freeze,
}.freeze

Instance Method Summary collapse

Instance Method Details

#find_mapping_data(framework, os, majver = nil, level = 'level_1', profile = 'server') ⇒ Array<Hash>

Find the mapping data for a given framework, OS, and major OS version, and return the maps filtered by level and profile.

Parameters:

  • framework (String)

    The framework to find the mapping data for

  • os (String)

    The OS to find the mapping data for

  • majver (String) (defaults to: nil)

    The major OS version to find the mapping data for

  • level (String) (defaults to: 'level_1')

    The level to filter the maps by. For STIG, this is the MAC.

  • profile (String) (defaults to: 'server')

    The profile to filter the maps by. For STIG, this is the confidentiality.

Returns:

  • (Array<Hash>)

    An array of hashes representing the maps



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/cem_spec_helper/mapping_data_spec.rb', line 96

def find_mapping_data(framework, os, majver = nil, level = 'level_1', profile = 'server')
  majver = 1 if os == 'Synthetic'
  md = (os == 'Synthetic') ? synthetic_mapping_data : mapping_data
  md.find { |m| m.framework == framework && m.os == os && m.majver == majver.to_s }.maps
    .map do |m|
      top_key = TOP_KEY_TYPES[framework]&.find do |key|
        m.key?("#{MAPPINGS_KEY}::#{framework}::#{key}")
      end
      raise "No top key found for #{framework}" if top_key.nil?

      m["#{MAPPINGS_KEY}::#{framework}::#{top_key}"][level][profile]
    end
end

#load_linux_mapping_dataArray<Mapping>

Load the mapping data for Linux

Returns:

  • (Array<Mapping>)

    An array of Mapping objects



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cem_spec_helper/mapping_data_spec.rb', line 30

def load_linux_mapping_data
  # Get frameworks with maps
  Dir[File.join(MAP_ROOT, '*')].each_with_object([]) do |fw_dir, arr|
    framework = File.basename(fw_dir)
    # Get OS' in frameworks
    Dir[File.join(fw_dir, '*')].each do |os_dir|
      os = File.basename(os_dir)
      # Get major OS versions in OS'
      Dir[File.join(os_dir, '*')].each do |ver_dir|
        ver = File.basename(ver_dir)
        mapping = Mapping.new(framework, os, ver)
        # Get map names and load map files
        Dir[File.join(ver_dir, '*.yaml')].each do |map_file|
          map_type = File.basename(map_file, '.yaml')
          mapping.add_map(map_type, map_file)
        end
        arr << mapping
      end
    end
  end
end

#load_mapping_dataArray<Mapping>

Because of the slight variation in the directory structure for Windows, we need to load the mapping data differently for Windows than for Linux.

Returns:

  • (Array<Mapping>)

    An array of Mapping objects



22
23
24
25
26
# File 'lib/cem_spec_helper/mapping_data_spec.rb', line 22

def load_mapping_data
  return load_windows_mapping_data if CemSpecHelper::MODULE_NAME.match?(/windows$/)

  load_linux_mapping_data
end

#load_synthetic_mapping_dataArray<Mapping>

Load the synthetic mapping data

Returns:

  • (Array<Mapping>)

    An array of Mapping objects



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/cem_spec_helper/mapping_data_spec.rb', line 74

def load_synthetic_mapping_data
  # Get frameworks with maps
  Dir[File.join(SYNTHETIC_MAP_ROOT, '*')].each_with_object([]) do |fw_dir, arr|
    framework = File.basename(fw_dir)
    mapping = Mapping.new(framework, 'Synthetic', '1')
    # Get map names and load map files
    Dir[File.join(fw_dir, '*.yaml')].each do |map_file|
      map_type = File.basename(map_file, '.yaml')
      mapping.add_map(map_type, map_file)
    end
    arr << mapping
  end
end

#load_windows_mapping_dataArray<Mapping>

Load the mapping data for Windows

Returns:

  • (Array<Mapping>)

    An array of Mapping objects



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cem_spec_helper/mapping_data_spec.rb', line 54

def load_windows_mapping_data
  # Get frameworks with maps
  Dir[File.join(MAP_ROOT, '*')].each_with_object([]) do |fw_dir, arr|
    framework = File.basename(fw_dir)
    # Get OS major versions in frameworks
    Dir[File.join(fw_dir, '*')].each do |ver_dir|
      ver = File.basename(ver_dir)
      mapping = Mapping.new(framework, 'windows', ver)
      # Get map names and load map files
      Dir[File.join(ver_dir, '*.yaml')].each do |map_file|
        map_type = File.basename(map_file, '.yaml')
        mapping.add_map(map_type, map_file)
      end
      arr << mapping
    end
  end
end

#mapping_dataArray<Mapping>

Returns An array of Mapping objects.

Returns:

  • (Array<Mapping>)

    An array of Mapping objects



116
117
118
# File 'lib/cem_spec_helper/mapping_data_spec.rb', line 116

def mapping_data
  @mapping_data ||= load_mapping_data
end

#synthetic_mapping_dataArray<Mapping>

Returns An array of Mapping objects.

Returns:

  • (Array<Mapping>)

    An array of Mapping objects



111
112
113
# File 'lib/cem_spec_helper/mapping_data_spec.rb', line 111

def synthetic_mapping_data
  @synthetic_mapping_data ||= load_synthetic_mapping_data
end