Class: Compliance::Fetcher

Inherits:
Fetchers::Url show all
Defined in:
lib/bundles/inspec-compliance/target.rb

Constant Summary

Constants inherited from Fetchers::Url

Fetchers::Url::GITHUB_URL_REGEX, Fetchers::Url::GITHUB_URL_WITH_TREE_REGEX, Fetchers::Url::MIME_TYPES

Instance Attribute Summary

Attributes inherited from Fetchers::Url

#archive_path, #files

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Fetchers::Url

#cache_key, #fetch, #initialize, resolve_from_string, transform

Constructor Details

This class inherits a constructor from Fetchers::Url

Class Method Details

.resolve(target) ⇒ Object

rubocop:disable PerceivedComplexity, Metrics/CyclomaticComplexity, Metrics/AbcSize



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/bundles/inspec-compliance/target.rb', line 16

def self.resolve(target) # rubocop:disable PerceivedComplexity, Metrics/CyclomaticComplexity, Metrics/AbcSize
  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
    # check if we have a compliance token
    config = Compliance::Configuration.new
    if config['token'].nil? && config['refresh_token'].nil?
      if config['server_type'] == 'automate'
        server = 'automate'
        msg = 'inspec compliance login_automate https://your_automate_server --user USER --ent ENT --dctoken DCTOKEN or --usertoken USERTOKEN'
      else
        server = 'compliance'
        msg = "inspec compliance login https://your_compliance_server --user admin --insecure --token 'PASTE TOKEN HERE' "
      end
      raise Inspec::FetcherFailure, <<EOF

Cannot fetch #{uri} because your #{server} token has not been
configured.

Please login using

#{msg}
EOF
    end

    # verifies that the target e.g base/ssh exists
    profile = uri.host + uri.path
    if !Compliance::API.exist?(config, profile)
      raise Inspec::FetcherFailure, "The compliance profile #{profile} was not found on the configured compliance server"
    end
    profile_fetch_url = Compliance::API.target_url(config, profile)
  end
  # We need to pass the token to the fetcher
  config['token'] = Compliance::API.get_token(config)

  new(profile_fetch_url, config)
rescue URI::Error => _e
  nil
end

Instance Method Details

#resolved_sourceObject

We want to save compliance: in the lockfile rather than url: to make sure we go back through the Compliance API handling.



68
69
70
71
72
73
74
# File 'lib/bundles/inspec-compliance/target.rb', line 68

def resolved_source
  @resolved_source ||= {
    compliance: compliance_profile_name,
    url: @target,
    sha256: sha256,
  }
end

#to_sObject



76
77
78
# File 'lib/bundles/inspec-compliance/target.rb', line 76

def to_s
  'Chef Compliance Profile Loader'
end