Skip to content

Commit

Permalink
Merge deee9fb into 5d8384f
Browse files Browse the repository at this point in the history
  • Loading branch information
arlimus committed Oct 31, 2017
2 parents 5d8384f + deee9fb commit b95b210
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
19 changes: 13 additions & 6 deletions lib/bundles/inspec-compliance/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ class API # rubocop:disable Metrics/ClassLength
extend Compliance::API::Login

# return all compliance profiles available for the user
# the user is either specified in the options hash or by default
# the username of the account is used that is logged in
def self.profiles(config)
owner = config['owner'] || config['user']

# Chef Compliance
if is_compliance_server?(config)
url = "#{config['server']}/user/compliance"
# Chef Automate
elsif is_automate_server?(config)
url = "#{config['server']}/profiles/#{config['user']}"
url = "#{config['server']}/profiles/#{owner}"
else
raise ServerConfigurationMissing
end
Expand All @@ -45,9 +49,8 @@ def self.profiles(config)
elsif is_automate_server_pre_080?(config)
mapped_profiles = profiles.values.flatten
else
owner_id = config['user']
mapped_profiles = profiles.map { |e|
e['owner_id'] = owner_id
e['owner_id'] = owner
e
}
end
Expand Down Expand Up @@ -85,8 +88,12 @@ def self.version(config)

# verifies that a profile
def self.exist?(config, profile)
_msg, profiles = Compliance::API.profiles(config)
owner, id, ver = profile_split(profile)

c = config.dup
c['owner'] = owner
_msg, profiles = Compliance::API.profiles(config)

if !profiles.empty?
profiles.any? do |p|
p['owner_id'] == owner &&
Expand All @@ -104,10 +111,10 @@ def self.upload(config, owner, profile_name, archive_path)
url = "#{config['server']}/owners/#{owner}/compliance/#{profile_name}/tar"
# Chef Automate pre 0.8.0
elsif is_automate_server_pre_080?(config)
url = "#{config['server']}/#{config['user']}"
url = "#{config['server']}/#{owner}"
# Chef Automate
else
url = "#{config['server']}/profiles/#{config['user']}"
url = "#{config['server']}/profiles/#{owner}"
end

headers = get_headers(config)
Expand Down
24 changes: 17 additions & 7 deletions lib/bundles/inspec-compliance/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,16 @@ def login_automate(server)
end

desc 'profiles', 'list all available profiles in Chef Compliance'

option :owner, type: :string, required: false,
desc: 'owner whose profiles to list'
def profiles
config = Compliance::Configuration.new
return if !loggedin(config)

# overwrite user
c = config.dup
c['owner'] = options['owner']

msg, profiles = Compliance::API.profiles(config)
profiles.sort_by! { |hsh| hsh['title'] }
if !profiles.empty?
Expand Down Expand Up @@ -145,11 +150,16 @@ def download(profile_name)

desc 'upload PATH', 'uploads a local profile to Chef Compliance'
option :overwrite, type: :boolean, default: false,
desc: 'Overwrite existing profile on Chef Compliance.'
desc: 'Overwrite existing profile on Server.'
option :owner, type: :string, required: false,
desc: 'Owner that should own the profile'
def upload(path) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, PerceivedComplexity, Metrics/CyclomaticComplexity
config = Compliance::Configuration.new
return if !loggedin(config)

c = config.dup
c['owner'] = options['owner']

unless File.exist?(path)
puts "Directory #{path} does not exist."
exit 1
Expand Down Expand Up @@ -177,18 +187,18 @@ def upload(path) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Percei
end

# determine user information
if (config['token'].nil? && config['refresh_token'].nil?) || config['user'].nil?
if (c['token'].nil? && c['refresh_token'].nil?) || c['user'].nil?
error.call('Please login via `inspec compliance login`')
end

# owner
owner = config['user']
owner = c['owner'] || c['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)
if Compliance::API.exist?(config, "#{owner}/#{profile_name}") && !options['overwrite']
if Compliance::API.exist?(c, "#{owner}/#{profile_name}") && !options['overwrite']
error.call('Profile exists on the server, use --overwrite')
end

Expand All @@ -210,9 +220,9 @@ def upload(path) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Percei
puts "Start upload to #{owner}/#{profile_name}"
pname = ERB::Util.url_encode(profile_name)

Compliance::API.is_automate_server?(config) ? upload_msg = 'Uploading to Chef Automate' : upload_msg = 'Uploading to Chef Compliance'
Compliance::API.is_automate_server?(c) ? upload_msg = 'Uploading to Chef Automate' : upload_msg = 'Uploading to Chef Compliance'
puts upload_msg
success, msg = Compliance::API.upload(config, owner, pname, archive_path)
success, msg = Compliance::API.upload(c, owner, pname, archive_path)

if success
puts 'Successfully uploaded profile'
Expand Down

0 comments on commit b95b210

Please sign in to comment.