Class: InspecPlugins::InspecToolsPlugin::CliCommand
- 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
- #compliance ⇒ Object
- #csv2inspec ⇒ Object
- #generate_ckl_metadata ⇒ Object
- #generate_inspec_metadata ⇒ Object
- #generate_map ⇒ Object
- #inspec2ckl ⇒ Object
- #inspec2csv ⇒ Object
- #inspec2xccdf ⇒ Object
- #pdf2inspec ⇒ Object
- #summary ⇒ Object
- #xccdf2inspec ⇒ Object
- #xlsx2inspec ⇒ Object
Instance Method Details
#compliance ⇒ Object
232 233 234 235 236 237 238 239 240 241 |
# File 'lib/inspec_tools/plugin_cli.rb', line 232 def compliance if [:threshold_file].nil? && [:threshold_inline].nil? puts 'Please provide threshold as a yaml file or inline yaml' exit(1) end threshold = YAML.load_file([:threshold_file]) unless [:threshold_file].nil? threshold = YAML.safe_load([:threshold_inline]) unless [:threshold_inline].nil? compliance = InspecTools::Summary.new(File.read([:inspec_json])).threshold(threshold) compliance ? exit(0) : exit(1) end |
#csv2inspec ⇒ Object
76 77 78 79 80 81 |
# File 'lib/inspec_tools/plugin_cli.rb', line 76 def csv2inspec csv = CSV.read([:csv], encoding: 'ISO8859-1') mapping = YAML.load_file([:mapping]) profile = InspecTools::CSVTool.new(csv, mapping, [:csv].split('/')[-1].split('.')[0], [:verbose]).to_inspec Utils::InspecUtil.unpack_inspec_json([:output], profile, [:separate_files], [:format]) end |
#generate_ckl_metadata ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/inspec_tools/plugin_cli.rb', line 162 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_metadata ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/inspec_tools/plugin_cli.rb', line 185 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_map ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/inspec_tools/plugin_cli.rb', line 138 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 |
#inspec2ckl ⇒ Object
115 116 117 118 119 120 121 122 |
# File 'lib/inspec_tools/plugin_cli.rb', line 115 def inspec2ckl = '{}' if ![:metadata].nil? = File.read([:metadata]) end ckl = InspecTools::Inspec.new(File.read([:inspec_json]), ).to_ckl File.write([:output], ckl) end |
#inspec2csv ⇒ Object
104 105 106 107 |
# File 'lib/inspec_tools/plugin_cli.rb', line 104 def inspec2csv csv = InspecTools::Inspec.new(File.read([:inspec_json])).to_csv Utils::CSVUtil.unpack_csv(csv, [:output]) end |
#inspec2xccdf ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/inspec_tools/plugin_cli.rb', line 60 def inspec2xccdf json = File.read([:inspec_json]) inspec_tool = InspecTools::Inspec.new(json) attr_hsh = YAML.load_file([:attributes]) xccdf = inspec_tool.to_xccdf(attr_hsh) File.write([:output], xccdf) end |
#pdf2inspec ⇒ Object
131 132 133 134 135 |
# File 'lib/inspec_tools/plugin_cli.rb', line 131 def pdf2inspec pdf = File.open([:pdf]) profile = InspecTools::PDF.new(pdf, [:output], [:debug]).to_inspec Utils::InspecUtil.unpack_inspec_json([:output], profile, [:separate_files], [:format]) end |
#summary ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/inspec_tools/plugin_cli.rb', line 207 def summary summary = InspecTools::Summary.new(File.read([:inspec_json])).to_summary unless .include?('json_full') || .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 [:json_full] puts summary[:status].to_json if [:json_counts] end |
#xccdf2inspec ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/inspec_tools/plugin_cli.rb', line 40 def xccdf2inspec xccdf = InspecTools::XCCDF.new(File.read([:xccdf]), [:replace_tags]) profile = xccdf.to_inspec if ![:metadata].nil? xccdf.(File.read([:metadata])) end Utils::InspecUtil.unpack_inspec_json([:output], profile, [:separate_files], [:format]) if ![:attributes].nil? attributes = xccdf.to_attributes File.write([:attributes], YAML.dump(attributes)) end end |
#xlsx2inspec ⇒ Object
92 93 94 95 96 97 |
# File 'lib/inspec_tools/plugin_cli.rb', line 92 def xlsx2inspec xlsx = Roo::Spreadsheet.open([:xlsx]) mapping = YAML.load_file([:mapping]) profile = InspecTools::XLSXTool.new(xlsx, mapping, [:xlsx].split('/')[-1].split('.')[0], [:verbose]).to_inspec([:control_name_prefix]) Utils::InspecUtil.unpack_inspec_json([:output], profile, [:separate_files], [:format]) end |