Class: InspecPlugins::InspecToolsPlugin::CliCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/inspec_tools/plugin_cli.rb

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

POSSIBLE_LOG_LEVELS =
%w{debug info warn error fatal}.freeze

Instance Method Summary collapse

Instance Method Details

#complianceObject



221
222
223
224
225
226
227
228
229
230
# File 'lib/inspec_tools/plugin_cli.rb', line 221

def compliance
  if options[:threshold_file].nil? && options[:threshold_inline].nil?
    puts 'Please provide threshold as a yaml file or inline yaml'
    exit(1)
  end
  threshold = YAML.load_file(options[:threshold_file]) unless options[:threshold_file].nil?
  threshold = YAML.safe_load(options[:threshold_inline]) unless options[:threshold_inline].nil?
  compliance = InspecTools::Summary.new(File.read(options[:inspec_json])).threshold(threshold)
  compliance ? exit(0) : exit(1)
end

#csv2inspecObject



83
84
85
86
87
88
# File 'lib/inspec_tools/plugin_cli.rb', line 83

def csv2inspec
  csv = CSV.read(options[:csv], encoding: 'ISO8859-1')
  mapping = YAML.load_file(options[:mapping])
  profile = InspecTools::CSVTool.new(csv, mapping, options[:csv].split('/')[-1].split('.')[0], options[:verbose]).to_inspec
  Utils::InspecUtil.unpack_inspec_json(options[:output], profile, options[:separate_files], options[:format])
end

#generate_ckl_metadataObject



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/inspec_tools/plugin_cli.rb', line 151

def 
   = {}

  ['stigid'] = ask('STID ID: ')
  ['role'] = ask('Role: ')
  ['type'] = ask('Type: ')
  ['hostname'] = ask('Hostname: ')
  ['ip'] = ask('IP Address: ')
  ['mac'] = ask('MAC Address: ')
  ['fqdn'] = ask('FQDN: ')
  ['tech_area'] = ask('Tech Area: ')
  ['target_key'] = ask('Target Key: ')
  ['web_or_database'] = ask('Web or Database: ')
  ['web_db_site'] = ask('Web DB Site: ')
  ['web_db_instance'] = ask('Web DB Instance: ')

  .delete_if { |_key, value| value.empty? }
  File.open('metadata.json', 'w') do |f|
    f.write(.to_json)
  end
end

#generate_inspec_metadataObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/inspec_tools/plugin_cli.rb', line 174

def 
   = {}

  ['maintainer'] = ask('Maintainer: ')
  ['copyright'] = ask('Copyright: ')
  ['copyright_email'] = ask('Copyright Email: ')
  ['license'] = ask('License: ')
  ['version'] = ask('Version: ')

  .delete_if { |_key, value| value.empty? }
  File.open('metadata.json', 'w') do |f|
    f.write(.to_json)
  end
end

#generate_mapObject



145
146
147
148
# File 'lib/inspec_tools/plugin_cli.rb', line 145

def generate_map
  generator = InspecTools::GenerateMap.new
  generator.generate_example('mapping.yml')
end

#inspec2cklObject



122
123
124
125
126
127
128
129
# File 'lib/inspec_tools/plugin_cli.rb', line 122

def inspec2ckl
   = '{}'
  if !options[:metadata].nil?
     = File.read(options[:metadata])
  end
  ckl = InspecTools::Inspec.new(File.read(options[:inspec_json]), ).to_ckl
  File.write(options[:output], ckl)
end

#inspec2csvObject



111
112
113
114
# File 'lib/inspec_tools/plugin_cli.rb', line 111

def inspec2csv
  csv = InspecTools::Inspec.new(File.read(options[:inspec_json])).to_csv
  Utils::CSVUtil.unpack_csv(csv, options[:output])
end

#inspec2xccdfObject



66
67
68
69
70
71
72
73
# File 'lib/inspec_tools/plugin_cli.rb', line 66

def inspec2xccdf
  json = File.read(options[:inspec_json])
   = options[:metadata] ? JSON.parse(File.read(options[:metadata])) : {}
  inspec_tool = InspecTools::Inspec.new(json, )
  attr_hsh = YAML.load_file(options[:attributes])
  xccdf = inspec_tool.to_xccdf(attr_hsh)
  File.write(options[:output], xccdf)
end

#pdf2inspecObject



138
139
140
141
142
# File 'lib/inspec_tools/plugin_cli.rb', line 138

def pdf2inspec
  pdf = File.open(options[:pdf])
  profile = InspecTools::PDF.new(pdf, options[:output], options[:debug]).to_inspec
  Utils::InspecUtil.unpack_inspec_json(options[:output], profile, options[:separate_files], options[:format])
end

#summaryObject



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/inspec_tools/plugin_cli.rb', line 196

def summary
  summary = InspecTools::Summary.new(File.read(options[:inspec_json])).to_summary

  unless options.include?('json_full') || options.include?('json_counts')
    puts "\nOverall compliance: #{summary[:compliance]}%\n\n"
    summary[:status].keys.each do |category|
      puts category
      summary[:status][category].keys.each do |impact|
        puts "\t#{impact} : #{summary[:status][category][impact]}"
      end
    end
  end

  json_summary = summary.to_json
  puts json_summary if options[:json_full]
  puts summary[:status].to_json if options[:json_counts]
end

#xccdf2inspecObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/inspec_tools/plugin_cli.rb', line 41

def xccdf2inspec
  xccdf = InspecTools::XCCDF.new(File.read(options[:xccdf]), options[:replace_tags])
  profile = xccdf.to_inspec

  if !options[:metadata].nil?
    xccdf.(File.read(options[:metadata]))
  end

  Utils::InspecUtil.unpack_inspec_json(options[:output], profile, options[:separate_files], options[:format])
  if !options[:attributes].nil?
    attributes = xccdf.to_attributes
    File.write(options[:attributes], YAML.dump(attributes))
  end
end

#xlsx2inspecObject



99
100
101
102
103
104
# File 'lib/inspec_tools/plugin_cli.rb', line 99

def xlsx2inspec
  xlsx = Roo::Spreadsheet.open(options[:xlsx])
  mapping = YAML.load_file(options[:mapping])
  profile = InspecTools::XLSXTool.new(xlsx, mapping, options[:xlsx].split('/')[-1].split('.')[0], options[:verbose]).to_inspec(options[:control_name_prefix])
  Utils::InspecUtil.unpack_inspec_json(options[:output], profile, options[:separate_files], options[:format])
end