Class: Inspec::Feature::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/inspec/feature/config.rb

Constant Summary collapse

VERIFICATION_KEY_NAME =
"progress-2022-05-04".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conf_path = nil) ⇒ Config

Returns a new instance of Config.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/inspec/feature/config.rb', line 11

def initialize(conf_path = nil)
  # If conf path is nil, read from source installation
  conf_path ||= File.join(Inspec.src_root, "etc", "features.yaml")

  # Verify path and sig file exists or else throw exception
  sig_path = conf_path.sub(/\.yaml/, ".sig")
  [conf_path, sig_path].each do |file|
    raise Inspec::FeatureConfigMissingError.new("No such file #{file}") unless File.exist?(file)
  end

  # Verify sig matches contents
  validation_key_path = Inspec::IafFile.find_validation_key(VERIFICATION_KEY_NAME)
  verification_key = Inspec::IafFile::KEY_ALG.new File.read validation_key_path
  signature = Base64.decode64 File.read sig_path
  digest = Inspec::IafFile::ARTIFACT_DIGEST.new
  unless verification_key.verify digest, signature, File.read(conf_path)
    # If not load default empty config and raise exception
    @cfg_data = load_error_data
    raise Inspec::FeatureConfigTamperedError.new("Feature yaml file does not match signature - tampered?")
  end

  # Read YAML data from path
  @cfg_data = YAML.load_file(conf_path)
  @features_by_name = {}
end

Instance Attribute Details

#cfg_dataObject (readonly)

Returns the value of attribute cfg_data.



9
10
11
# File 'lib/inspec/feature/config.rb', line 9

def cfg_data
  @cfg_data
end

#validObject (readonly)

Returns the value of attribute valid.



9
10
11
# File 'lib/inspec/feature/config.rb', line 9

def valid
  @valid
end

Instance Method Details

#[](feature_name) ⇒ Object



44
45
46
47
48
49
# File 'lib/inspec/feature/config.rb', line 44

def [](feature_name)
  raw_info = cfg_data["features"][feature_name]
  return nil unless raw_info

  @features_by_name[feature_name] ||= Inspec::Feature.new(feature_name.to_sym, raw_info)
end

#feature_name?(query) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/inspec/feature/config.rb', line 51

def feature_name?(query)
  cfg_data["features"].key?(query.to_s)
end

#featuresObject



55
56
57
# File 'lib/inspec/feature/config.rb', line 55

def features
  @features ||= load_features
end

#with_each_featureObject



37
38
39
40
41
42
# File 'lib/inspec/feature/config.rb', line 37

def with_each_feature
  cfg_data["features"].each do |feature_name, raw_info|
    feat = @features_by_name[feature_name] ||= Inspec::Feature.new(feature_name.to_sym, raw_info)
    yield(feat)
  end
end