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.



312
313
314
315
316
317
318
319
320
321
322
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 312

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.



310
311
312
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 310

def framework
  @framework
end

#hiera_confObject (readonly)

Returns the value of attribute hiera_conf.



310
311
312
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 310

def hiera_conf
  @hiera_conf
end

#major_versionObject (readonly)

Returns the value of attribute major_version.



310
311
312
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 310

def major_version
  @major_version
end

#module_nameObject (readonly)

Returns the value of attribute module_name.



310
311
312
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 310

def module_name
  @module_name
end

#os_factsObject (readonly)

Returns the value of attribute os_facts.



310
311
312
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 310

def os_facts
  @os_facts
end

#osfamilyObject (readonly)

Returns the value of attribute osfamily.



310
311
312
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 310

def osfamily
  @osfamily
end

#osnameObject (readonly)

Returns the value of attribute osname.



310
311
312
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 310

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:



328
329
330
331
332
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
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 328

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



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

def add_rule(rule_hash)
  @rules << rule_hash
end

#controlsObject



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

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

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



427
428
429
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 427

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

#map_dataObject



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

def map_data
  mapper.map_data
end

#map_type(control_id) ⇒ Object



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

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

#mapperObject



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

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

#resource_dataObject



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

def resource_data
  @resource_data ||= load_resource_data
end

#resourcesObject



369
370
371
372
373
# File 'lib/abide_dev_utils/cem/benchmark.rb', line 369

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



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

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



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

def title
  mapper.title
end

#title_keyObject



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

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

#versionObject



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

def version
  mapper.version
end