class Inspec::Deprecation::ConfigFile

Constants

GroupEntry
VALID_ACTIONS

What actions may you specify to be taken when a deprecation is encountered?

VALID_GROUP_FIELDS

Note that 'comment' is ignored, but listed here so you can have it present and pass validation.

Attributes

groups[R]
unknown_group_action[R]

Public Class Methods

new(io = nil) click to toggle source
# File lib/inspec/utils/deprecation/config_file.rb, line 25
def initialize(io = nil)
  io ||= open_default_config_io
  begin
    @raw_data = JSON.parse(io.read)
  rescue JSON::ParserError => e
    raise Inspec::Deprecation::MalformedConfigFileError, "Could not parse deprecation config file: #{e.message}"
  end

  @groups = {}
  @unknown_group_action = :warn
  validate!
  silence_deprecations_from_cli
end

Private Instance Methods

open_default_config_io() click to toggle source
# File lib/inspec/utils/deprecation/config_file.rb, line 41
def open_default_config_io
  default_path = File.join(Inspec.src_root, "etc", "deprecations.json")
  unless File.exist?(default_path)
    raise Inspec::Deprecation::MalformedConfigError, "Missing deprecation config file: #{default_path}"
  end

  File.open(default_path)
end
silence_deprecations_from_cli() click to toggle source
# File lib/inspec/utils/deprecation/config_file.rb, line 50
def silence_deprecations_from_cli
  # Read --silence-deprecations CLI option
  cfg = Inspec::Config.cached
  return unless cfg[:silence_deprecations]

  groups_to_silence = cfg[:silence_deprecations]
  silence_all = groups_to_silence.include?("all")

  groups.each do |group_name, group|
    # Only silence things that warn. Don't silence things that exit;
    # those harsher measures are usually protecting removed code and ignoring
    # and continuing regardless would be perilous and lead to errors.
    if %i{warn fail_control}.include?(group.action) &&
        (silence_all || groups_to_silence.include?(group_name.to_s))
      group.action = :ignore
    end
  end
end
validate!() click to toggle source
#
Validation
#
# File lib/inspec/utils/deprecation/config_file.rb, line 72
def validate!
  validate_file_version
  validate_unknown_group_action

  unless @raw_data.key?("groups")
    raise Inspec::Deprecation::InvalidConfigFileError, "Missing groups field"
  end
  unless @raw_data["groups"].is_a?(Hash)
    raise Inspec::Deprecation::InvalidConfigFileError, "Groups field must be a Hash"
  end

  @raw_data["groups"].each do |group_name, group_info|
    validate_group_entry(group_name, group_info)
  end
end
validate_file_version() click to toggle source
# File lib/inspec/utils/deprecation/config_file.rb, line 88
def validate_file_version
  unless @raw_data.key?("file_version")
    raise Inspec::Deprecation::InvalidConfigFileError, "Missing file_version field"
  end
  unless @raw_data["file_version"] == "1.0.0"
    raise Inspec::Deprecation::InvalidConfigFileError, "Unrecognized file_version '#{@raw_data["file_version"]}' - supported versions: 1.0.0"
  end
end
validate_group_entry(name, opts) click to toggle source
# File lib/inspec/utils/deprecation/config_file.rb, line 106
def validate_group_entry(name, opts)
  opts.each do |seen_field, _value|
    unless VALID_GROUP_FIELDS.include?(seen_field)
      raise Inspec::Deprecation::InvalidConfigFileError, "Unrecognized field for group '#{name}' - saw '#{seen_field}', supported fields: #{VALID_GROUP_FIELDS.map(&:to_s).join(", ")}"
    end
  end

  entry = GroupEntry.new(name.to_sym)

  opts["action"] = (opts["action"] || :warn).to_sym
  unless VALID_ACTIONS.include?(opts["action"])
    raise Inspec::Deprecation::UnrecognizedActionError, "Unrecognized action for group '#{name}' - saw '#{opts["action"]}', supported actions: #{VALID_ACTIONS.map(&:to_s).join(", ")}"
  end

  entry.action = opts["action"]

  entry.suffix = opts["suffix"]
  entry.prefix = opts["prefix"]
  entry.exit_status = opts["exit_status"]

  groups[name.to_sym] = entry
end
validate_unknown_group_action() click to toggle source
# File lib/inspec/utils/deprecation/config_file.rb, line 97
def validate_unknown_group_action
  seen_action = (@raw_data["unknown_group_action"] || @unknown_group_action).to_sym
  unless VALID_ACTIONS.include?(seen_action)
    raise Inspec::Deprecation::UnrecognizedActionError, "Unrecognized value '#{seen_action}' for field 'unknown_group_action' - supported actions: #{VALID_ACTIONS.map(&:to_s).join(", ")}"
  end

  @unknown_group_action = seen_action
end