Module: InspecPlugins::Compliance::API::Login::Automate2Server

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

Class Method Summary collapse

Class Method Details

.login(options) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api/login.rb', line 21

def self.(options)
  verify_thor_options(options)

  options["url"] = options["server"] + "/api/v0"
  token = options["dctoken"] || options["token"]
  success, msg = API::Login.(options)
  success ? store_access_token(options, token) : msg
end

.store_access_token(options, token) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api/login.rb', line 30

def self.store_access_token(options, token)
  config = InspecPlugins::Compliance::Configuration.new
  config.clean

  config["automate"] = {}
  config["automate"]["ent"] = "automate"
  config["automate"]["token_type"] = "dctoken"
  config["server"] = options["url"]
  config["user"] = options["user"]
  config["owner"] = options["user"]
  config["insecure"] = options["insecure"] || false
  config["server_type"] = options["server_type"]
  config["token"] = token
  config["version"] = "0"

  config.store
  API::Login.configuration_stored_message(config)
end

.verify_thor_options(o) ⇒ Object

Raises:

  • (ArgumentError)


49
50
51
52
53
54
55
56
57
58
59
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api/login.rb', line 49

def self.verify_thor_options(o)
  error_msg = []

  error_msg.push("Please specify a user using `--user='USER'`") if o["user"].nil?

  if o["token"].nil? && o["dctoken"].nil?
    error_msg.push("Please specify a token using `--token='APITOKEN'`")
  end

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