inspec/lib/extras/inspec-compliance/target.rb

68 lines
1.9 KiB
Ruby
Raw Normal View History

2016-02-05 07:38:45 +00:00
# encoding: utf-8
# author: Christoph Hartmann
# author: Dominik Richter
require 'uri'
# InSpec Target Helper for Chef Compliance
# reuses UrlHelper, but it knows the target server and the access token already
# similar to `inspec exec http://localhost:2134/owners/%base%/compliance/%ssh%/tar --user %token%`
module Compliance
class ChefComplianceHelper < Inspec::Targets::UrlHelper
def handles?(target)
# check for local scheme compliance://
uri = URI(target)
return unless URI(uri).scheme == 'compliance'
# check if we have a compliance token
config = Compliance::Configuration.new
return if config['token'].nil?
# get profile name
profile = get_profile_name(uri)
# verifies that the target e.g base/ssh exists
profiles = Compliance::API.get_profiles
if !profiles.empty?
index = profiles.index { |p| "#{p[:org]}/#{p[:name]}" == profile }
!index.nil? && index >= 0
else
false
end
rescue URI::Error => _e
false
end
# generates proper url
def resolve(target, opts = {})
profile = get_profile_name(URI(target))
# generates server url
target = build_target_url(profile)
config = Compliance::Configuration.new
opts['user'] = config['token']
puts target
super(target, opts)
end
# extracts profile name from url
def get_profile_name(uri)
uri.host + uri.path
end
def build_target_url(target)
owner, profile = target.split('/')
config = Compliance::Configuration.new
url = "#{config['server']}/owners/%owner_name%/compliance/%profile_name%/tar"
.gsub('%owner_name%', owner)
.gsub('%profile_name%', profile)
url
end
def to_s
'Chef Compliance Profile Loader'
end
end
Inspec::Targets.add_module('chefcompliance', ChefComplianceHelper.new)
end