Merge pull request #1402 from chef/chris-rock/compliance-download

Download InSpec profiles from Chef Compliance
This commit is contained in:
Dominik Richter 2017-01-05 14:02:53 +01:00 committed by GitHub
commit 7fbc73db86
3 changed files with 40 additions and 11 deletions

View file

@ -143,5 +143,15 @@ Please login using `inspec compliance login https://compliance.test --user admin
end end
headers headers
end end
def self.target_url(config, profile)
if config['server_type'] == 'automate'
target = "#{config['server']}/#{profile}/tar"
else
owner, id = profile.split('/')
target = "#{config['server']}/owners/#{owner}/compliance/#{id}/tar"
end
target
end
end end
end end

View file

@ -116,6 +116,35 @@ module Compliance
run_tests(tests, opts) run_tests(tests, opts)
end end
desc 'download PROFILE', 'downloads a profile from Chef Compliance'
option :name, type: :string,
desc: 'Name of the archive filename (file type will be added)'
def download(profile_name)
o = options.dup
configure_logger(o)
config = Compliance::Configuration.new
return if !loggedin(config)
if Compliance::API.exist?(config, profile_name)
puts "Downloading `#{profile_name}`"
fetcher = Compliance::Fetcher.resolve(
{
compliance: profile_name,
},
)
# we provide a name, the fetcher adds the extension
_owner, id = profile_name.split('/')
file_name = fetcher.fetch(o.name || id)
puts "Profile stored to #{file_name}"
else
puts "Profile #{profile_name} is not available in Chef Compliance."
exit 1
end
end
desc 'upload PATH', 'uploads a local profile to Chef Compliance' desc 'upload PATH', 'uploads a local profile to Chef Compliance'
option :overwrite, type: :boolean, default: false, option :overwrite, type: :boolean, default: false,
desc: 'Overwrite existing profile on Chef Compliance.' desc: 'Overwrite existing profile on Chef Compliance.'

View file

@ -53,23 +53,13 @@ EOF
if !Compliance::API.exist?(config, profile) if !Compliance::API.exist?(config, profile)
fail Inspec::FetcherFailure, "The compliance profile #{profile} was not found on the configured compliance server" fail Inspec::FetcherFailure, "The compliance profile #{profile} was not found on the configured compliance server"
end end
profile_fetch_url = target_url(profile, config) profile_fetch_url = Compliance::API.target_url(config, profile)
end end
new(profile_fetch_url, config) new(profile_fetch_url, config)
rescue URI::Error => _e rescue URI::Error => _e
nil nil
end end
def self.target_url(profile, config)
if config['server_type'] == 'automate'
target = "#{config['server']}/#{profile}/tar"
else
owner, id = profile.split('/')
target = "#{config['server']}/owners/#{owner}/compliance/#{id}/tar"
end
target
end
# We want to save compliance: in the lockfile rather than url: to # We want to save compliance: in the lockfile rather than url: to
# make sure we go back through the Compliance API handling. # make sure we go back through the Compliance API handling.
def resolved_source def resolved_source