Module: InspecPlugins::Compliance::API::Login::AutomateServer

Defined in:
lib/plugins/inspec-compliance/lib/inspec-compliance/api/login.rb

Class Method Summary collapse

Class Method Details

.login(options) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api/login.rb', line 70

def self.(options)
  verify_thor_options(options)

  options['url'] = options['server'] + '/compliance'
  token = options['dctoken'] || options['token']
  store_access_token(options, token)
end

.store_access_token(options, token) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api/login.rb', line 78

def self.store_access_token(options, token)
  token_type = if options['token']
                 'usertoken'
               else
                 'dctoken'
               end

  config = InspecPlugins::Compliance::Configuration.new

  config.clean

  config['automate'] = {}
  config['automate']['ent'] = options['ent']
  config['automate']['token_type'] = token_type
  config['server'] = options['url']
  config['user'] = options['user']
  config['insecure'] = options['insecure'] || false
  config['server_type'] = options['server_type'].to_s
  config['token'] = token
  config['version'] = InspecPlugins::Compliance::API.version(config)

  config.store
  config
end

.verify_thor_options(o) ⇒ Object

Automate login requires ‘–ent`, `–user`, and either `–token` or `–dctoken`

Raises:

  • (ArgumentError)


104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api/login.rb', line 104

def self.verify_thor_options(o)
  error_msg = []

  error_msg.push('Please specify a user using `--user=\'USER\'`') if o['user'].nil?
  error_msg.push('Please specify an enterprise using `--ent=\'automate\'`') if o['ent'].nil?

  if o['token'].nil? && o['dctoken'].nil?
    error_msg.push('Please specify a token using `--token=\'AUTOMATE_TOKEN\'` or `--dctoken=\'DATA_COLLECTOR_TOKEN\'`')
  end

  raise ArgumentError, error_msg.join("\n") unless error_msg.empty?
end