oso-cloud 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0afcafa1d502f02d19ed93b21ad4606eaf59056114c496c5a9d8b72f55994c05
4
- data.tar.gz: e9bc9ad6450429c9d2c5a1574e4451316e1f043b666da2cfbdc3ae69e1e449cf
3
+ metadata.gz: 3991004d30f9fb3930a74494eb15f9658d469bf198440888ef4247b04e0e90fe
4
+ data.tar.gz: 19a847a4fa5ef0940606838879aa99cb5e1cb9f0b0b591fac9d30ee1b03adf90
5
5
  SHA512:
6
- metadata.gz: 8e837b4bd3d8c16aa1859c93453756a35c844c5371ab9cad18d2afb88748dfc413993be213da260f73bff9d9b83ef28a734e2cfd994e706220c92c4d2ce62df4
7
- data.tar.gz: 179d6e9639dd2cf447c4c71c6f888b5474f633ad1f323f15b46a8729e8acb86419e2e5b4f628628a4c8655d874881ee6e938d379dbd794f68ea6a18b593c580d
6
+ metadata.gz: a69a9097271580148040e9790d49b297d2ca4c90fb5c032d088f51b619e0e995b5348312e46e3748fc2d2d95a4c16cf9a98bba017d5e09292b64a65202b47b25
7
+ data.tar.gz: 3dfcf182d1283311f79c5ad806ff53e8e8a40c2f36b12cc54b459cfa3d23c215b92e5b59d24b9553df8e2c3a2b874a70e67f87e7ab2f9a647e492188dc4960fc
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- oso-cloud (1.1.0)
4
+ oso-cloud (1.2.0)
5
5
  faraday (~> 2.5.2)
6
6
  faraday-retry (~> 2.0.0)
7
7
 
@@ -14,7 +14,7 @@ GEM
14
14
  faraday-net_http (3.0.2)
15
15
  faraday-retry (2.0.0)
16
16
  faraday (~> 2.0)
17
- minitest (5.15.0)
17
+ minitest (5.18.0)
18
18
  rake (12.3.3)
19
19
  ruby2_keywords (0.0.5)
20
20
 
data/lib/oso/api.rb CHANGED
@@ -4,6 +4,7 @@ require 'faraday'
4
4
  require 'faraday/retry'
5
5
 
6
6
  require 'oso/helpers'
7
+ require 'oso/version'
7
8
 
8
9
  module OsoCloud
9
10
  # @!visibility private
@@ -202,7 +203,7 @@ module OsoCloud
202
203
 
203
204
  # @!visibility private
204
205
  class Api
205
- def initialize(url: 'https://cloud.osohq.com', api_key: nil, options: nil)
206
+ def initialize(url: 'https://api.osohq.com', api_key: nil, options: nil)
206
207
  @url = url
207
208
  @connection = Faraday.new(url: url) do |faraday|
208
209
  faraday.request :json
@@ -210,7 +211,7 @@ module OsoCloud
210
211
  # responses are processed in reverse order; this stack implies the
211
212
  # retries are attempted before an error is raised, and the json
212
213
  # parser is only applied if there are no errors
213
- faraday.response :json, preserve_raw: true
214
+ faraday.response :json, parser_options: { symbolize_names: true }
214
215
  faraday.response :raise_error
215
216
  faraday.request :retry, {
216
217
  max: (options && options[:max_retries]) || 10,
@@ -249,117 +250,91 @@ module OsoCloud
249
250
  end
250
251
  end
251
252
  @api_key = api_key
253
+ @user_agent = "Oso Cloud (ruby #{RUBY_VERSION}p#{RUBY_PATCHLEVEL}; rv:#{VERSION})"
254
+ @last_offset = nil
252
255
  end
253
256
 
254
257
  def get_policy
255
- params = {}
256
- data = nil
257
258
  url = '/policy'
258
- result = GET(url, params, data)
259
+ result = GET(url, nil)
259
260
  GetPolicyResult.new(**result)
260
261
  end
261
262
 
262
263
  def post_policy(data)
263
- params = {}
264
- data = OsoCloud::Helpers.to_hash(data)
265
264
  url = '/policy'
266
- result = POST(url, params, data)
265
+ result = POST(url, nil, data, true)
267
266
  ApiResult.new(**result)
268
267
  end
269
268
 
270
269
  def post_facts(data)
271
- params = {}
272
- data = OsoCloud::Helpers.to_hash(data)
273
270
  url = '/facts'
274
- result = POST(url, params, data)
271
+ result = POST(url, nil, data, true)
275
272
  Fact.new(**result)
276
273
  end
277
274
 
278
275
  def delete_facts(data)
279
- params = {}
280
- data = OsoCloud::Helpers.to_hash(data)
281
276
  url = '/facts'
282
- result = DELETE(url, params, data)
277
+ result = DELETE(url, data)
283
278
  ApiResult.new(**result)
284
279
  end
285
280
 
286
281
  def post_bulk_load(data)
287
- params = {}
288
- data = OsoCloud::Helpers.to_hash(data)
289
282
  url = '/bulk_load'
290
- result = POST(url, params, data)
283
+ result = POST(url, nil, data, true)
291
284
  ApiResult.new(**result)
292
285
  end
293
286
 
294
287
  def post_bulk_delete(data)
295
- params = {}
296
- data = OsoCloud::Helpers.to_hash(data)
297
288
  url = '/bulk_delete'
298
- result = POST(url, params, data)
289
+ result = POST(url, nil, data, true)
299
290
  ApiResult.new(**result)
300
291
  end
301
292
 
302
293
  def post_bulk(data)
303
- params = {}
304
- data = OsoCloud::Helpers.to_hash(data)
305
294
  url = '/bulk'
306
- result = POST(url, params, data)
295
+ result = POST(url, nil, data, true)
307
296
  ApiResult.new(**result)
308
297
  end
309
298
 
310
299
  def post_authorize(data)
311
- params = {}
312
- data = OsoCloud::Helpers.to_hash(data)
313
300
  url = '/authorize'
314
- result = POST(url, params, data)
301
+ result = POST(url, nil, data, false)
315
302
  AuthorizeResult.new(**result)
316
303
  end
317
304
 
318
305
  def post_authorize_resources(data)
319
- params = {}
320
- data = OsoCloud::Helpers.to_hash(data)
321
306
  url = '/authorize_resources'
322
- result = POST(url, params, data)
307
+ result = POST(url, nil, data, false)
323
308
  AuthorizeResourcesResult.new(**result)
324
309
  end
325
310
 
326
311
  def post_list(data)
327
- params = {}
328
- data = OsoCloud::Helpers.to_hash(data)
329
312
  url = '/list'
330
- result = POST(url, params, data)
313
+ result = POST(url, nil, data, false)
331
314
  ListResult.new(**result)
332
315
  end
333
316
 
334
317
  def post_actions(data)
335
- params = {}
336
- data = OsoCloud::Helpers.to_hash(data)
337
318
  url = '/actions'
338
- result = POST(url, params, data)
319
+ result = POST(url, nil, data, false)
339
320
  ActionsResult.new(**result)
340
321
  end
341
322
 
342
323
  def post_query(data)
343
- params = {}
344
- data = OsoCloud::Helpers.to_hash(data)
345
324
  url = '/query'
346
- result = POST(url, params, data)
325
+ result = POST(url, nil, data, false)
347
326
  QueryResult.new(**result)
348
327
  end
349
328
 
350
329
  def get_stats
351
- params = {}
352
- data = nil
353
330
  url = '/stats'
354
- result = GET(url, params, data)
331
+ result = GET(url, {})
355
332
  StatsResult.new(**result)
356
333
  end
357
334
 
358
335
  def clear_data
359
- params = {}
360
- data = nil
361
336
  url = '/clear_data'
362
- result = POST(url, params, data)
337
+ result = POST(url, nil, nil, true)
363
338
  ApiResult.new(**result)
364
339
  end
365
340
 
@@ -376,57 +351,67 @@ module OsoCloud
376
351
  params["args.#{i}.id"] = arg_query.id
377
352
  end
378
353
  end
379
- data = nil
380
354
  url = '/facts'
381
- result = GET(url, params, data)
355
+ result = GET(url, params)
382
356
  result.map { |v| Fact.new(**v) }
383
357
  end
384
358
 
385
359
  def headers
386
- {
360
+ default_headers = {
387
361
  'Authorization' => format('Bearer %s', @api_key),
388
- 'User-Agent' => 'Oso Cloud (ruby)',
362
+ 'User-Agent' => @user_agent,
389
363
  Accept: 'application/json',
390
364
  'Content-Type': 'application/json',
391
- 'X-OsoApiVersion': '0'
365
+ 'X-OsoApiVersion': '0',
392
366
  }
367
+ # set OsoOffset is last_offset is not nil
368
+ default_headers[:OsoOffset] = @last_offset unless @last_offset.nil?
369
+ default_headers
393
370
  end
394
371
 
395
- def GET(path, params, _body)
396
- response = @connection.get("api#{path}", params, headers)
397
- handle_faraday_response response
372
+ def GET(path, params)
373
+ response = @connection.get("api#{path}") do |req|
374
+ req.params = params unless params.nil?
375
+ req.headers = headers
376
+ end
377
+ response.body
398
378
  rescue Faraday::Error => e
399
379
  handle_faraday_error e
400
380
  end
401
381
 
402
- def POST(path, params, body)
403
- response = @connection.post("api#{path}", body, headers) do |req|
404
- req.params = params
382
+ def POST(path, params, body, isMutation)
383
+ response = @connection.post("api#{path}") do |req|
384
+ req.params = params unless params.nil?
385
+ req.body = OsoCloud::Helpers.to_hash(body) unless body.nil?
386
+ req.headers = headers
405
387
  end
406
- handle_faraday_response response
388
+
389
+ if isMutation
390
+ @last_offset = response.headers[:OsoOffset]
391
+ end
392
+ response.body
407
393
  rescue Faraday::Error => e
408
394
  handle_faraday_error e
409
395
  end
410
396
 
411
- def DELETE(path, params, body)
412
- response = @connection.delete("api#{path}", params, headers) do |req|
413
- req.body = body
397
+ def DELETE(path, body)
398
+ response = @connection.delete("api#{path}") do |req|
399
+ req.headers = headers
400
+ req.body = OsoCloud::Helpers.to_hash(body) unless body.nil?
414
401
  end
415
- handle_faraday_response response
402
+ response.body
416
403
  rescue Faraday::Error => e
417
404
  handle_faraday_error e
418
405
  end
419
406
 
420
- def handle_faraday_response(response)
421
- # TODO: (@patrickod) refactor duplicative JSON parsing
422
- JSON.parse(response.env[:raw_body], symbolize_names: true)
423
- end
424
-
425
407
  def handle_faraday_error(error)
426
- err = JSON.parse(error.response[:body], symbolize_names: true)
427
- raise ApiError.new(**err)
428
- rescue JSON::ParserError => e
429
- raise ApiError.new(message: e.message)
408
+ resp = error.response
409
+ err = if resp.respond_to? :body
410
+ resp.body[:message]
411
+ else
412
+ error.message
413
+ end
414
+ raise ApiError.new(message: err)
430
415
  end
431
416
  end
432
417
  end
data/lib/oso/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module OsoCloud
2
- VERSION = '1.1.0'.freeze
2
+ VERSION = '1.2.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oso-cloud
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oso Security, Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-03-31 00:00:00.000000000 Z
11
+ date: 2023-06-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday