Module: InspecPlugins::Compliance::API::Login::ComplianceServer

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

Constant Summary

Constants included from Inspec::Dist

Inspec::Dist::AUTOMATE_PRODUCT_NAME, Inspec::Dist::COMPLIANCE_PRODUCT_NAME, Inspec::Dist::EXEC_NAME, Inspec::Dist::PRODUCT_NAME, Inspec::Dist::SERVER_PRODUCT_NAME

Class Method Summary collapse

Class Method Details

.compliance_login_refresh_token(options) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api/login.rb', line 150

def self.(options)
  success, msg, token = InspecPlugins::Compliance::API.get_token_via_refresh_token(
    options["url"],
    options["refresh_token"],
    options["insecure"]
  )

  raise msg unless success

  compliance_store_access_token(options, token)
end

.compliance_login_user_pass(options) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api/login.rb', line 137

def self.(options)
  success, msg, token = InspecPlugins::Compliance::API.get_token_via_password(
    options["url"],
    options["user"],
    options["password"],
    options["insecure"]
  )

  raise msg unless success

  compliance_store_access_token(options, token)
end

.compliance_store_access_token(options, token) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api/login.rb', line 162

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

  config["user"] = options["user"] if options["user"]
  config["server"] = options["url"]
  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

.compliance_verify_thor_options(o) ⇒ Object

Compliance login requires ‘–user` or `–refresh_token` If `–user` then either `–password`, `–token`, or `–refresh-token`, is required

Raises:

  • (ArgumentError)


179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api/login.rb', line 179

def self.compliance_verify_thor_options(o)
  error_msg = []

  error_msg.push("Please specify a server using `#{EXEC_NAME} compliance login https://SERVER`") if o["server"].nil?

  if o["user"].nil? && o["refresh_token"].nil?
    error_msg.push("Please specify a `--user='USER'` or a `--refresh-token='TOKEN'`")
  end

  if o["user"] && o["password"].nil? && o["token"].nil? && o["refresh_token"].nil?
    error_msg.push("Please specify either a `--password`, `--token`, or `--refresh-token`")
  end

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

.login(options) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api/login.rb', line 123

def self.(options)
  compliance_verify_thor_options(options)

  options["url"] = options["server"] + "/api"

  if options["user"] && options["token"]
    compliance_store_access_token(options, options["token"])
  elsif options["user"] && options["password"]
    (options)
  elsif options["refresh_token"]
    (options)
  end
end