-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #227 from chef-cookbooks/chris-rock/automate-fetcher
add automate fetcher for chef solo
- Loading branch information
Showing
3 changed files
with
81 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
module ChefAutomate | ||
class Fetcher < Compliance::Fetcher | ||
name 'chef-automate' | ||
|
||
# it positions itself before `compliance` fetcher | ||
# only load it, if you want to use audit cookbook in Chef Solo with Chef Automate | ||
priority 502 | ||
|
||
def self.resolve(target) | ||
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? | ||
|
||
# we have detailed information available in our lockfile, no need to ask the server | ||
if target.respond_to?(:key?) && target.key?(:url) | ||
profile_fetch_url = target[:url] | ||
config = {} | ||
else | ||
# verifies that the target e.g base/ssh exists | ||
profile = sanitize_profile_name(uri) | ||
owner, id = profile.split('/') | ||
profile_path = "/compliance/profiles/#{owner}/#{id}/tar" | ||
dc = Chef::Config[:data_collector] | ||
url = URI(dc[:server_url]) | ||
url.path = profile_path | ||
profile_fetch_url = url.to_s | ||
config = { | ||
'token' => dc[:token], | ||
} | ||
end | ||
|
||
new(profile_fetch_url, config) | ||
rescue URI::Error => _e | ||
nil | ||
end | ||
|
||
# returns a parsed url for `admin/profile` or `compliance://admin/profile` | ||
# TODO: remove in future, copied from inspec to support older versions of inspec | ||
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 initialize(url, opts) | ||
options = { | ||
'insecure' => true, | ||
'token' => opts['token'], | ||
'server_type' => 'automate', | ||
'automate' => { | ||
'ent' => '', | ||
'token_type' => 'dctoken', | ||
}, | ||
} | ||
super(url, options) | ||
end | ||
|
||
def to_s | ||
'Chef Automate for Chef Solo Fetcher' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters