Class: Utils::InspecUtil

Inherits:
Object show all
Defined in:
lib/utilities/inspec_util.rb,
lib/exceptions/impact_input_error.rb,
lib/exceptions/severity_input_error.rb

Defined Under Namespace

Classes: ImpactInputError, SeverityInputError

Constant Summary collapse

DATA_NOT_FOUND_MESSAGE =
'N/A'.freeze
WIDTH =
80
IMPACT_SCORES =
{
  'none' => 0.0,
  'low' => 0.1,
  'medium' => 0.4,
  'high' => 0.7,
  'critical' => 0.9
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.control_finding_details(control, control_clk_status) ⇒ Object



170
171
172
173
174
175
176
177
# File 'lib/utilities/inspec_util.rb', line 170

def self.control_finding_details(control, control_clk_status)
  result = "One or more of the automated tests failed or was inconclusive for the control \n\n #{control[:message].sort.join}" if control_clk_status == 'Open'
  result = "All Automated tests passed for the control \n\n #{control[:message].join}" if control_clk_status == 'NotAFinding'
  result = "Automated test skipped due to known accepted condition in the control : \n\n#{control[:message].join}" if control_clk_status == 'Not_Reviewed'
  result = "Justification: \n #{control[:message].join}" if control_clk_status == 'Not_Applicable'
  result = 'No test available or some test errors occurred for this control' if control_clk_status == 'Profile_Error'
  result
end

.control_status(control, for_summary = false) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/utilities/inspec_util.rb', line 154

def self.control_status(control, for_summary = false)
  status_list = control[:status].uniq
  if control[:impact].to_f.zero?
    'Not_Applicable'
  elsif status_list.include?('failed')
    'Open'
  elsif status_list.include?('passed')
    'NotAFinding'
  elsif status_list.include?('error') && for_summary
    'Profile_Error'
  else
    # profile skipped or profile error
    'Not_Reviewed'
  end
end

.get_impact(severity, use_cvss_terms: true) ⇒ Object

TODO:

Allow for the user to pass in a hash for the desired mapping of text

‘impact’ level.

mapped to a float between 0.0 - 1.0.

values to numbers or to override our hard coded values.

Parameters:

  • severity (String)

    the string value you want to map to an InSpec

Raises:



195
196
197
198
199
200
201
202
# File 'lib/utilities/inspec_util.rb', line 195

def self.get_impact(severity, use_cvss_terms: true)
  return float_to_impact(severity, use_cvss_terms) if severity.is_a?(Float)

  return string_to_impact(severity, use_cvss_terms) if severity.is_a?(String)

  raise SeverityInputError, "'#{severity}' is not a valid severity value. It should be a Float between 0.0 and " \
                            '1.0 or one of the approved keywords.'
end

.get_impact_string(impact, use_cvss_terms: true) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/utilities/inspec_util.rb', line 242

def self.get_impact_string(impact, use_cvss_terms: true)
  return if impact.nil?

  value = impact.to_f
  unless value.between?(0, 1)
    raise ImpactInputError, "'#{value}' is not a valid impact score. Valid impact scores: [0.0 - 1.0]."
  end

  IMPACT_SCORES.reverse_each do |name, impact_score|
    if name == 'critical' && value >= impact_score && use_cvss_terms
      return 'high'
    elsif value >= impact_score
      return name
    else
      next
    end
  end
end

.get_platform(json) ⇒ Object



139
140
141
# File 'lib/utilities/inspec_util.rb', line 139

def self.get_platform(json)
  json['profiles'].find { |profile| !profile[:platform].nil? }
end

.parse_data_for_ckl(json) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/utilities/inspec_util.rb', line 83

def self.parse_data_for_ckl(json)
  data = {}

  # Parse for inspec profile results json
  json['profiles'].each do |profile|
    profile['controls'].each do |control|
      c_id = control['id'].to_sym
      data[c_id] = {}

      data[c_id][:vuln_num]       = control['id'] unless control['id'].nil?
      data[c_id][:rule_title]     = control['title'] unless control['title'].nil?
      data[c_id][:vuln_discuss]   = control['desc'] unless control['desc'].nil?

      unless control['tags'].nil?
        data[c_id][:severity]       = control['tags']['severity'] unless control['tags']['severity'].nil?
        data[c_id][:gid]            = control['tags']['gid'] unless control['tags']['gid'].nil?
        data[c_id][:group_title]    = control['tags']['gtitle'] unless control['tags']['gtitle'].nil?
        data[c_id][:rule_id]        = control['tags']['rid'] unless control['tags']['rid'].nil?
        data[c_id][:rule_ver]       = control['tags']['stig_id'] unless control['tags']['stig_id'].nil?
        data[c_id][:cci_ref]        = control['tags']['cci'] unless control['tags']['cci'].nil?
        data[c_id][:nist]           = control['tags']['nist'].join(' ') unless control['tags']['nist'].nil?
      end

      if control['descriptions'].respond_to?(:find)
        data[c_id][:check_content]  = control['descriptions'].find { |c| c['label'] == 'fix' }&.dig('data')
        data[c_id][:fix_text]       = control['descriptions'].find { |c| c['label'] == 'check' }&.dig('data')
      end

      data[c_id][:impact]         = control['impact'].to_s unless control['impact'].nil?
      data[c_id][:profile_name]   = profile['name'].to_s unless profile['name'].nil?
      data[c_id][:profile_shasum] = profile['sha256'].to_s unless profile['sha256'].nil?

      data[c_id][:status] = []
      data[c_id][:message] = []

      if control.key?('results')
        control['results'].each do |result|
          if !result['backtrace'].nil?
            result['status'] = 'error'
          end
          data[c_id][:status].push(result['status'])
          data[c_id][:message].push("SKIPPED -- Test: #{result['code_desc']}\nMessage: #{result['skip_message']}\n") if result['status'] == 'skipped'
          data[c_id][:message].push("FAILED -- Test: #{result['code_desc']}\nMessage: #{result['message']}\n") if result['status'] == 'failed'
          data[c_id][:message].push("PASS -- #{result['code_desc']}\n") if result['status'] == 'passed'
          data[c_id][:message].push("PROFILE_ERROR -- Test: #{result['code_desc']}\nMessage: #{result['backtrace']}\n") if result['status'] == 'error'
        end
      end

      if data[c_id][:impact].to_f.zero?
        data[c_id][:message].unshift("NOT_APPLICABLE -- Description: #{control['desc']}\n\n")
      end
    end
  end
  data
end

.parse_data_for_xccdf(json) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/utilities/inspec_util.rb', line 34

def self.parse_data_for_xccdf(json)
  data = {}

  controls = []
  if json['profiles'].nil?
    controls = json['controls']
  elsif json['profiles'].length == 1
    controls = json['profiles'].last['controls']
  else
    json['profiles'].each do |profile|
      controls.concat(profile['controls'])
    end
  end
  c_data = {}

  controls.each do |control|
    c_id = control['id'].to_sym
    c_data[c_id] = {}
    c_data[c_id]['id']             = control['id']    || DATA_NOT_FOUND_MESSAGE
    c_data[c_id]['title']          = control['title'] || DATA_NOT_FOUND_MESSAGE
    c_data[c_id]['desc']           = control['desc'] || DATA_NOT_FOUND_MESSAGE
    c_data[c_id]['severity']       = control['tags']['severity'] || DATA_NOT_FOUND_MESSAGE
    c_data[c_id]['gid']            = control['tags']['gid'] || DATA_NOT_FOUND_MESSAGE
    c_data[c_id]['gtitle']         = control['tags']['gtitle'] || DATA_NOT_FOUND_MESSAGE
    c_data[c_id]['gdescription']   = control['tags']['gdescription'] || DATA_NOT_FOUND_MESSAGE
    c_data[c_id]['rid']            = control['tags']['rid'] || DATA_NOT_FOUND_MESSAGE
    c_data[c_id]['rversion']       = control['tags']['rversion'] || DATA_NOT_FOUND_MESSAGE
    c_data[c_id]['rweight']        = control['tags']['rweight'] || DATA_NOT_FOUND_MESSAGE
    c_data[c_id]['stig_id']        = control['tags']['stig_id'] || DATA_NOT_FOUND_MESSAGE
    c_data[c_id]['cci']            = control['tags']['cci'] || DATA_NOT_FOUND_MESSAGE
    c_data[c_id]['nist']           = control['tags']['nist'] || ['unmapped']
    c_data[c_id]['check']          = control['tags']['check'] || DATA_NOT_FOUND_MESSAGE
    c_data[c_id]['checkref']       = control['tags']['checkref'] || DATA_NOT_FOUND_MESSAGE
    c_data[c_id]['fix']            = control['tags']['fix'] || DATA_NOT_FOUND_MESSAGE
    c_data[c_id]['fixref']         = control['tags']['fixref'] || DATA_NOT_FOUND_MESSAGE
    c_data[c_id]['fix_id']         = control['tags']['fix_id'] || DATA_NOT_FOUND_MESSAGE
    c_data[c_id]['rationale']      = control['tags']['rationale'] || DATA_NOT_FOUND_MESSAGE
    c_data[c_id]['cis_family']     = control['tags']['cis_family'] || DATA_NOT_FOUND_MESSAGE
    c_data[c_id]['cis_rid']        = control['tags']['cis_rid'] || DATA_NOT_FOUND_MESSAGE
    c_data[c_id]['cis_level']      = control['tags']['cis_level'] || DATA_NOT_FOUND_MESSAGE
    c_data[c_id]['impact']         = control['impact'].to_s || DATA_NOT_FOUND_MESSAGE
    c_data[c_id]['code']           = control['code'].to_s || DATA_NOT_FOUND_MESSAGE
  end

  data['controls'] = c_data.values
  data['status'] = 'success'
  data
end

.to_dotted_hash(hash, recursive_key = '') ⇒ Object



143
144
145
146
147
148
149
150
151
152
# File 'lib/utilities/inspec_util.rb', line 143

def self.to_dotted_hash(hash, recursive_key = '')
  hash.each_with_object({}) do |(k, v), ret|
    key = recursive_key + k.to_s
    if v.is_a? Hash
      ret.merge! to_dotted_hash(v, key + '.')
    else
      ret[key] = v
    end
  end
end

.unpack_inspec_json(directory, inspec_json, separated, output_format) ⇒ Object



261
262
263
264
265
266
267
268
269
270
# File 'lib/utilities/inspec_util.rb', line 261

def self.unpack_inspec_json(directory, inspec_json, separated, output_format)
  if directory == 'id'
    directory = inspec_json['name']
  end
  controls = generate_controls(inspec_json)
  unpack_profile(directory || 'profile', controls, separated, output_format || 'json')
  create_inspec_yml(directory || 'profile', inspec_json)
  create_license(directory || 'profile', inspec_json)
  create_readme_md(directory || 'profile', inspec_json)
end

Instance Method Details

#get_impact(severity) ⇒ Object

Takes in the STIG severity tag and converts it to the InSpec #impact control tag. At the moment the mapping is static, so that:

high => 0.7
medium => 0.5
low => 0.3


195
196
197
198
199
200
201
202
# File 'lib/utilities/inspec_util.rb', line 195

def self.get_impact(severity, use_cvss_terms: true)
  return float_to_impact(severity, use_cvss_terms) if severity.is_a?(Float)

  return string_to_impact(severity, use_cvss_terms) if severity.is_a?(String)

  raise SeverityInputError, "'#{severity}' is not a valid severity value. It should be a Float between 0.0 and " \
                            '1.0 or one of the approved keywords.'
end