oso-cloud 1.1.1 → 1.3.0.dev.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: 9268075db2dd7d76b41905d870962afa84ec350ff9d0b335f4c473cef169868e
4
- data.tar.gz: 7eacd7959071d7d2500ceeebf14d09846d5482cd5189139a92667155df796db9
3
+ metadata.gz: 93c11d4552281f1cbf95ec309bbbcd5e8d1289a7f0ee2000724f8d6eb4386617
4
+ data.tar.gz: 62fd22884497116c5db29b06a012b2b06ad82de78b96ed4823b0dbd22862ef37
5
5
  SHA512:
6
- metadata.gz: 8fbea6dd735db2f768fd9d1c8a6af8b3219abc5e688b3d9bc2e557b512aa9303fa2e6afa792f701aa60bebc0443d6703fa6fcd3e48ce5a6fcfd3aad8a09e17c4
7
- data.tar.gz: 84baac085041cf7d3e34c0b2261b3bf66a83245ea82d42b42ed3d47de39600d28939e31bf119d000477d5e67dce09a40a3a4a64bd60ecf0bf80ceb1d2f3d8c97
6
+ metadata.gz: 5139439b2e01ab083c0e73827ddb520a54dc0c2606d1ca20203bad6b56fc446f747a638cc603a83584da35ae65e500ecf4354fd53065c109fa044aef6ba2950f
7
+ data.tar.gz: eac48b47137286b49a1e27197cfb7867bbcd982b6fe6523f9e50ed9d8659934ceaea9b548f3d543510e9983872c9a9859400d417f149af8ef82f9319ea7b9f3e
data/Gemfile.lock CHANGED
@@ -1,22 +1,46 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- oso-cloud (1.1.1)
4
+ oso-cloud (1.2.0)
5
5
  faraday (~> 2.5.2)
6
6
  faraday-retry (~> 2.0.0)
7
+ faraday-typhoeus (~> 1.0.0)
7
8
 
8
9
  GEM
9
10
  remote: https://rubygems.org/
10
11
  specs:
12
+ binding_of_caller (1.0.0)
13
+ debug_inspector (>= 0.0.1)
14
+ coderay (1.1.3)
15
+ debug_inspector (1.1.0)
16
+ ethon (0.16.0)
17
+ ffi (>= 1.15.0)
11
18
  faraday (2.5.2)
12
19
  faraday-net_http (>= 2.0, < 3.1)
13
20
  ruby2_keywords (>= 0.0.4)
14
21
  faraday-net_http (3.0.2)
15
22
  faraday-retry (2.0.0)
16
23
  faraday (~> 2.0)
17
- minitest (5.15.0)
24
+ faraday-typhoeus (1.0.0)
25
+ faraday (~> 2.0)
26
+ typhoeus (~> 1.4)
27
+ ffi (1.15.5)
28
+ interception (0.5)
29
+ method_source (1.0.0)
30
+ minitest (5.18.0)
31
+ pry (0.14.2)
32
+ coderay (~> 1.1)
33
+ method_source (~> 1.0)
34
+ pry-rescue (1.5.2)
35
+ interception (>= 0.5)
36
+ pry (>= 0.12.0)
37
+ pry-stack_explorer (0.6.1)
38
+ binding_of_caller (~> 1.0)
39
+ pry (~> 0.13)
18
40
  rake (12.3.3)
19
41
  ruby2_keywords (0.0.5)
42
+ typhoeus (1.4.0)
43
+ ethon (>= 0.9.0)
20
44
 
21
45
  PLATFORMS
22
46
  ruby
@@ -24,6 +48,8 @@ PLATFORMS
24
48
  DEPENDENCIES
25
49
  minitest (~> 5.15)
26
50
  oso-cloud!
51
+ pry-rescue
52
+ pry-stack_explorer
27
53
  rake (~> 12.0)
28
54
 
29
55
  BUNDLED WITH
data/lib/oso/api.rb CHANGED
@@ -2,6 +2,7 @@ require 'json'
2
2
  require 'uri'
3
3
  require 'faraday'
4
4
  require 'faraday/retry'
5
+ require 'faraday/typhoeus'
5
6
 
6
7
  require 'oso/helpers'
7
8
  require 'oso/version'
@@ -203,7 +204,7 @@ module OsoCloud
203
204
 
204
205
  # @!visibility private
205
206
  class Api
206
- def initialize(url: 'https://cloud.osohq.com', api_key: nil, options: nil)
207
+ def initialize(url: 'https://api.osohq.com', api_key: nil, options: nil)
207
208
  @url = url
208
209
  @connection = Faraday.new(url: url) do |faraday|
209
210
  faraday.request :json
@@ -211,7 +212,7 @@ module OsoCloud
211
212
  # responses are processed in reverse order; this stack implies the
212
213
  # retries are attempted before an error is raised, and the json
213
214
  # parser is only applied if there are no errors
214
- faraday.response :json, preserve_raw: true
215
+ faraday.response :json, parser_options: { symbolize_names: true }
215
216
  faraday.response :raise_error
216
217
  faraday.request :retry, {
217
218
  max: (options && options[:max_retries]) || 10,
@@ -246,122 +247,106 @@ module OsoCloud
246
247
  end
247
248
  end
248
249
  else
249
- faraday.adapter :net_http
250
+ # faraday.adapter :net_http
251
+ # faraday.adapter :net_http_persistent, pool_size: 5 do |http|
252
+ # # yields Net::HTTP::Persistent
253
+ # http.idle_timeout = 1000
254
+ # end
255
+ faraday.adapter :typhoeus, forbid_reuse: true, maxredirs: 1
250
256
  end
251
257
  end
252
258
  @api_key = api_key
253
259
  @user_agent = "Oso Cloud (ruby #{RUBY_VERSION}p#{RUBY_PATCHLEVEL}; rv:#{VERSION})"
260
+ @last_offset = nil
254
261
  end
255
262
 
256
263
  def get_policy
257
- params = {}
258
- data = nil
259
264
  url = '/policy'
260
- result = GET(url, params, data)
265
+ result = GET(url, nil)
261
266
  GetPolicyResult.new(**result)
262
267
  end
263
268
 
264
269
  def post_policy(data)
265
- params = {}
266
- data = OsoCloud::Helpers.to_hash(data)
267
270
  url = '/policy'
268
- result = POST(url, params, data)
271
+ result = POST(url, nil, data, true)
269
272
  ApiResult.new(**result)
270
273
  end
271
274
 
272
275
  def post_facts(data)
273
- params = {}
274
- data = OsoCloud::Helpers.to_hash(data)
275
276
  url = '/facts'
276
- result = POST(url, params, data)
277
+ result = POST(url, nil, data, true)
277
278
  Fact.new(**result)
278
279
  end
279
280
 
280
281
  def delete_facts(data)
281
- params = {}
282
- data = OsoCloud::Helpers.to_hash(data)
283
282
  url = '/facts'
284
- result = DELETE(url, params, data)
283
+ result = DELETE(url, data)
285
284
  ApiResult.new(**result)
286
285
  end
287
286
 
288
287
  def post_bulk_load(data)
289
- params = {}
290
- data = OsoCloud::Helpers.to_hash(data)
291
288
  url = '/bulk_load'
292
- result = POST(url, params, data)
289
+ result = POST(url, nil, data, true)
293
290
  ApiResult.new(**result)
294
291
  end
295
292
 
296
293
  def post_bulk_delete(data)
297
- params = {}
298
- data = OsoCloud::Helpers.to_hash(data)
299
294
  url = '/bulk_delete'
300
- result = POST(url, params, data)
295
+ result = POST(url, nil, data, true)
301
296
  ApiResult.new(**result)
302
297
  end
303
298
 
304
299
  def post_bulk(data)
305
- params = {}
306
- data = OsoCloud::Helpers.to_hash(data)
307
300
  url = '/bulk'
308
- result = POST(url, params, data)
301
+ result = POST(url, nil, data, true)
309
302
  ApiResult.new(**result)
310
303
  end
311
304
 
312
305
  def post_authorize(data)
313
- params = {}
314
- data = OsoCloud::Helpers.to_hash(data)
315
306
  url = '/authorize'
316
- result = POST(url, params, data)
307
+ result = POST(url, nil, data, false)
317
308
  AuthorizeResult.new(**result)
318
309
  end
319
310
 
320
311
  def post_authorize_resources(data)
321
- params = {}
322
- data = OsoCloud::Helpers.to_hash(data)
323
312
  url = '/authorize_resources'
324
- result = POST(url, params, data)
313
+ result = POST(url, nil, data, false)
325
314
  AuthorizeResourcesResult.new(**result)
326
315
  end
327
316
 
328
317
  def post_list(data)
329
- params = {}
330
- data = OsoCloud::Helpers.to_hash(data)
331
318
  url = '/list'
332
- result = POST(url, params, data)
319
+ result = POST(url, nil, data, false)
333
320
  ListResult.new(**result)
334
321
  end
335
322
 
336
323
  def post_actions(data)
337
- params = {}
338
- data = OsoCloud::Helpers.to_hash(data)
339
324
  url = '/actions'
340
- result = POST(url, params, data)
325
+ result = POST(url, nil, data, false)
341
326
  ActionsResult.new(**result)
342
327
  end
343
328
 
329
+ def post_actions_batch(batch_data)
330
+ url = '/actions'
331
+ results = POST_BATCH(url, nil, batch_data, false)
332
+ results.map { |result| ActionsResult.new(**result.body) }
333
+ end
334
+
344
335
  def post_query(data)
345
- params = {}
346
- data = OsoCloud::Helpers.to_hash(data)
347
336
  url = '/query'
348
- result = POST(url, params, data)
337
+ result = POST(url, nil, data, false)
349
338
  QueryResult.new(**result)
350
339
  end
351
340
 
352
341
  def get_stats
353
- params = {}
354
- data = nil
355
342
  url = '/stats'
356
- result = GET(url, params, data)
343
+ result = GET(url, {})
357
344
  StatsResult.new(**result)
358
345
  end
359
346
 
360
347
  def clear_data
361
- params = {}
362
- data = nil
363
348
  url = '/clear_data'
364
- result = POST(url, params, data)
349
+ result = POST(url, nil, nil, true)
365
350
  ApiResult.new(**result)
366
351
  end
367
352
 
@@ -378,57 +363,86 @@ module OsoCloud
378
363
  params["args.#{i}.id"] = arg_query.id
379
364
  end
380
365
  end
381
- data = nil
382
366
  url = '/facts'
383
- result = GET(url, params, data)
367
+ result = GET(url, params)
384
368
  result.map { |v| Fact.new(**v) }
385
369
  end
386
370
 
387
371
  def headers
388
- {
372
+ default_headers = {
389
373
  'Authorization' => format('Bearer %s', @api_key),
390
374
  'User-Agent' => @user_agent,
391
375
  Accept: 'application/json',
392
376
  'Content-Type': 'application/json',
393
- 'X-OsoApiVersion': '0'
377
+ 'X-OsoApiVersion': '0',
394
378
  }
379
+ # set OsoOffset is last_offset is not nil
380
+ default_headers[:OsoOffset] = @last_offset unless @last_offset.nil?
381
+ default_headers
395
382
  end
396
383
 
397
- def GET(path, params, _body)
398
- response = @connection.get("api#{path}", params, headers)
399
- handle_faraday_response response
384
+ def GET(path, params)
385
+ response = @connection.get("api#{path}") do |req|
386
+ req.params = params unless params.nil?
387
+ req.headers = headers
388
+ end
389
+ response.body
400
390
  rescue Faraday::Error => e
401
391
  handle_faraday_error e
402
392
  end
403
393
 
404
- def POST(path, params, body)
405
- response = @connection.post("api#{path}", body, headers) do |req|
406
- req.params = params
394
+ def POST_BATCH(path, params, body_array, isMutation)
395
+ responses = []
396
+ @connection.in_parallel do
397
+ body_array.each do |body|
398
+ responses << @connection.post("api#{path}") do |req|
399
+ req.params = params unless params.nil?
400
+ req.body = OsoCloud::Helpers.to_hash(body) unless body.nil?
401
+ req.headers = headers
402
+ end
403
+ end
404
+ end
405
+ if isMutation
406
+ @last_offset = responses[-1].headers[:OsoOffset]
407
407
  end
408
- handle_faraday_response response
408
+ responses
409
409
  rescue Faraday::Error => e
410
410
  handle_faraday_error e
411
411
  end
412
412
 
413
- def DELETE(path, params, body)
414
- response = @connection.delete("api#{path}", params, headers) do |req|
415
- req.body = body
413
+ def POST(path, params, body, isMutation)
414
+ response = @connection.post("api#{path}") do |req|
415
+ req.params = params unless params.nil?
416
+ req.body = OsoCloud::Helpers.to_hash(body) unless body.nil?
417
+ req.headers = headers
416
418
  end
417
- handle_faraday_response response
419
+
420
+ if isMutation
421
+ @last_offset = response.headers[:OsoOffset]
422
+ end
423
+ response.body
418
424
  rescue Faraday::Error => e
419
425
  handle_faraday_error e
420
426
  end
421
427
 
422
- def handle_faraday_response(response)
423
- # TODO: (@patrickod) refactor duplicative JSON parsing
424
- JSON.parse(response.env[:raw_body], symbolize_names: true)
428
+ def DELETE(path, body)
429
+ response = @connection.delete("api#{path}") do |req|
430
+ req.headers = headers
431
+ req.body = OsoCloud::Helpers.to_hash(body) unless body.nil?
432
+ end
433
+ response.body
434
+ rescue Faraday::Error => e
435
+ handle_faraday_error e
425
436
  end
426
437
 
427
438
  def handle_faraday_error(error)
428
- err = JSON.parse(error.response[:body], symbolize_names: true)
429
- raise ApiError.new(**err)
430
- rescue JSON::ParserError => e
431
- raise ApiError.new(message: e.message)
439
+ resp = error.response
440
+ err = if resp.respond_to? :body
441
+ resp.body[:message]
442
+ else
443
+ error.message
444
+ end
445
+ raise ApiError.new(message: err)
432
446
  end
433
447
  end
434
448
  end
data/lib/oso/oso.rb CHANGED
@@ -32,7 +32,6 @@ module OsoCloud
32
32
  @api = OsoCloud::Core::Api.new(url: url, api_key: api_key)
33
33
  end
34
34
 
35
- ##
36
35
  # Update the active policy
37
36
  #
38
37
  # Updates the active policy in Oso Cloud, The string passed into
@@ -270,5 +269,22 @@ module OsoCloud
270
269
  context_facts: OsoCloud::Helpers.params_to_facts(context_facts)))
271
270
  OsoCloud::Helpers.facts_to_params(result.results)
272
271
  end
272
+
273
+ def actions_batch(actor, resources, context_facts = [])
274
+ actor_typed_id = actor.to_api_value
275
+ data = resources.map do |r|
276
+ resource_typed_id = r.to_api_value
277
+ OsoCloud::Core::ActionsQuery.new(
278
+ actor_type: actor_typed_id.type,
279
+ actor_id: actor_typed_id.id,
280
+ resource_type: resource_typed_id.type,
281
+ resource_id: resource_typed_id.id,
282
+ context_facts: OsoCloud::Helpers.params_to_facts(context_facts)
283
+ )
284
+ end
285
+ @api.post_actions_batch(data).map { |result| result.results}
286
+ end
273
287
  end
288
+
274
289
  end
290
+
data/lib/oso/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module OsoCloud
2
- VERSION = '1.1.1'.freeze
2
+ VERSION = '1.3.0.dev.0'.freeze
3
3
  end
data/oso-cloud.gemspec CHANGED
@@ -22,5 +22,10 @@ Gem::Specification.new do |spec|
22
22
 
23
23
  spec.add_dependency 'faraday', '~> 2.5.2'
24
24
  spec.add_dependency 'faraday-retry', '~> 2.0.0'
25
+ # spec.add_dependency 'faraday-net_http_persistent', '~> 2.0'
26
+ spec.add_dependency 'faraday-typhoeus', '~> 1.0.0'
27
+ #
25
28
  spec.add_development_dependency 'minitest', '~> 5.15'
29
+ spec.add_development_dependency 'pry-rescue'
30
+ spec.add_development_dependency 'pry-stack_explorer'
26
31
  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.1
4
+ version: 1.3.0.dev.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oso Security, Inc.
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-04-27 00:00:00.000000000 Z
11
+ date: 2023-09-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: 2.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday-typhoeus
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.0.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.0
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: minitest
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -52,7 +66,35 @@ dependencies:
52
66
  - - "~>"
53
67
  - !ruby/object:Gem::Version
54
68
  version: '5.15'
55
- description:
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-rescue
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-stack_explorer
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
56
98
  email:
57
99
  - support@osohq.com
58
100
  executables: []
@@ -77,7 +119,7 @@ homepage: https://www.osohq.com/
77
119
  licenses:
78
120
  - Apache-2.0
79
121
  metadata: {}
80
- post_install_message:
122
+ post_install_message:
81
123
  rdoc_options: []
82
124
  require_paths:
83
125
  - lib
@@ -88,12 +130,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
88
130
  version: 3.0.0
89
131
  required_rubygems_version: !ruby/object:Gem::Requirement
90
132
  requirements:
91
- - - ">="
133
+ - - ">"
92
134
  - !ruby/object:Gem::Version
93
- version: '0'
135
+ version: 1.3.1
94
136
  requirements: []
95
137
  rubygems_version: 3.2.33
96
- signing_key:
138
+ signing_key:
97
139
  specification_version: 4
98
140
  summary: Oso Cloud Ruby client
99
141
  test_files: []