2016-02-05 07:38:45 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
# author: Christoph Hartmann
|
|
|
|
# author: Dominik Richter
|
|
|
|
|
|
|
|
require 'net/http'
|
|
|
|
require 'uri'
|
|
|
|
|
|
|
|
module Compliance
|
2017-05-16 21:56:56 +00:00
|
|
|
class ServerConfigurationMissing < StandardError
|
|
|
|
end
|
|
|
|
|
2016-02-05 07:38:45 +00:00
|
|
|
# API Implementation does not hold any state by itself,
|
|
|
|
# everything will be stored in local Configuration store
|
2016-11-15 19:19:39 +00:00
|
|
|
class API # rubocop:disable Metrics/ClassLength
|
2016-04-13 11:47:33 +00:00
|
|
|
# return all compliance profiles available for the user
|
|
|
|
def self.profiles(config)
|
2017-05-16 21:56:56 +00:00
|
|
|
# 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']}"
|
|
|
|
else
|
|
|
|
raise ServerConfigurationMissing
|
|
|
|
end
|
|
|
|
|
2016-11-30 14:30:11 +00:00
|
|
|
headers = get_headers(config)
|
|
|
|
response = Compliance::HTTP.get(url, headers, config['insecure'])
|
2016-04-13 11:47:33 +00:00
|
|
|
data = response.body
|
2016-08-17 16:15:11 +00:00
|
|
|
response_code = response.code
|
|
|
|
case response_code
|
|
|
|
when '200'
|
|
|
|
msg = 'success'
|
2016-04-13 11:47:33 +00:00
|
|
|
profiles = JSON.parse(data)
|
|
|
|
# iterate over profiles
|
2017-05-16 21:56:56 +00:00
|
|
|
if is_compliance_server?(config)
|
2017-04-13 15:24:17 +00:00
|
|
|
mapped_profiles = []
|
|
|
|
profiles.values.each { |org|
|
|
|
|
mapped_profiles += org.values
|
|
|
|
}
|
2017-05-16 21:56:56 +00:00
|
|
|
# Chef Automate pre 0.8.0
|
|
|
|
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
|
|
|
|
}
|
2016-11-15 19:19:39 +00:00
|
|
|
end
|
2016-08-17 16:15:11 +00:00
|
|
|
return msg, mapped_profiles
|
|
|
|
when '401'
|
|
|
|
msg = '401 Unauthorized. Please check your token.'
|
|
|
|
return msg, []
|
2016-04-04 15:29:13 +00:00
|
|
|
else
|
2016-08-17 16:15:11 +00:00
|
|
|
msg = "An unexpected error occurred (HTTP #{response_code}): #{response.message}"
|
|
|
|
return msg, []
|
2016-04-04 15:29:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# return the server api version
|
|
|
|
# NB this method does not use Compliance::Configuration to allow for using
|
|
|
|
# it before we know the version (e.g. oidc or not)
|
2017-05-16 21:56:56 +00:00
|
|
|
def self.version(config)
|
|
|
|
url = config['server']
|
|
|
|
insecure = config['insecure']
|
|
|
|
|
|
|
|
raise ServerConfigurationMissing if url.nil?
|
|
|
|
|
|
|
|
headers = get_headers(config)
|
|
|
|
response = Compliance::HTTP.get(url+'/version', headers, insecure)
|
|
|
|
return {} if response.code == '404'
|
2017-05-25 19:01:20 +00:00
|
|
|
|
2017-05-16 21:56:56 +00:00
|
|
|
data = response.body
|
2017-05-25 19:01:20 +00:00
|
|
|
return {} if data.nil? || data.empty?
|
2017-05-16 21:56:56 +00:00
|
|
|
|
2017-05-25 19:01:20 +00:00
|
|
|
parsed = JSON.parse(data)
|
|
|
|
return {} unless parsed.key?('version') && !parsed['version'].empty?
|
|
|
|
|
|
|
|
parsed
|
2016-02-05 07:38:45 +00:00
|
|
|
end
|
|
|
|
|
2016-02-05 10:06:00 +00:00
|
|
|
# verifies that a profile
|
2016-04-13 11:47:33 +00:00
|
|
|
def self.exist?(config, profile)
|
2016-08-18 16:34:09 +00:00
|
|
|
_msg, profiles = Compliance::API.profiles(config)
|
2017-09-13 20:53:36 +00:00
|
|
|
owner, id, ver = profile_split(profile)
|
2016-02-05 10:06:00 +00:00
|
|
|
if !profiles.empty?
|
2017-09-13 20:53:36 +00:00
|
|
|
profiles.any? do |p|
|
|
|
|
p['owner_id'] == owner &&
|
|
|
|
p['name'] == id &&
|
|
|
|
(ver.nil? || p['version'] == ver)
|
|
|
|
end
|
2016-02-05 10:06:00 +00:00
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-13 11:47:33 +00:00
|
|
|
def self.upload(config, owner, profile_name, archive_path)
|
2017-05-16 21:56:56 +00:00
|
|
|
# Chef Compliance
|
|
|
|
if is_compliance_server?(config)
|
|
|
|
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']}"
|
|
|
|
# Chef Automate
|
|
|
|
else
|
|
|
|
url = "#{config['server']}/profiles/#{config['user']}"
|
|
|
|
end
|
|
|
|
|
2016-11-30 14:30:11 +00:00
|
|
|
headers = get_headers(config)
|
|
|
|
res = Compliance::HTTP.post_file(url, headers, archive_path, config['insecure'])
|
2016-04-13 11:47:33 +00:00
|
|
|
[res.is_a?(Net::HTTPSuccess), res.body]
|
2016-02-05 07:38:45 +00:00
|
|
|
end
|
|
|
|
|
2017-01-18 01:38:34 +00:00
|
|
|
# Use username and refresh_token to get an API access token
|
2016-09-19 14:00:35 +00:00
|
|
|
def self.get_token_via_refresh_token(url, refresh_token, insecure)
|
2016-04-13 11:47:33 +00:00
|
|
|
uri = URI.parse("#{url}/login")
|
2016-03-23 13:32:10 +00:00
|
|
|
req = Net::HTTP::Post.new(uri.path)
|
2016-09-19 14:00:35 +00:00
|
|
|
req.body = { token: refresh_token }.to_json
|
2016-04-13 11:47:33 +00:00
|
|
|
access_token = nil
|
|
|
|
response = Compliance::HTTP.send_request(uri, req, insecure)
|
|
|
|
data = response.body
|
2016-09-19 14:00:35 +00:00
|
|
|
if response.code == '200'
|
2016-03-23 13:32:10 +00:00
|
|
|
begin
|
|
|
|
tokendata = JSON.parse(data)
|
|
|
|
access_token = tokendata['access_token']
|
2016-09-19 14:00:35 +00:00
|
|
|
msg = 'Successfully fetched API access token'
|
2016-03-23 13:32:10 +00:00
|
|
|
success = true
|
|
|
|
rescue JSON::ParserError => e
|
|
|
|
success = false
|
|
|
|
msg = e.message
|
|
|
|
end
|
|
|
|
else
|
|
|
|
success = false
|
2016-09-19 14:00:35 +00:00
|
|
|
msg = "Failed to authenticate to #{url} \n\
|
|
|
|
Response code: #{response.code}\n Body: #{response.body}"
|
|
|
|
end
|
|
|
|
|
|
|
|
[success, msg, access_token]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Use username and password to get an API access token
|
|
|
|
def self.get_token_via_password(url, username, password, insecure)
|
|
|
|
uri = URI.parse("#{url}/login")
|
|
|
|
req = Net::HTTP::Post.new(uri.path)
|
|
|
|
req.body = { userid: username, password: password }.to_json
|
|
|
|
access_token = nil
|
|
|
|
response = Compliance::HTTP.send_request(uri, req, insecure)
|
|
|
|
data = response.body
|
|
|
|
if response.code == '200'
|
|
|
|
access_token = data
|
|
|
|
msg = 'Successfully fetched an API access token valid for 12 hours'
|
|
|
|
success = true
|
|
|
|
else
|
|
|
|
success = false
|
|
|
|
msg = "Failed to authenticate to #{url} \n\
|
|
|
|
Response code: #{response.code}\n Body: #{response.body}"
|
2016-03-23 13:32:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
[success, msg, access_token]
|
|
|
|
end
|
2016-11-30 14:30:11 +00:00
|
|
|
|
|
|
|
def self.get_headers(config)
|
2017-01-18 01:38:34 +00:00
|
|
|
token = get_token(config)
|
2017-05-16 21:56:56 +00:00
|
|
|
if is_automate_server?(config)
|
2016-11-30 14:30:11 +00:00
|
|
|
headers = { 'chef-delivery-enterprise' => config['automate']['ent'] }
|
|
|
|
if config['automate']['token_type'] == 'dctoken'
|
2017-01-18 01:38:34 +00:00
|
|
|
headers['x-data-collector-token'] = token
|
2016-11-30 14:30:11 +00:00
|
|
|
else
|
|
|
|
headers['chef-delivery-user'] = config['user']
|
2017-01-18 01:38:34 +00:00
|
|
|
headers['chef-delivery-token'] = token
|
2016-11-30 14:30:11 +00:00
|
|
|
end
|
|
|
|
else
|
2017-01-18 01:38:34 +00:00
|
|
|
headers = { 'Authorization' => "Bearer #{token}" }
|
2016-11-30 14:30:11 +00:00
|
|
|
end
|
|
|
|
headers
|
|
|
|
end
|
2017-01-05 11:37:43 +00:00
|
|
|
|
2017-01-18 01:38:34 +00:00
|
|
|
def self.get_token(config)
|
|
|
|
return config['token'] unless config['refresh_token']
|
|
|
|
_success, _msg, token = get_token_via_refresh_token(config['server'], config['refresh_token'], config['insecure'])
|
|
|
|
token
|
|
|
|
end
|
|
|
|
|
2017-01-05 11:37:43 +00:00
|
|
|
def self.target_url(config, profile)
|
2017-09-13 20:53:36 +00:00
|
|
|
owner, id, ver = profile_split(profile)
|
|
|
|
|
|
|
|
return "#{config['server']}/owners/#{owner}/compliance/#{id}/tar" unless is_automate_server?(config)
|
|
|
|
|
|
|
|
if ver.nil?
|
|
|
|
"#{config['server']}/profiles/#{owner}/#{id}/tar"
|
2017-01-05 11:37:43 +00:00
|
|
|
else
|
2017-09-13 20:53:36 +00:00
|
|
|
"#{config['server']}/profiles/#{owner}/#{id}/version/#{ver}/tar"
|
2017-01-05 11:37:43 +00:00
|
|
|
end
|
2017-09-13 20:53:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.profile_split(profile)
|
|
|
|
owner, id = profile.split('/')
|
|
|
|
id, version = id.split('#')
|
|
|
|
[owner, id, version]
|
2017-01-05 11:37:43 +00:00
|
|
|
end
|
2017-05-16 21:56:56 +00:00
|
|
|
|
|
|
|
# returns a parsed url for `admin/profile` or `compliance://admin/profile`
|
|
|
|
def self.sanitize_profile_name(profile)
|
|
|
|
if URI(profile).scheme == 'compliance'
|
|
|
|
uri = URI(profile)
|
|
|
|
else
|
|
|
|
uri = URI("compliance://#{profile}")
|
|
|
|
end
|
|
|
|
uri.to_s.sub(%r{^compliance:\/\/}, '')
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.is_compliance_server?(config)
|
|
|
|
config['server_type'] == 'compliance'
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.is_automate_server_pre_080?(config)
|
2017-06-23 15:29:50 +00:00
|
|
|
# Automate versions before 0.8.x do not have a valid version in the config
|
2017-06-13 08:05:09 +00:00
|
|
|
return false unless config['server_type'] == 'automate'
|
2017-06-23 15:29:50 +00:00
|
|
|
server_version_from_config(config).nil?
|
2017-05-16 21:56:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.is_automate_server_080_and_later?(config)
|
2017-06-13 08:05:09 +00:00
|
|
|
# Automate versions 0.8.x and later will have a "version" key in the config
|
2017-06-23 15:29:50 +00:00
|
|
|
# that is properly parsed out via server_version_from_config below
|
2017-06-13 08:05:09 +00:00
|
|
|
return false unless config['server_type'] == 'automate'
|
2017-06-23 15:29:50 +00:00
|
|
|
!server_version_from_config(config).nil?
|
2017-05-16 21:56:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.is_automate_server?(config)
|
|
|
|
config['server_type'] == 'automate'
|
|
|
|
end
|
2017-06-23 15:29:50 +00:00
|
|
|
|
|
|
|
def self.server_version_from_config(config)
|
|
|
|
# Automate versions 0.8.x and later will have a "version" key in the config
|
|
|
|
# that looks like: "version":{"api":"compliance","version":"0.8.24"}
|
|
|
|
return nil unless config.key?('version')
|
|
|
|
return nil unless config['version'].is_a?(Hash)
|
|
|
|
config['version']['version']
|
|
|
|
end
|
2016-02-05 07:38:45 +00:00
|
|
|
end
|
|
|
|
end
|