Class: Compliance::ChefComplianceHelper

Inherits:
Inspec::Targets::UrlHelper show all
Defined in:
lib/bundles/inspec-compliance/target.rb

Instance Method Summary collapse

Methods inherited from Inspec::Targets::UrlHelper

#download_archive, #resolve_archive

Instance Method Details

#build_target_url(target) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/bundles/inspec-compliance/target.rb', line 46

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

#get_profile_name(uri) ⇒ Object

extracts profile name from url



42
43
44
# File 'lib/bundles/inspec-compliance/target.rb', line 42

def get_profile_name(uri)
  uri.host + uri.path
end

#handles?(target) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/bundles/inspec-compliance/target.rb', line 12

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
  Compliance::API.exist?(profile)
rescue URI::Error => _e
  false
end

#resolve(target, opts = {}) ⇒ Object

generates proper url



31
32
33
34
35
36
37
38
39
# File 'lib/bundles/inspec-compliance/target.rb', line 31

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

#to_sObject



55
56
57
# File 'lib/bundles/inspec-compliance/target.rb', line 55

def to_s
  'Chef Compliance Profile Loader'
end