-
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.
implement chef-server fetcher and reporter
Signed-off-by: Christoph Hartmann <chris@lollyrock.com>
- Loading branch information
1 parent
5628f4e
commit 9ee6d0f
Showing
11 changed files
with
252 additions
and
37 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
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,29 @@ | ||
# Example: Vagrant with Chef Server and Chef Compliance | ||
|
||
This directory contains a simple vagrant setup that expects you have a Chef Server already running. | ||
|
||
1.) Upload cookbook to Chef Server | ||
|
||
``` | ||
mkdir cookbooks | ||
cd cookbooks | ||
git clone https://github.com/chef-cookbooks/audit.git | ||
cd .. | ||
chef exec knife cookbook upload audit -o ./cookbooks -c test-chef-server/knife.rb | ||
``` | ||
|
||
2.) Adapt the chef Server settings in vagrant file: | ||
|
||
``` | ||
chef.chef_server_url = 'https://192.168.33.101/organizations/brewinc' | ||
chef.validation_key_path = 'brewinc-validator.pem' | ||
chef.validation_client_name = 'brewinc-validator' | ||
``` | ||
|
||
3.) Start node with chef-client | ||
|
||
``` | ||
vagrant up | ||
# or if you have it already up | ||
vagrant provision | ||
``` |
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,51 @@ | ||
# encoding: utf-8 | ||
# -*- mode: ruby -*- | ||
# vi: set ft=ruby : | ||
|
||
NODE_SCRIPT = <<EOF.freeze | ||
echo "Prepare Chef Client node" | ||
apt-get update | ||
# ensure the time is uptodate | ||
apt-get -y install ntp | ||
service ntp stop | ||
ntpdate -s time.nist.gov | ||
service ntp start | ||
EOF | ||
|
||
def set_hostname(server) | ||
server.vm.provision 'shell', inline: "hostname #{server.vm.hostname}" | ||
end | ||
|
||
Vagrant.configure(2) do |config| | ||
|
||
config.vm.define 'chef-client-node' do |server| | ||
server.vm.box = 'bento/ubuntu-14.04' | ||
server.vm.box_url = 'http://opscode-vm-bento.s3.amazonaws.com/vagrant/virtualbox/opscode_ubuntu-14.04_chef-provisionerless.box' | ||
server.vm.hostname = 'audit-node' | ||
server.vm.network 'private_network', ip: '192.168.33.102' | ||
server.vm.synced_folder '.', '/vagrant' | ||
config.vm.provision :shell, inline: NODE_SCRIPT.dup | ||
set_hostname(server) | ||
|
||
config.vm.provision :chef_client do |chef| | ||
chef.chef_server_url = 'https://192.168.33.101/organizations/brewinc' | ||
chef.validation_key_path = 'brewinc-validator.pem' | ||
chef.validation_client_name = 'brewinc-validator' | ||
chef.log_level = :debug | ||
chef.add_recipe 'audit::default' | ||
chef.json = { | ||
audit: { | ||
collector: "chef-server", | ||
insecure: true, | ||
profiles: [{ | ||
name: "linux", | ||
compliance: "base/linux" | ||
},{ | ||
name: "ssh", | ||
compliance: "base/ssh" | ||
}] | ||
}, | ||
} | ||
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
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,100 @@ | ||
# encoding: utf-8 | ||
# author: Christoph Hartmann | ||
|
||
require 'uri' | ||
|
||
require 'bundles/inspec-compliance/target' | ||
require 'inspec/fetcher' | ||
require 'inspec/errors' | ||
|
||
# This class implements an InSpec fetcher for for Chef Server. The implementation | ||
# is based on the Chef Compliance fetcher and only adapts the calls to redirect | ||
# the requests via Chef Server. | ||
# | ||
# This implementation depends on chef-client runtime, therefore it is only executable | ||
# inside of a chef-client run | ||
module ChefServer | ||
class Fetcher < Compliance::Fetcher | ||
name 'chef-server' | ||
|
||
# it positions itself before `compliance` fetcher | ||
# only load it, if the Chef Server is integrated with Chef Compliance | ||
priority 501 | ||
|
||
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? | ||
|
||
profile = uri.host + uri.path | ||
config = { | ||
'insecure' => true, | ||
} | ||
new(target_url(profile, config), config) | ||
rescue URI::Error => _e | ||
nil | ||
end | ||
|
||
def self.chef_server_url_base | ||
cs = URI(Chef::Config[:chef_server_url]) | ||
cs.path = '' | ||
cs.to_s | ||
end | ||
|
||
def self.chef_server_org | ||
Chef::Config[:chef_server_url].split('/').last | ||
end | ||
|
||
def self.target_url(profile, config) | ||
o, p = profile.split('/') | ||
reqpath ="organizations/#{chef_server_org}/owners/#{o}/compliance/#{p}/tar" | ||
|
||
if config['insecure'] | ||
Chef::Config[:verify_api_cert] = false | ||
Chef::Config[:ssl_verify_mode] = :verify_none | ||
end | ||
|
||
construct_url(chef_server_url_base + '/compliance/', reqpath) | ||
end | ||
|
||
# | ||
# We want to save compliance: in the lockfile rather than url: to | ||
# make sure we go back through the ComplianceAPI handling. | ||
# | ||
def resolved_source | ||
{ compliance: chef_server_url } | ||
end | ||
|
||
# Downloads archive to temporary file from Chef Compliance via Chef Server | ||
def download_archive_to_temp | ||
return @temp_archive_path if ! @temp_archive_path.nil? | ||
Inspec::Log.debug("Fetching URL: #{@target}") | ||
|
||
Chef::Config[:verify_api_cert] = false # FIXME | ||
Chef::Config[:ssl_verify_mode] = :verify_none # FIXME | ||
|
||
rest = Chef::ServerAPI.new(@target, Chef::Config) | ||
archive = with_http_rescue do | ||
rest.streaming_request(@target) | ||
end | ||
@archive_type = '.tar.gz' | ||
Inspec::Log.debug("Archive stored at temporary location: #{archive.path}") | ||
@temp_archive_path = archive.path | ||
end | ||
|
||
def to_s | ||
'Chef Server/Compliance Profile Loader' | ||
end | ||
|
||
private | ||
|
||
def chef_server_url | ||
m = %r{^#{@config['server']}/owners/(?<owner>[^/]+)/compliance/(?<id>[^/]+)/tar$}.match(@target) | ||
"#{m[:owner]}/#{m[:id]}" | ||
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
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
Oops, something went wrong.