oso-oso 0.5.2 → 0.6.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
  SHA1:
3
- metadata.gz: eb279f6dd2c9ea6d862b47f1e6cc109bab429d19
4
- data.tar.gz: 8c998ace146905bfb32574fe7d233fa0cb69e240
3
+ metadata.gz: 70321ccb3dc8e6468b89109aa7d9bbb97868d1be
4
+ data.tar.gz: 8b7c1873eba4dae6d1150f8f91a74a3d5a72e51c
5
5
  SHA512:
6
- metadata.gz: 1bd787012da6991d59d14877111b4022bc8855d16e3556faf859eaa1e09dcb41cb0e6d91d6d7e52faba7cf2c0a8b7993a784553221afcb6fa9dc7633ea40ec1b
7
- data.tar.gz: c26f0285df44ac39e6f10717f0ca5143cf32ca1193133ce1a977885688fe72f0665de74d0c13cbc0d3ab71f4f85aa6fd39719b0872ebc4e738c25aa798fd6887
6
+ metadata.gz: 639ea45ef8a86ebf97ae20ee1977499e18f04827b05d869369c7ec01792eb7a7747c6bae9f7d7789af4993a4b04edef5b375c9caa504c6e09afe701ed931874f
7
+ data.tar.gz: 7dc68ed639125472e808ff375059ceb6d70837fad20c05672a183452bef78eeee56dcc0dc58579d21536749bc8e2fbc2628baa349473984c824d23b38d1270f4
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- oso-oso (0.5.2)
4
+ oso-oso (0.6.0)
5
5
  ffi (~> 1.0)
6
6
 
7
7
  GEM
Binary file
Binary file
@@ -35,6 +35,8 @@ module Oso
35
35
  class MissingConstructorError < PolarRuntimeError; end
36
36
  class UnregisteredInstanceError < PolarRuntimeError; end
37
37
  class DuplicateInstanceRegistrationError < PolarRuntimeError; end
38
+
39
+ # TODO: I think this should probably have some arguments to say what the call is
38
40
  class InvalidCallError < PolarRuntimeError; end
39
41
  class InvalidConstructorError < PolarRuntimeError; end
40
42
  class InvalidQueryTypeError < PolarRuntimeError; end
@@ -69,7 +69,7 @@ module Oso
69
69
  # @param name [String]
70
70
  # @return [Symbol] if constructor is the default of `:new`.
71
71
  # @return [Proc] if a custom constructor was registered.
72
- # @raise [UnregisteredConstructorError] if the constructor has not been registered.
72
+ # @raise [MissingConstructorError] if the constructor has not been registered.
73
73
  def get_constructor(name)
74
74
  raise MissingConstructorError, name unless constructors.key? name
75
75
 
@@ -121,6 +121,7 @@ module Oso
121
121
  # @raise [PolarRuntimeError] if instance construction fails.
122
122
  def make_instance(cls_name, args:, kwargs:, id:) # rubocop:disable Metrics/MethodLength
123
123
  constructor = get_constructor(cls_name)
124
+ # The kwargs.empty? checks are for Ruby < 2.7.
124
125
  instance = if constructor == :new
125
126
  if kwargs.empty?
126
127
  get_class(cls_name).__send__(:new, *args)
@@ -146,9 +147,9 @@ module Oso
146
147
  # @return [Boolean]
147
148
  def subspecializer?(instance_id, left_tag:, right_tag:)
148
149
  mro = get_instance(instance_id).class.ancestors
149
- mro.index(get_class(left_tag)) < mro.index(get_class(right_tag))
150
- rescue StandardError
151
- false
150
+ left_index = mro.index(get_class(left_tag))
151
+ right_index = mro.index(get_class(right_tag))
152
+ left_index && right_index && left_index < right_index
152
153
  end
153
154
 
154
155
  # Check if instance is an instance of class.
@@ -160,8 +161,6 @@ module Oso
160
161
  instance = to_ruby(instance)
161
162
  cls = get_class(class_tag)
162
163
  instance.is_a? cls
163
- rescue PolarRuntimeError
164
- false
165
164
  end
166
165
 
167
166
  # Check if two instances unify
@@ -173,8 +172,6 @@ module Oso
173
172
  left_instance = get_instance(left_instance_id)
174
173
  right_instance = get_instance(right_instance_id)
175
174
  left_instance == right_instance
176
- rescue PolarRuntimeError
177
- false
178
175
  end
179
176
 
180
177
  # Turn a Ruby value into a Polar term that's ready to be sent across the
@@ -43,16 +43,18 @@ module Oso
43
43
  # @param args [Array<Hash>]
44
44
  # @raise [InvalidCallError] if the method doesn't exist on the instance or
45
45
  # the args passed to the method are invalid.
46
- def register_call(attribute, call_id:, instance:, args:) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
46
+ def register_call(attribute, call_id:, instance:, args:, kwargs:) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
47
47
  return if calls.key?(call_id)
48
48
 
49
49
  instance = host.to_ruby(instance)
50
- if args.nil?
51
- result = instance.__send__(attribute)
52
- else
53
- args = args.map { |a| host.to_ruby(a) }
54
- result = instance.__send__(attribute, *args)
55
- end
50
+ args = args.map { |a| host.to_ruby(a) }
51
+ kwargs = Hash[kwargs.map { |k, v| [k.to_sym, host.to_ruby(v)] }]
52
+ # The kwargs.empty? check is for Ruby < 2.7.
53
+ result = if kwargs.empty?
54
+ instance.__send__(attribute, *args)
55
+ else
56
+ instance.__send__(attribute, *args, **kwargs)
57
+ end
56
58
  result = [result].to_enum unless result.is_a? Enumerator # Call must be a generator.
57
59
  calls[call_id] = result.lazy
58
60
  rescue ArgumentError, NoMethodError
@@ -93,8 +95,8 @@ module Oso
93
95
  # @param call_id [Integer]
94
96
  # @param instance [Hash<String, Object>]
95
97
  # @raise [Error] if the FFI call raises one.
96
- def handle_call(attribute, call_id:, instance:, args:)
97
- register_call(attribute, call_id: call_id, instance: instance, args: args)
98
+ def handle_call(attribute, call_id:, instance:, args:, kwargs:)
99
+ register_call(attribute, call_id: call_id, instance: instance, args: args, kwargs: kwargs)
98
100
  result = JSON.dump(next_call_result(call_id))
99
101
  call_result(result, call_id: call_id)
100
102
  rescue InvalidCallError => e
@@ -104,28 +106,17 @@ module Oso
104
106
  call_result(nil, call_id: call_id)
105
107
  end
106
108
 
107
- def handle_make_external(data) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
109
+ def handle_make_external(data) # rubocop:disable Metrics/AbcSize
108
110
  id = data['instance_id']
109
111
  raise DuplicateInstanceRegistrationError, id if host.instance? id
110
112
 
111
113
  constructor = data['constructor']['value']
112
- if constructor.key? 'InstanceLiteral'
113
- cls_name = constructor['InstanceLiteral']['tag']
114
- fields = constructor['InstanceLiteral']['fields']['fields']
115
- kwargs = Hash[fields.map { |k, v| [k.to_sym, host.to_ruby(v)] }]
116
- args = []
117
- elsif constructor.key? 'Call'
118
- cls_name = constructor['Call']['name']
119
- args = constructor['Call']['args'].map { |arg| host.to_ruby(arg) }
120
- kwargs = constructor['Call']['kwargs']
121
- kwargs = if kwargs.nil?
122
- {}
123
- else
124
- Hash[kwargs.map { |k, v| [k.to_sym, host.to_ruby(v)] }]
125
- end
126
- else
127
- raise InvalidConstructorError
128
- end
114
+ raise InvalidConstructorError unless constructor.key? 'Call'
115
+
116
+ cls_name = constructor['Call']['name']
117
+ args = constructor['Call']['args'].map { |arg| host.to_ruby(arg) }
118
+ kwargs = constructor['Call']['kwargs'] || {}
119
+ kwargs = Hash[kwargs.map { |k, v| [k.to_sym, host.to_ruby(v)] }]
129
120
  host.make_instance(cls_name, args: args, kwargs: kwargs, id: id)
130
121
  end
131
122
 
@@ -134,7 +125,7 @@ module Oso
134
125
  # @yieldparam [Hash<String, Object>]
135
126
  # @return [Enumerator]
136
127
  # @raise [Error] if any of the FFI calls raise one.
137
- def start # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
128
+ def start # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
138
129
  Enumerator.new do |yielder| # rubocop:disable Metrics/BlockLength
139
130
  loop do # rubocop:disable Metrics/BlockLength
140
131
  event = ffi_query.next_event
@@ -149,8 +140,9 @@ module Oso
149
140
  call_id = event.data['call_id']
150
141
  instance = event.data['instance']
151
142
  attribute = event.data['attribute']
152
- args = event.data['args']
153
- handle_call(attribute, call_id: call_id, instance: instance, args: args)
143
+ args = event.data['args'] || []
144
+ kwargs = event.data['kwargs'] || {}
145
+ handle_call(attribute, call_id: call_id, instance: instance, args: args, kwargs: kwargs)
154
146
  when 'ExternalIsSubSpecializer'
155
147
  instance_id = event.data['instance_id']
156
148
  left_tag = event.data['left_class_tag']
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Oso
4
- VERSION = '0.5.2'
4
+ VERSION = '0.6.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oso-oso
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oso Security, Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-09 00:00:00.000000000 Z
11
+ date: 2020-09-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ffi