Merge pull request #1126 from chef/ssd/better-compliance-errors

Improve error messages from compliance fetcher
This commit is contained in:
Christoph Hartmann 2016-09-26 02:05:23 +02:00 committed by GitHub
commit 511b084078
4 changed files with 42 additions and 2 deletions

View file

@ -4,6 +4,7 @@
require 'uri'
require 'inspec/fetcher'
require 'inspec/errors'
# InSpec Target Helper for Chef Compliance
# reuses UrlHelper, but it knows the target server and the access token already
@ -24,11 +25,23 @@ module Compliance
# check if we have a compliance token
config = Compliance::Configuration.new
return nil if config['token'].nil?
if config['token'].nil?
fail Inspec::FetcherFailure, <<EOF
Cannot fetch #{uri} because your compliance token has not been
configured.
Please login using
inspec compliance login https://your_compliance_server --user admin --insecure --token 'PASTE TOKEN HERE'
EOF
end
# verifies that the target e.g base/ssh exists
profile = uri.host + uri.path
Compliance::API.exist?(config, profile)
if !Compliance::API.exist?(config, profile)
fail Inpsec::FetcherFailure, "The compliance profile #{profile} was not found on the configured compliance server"
end
new(target_url(profile, config), config)
rescue URI::Error => _e
nil

View file

@ -132,6 +132,16 @@ module Inspec
Logger.const_get(l.upcase)
end
def pretty_handle_exception(exception)
case exception
when Inspec::Error
$stderr.puts exception.message
exit(1)
else
raise exception # rubocop:disable Style/SignalException
end
end
def configure_logger(o)
#
# TODO(ssd): This is a big gross, but this configures the

View file

@ -48,6 +48,8 @@ class Inspec::InspecCLI < Inspec::BaseCLI # rubocop:disable Metrics/ClassLength
fdst = File.expand_path(dst)
File.write(fdst, JSON.dump(profile.info))
end
rescue StandardError => e
pretty_handle_exception(e)
end
desc 'check PATH', 'verify all tests at the specified PATH'
@ -97,6 +99,8 @@ class Inspec::InspecCLI < Inspec::BaseCLI # rubocop:disable Metrics/ClassLength
end
end
exit 1 unless result[:summary][:valid]
rescue StandardError => e
pretty_handle_exception(e)
end
desc 'vendor', 'Download all dependencies and generate a lockfile'
@ -105,6 +109,8 @@ class Inspec::InspecCLI < Inspec::BaseCLI # rubocop:disable Metrics/ClassLength
profile = Inspec::Profile.for_target('./', opts.merge(cache: Inspec::Cache.new(path)))
lockfile = profile.generate_lockfile
File.write('inspec.lock', lockfile.to_yaml)
rescue StandardError => e
pretty_handle_exception(e)
end
desc 'archive PATH', 'archive a profile to tar.gz (default) or zip'
@ -136,6 +142,8 @@ class Inspec::InspecCLI < Inspec::BaseCLI # rubocop:disable Metrics/ClassLength
# generate archive
exit 1 unless profile.archive(opts)
rescue StandardError => e
pretty_handle_exception(e)
end
desc 'exec PATHS', 'run all test files at the specified PATH.'
@ -147,6 +155,8 @@ class Inspec::InspecCLI < Inspec::BaseCLI # rubocop:disable Metrics/ClassLength
# run tests
run_tests(targets, o)
rescue StandardError => e
pretty_handle_exception(e)
end
desc 'detect', 'detect the target OS'
@ -165,6 +175,8 @@ class Inspec::InspecCLI < Inspec::BaseCLI # rubocop:disable Metrics/ClassLength
mark_text(res[item.to_sym]))
}
end
rescue StandardError => e
pretty_handle_exception(e)
end
desc 'shell', 'open an interactive debugging shell'
@ -196,12 +208,16 @@ class Inspec::InspecCLI < Inspec::BaseCLI # rubocop:disable Metrics/ClassLength
exit 0
rescue RuntimeError, Train::UserError => e
$stderr.puts e.message
rescue StandardError => e
pretty_handle_exception(e)
end
desc 'env', 'Output shell-appropriate completion configuration'
def env(shell = nil)
p = Inspec::EnvPrinter.new(self.class, shell)
p.print_and_exit!
rescue StandardError => e
pretty_handle_exception(e)
end
desc 'version', 'prints the version of this tool'

View file

@ -9,4 +9,5 @@ module Inspec
class CyclicDependencyError < Error; end
class UnsatisfiedVersionSpecification < Error; end
class DuplicateDep < Error; end
class FetcherFailure < Error; end
end