2016-02-05 07:38:45 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
# author: Christoph Hartmann
|
|
|
|
# author: Dominik Richter
|
|
|
|
|
|
|
|
require 'thor'
|
2016-03-23 15:40:01 +00:00
|
|
|
require 'erb'
|
2016-02-05 07:38:45 +00:00
|
|
|
|
|
|
|
module Compliance
|
2016-02-05 10:06:00 +00:00
|
|
|
class ComplianceCLI < Inspec::BaseCLI # rubocop:disable Metrics/ClassLength
|
2016-02-05 07:38:45 +00:00
|
|
|
namespace 'compliance'
|
|
|
|
|
2016-04-08 12:03:41 +00:00
|
|
|
desc 'login SERVER', 'Log in to a Chef Compliance SERVER'
|
|
|
|
option :server, type: :string, desc: 'Chef Compliance Server URL'
|
2016-03-01 19:51:23 +00:00
|
|
|
option :insecure, aliases: :k, type: :boolean,
|
|
|
|
desc: 'Explicitly allows InSpec to perform "insecure" SSL connections and transfers'
|
2016-04-08 12:03:41 +00:00
|
|
|
option :user, type: :string, required: false,
|
|
|
|
desc: 'Chef Compliance Username (for legacy auth)'
|
|
|
|
option :password, type: :string, required: false,
|
|
|
|
desc: 'Chef Compliance Password (for legacy auth)'
|
2016-03-14 14:08:27 +00:00
|
|
|
option :apipath, type: :string, default: '/api',
|
|
|
|
desc: 'Set the path to the API, defaults to /api'
|
2016-04-08 12:03:41 +00:00
|
|
|
option :token, type: :string, required: false,
|
2016-03-23 13:32:10 +00:00
|
|
|
desc: 'Chef Compliance access token'
|
2016-04-08 12:03:41 +00:00
|
|
|
option :refresh_token, type: :string, required: false,
|
|
|
|
desc: 'Chef Compliance refresh token'
|
2016-08-16 15:21:13 +00:00
|
|
|
def login(server) # rubocop:disable Metrics/AbcSize
|
2016-04-13 11:47:33 +00:00
|
|
|
# show warning if the Compliance Server does not support
|
|
|
|
|
2016-04-08 12:03:41 +00:00
|
|
|
options['server'] = server
|
|
|
|
url = options['server'] + options['apipath']
|
|
|
|
if !options['user'].nil? && !options['password'].nil?
|
|
|
|
# username / password
|
2016-08-18 00:00:26 +00:00
|
|
|
_success, msg = login_legacy(url, options['user'], options['password'], options['insecure'])
|
2016-04-13 11:47:33 +00:00
|
|
|
elsif !options['user'].nil? && !options['token'].nil?
|
2016-04-08 12:03:41 +00:00
|
|
|
# access token
|
2016-08-18 00:00:26 +00:00
|
|
|
_success, msg = store_access_token(url, options['user'], options['token'], options['insecure'])
|
2016-04-08 12:03:41 +00:00
|
|
|
elsif !options['refresh_token'].nil? && !options['user'].nil?
|
|
|
|
# refresh token
|
2016-08-18 00:00:26 +00:00
|
|
|
_success, msg = store_refresh_token(url, options['refresh_token'], true, options['user'], options['insecure'])
|
2016-04-13 11:47:33 +00:00
|
|
|
# TODO: we should login with the refreshtoken here
|
|
|
|
elsif !options['refresh_token'].nil?
|
2016-08-18 00:00:26 +00:00
|
|
|
_success, msg = login_refreshtoken(url, options)
|
2016-04-04 15:29:13 +00:00
|
|
|
else
|
2016-04-13 11:47:33 +00:00
|
|
|
puts 'Please run `inspec compliance login` with options --token or --refresh_token and --user'
|
|
|
|
exit 1
|
2016-04-04 15:29:13 +00:00
|
|
|
end
|
|
|
|
|
2016-08-18 00:00:26 +00:00
|
|
|
puts '', msg
|
2016-02-05 07:38:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
desc 'profiles', 'list all available profiles in Chef Compliance'
|
|
|
|
def profiles
|
2016-04-13 11:47:33 +00:00
|
|
|
config = Compliance::Configuration.new
|
2016-04-29 00:20:25 +00:00
|
|
|
return if !loggedin(config)
|
|
|
|
|
2016-08-17 16:15:11 +00:00
|
|
|
msg, profiles = Compliance::API.profiles(config)
|
2016-02-05 07:38:45 +00:00
|
|
|
if !profiles.empty?
|
|
|
|
# iterate over profiles
|
2016-02-05 10:06:00 +00:00
|
|
|
headline('Available profiles:')
|
2016-02-05 07:38:45 +00:00
|
|
|
profiles.each { |profile|
|
2016-02-05 10:06:00 +00:00
|
|
|
li("#{profile[:org]}/#{profile[:name]}")
|
2016-02-05 07:38:45 +00:00
|
|
|
}
|
|
|
|
else
|
2016-08-17 16:15:11 +00:00
|
|
|
puts msg, 'Could not find any profiles'
|
2016-08-18 16:34:09 +00:00
|
|
|
exit 1
|
2016-02-05 07:38:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'exec PROFILE', 'executes a Chef Compliance profile'
|
2016-03-06 14:07:12 +00:00
|
|
|
exec_options
|
2016-02-05 07:38:45 +00:00
|
|
|
def exec(*tests)
|
2016-04-29 00:20:25 +00:00
|
|
|
config = Compliance::Configuration.new
|
|
|
|
return if !loggedin(config)
|
|
|
|
|
2016-02-05 07:38:45 +00:00
|
|
|
# iterate over tests and add compliance scheme
|
|
|
|
tests = tests.map { |t| 'compliance://' + t }
|
|
|
|
|
|
|
|
# execute profile from inspec exec implementation
|
|
|
|
diagnose
|
2016-02-21 22:17:01 +00:00
|
|
|
run_tests(tests, opts)
|
2016-02-05 07:38:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
desc 'upload PATH', 'uploads a local profile to Chef Compliance'
|
2016-02-05 10:06:00 +00:00
|
|
|
option :overwrite, type: :boolean, default: false,
|
|
|
|
desc: 'Overwrite existing profile on Chef Compliance.'
|
2016-04-29 00:20:25 +00:00
|
|
|
def upload(path) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, PerceivedComplexity, Metrics/CyclomaticComplexity
|
|
|
|
config = Compliance::Configuration.new
|
|
|
|
return if !loggedin(config)
|
|
|
|
|
2016-04-13 14:54:29 +00:00
|
|
|
unless File.exist?(path)
|
|
|
|
puts "Directory #{path} does not exist."
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
2016-02-05 10:06:00 +00:00
|
|
|
o = options.dup
|
|
|
|
configure_logger(o)
|
|
|
|
# check the profile, we only allow to upload valid profiles
|
2016-02-22 20:11:49 +00:00
|
|
|
profile = Inspec::Profile.for_target(path, o)
|
2016-02-05 10:06:00 +00:00
|
|
|
|
|
|
|
# start verification process
|
|
|
|
error_count = 0
|
|
|
|
error = lambda { |msg|
|
|
|
|
error_count += 1
|
|
|
|
puts msg
|
|
|
|
}
|
|
|
|
|
|
|
|
result = profile.check
|
|
|
|
unless result[:summary][:valid]
|
|
|
|
error.call('Profile check failed. Please fix the profile before upload.')
|
|
|
|
else
|
|
|
|
puts('Profile is valid')
|
|
|
|
end
|
|
|
|
|
|
|
|
# determine user information
|
|
|
|
if config['token'].nil? || config['user'].nil?
|
|
|
|
error.call('Please login via `inspec compliance login`')
|
|
|
|
end
|
|
|
|
|
|
|
|
# owner
|
|
|
|
owner = config['user']
|
|
|
|
# read profile name from inspec.yml
|
|
|
|
profile_name = profile.params[:name]
|
|
|
|
|
|
|
|
# check that the profile is not uploaded already,
|
|
|
|
# confirm upload to the user (overwrite with --force)
|
2016-04-13 11:47:33 +00:00
|
|
|
if Compliance::API.exist?(config, "#{owner}/#{profile_name}") && !options['overwrite']
|
2016-02-05 10:06:00 +00:00
|
|
|
error.call('Profile exists on the server, use --overwrite')
|
|
|
|
end
|
|
|
|
|
|
|
|
# abort if we found an error
|
|
|
|
if error_count > 0
|
|
|
|
puts "Found #{error_count} error(s)"
|
|
|
|
exit 1
|
|
|
|
end
|
2016-02-05 07:38:45 +00:00
|
|
|
|
2016-02-05 10:06:00 +00:00
|
|
|
# if it is a directory, tar it to tmp directory
|
|
|
|
if File.directory?(path)
|
2016-03-23 15:13:23 +00:00
|
|
|
archive_path = Dir::Tmpname.create([profile_name, '.tar.gz']) {}
|
|
|
|
# archive_path = file.path
|
2016-02-05 10:06:00 +00:00
|
|
|
puts "Generate temporary profile archive at #{archive_path}"
|
2016-03-23 15:13:23 +00:00
|
|
|
profile.archive({ output: archive_path, ignore_errors: false, overwrite: true })
|
2016-02-05 10:06:00 +00:00
|
|
|
else
|
|
|
|
archive_path = path
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "Start upload to #{owner}/#{profile_name}"
|
2016-03-23 15:40:01 +00:00
|
|
|
pname = ERB::Util.url_encode(profile_name)
|
2016-02-05 10:06:00 +00:00
|
|
|
|
2016-04-13 11:47:33 +00:00
|
|
|
puts 'Uploading to Chef Compliance'
|
|
|
|
success, msg = Compliance::API.upload(config, owner, pname, archive_path)
|
2016-02-05 07:38:45 +00:00
|
|
|
|
2016-02-05 10:06:00 +00:00
|
|
|
if success
|
2016-02-05 07:38:45 +00:00
|
|
|
puts 'Successfully uploaded profile'
|
|
|
|
else
|
2016-02-05 10:06:00 +00:00
|
|
|
puts 'Error during profile upload:'
|
|
|
|
puts msg
|
2016-08-18 16:34:09 +00:00
|
|
|
exit 1
|
2016-02-05 07:38:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'version', 'displays the version of the Chef Compliance server'
|
|
|
|
def version
|
2016-04-04 15:29:13 +00:00
|
|
|
config = Compliance::Configuration.new
|
|
|
|
info = Compliance::API.version(config['server'], config['insecure'])
|
2016-02-05 07:38:45 +00:00
|
|
|
if !info.nil? && info['version']
|
|
|
|
puts "Chef Compliance version: #{info['version']}"
|
|
|
|
else
|
|
|
|
puts 'Could not determine server version.'
|
2016-08-18 16:34:09 +00:00
|
|
|
exit 1
|
2016-02-05 07:38:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'logout', 'user logout from Chef Compliance'
|
|
|
|
def logout
|
2016-04-13 14:54:29 +00:00
|
|
|
config = Compliance::Configuration.new
|
|
|
|
unless config.supported?(:oidc) || config['token'].nil?
|
2016-04-13 11:47:33 +00:00
|
|
|
config = Compliance::Configuration.new
|
|
|
|
url = "#{config['server']}/logout"
|
|
|
|
Compliance::API.post(url, config['token'], config['insecure'], !config.supported?(:oidc))
|
2016-04-04 15:29:13 +00:00
|
|
|
end
|
|
|
|
|
2016-04-13 11:47:33 +00:00
|
|
|
success = config.destroy
|
|
|
|
|
2016-04-04 15:29:13 +00:00
|
|
|
if success
|
2016-02-05 07:38:45 +00:00
|
|
|
puts 'Successfully logged out'
|
|
|
|
else
|
|
|
|
puts 'Could not log out'
|
|
|
|
end
|
|
|
|
end
|
2016-04-13 11:47:33 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def login_refreshtoken(url, options)
|
|
|
|
success, msg, access_token = Compliance::API.post_refresh_token(url, options['refresh_token'], options['insecure'])
|
|
|
|
if success
|
|
|
|
config = Compliance::Configuration.new
|
|
|
|
config['server'] = url
|
|
|
|
config['token'] = access_token
|
|
|
|
config['insecure'] = options['insecure']
|
|
|
|
config['version'] = Compliance::API.version(url, options['insecure'])
|
|
|
|
config.store
|
|
|
|
end
|
|
|
|
|
|
|
|
[success, msg]
|
|
|
|
end
|
|
|
|
|
|
|
|
def login_legacy(url, username, password, insecure)
|
|
|
|
config = Compliance::Configuration.new
|
|
|
|
success, data = Compliance::API.legacy_login_post(url+'/oauth/token', username, password, insecure)
|
|
|
|
if !data.nil?
|
|
|
|
tokendata = JSON.parse(data)
|
|
|
|
if tokendata['access_token']
|
|
|
|
config['server'] = url
|
|
|
|
config['user'] = username
|
|
|
|
config['token'] = tokendata['access_token']
|
|
|
|
config['insecure'] = insecure
|
|
|
|
config['version'] = Compliance::API.version(url, insecure)
|
|
|
|
config.store
|
|
|
|
success = true
|
|
|
|
msg = 'Successfully authenticated'
|
|
|
|
else
|
2016-08-18 00:00:26 +00:00
|
|
|
msg = 'Response does not include a token'
|
2016-04-13 11:47:33 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
msg = "Authentication failed for Server: #{url}"
|
|
|
|
end
|
|
|
|
[success, msg]
|
|
|
|
end
|
|
|
|
|
|
|
|
# saves a user access token (limited time)
|
|
|
|
def store_access_token(url, user, token, insecure)
|
|
|
|
config = Compliance::Configuration.new
|
|
|
|
config['server'] = url
|
|
|
|
config['insecure'] = insecure
|
|
|
|
config['user'] = user
|
|
|
|
config['token'] = token
|
|
|
|
config['version'] = Compliance::API.version(url, insecure)
|
|
|
|
config.store
|
|
|
|
|
|
|
|
[true, 'access token stored']
|
|
|
|
end
|
|
|
|
|
|
|
|
# saves the a user refresh token supplied by the user
|
|
|
|
def store_refresh_token(url, refresh_token, verify, user, insecure)
|
|
|
|
config = Compliance::Configuration.new
|
|
|
|
config['server'] = url
|
|
|
|
config['refresh_token'] = refresh_token
|
|
|
|
config['user'] = user
|
|
|
|
config['insecure'] = insecure
|
|
|
|
config['version'] = Compliance::API.version(url, insecure)
|
|
|
|
|
|
|
|
if !verify
|
|
|
|
config.store
|
|
|
|
success = true
|
|
|
|
msg = 'refresh token stored'
|
|
|
|
else
|
|
|
|
success, msg, access_token = Compliance::API.post_refresh_token(url, refresh_token, insecure)
|
|
|
|
if success
|
|
|
|
config['token'] = access_token
|
|
|
|
config.store
|
|
|
|
msg = 'token verified and stored'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
[success, msg]
|
|
|
|
end
|
2016-04-29 00:20:25 +00:00
|
|
|
|
|
|
|
def loggedin(config)
|
|
|
|
serverknown = !config['server'].nil?
|
|
|
|
puts 'You need to login first with `inspec compliance login`' if !serverknown
|
|
|
|
serverknown
|
|
|
|
end
|
2016-02-05 07:38:45 +00:00
|
|
|
end
|
|
|
|
|
2016-02-05 13:48:55 +00:00
|
|
|
# register the subcommand to Inspec CLI registry
|
2016-02-08 21:25:07 +00:00
|
|
|
Inspec::Plugins::CLI.add_subcommand(ComplianceCLI, 'compliance', 'compliance SUBCOMMAND ...', 'Chef Compliance commands', {})
|
2016-02-05 07:38:45 +00:00
|
|
|
end
|