feature: bring back profile check

This commit is contained in:
Dominik Richter 2015-10-26 03:09:35 +01:00
parent f1fc4183e5
commit 83082b2e7b
2 changed files with 69 additions and 4 deletions

View file

@ -38,8 +38,7 @@ class VulcanoCLI < Thor
desc: 'Allow remote scans with self-signed certificates (WinRM).'
end
desc 'json PATH', 'read all tests in PATH and generate a JSON-profile'
target_options
desc 'json PATH', 'read all tests in PATH and generate a JSON profile'
option :id, type: :string,
desc: 'Attach a profile ID to all test results'
option :output, aliases: :o, type: :string,
@ -48,7 +47,7 @@ class VulcanoCLI < Thor
profile = Vulcano::Profile.from_path(path, options)
dst = options[:output].to_s
if dst.empty?
puts JSON.pretty_generate(profile.params)
puts JSON.pretty_generate(profile.info)
else
if File.exist? dst
puts "----> updating #{dst}"
@ -56,10 +55,18 @@ class VulcanoCLI < Thor
puts "----> creating #{dst}"
end
fdst = File.expand_path(dst)
File.write(fdst, JSON.dump(profile.params))
File.write(fdst, JSON.dump(profile.info))
end
end
desc 'check PATH', 'verify test structure in PATH'
def check(path)
o = options.dup
o[:logger] = Logger.new(STDOUT)
profile = Vulcano::Profile.from_path(path, o)
exit 1 unless profile.check
end
desc 'exec PATHS', 'run all test files'
option :id, type: :string,
desc: 'Attach a profile ID to all test results'

View file

@ -41,10 +41,68 @@ module Vulcano
desc: rule.desc,
impact: rule.impact,
code: rule.instance_variable_get(:@__code),
checks: rule.instance_variable_get(:@checks),
}
end
end
def info
res = @params.dup
rules = {}
res['rules'].each do |id, rule|
next if id.to_s.empty?
data = rule.dup
data.delete(:checks)
data[:impact] ||= 0.5
data[:impact] = 1.0 if data[:impact] > 1.0
data[:impact] = 0.0 if data[:impact] < 0.0
rules[id] = data
end
res['rules'] = rules
res
end
def check # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
no_errors = true
no_warnings = true
warn = ->(msg) { @logger.warn(msg); no_warnings = false }
error = ->(msg) { @logger.error(msg); no_warnings = false; no_errors = false }
@logger.info "Checking profile in #{@path}"
if @params['name'].to_s.empty?
error.call('No profile name defined')
elsif !(@params['name'].to_s =~ %r{^\S+\/\S+$})
error.call('Profile name must be defined as: OWNER/ID')
end
warn.call('No version defined') if @params['version'].to_s.empty?
warn.call('No title defined') if @params['title'].to_s.empty?
warn.call('No maintainer defined') if @params['maintainer'].to_s.empty?
warn.call('No supports defined') if @params['supports'].empty?
@logger.info 'Metadata OK.' if no_warnings
no_warnings = true
if @params['rules'].empty?
warn.call('No rules were found.')
else
@logger.debug "Found #{@params['rules'].length} rules."
end
@params['rules'].each do |id, rule|
error.call('Avoid rules with empty IDs') if id.nil? or id.empty?
warn.call("Rule #{id} has no title") if rule[:title].to_s.empty?
warn.call("Rule #{id} has no description") if rule[:desc].to_s.empty?
warn.call("Rule #{id} has impact > 1.0") if rule[:impact].to_f > 1.0
warn.call("Rule #{id} has impact < 0.0") if rule[:impact].to_f < 0.0
warn.call("Rule #{id} has no tests defined") if rule[:checks].nil? or rule[:checks].empty?
end
@logger.info 'Rule definitions OK.' if no_warnings
no_errors
end
private
def read_metadata