2016-02-05 07:38:45 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
# author: Christoph Hartmann
|
|
|
|
# author: Dominik Richter
|
|
|
|
|
|
|
|
require 'uri'
|
2016-02-22 01:13:42 +00:00
|
|
|
require 'inspec/fetcher'
|
2016-09-22 13:02:20 +00:00
|
|
|
require 'inspec/errors'
|
2016-02-05 07:38:45 +00:00
|
|
|
|
|
|
|
# InSpec Target Helper for Chef Compliance
|
|
|
|
# reuses UrlHelper, but it knows the target server and the access token already
|
|
|
|
# similar to `inspec exec http://localhost:2134/owners/%base%/compliance/%ssh%/tar --user %token%`
|
|
|
|
module Compliance
|
2016-09-09 09:21:54 +00:00
|
|
|
class Fetcher < Fetchers::Url
|
2016-02-22 01:13:42 +00:00
|
|
|
name 'compliance'
|
|
|
|
priority 500
|
2016-11-15 19:19:39 +00:00
|
|
|
def self.resolve(target) # rubocop:disable PerceivedComplexity
|
2016-09-09 09:21:54 +00:00
|
|
|
uri = if target.is_a?(String) && URI(target).scheme == 'compliance'
|
|
|
|
URI(target)
|
|
|
|
elsif target.respond_to?(:key?) && target.key?(:compliance)
|
|
|
|
URI("compliance://#{target[:compliance]}")
|
|
|
|
end
|
|
|
|
|
|
|
|
return nil if uri.nil?
|
2016-02-05 07:38:45 +00:00
|
|
|
|
|
|
|
# check if we have a compliance token
|
|
|
|
config = Compliance::Configuration.new
|
2016-09-22 13:02:20 +00:00
|
|
|
if config['token'].nil?
|
2016-11-15 19:19:39 +00:00
|
|
|
if config['automate'][0]
|
|
|
|
server = 'automate'
|
|
|
|
msg = 'inspec compliance automate https://your_automate_server --user USER --ent ENT --dctoken DCTOKEN or --usertoken USERTOKEN'
|
|
|
|
else
|
|
|
|
server = 'compliance'
|
|
|
|
msg = "inspec compliance login https://your_compliance_server --user admin --insecure --token 'PASTE TOKEN HERE' "
|
|
|
|
end
|
2016-09-22 13:02:20 +00:00
|
|
|
fail Inspec::FetcherFailure, <<EOF
|
|
|
|
|
2016-11-15 19:19:39 +00:00
|
|
|
Cannot fetch #{uri} because your #{server} token has not been
|
2016-09-22 13:02:20 +00:00
|
|
|
configured.
|
|
|
|
|
|
|
|
Please login using
|
|
|
|
|
2016-11-15 19:19:39 +00:00
|
|
|
#{msg}
|
2016-09-22 13:02:20 +00:00
|
|
|
EOF
|
|
|
|
end
|
2016-02-05 07:38:45 +00:00
|
|
|
|
|
|
|
# verifies that the target e.g base/ssh exists
|
2016-02-22 01:13:42 +00:00
|
|
|
profile = uri.host + uri.path
|
2016-09-22 13:02:20 +00:00
|
|
|
if !Compliance::API.exist?(config, profile)
|
2016-11-08 17:19:49 +00:00
|
|
|
fail Inspec::FetcherFailure, "The compliance profile #{profile} was not found on the configured compliance server"
|
2016-09-22 13:02:20 +00:00
|
|
|
end
|
2016-09-09 09:21:54 +00:00
|
|
|
new(target_url(profile, config), config)
|
2016-02-22 01:13:42 +00:00
|
|
|
rescue URI::Error => _e
|
|
|
|
nil
|
2016-02-05 07:38:45 +00:00
|
|
|
end
|
|
|
|
|
2016-09-09 09:21:54 +00:00
|
|
|
def self.target_url(profile, config)
|
2016-11-15 19:19:39 +00:00
|
|
|
if config['automate'][0]
|
|
|
|
target = "#{config['server']}/#{profile}/tar"
|
|
|
|
else
|
|
|
|
owner, id = profile.split('/')
|
|
|
|
target = "#{config['server']}/owners/#{owner}/compliance/#{id}/tar"
|
|
|
|
end
|
|
|
|
target
|
2016-02-05 07:38:45 +00:00
|
|
|
end
|
|
|
|
|
2016-09-09 09:21:54 +00:00
|
|
|
#
|
2016-09-09 13:46:36 +00:00
|
|
|
# We want to save compliance: in the lockfile rather than url: to
|
2016-09-09 09:21:54 +00:00
|
|
|
# make sure we go back through the ComplianceAPI handling.
|
|
|
|
#
|
|
|
|
def resolved_source
|
2016-09-09 13:46:36 +00:00
|
|
|
{ compliance: supermarket_profile_name }
|
2016-09-09 09:21:54 +00:00
|
|
|
end
|
|
|
|
|
2016-02-05 07:38:45 +00:00
|
|
|
def to_s
|
|
|
|
'Chef Compliance Profile Loader'
|
|
|
|
end
|
2016-09-09 09:21:54 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def supermarket_profile_name
|
|
|
|
m = %r{^#{@config['server']}/owners/(?<owner>[^/]+)/compliance/(?<id>[^/]+)/tar$}.match(@target)
|
|
|
|
"#{m[:owner]}/#{m[:id]}"
|
|
|
|
end
|
2016-02-05 07:38:45 +00:00
|
|
|
end
|
|
|
|
end
|