Class: InspecPlugins::Compliance::API
- Inherits:
-
Object
- Object
- InspecPlugins::Compliance::API
show all
- Extended by:
- Login
- Includes:
- Inspec::Dist
- Defined in:
- lib/plugins/inspec-compliance/lib/inspec-compliance/api.rb,
lib/plugins/inspec-compliance/lib/inspec-compliance/api/login.rb
Overview
API Implementation does not hold any state by itself, everything will be stored in local Configuration store
Defined Under Namespace
Modules: Login
Constant Summary
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
-
.authenticate_login_using_version_api(url, api_token, insecure) ⇒ Object
Use API access token to validate login using version API.
-
.determine_server_type(url, insecure) ⇒ Object
-
.exist?(config, profile) ⇒ Boolean
verifies that a profile exists.
-
.get_headers(config) ⇒ Object
-
.get_token(config) ⇒ Object
-
.get_token_via_password(url, username, password, insecure) ⇒ Object
Use username and password to get an API access token.
-
.get_token_via_refresh_token(url, refresh_token, insecure) ⇒ Object
Use username and refresh_token to get an API access token.
-
.is_automate2_server?(config) ⇒ Boolean
-
.is_automate_server?(config) ⇒ Boolean
-
.is_automate_server_080_and_later?(config) ⇒ Boolean
-
.is_automate_server_pre_080?(config) ⇒ Boolean
-
.is_compliance_server?(config) ⇒ Boolean
-
.profile_split(profile) ⇒ Object
-
.profiles(config, profile_filter = nil) ⇒ Object
return all compliance profiles available for the user the user is either specified in the options hash or by default the username of the account is used that is logged in.
-
.sanitize_profile_name(profile) ⇒ Object
returns a parsed url for ‘admin/profile` or `compliance://admin/profile`.
-
.server_version_from_config(config) ⇒ Object
-
.target_is_automate2_server?(url, insecure) ⇒ Boolean
-
.target_is_automate_server?(url, insecure) ⇒ Boolean
-
.target_is_compliance_server?(url, insecure) ⇒ Boolean
-
.target_url(config, profile) ⇒ Object
-
.upload(config, owner, profile_name, archive_path) ⇒ Object
-
.version(config) ⇒ Object
return the server api version NB this method does not use Compliance::Configuration to allow for using it before we know the version (e.g. oidc or not).
Methods included from Login
authenticate_login, configuration_stored_message, login
Class Method Details
.authenticate_login_using_version_api(url, api_token, insecure) ⇒ Object
Use API access token to validate login using version API
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api.rb', line 174
def self.authenticate_login_using_version_api(url, api_token, insecure)
uri = URI.parse("#{url}/version")
req = Net::HTTP::Get.new(uri.path)
req["api-token"] = api_token
response = InspecPlugins::Compliance::HTTP.send_request(uri, req, insecure)
if response.code == "200"
msg = "Successfully Logged In"
success = true
else
success = false
msg = "Failed to authenticate to #{url} \n\Response code: #{response.code}\nBody: #{response.body}"
end
[success, msg]
end
|
.determine_server_type(url, insecure) ⇒ Object
299
300
301
302
303
304
305
306
307
308
309
310
|
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api.rb', line 299
def self.determine_server_type(url, insecure)
if target_is_automate2_server?(url, insecure)
:automate2
elsif target_is_automate_server?(url, insecure)
:automate
elsif target_is_compliance_server?(url, insecure)
:compliance
else
Inspec::Log.debug("Could not determine server type using known endpoints")
nil
end
end
|
.exist?(config, profile) ⇒ Boolean
verifies that a profile exists
117
118
119
120
|
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api.rb', line 117
def self.exist?(config, profile)
_msg, profiles = InspecPlugins::Compliance::API.profiles(config, profile)
!profiles.empty?
end
|
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api.rb', line 211
def self.(config)
token = get_token(config)
if is_automate_server?(config) || is_automate2_server?(config)
= { "chef-delivery-enterprise" => config["automate"]["ent"] }
if config["automate"]["token_type"] == "dctoken"
["x-data-collector-token"] = token
else
["chef-delivery-user"] = config["user"]
["chef-delivery-token"] = token
end
else
= { "Authorization" => "Bearer #{token}" }
end
end
|
.get_token(config) ⇒ Object
227
228
229
230
231
232
|
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api.rb', line 227
def self.get_token(config)
return config["token"] unless config["refresh_token"]
_success, _msg, token = get_token_via_refresh_token(config["server"], config["refresh_token"], config["insecure"])
token
end
|
.get_token_via_password(url, username, password, insecure) ⇒ Object
Use username and password to get an API access token
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api.rb', line 191
def self.get_token_via_password(url, username, password, insecure)
uri = URI.parse("#{url}/login")
req = Net::HTTP::Post.new(uri.path)
req.body = { userid: username, password: password }.to_json
access_token = nil
response = InspecPlugins::Compliance::HTTP.send_request(uri, req, insecure)
data = response.body
if response.code == "200"
access_token = data
msg = "Successfully fetched an API access token valid for 12 hours"
success = true
else
success = false
msg = "Failed to authenticate to #{url} \n\
Response code: #{response.code}\n Body: #{response.body}"
end
[success, msg, access_token]
end
|
.get_token_via_refresh_token(url, refresh_token, insecure) ⇒ Object
Use username and refresh_token to get an API access token
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api.rb', line 147
def self.get_token_via_refresh_token(url, refresh_token, insecure)
uri = URI.parse("#{url}/login")
req = Net::HTTP::Post.new(uri.path)
req.body = { token: refresh_token }.to_json
access_token = nil
response = InspecPlugins::Compliance::HTTP.send_request(uri, req, insecure)
data = response.body
if response.code == "200"
begin
tokendata = JSON.parse(data)
access_token = tokendata["access_token"]
msg = "Successfully fetched API access token"
success = true
rescue JSON::ParserError => e
success = false
msg = e.message
end
else
success = false
msg = "Failed to authenticate to #{url} \n\
Response code: #{response.code}\n Body: #{response.body}"
end
[success, msg, access_token]
end
|
.is_automate2_server?(config) ⇒ Boolean
282
283
284
|
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api.rb', line 282
def self.is_automate2_server?(config)
config["server_type"] == "automate2"
end
|
.is_automate_server?(config) ⇒ Boolean
286
287
288
|
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api.rb', line 286
def self.is_automate_server?(config)
config["server_type"] == "automate"
end
|
.is_automate_server_080_and_later?(config) ⇒ Boolean
274
275
276
277
278
279
280
|
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api.rb', line 274
def self.is_automate_server_080_and_later?(config)
return false unless config["server_type"] == "automate"
!server_version_from_config(config).nil?
end
|
.is_automate_server_pre_080?(config) ⇒ Boolean
267
268
269
270
271
272
|
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api.rb', line 267
def self.is_automate_server_pre_080?(config)
return false unless config["server_type"] == "automate"
server_version_from_config(config).nil?
end
|
.is_compliance_server?(config) ⇒ Boolean
263
264
265
|
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api.rb', line 263
def self.is_compliance_server?(config)
config["server_type"] == "compliance"
end
|
.profile_split(profile) ⇒ Object
247
248
249
250
251
|
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api.rb', line 247
def self.profile_split(profile)
owner, id = profile.split("/")
id, version = id.split("#")
[owner, id, version]
end
|
.profiles(config, profile_filter = nil) ⇒ Object
return all compliance profiles available for the user the user is either specified in the options hash or by default the username of the account is used that is logged in
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api.rb', line 25
def self.profiles(config, profile_filter = nil)
owner = config["owner"] || config["user"]
if is_compliance_server?(config)
url = "#{config["server"]}/user/compliance"
elsif is_automate2_server?(config)
url = "#{config["server"]}/compliance/profiles/search"
elsif is_automate_server?(config)
url = "#{config["server"]}/profiles/#{owner}"
else
raise ServerConfigurationMissing
end
= (config)
if profile_filter
_owner, id, ver = profile_split(profile_filter)
else
id, ver = nil
end
if is_automate2_server?(config)
body = { owner: owner, name: id }.to_json
response = InspecPlugins::Compliance::HTTP.(url, , body, config["insecure"])
else
response = InspecPlugins::Compliance::HTTP.get(url, , config["insecure"])
end
data = response.body
response_code = response.code
case response_code
when "200"
msg = "success"
profiles = JSON.parse(data)
if is_compliance_server?(config)
mapped_profiles = []
profiles.values.each do |org|
mapped_profiles += org.values
end
elsif is_automate_server_pre_080?(config)
mapped_profiles = profiles.values.flatten
elsif is_automate2_server?(config)
mapped_profiles = []
profiles["profiles"].each do |p|
mapped_profiles << p
end
else
mapped_profiles = profiles.map do |e|
e["owner_id"] = owner
e
end
end
mapped_profiles.select! do |p|
(!ver || p["version"] == ver) && (!id || p["name"] == id)
end
[msg, mapped_profiles]
when "401"
msg = "401 Unauthorized. Please check your token."
[msg, []]
else
msg = "An unexpected error occurred (HTTP #{response_code}): #{response.message}"
[msg, []]
end
end
|
.sanitize_profile_name(profile) ⇒ Object
returns a parsed url for ‘admin/profile` or `compliance://admin/profile`
254
255
256
257
258
259
260
261
|
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api.rb', line 254
def self.sanitize_profile_name(profile)
if URI(profile).scheme == "compliance"
uri = URI(profile)
else
uri = URI("compliance://#{profile}")
end
uri.to_s.sub(%r{^compliance:\/\/}, "")
end
|
.server_version_from_config(config) ⇒ Object
290
291
292
293
294
295
296
297
|
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api.rb', line 290
def self.server_version_from_config(config)
return nil unless config.key?("version")
return nil unless config["version"].is_a?(Hash)
config["version"]["version"]
end
|
.target_is_automate2_server?(url, insecure) ⇒ Boolean
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api.rb', line 312
def self.target_is_automate2_server?(url, insecure)
automate_endpoint = "/dex/auth"
response = InspecPlugins::Compliance::HTTP.get(url + automate_endpoint, nil, insecure)
if response.code == "400"
Inspec::Log.debug(
"Received 400 from #{url}#{automate_endpoint} - " \
"assuming target is a #{AUTOMATE_PRODUCT_NAME}2 instance"
)
true
else
Inspec::Log.debug(
"Received #{response.code} from #{url}#{automate_endpoint} - " \
"assuming target is not an #{AUTOMATE_PRODUCT_NAME}2 instance"
)
false
end
end
|
.target_is_automate_server?(url, insecure) ⇒ Boolean
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api.rb', line 330
def self.target_is_automate_server?(url, insecure)
automate_endpoint = "/compliance/version"
response = InspecPlugins::Compliance::HTTP.get(url + automate_endpoint, nil, insecure)
case response.code
when "401"
Inspec::Log.debug(
"Received 401 from #{url}#{automate_endpoint} - " \
"assuming target is a #{AUTOMATE_PRODUCT_NAME} instance"
)
true
when "200"
if response.body.include?("Are You Looking For the #{SERVER_PRODUCT_NAME}?")
Inspec::Log.debug(
"Received 200 from #{url}#{automate_endpoint} - " \
"assuming target is an #{AUTOMATE_PRODUCT_NAME} instance"
)
true
else
Inspec::Log.debug(
"Received 200 from #{url}#{automate_endpoint} " \
"but did not receive the Chef Manage page - " \
"assuming target is not a #{AUTOMATE_PRODUCT_NAME} instance"
)
false
end
else
Inspec::Log.debug(
"Received unexpected status code #{response.code} " \
"from #{url}#{automate_endpoint} - " \
"assuming target is not a #{AUTOMATE_PRODUCT_NAME} instance"
)
false
end
end
|
.target_is_compliance_server?(url, insecure) ⇒ Boolean
368
369
370
371
372
373
374
375
376
377
378
379
380
|
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api.rb', line 368
def self.target_is_compliance_server?(url, insecure)
compliance_endpoint = "/api/version"
response = InspecPlugins::Compliance::HTTP.get(url + compliance_endpoint, nil, insecure)
return false unless response.code == "200"
Inspec::Log.debug(
"Received 200 from #{url}#{compliance_endpoint} - " \
"assuming target is a #{AUTOMATE_PRODUCT_NAME} server"
)
true
end
|
.target_url(config, profile) ⇒ Object
234
235
236
237
238
239
240
241
242
243
244
245
|
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api.rb', line 234
def self.target_url(config, profile)
owner, id, ver = profile_split(profile)
return "#{config["server"]}/compliance/profiles/tar" if is_automate2_server?(config)
return "#{config["server"]}/owners/#{owner}/compliance/#{id}/tar" unless is_automate_server?(config)
if ver.nil?
"#{config["server"]}/profiles/#{owner}/#{id}/tar"
else
"#{config["server"]}/profiles/#{owner}/#{id}/version/#{ver}/tar"
end
end
|
.upload(config, owner, profile_name, archive_path) ⇒ Object
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api.rb', line 122
def self.upload(config, owner, profile_name, archive_path)
if is_compliance_server?(config)
url = "#{config["server"]}/owners/#{owner}/compliance/#{profile_name}/tar"
elsif is_automate_server_pre_080?(config)
url = "#{config["server"]}/#{owner}"
elsif is_automate2_server?(config)
url = "#{config["server"]}/compliance/profiles?owner=#{owner}"
else
url = "#{config["server"]}/profiles/#{owner}"
end
= (config)
if is_automate2_server?(config)
res = InspecPlugins::Compliance::HTTP.post_multipart_file(url, , archive_path, config["insecure"])
else
res = InspecPlugins::Compliance::HTTP.post_file(url, , archive_path, config["insecure"])
end
[res.is_a?(Net::HTTPSuccess), res.body]
end
|
.version(config) ⇒ Object
return the server api version NB this method does not use Compliance::Configuration to allow for using it before we know the version (e.g. oidc or not)
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/api.rb', line 97
def self.version(config)
url = config["server"]
insecure = config["insecure"]
raise ServerConfigurationMissing if url.nil?
= (config)
response = InspecPlugins::Compliance::HTTP.get(url + "/version", , insecure)
return {} if response.code == "404"
data = response.body
return {} if data.nil? || data.empty?
parsed = JSON.parse(data)
return {} unless parsed.key?("version") && !parsed["version"].empty?
parsed
end
|