Class: InspecTools::CLI

Inherits:
Command show all
Defined in:
lib/inspec_tools/cli.rb

Instance Method Summary collapse

Methods inherited from Command

dispatch

Instance Method Details

#complianceObject



191
192
193
194
195
196
197
198
199
200
# File 'lib/inspec_tools/cli.rb', line 191

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



55
56
57
58
59
60
# File 'lib/inspec_tools/cli.rb', line 55

def csv2inspec
  csv = CSV.read(options[:csv], encoding: 'ISO8859-1')
  mapping = YAML.load_file(options[:mapping])
  profile = 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



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/inspec_tools/cli.rb', line 125

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



148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/inspec_tools/cli.rb', line 148

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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/inspec_tools/cli.rb', line 101

def generate_map
  template = '
  # Setting csv_header to true will skip the csv file header
  skip_csv_header: true
  width   : 80


  control.id: 0
  control.title: 15
  control.desc: 16
  control.tags:
          severity: 1
          rid: 8
          stig_id: 3
          cci: 2
          check: 12
          fix: 10
  '
  myfile = File.new('mapping.yml', 'w')
  myfile.puts template
  myfile.close
end

#inspec2cklObject



78
79
80
81
82
83
84
85
# File 'lib/inspec_tools/cli.rb', line 78

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



67
68
69
70
# File 'lib/inspec_tools/cli.rb', line 67

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

#inspec2xccdfObject



39
40
41
42
43
44
45
# File 'lib/inspec_tools/cli.rb', line 39

def inspec2xccdf
  json = File.read(options[:inspec_json])
  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



94
95
96
97
98
# File 'lib/inspec_tools/cli.rb', line 94

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



170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/inspec_tools/cli.rb', line 170

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

  puts "\ncompliance: #{summary[:compliance]}%\n\n"
  summary[:status].keys.each do |status|
    puts status
    summary[:status][status.to_sym].keys.each do |impact|
      print "\t#{impact} : #{summary[:status][status.to_sym][impact.to_sym]}\n"
    end
  end if options[:cli]

  File.write(options[:output], summary.to_json) if options[:output]
end

#versionObject



203
204
205
# File 'lib/inspec_tools/cli.rb', line 203

def version
  puts VERSION
end

#xccdf2inspecObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/inspec_tools/cli.rb', line 19

def xccdf2inspec
  xccdf = 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