Class: Inspec::WaiverFileReader

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

Class Method Summary collapse

Class Method Details

.fetch_waivers_by_profile(profile_id, files) ⇒ Object



8
9
10
11
# File 'lib/inspec/waiver_file_reader.rb', line 8

def self.fetch_waivers_by_profile(profile_id, files)
  read_waivers_from_file(profile_id, files) if @waivers_data.nil? || @waivers_data[profile_id].nil?
  @waivers_data[profile_id]
end

.read_waivers_from_file(profile_id, files) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/inspec/waiver_file_reader.rb', line 13

def self.read_waivers_from_file(profile_id, files)
  @waivers_data ||= {}
  output = {}

  files.each do |file_path|
    file_extension = File.extname(file_path)
    data = nil
    if [".yaml", ".yml"].include? file_extension
      data = Secrets::YAML.resolve(file_path)
      unless data.nil?
        data = data.inputs
        validate_json_yaml(data)
      end
    elsif file_extension == ".csv"
      data = Waivers::CSVFileReader.resolve(file_path)
      headers = Waivers::CSVFileReader.headers
      validate_headers(headers)
    elsif file_extension == ".json"
      data = Waivers::JSONFileReader.resolve(file_path)
      validate_json_yaml(data) unless data.nil?
    end
    output.merge!(data) if !data.nil? && data.is_a?(Hash)

    if data.nil?
      raise Inspec::Exceptions::WaiversFileNotReadable,
          "Cannot find parser for waivers file '#{file_path}'. " \
          "Check to make sure file has the appropriate extension."
    end
  end

  @waivers_data[profile_id] = output
end

.validate_headers(headers, json_yaml = false) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/inspec/waiver_file_reader.rb', line 46

def self.validate_headers(headers, json_yaml = false)
  required_fields = json_yaml ? %w{justification} : %w{control_id justification}
  all_fields = %w{control_id justification expiration_date run}

  Inspec::Log.warn "Missing column headers: #{(required_fields - headers)}" unless (required_fields - headers).empty?
  Inspec::Log.warn "Invalid column header: Column can't be nil" if headers.include? nil
  Inspec::Log.warn "Extra column headers: #{(headers - all_fields)}" unless (headers - all_fields).empty?
end

.validate_json_yaml(data) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/inspec/waiver_file_reader.rb', line 55

def self.validate_json_yaml(data)
  headers = []
  data.each_value do |value|
    headers.push value.keys
  end
  validate_headers(headers.flatten.uniq, true)
end