ndb-rails 1.20.0 → 1.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b2dd42ff7386e4b2e74d41f59afbd7349e8535caeb9c3a4e366585fff0379d6f
4
- data.tar.gz: 36b7f7c75176a93b169222e681ded1d401b73833a8c49097c20235431f725bba
3
+ metadata.gz: ba5189f5c43b7feced373912950a07584d3014b19f8555363a1fb7c39cd58017
4
+ data.tar.gz: 5c2c9a27e258f484ad24cc08563e272564dbd619c8e47204acc1c8a6d3e0a177
5
5
  SHA512:
6
- metadata.gz: ad8bc303912d5ad086493e560c38e0c60da9ae1ebbe1433d9ba1ade7579466a00722cc07821cb0f983d95d3eb7013431ba7b03c1746d202bac80a957e84e1cca
7
- data.tar.gz: '08c9aafae94c2f0b7e113d06896ad4dbbc2a7308bf193d446c0f9c6c7f10da99284bd3d0f889ce936ff9661dde749d463069529ed798a8cac53d9aa80d5c1006'
6
+ metadata.gz: eb713f9a8d121ba7481fe5b41b3447f6fa873b542573233168e5a7ccc15078f7ba8b9b37c7d6e651d64d6731cf8d404d0dbe421c9b46fc9b2325d64a4fde77ce
7
+ data.tar.gz: 624f0cb506cc7f5a0c4b547a41e7d8243484813ecd00a9d8e46433b8a01ab4dbe5b700a03467c6060fab728d5ac75701db502b60b51b4df37fe11f2524dd8e05
@@ -26,7 +26,7 @@ module Ndb
26
26
 
27
27
  class << self
28
28
  QUERY_METHODS = %i[
29
- where where_raw order limit offset page after group unwind includes lock find find_by find_by! first last count pluck exists?
29
+ where where_raw order limit offset page after group unwind includes lock encrypted find find_by find_by! first last count pluck exists?
30
30
  find_each in_batches sum average minimum maximum to_a each destroy_all delete_all to_ndb
31
31
  ].freeze
32
32
 
@@ -150,9 +150,14 @@ module Ndb
150
150
  @source
151
151
  end
152
152
 
153
- def attribute(name, *args, index: false, unique: false, time: false, expires: false, **options)
153
+ # encrypted: true makes the daemon encrypt the attribute before it
154
+ # stores it; blind: :numeric (or :exact, :lower, :upper) also lets where and
155
+ # find_by find it by value, and index or unique name it.
156
+ def attribute(name, *args, index: false, unique: false, time: false, expires: false, encrypted: false, blind: nil, **options)
154
157
  own_indexed << name.to_s if index
155
158
  own_unique << name.to_s if unique
159
+ own_encrypted << name.to_s if encrypted && !blind
160
+ own_blind[name.to_s] = blind.to_s if blind
156
161
  @time_field = name.to_s if time
157
162
  @expires_field = name.to_s if expires
158
163
 
@@ -201,12 +206,22 @@ module Ndb
201
206
  inherited_list(:unique_fields) | own_unique
202
207
  end
203
208
 
209
+ def encrypted_fields
210
+ inherited_list(:encrypted_fields) | own_encrypted
211
+ end
212
+
213
+ def blind_fields
214
+ (superclass.respond_to?(:blind_fields) ? superclass.blind_fields : {}).merge(own_blind)
215
+ end
216
+
204
217
  # The ensure_collection body for this model.
205
218
  def collection_definition
206
219
  definition = {}
207
220
 
208
221
  definition["indexes"] = indexed_fields if indexed_fields.any?
209
222
  definition["unique"] = unique_fields if unique_fields.any?
223
+ definition["encrypted"] = encrypted_fields if encrypted_fields.any?
224
+ definition["blind"] = blind_fields if blind_fields.any?
210
225
  definition["time"] = time_field if time_field
211
226
  definition["expires_field"] = expires_field if expires_field
212
227
  definition["ttl"] = ttl if ttl
@@ -308,6 +323,14 @@ module Ndb
308
323
  @own_unique ||= []
309
324
  end
310
325
 
326
+ def own_encrypted
327
+ @own_encrypted ||= []
328
+ end
329
+
330
+ def own_blind
331
+ @own_blind ||= {}
332
+ end
333
+
311
334
  def inherited_list(name)
312
335
  superclass.respond_to?(name) ? superclass.public_send(name) : []
313
336
  end
@@ -61,6 +61,14 @@ module Ndb
61
61
  # block, the record is yielded and the lock ends with the block; without
62
62
  # one, it ends at the first save or destroy, or at unlock!. See
63
63
  # Ndb::Locking. ttl and wait: see Ndb::Client#lock.
64
+ # Reads that keep the encrypted fields sealed ("ndb:e1:k1:..."): a list
65
+ # of secrets that shows none of them. The other reads get the values.
66
+ #
67
+ # Secret.encrypted.where(owner: user.id)
68
+ def encrypted
69
+ with(keep_encrypted: true)
70
+ end
71
+
64
72
  def lock(ttl: nil, wait: nil)
65
73
  with(lock: { ttl: ttl, wait: wait }.compact)
66
74
  end
@@ -158,7 +166,7 @@ module Ndb
158
166
  def load
159
167
  return self if loaded?
160
168
 
161
- @result = klass.ndb_find(to_query)
169
+ @result = fetch(to_query)
162
170
  @records = @result.rows.map { |row| klass.instantiate(row) }
163
171
 
164
172
  self
@@ -265,14 +273,14 @@ module Ndb
265
273
  end
266
274
 
267
275
  def exists?
268
- klass.ndb_find(to_query.slice("query").merge("limit" => 1, "fields" => [klass.primary_key])).rows.any?
276
+ fetch(to_query.slice("query").merge("limit" => 1, "fields" => [klass.primary_key])).rows.any?
269
277
  end
270
278
 
271
279
  def pluck(*fields)
272
280
  raise ArgumentError, "pluck needs at least one field" if fields.empty?
273
281
 
274
282
  names = fields.map(&:to_s)
275
- rows = klass.ndb_find(to_query.merge("fields" => names)).rows
283
+ rows = fetch(to_query.merge("fields" => names)).rows
276
284
 
277
285
  rows.map do |row|
278
286
  values = names.map { |name| klass.attribute_types[name].cast(row.dig(*name.split("."))) }
@@ -284,7 +292,7 @@ module Ndb
284
292
  def count
285
293
  return calculate(:count) unless values[:group].empty? && values[:unwind].nil?
286
294
 
287
- klass.ndb_find(to_query.slice("query").merge("count" => true, "limit" => 1, "fields" => [klass.primary_key])).total
295
+ fetch(to_query.slice("query").merge("count" => true, "limit" => 1, "fields" => [klass.primary_key])).total
288
296
  end
289
297
 
290
298
  # Pagination
@@ -343,6 +351,14 @@ module Ndb
343
351
 
344
352
  private
345
353
 
354
+ def fetch(body)
355
+ sealed_or_plain { klass.ndb_find(body) }
356
+ end
357
+
358
+ def sealed_or_plain(&)
359
+ values[:keep_encrypted] ? Rails.client.keep_encrypted(&) : yield
360
+ end
361
+
346
362
  def add_clause(clause)
347
363
  with(where: values[:where] + [clause])
348
364
  end
@@ -418,7 +434,7 @@ module Ndb
418
434
 
419
435
  def calculate(op, field = nil)
420
436
  key = op.to_s
421
- rows = klass.ndb_aggregate(aggregate_body(op, field)).rows
437
+ rows = sealed_or_plain { klass.ndb_aggregate(aggregate_body(op, field)) }.rows
422
438
 
423
439
  return rows.to_h { |row| [group_key(row["_id"]), row[key]] } unless values[:group].empty?
424
440
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Ndb
4
4
  module Rails
5
- VERSION = "1.20.0"
5
+ VERSION = "1.21.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ndb-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.20.0
4
+ version: 1.21.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - thadeu
@@ -43,7 +43,7 @@ dependencies:
43
43
  requirements:
44
44
  - - ">="
45
45
  - !ruby/object:Gem::Version
46
- version: 1.20.0
46
+ version: 1.21.0
47
47
  - - "<"
48
48
  - !ruby/object:Gem::Version
49
49
  version: '2'
@@ -53,7 +53,7 @@ dependencies:
53
53
  requirements:
54
54
  - - ">="
55
55
  - !ruby/object:Gem::Version
56
- version: 1.20.0
56
+ version: 1.21.0
57
57
  - - "<"
58
58
  - !ruby/object:Gem::Version
59
59
  version: '2'