File: auto_encrypter_spec.rb

package info (click to toggle)
ruby-mongo 2.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,764 kB
  • sloc: ruby: 108,806; makefile: 5; sh: 2
file content (450 lines) | stat: -rw-r--r-- 12,474 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# frozen_string_literal: true
# rubocop:todo all

require 'spec_helper'
require 'tempfile'

describe Mongo::Crypt::AutoEncrypter do
  require_libmongocrypt
  min_server_fcv '4.2'
  require_enterprise
  clean_slate

  include_context 'define shared FLE helpers'

  let(:auto_encrypter) do
    described_class.new(
      auto_encryption_options.merge(
        client: authorized_client.use(:auto_encryption),
        extra_options: auto_encrypter_extra_options
      )
    )
  end

  let(:auto_encrypter_extra_options) do
    # Spawn mongocryptd on non-default port for sharded cluster tests
    extra_options
  end

  let(:client) { authorized_client }

  let(:db_name) { 'auto_encryption' }
  let(:collection_name) { 'users' }

  let(:command) do
    {
      'insert' => collection_name,
      'ordered' => true,
      'lsid' => {
        'id' => BSON::Binary.new(Base64.decode64("CzgjT+byRK+FKUWG6QbyjQ==\n"), :uuid)
      },
      'documents' => [
        {
          'ssn' => ssn,
          '_id' => BSON::ObjectId('5e16516e781d8a89b94df6df')
        }
      ]
    }
  end

  let(:encrypted_command) do
    command.merge(
      'documents' => [
        {
          'ssn' => BSON::Binary.new(Base64.decode64(encrypted_ssn), :ciphertext),
          '_id' => BSON::ObjectId('5e16516e781d8a89b94df6df')
        }
      ]
    )
  end

  let(:operation_context) { Mongo::Operation::Context.new }

  shared_context 'with jsonSchema validator' do
    before do
      users_collection = client.use(db_name)[collection_name]
      users_collection.drop
      client.use(db_name)[collection_name,
        {
          'validator' => { '$jsonSchema' => schema_map }
        }
      ].create
    end
  end

  shared_context 'without jsonSchema validator' do
    before do
      users_collection = client.use(db_name)[collection_name]
      users_collection.drop
      users_collection.create
    end
  end

  shared_examples 'a functioning auto encrypter' do
    describe '#encrypt' do
      it 'replaces the ssn field with a BSON::Binary' do
        result = auto_encrypter.encrypt(db_name, command, operation_context)
        expect(result).to eq(encrypted_command)
      end
    end

    describe '#decrypt' do
      it 'returns the unencrypted document' do
        result = auto_encrypter.decrypt(encrypted_command, operation_context)
        expect(result).to eq(command)
      end
    end
  end

  before do
    key_vault_collection.drop
    key_vault_collection.insert_one(data_key)
  end

  after do
    auto_encrypter.close
  end

  describe '#initialize' do
    include_context 'with local kms_providers'

    let(:auto_encryption_options) do
      {
        kms_providers: local_kms_providers,
        key_vault_namespace: key_vault_namespace,
        schema_map: { "#{db_name}.#{collection_name}": schema_map },
      }
    end

    let(:auto_encrypter) do
      described_class.new(
        auto_encryption_options.merge(
          client: client,
          # Spawn mongocryptd on non-default port for sharded cluster tests
          extra_options: extra_options
        )
      )
    end

    context 'when client has an unlimited pool' do
      let(:client) do
        new_local_client_nmio(
          SpecConfig.instance.addresses,
          SpecConfig.instance.test_options.merge(
            max_pool_size: 0,
            database: 'auto_encryption'
          ),
        )
      end

      it 'reuses the client as key_vault_client and metadata_client' do
        expect(auto_encrypter.key_vault_client).to eq(client)
        expect(auto_encrypter.metadata_client).to eq(client)
      end
    end

    context 'when client has a limited pool' do
      let(:client) do
        new_local_client_nmio(
          SpecConfig.instance.addresses,
          SpecConfig.instance.test_options.merge(
            max_pool_size: 20,
            database: 'auto_encryption'
          ),
        )
      end

      it 'creates new client for key_vault_client and metadata_client' do
        expect(auto_encrypter.key_vault_client).not_to eq(client)
        expect(auto_encrypter.metadata_client).not_to eq(client)
      end
    end

    context 'when crypt shared library is available' do
      it 'does not create a mongocryptd client' do
        allow_any_instance_of(Mongo::Crypt::Handle).to receive(:"crypt_shared_lib_available?").and_return true
        expect(auto_encrypter.mongocryptd_client).to be_nil
      end
    end
  end

  shared_examples 'with schema map in auto encryption commands' do
    include_context 'without jsonSchema validator'

    let(:auto_encryption_options) do
      {
        kms_providers: kms_providers,
        kms_tls_options: kms_tls_options,
        key_vault_namespace: key_vault_namespace,
        schema_map: { "#{db_name}.#{collection_name}": schema_map },
      }
    end

    context 'with AWS KMS providers' do
      include_context 'with AWS kms_providers'
      it_behaves_like 'a functioning auto encrypter'
    end

    context 'with Azure KMS providers' do
      include_context 'with Azure kms_providers'
      it_behaves_like 'a functioning auto encrypter'
    end

    context 'with GCP KMS providers' do
      include_context 'with GCP kms_providers'
      it_behaves_like 'a functioning auto encrypter'
    end

    context 'with KMIP KMS providers' do
      include_context 'with KMIP kms_providers'
      it_behaves_like 'a functioning auto encrypter'
    end

    context 'with local KMS providers' do
      include_context 'with local kms_providers'
      it_behaves_like 'a functioning auto encrypter'
    end
  end

  shared_examples 'with schema map file in auto encryption commands' do
    include_context 'without jsonSchema validator'

    let(:schema_map_file) do
      file = Tempfile.new('schema_map.json')
      file.write(JSON.dump(
        {
          "#{db_name}.#{collection_name}" => schema_map
        }
      ))
      file.flush
      file
    end

    after do
      schema_map_file.close
    end

    let(:auto_encryption_options) do
      {
        kms_providers: kms_providers,
        kms_tls_options: kms_tls_options,
        key_vault_namespace: key_vault_namespace,
        schema_map_path: schema_map_file.path
      }
    end

    context 'with AWS KMS providers' do
      include_context 'with AWS kms_providers'
      it_behaves_like 'a functioning auto encrypter'
    end

    context 'with Azure KMS providers' do
      include_context 'with Azure kms_providers'
      it_behaves_like 'a functioning auto encrypter'
    end

    context 'with GCP KMS providers' do
      include_context 'with GCP kms_providers'
      it_behaves_like 'a functioning auto encrypter'
    end

    context 'with KMIP KMS providers' do
      include_context 'with KMIP kms_providers'
      it_behaves_like 'a functioning auto encrypter'
    end

    context 'with local KMS providers' do
      include_context 'with local kms_providers'
      it_behaves_like 'a functioning auto encrypter'
    end
  end

  shared_examples 'with schema map collection validator' do
    include_context 'with jsonSchema validator'

    let(:auto_encryption_options) do
      {
        kms_providers: kms_providers,
        kms_tls_options: kms_tls_options,
        key_vault_namespace: key_vault_namespace
      }
    end

    context 'with AWS KMS providers' do
      include_context 'with AWS kms_providers'
      it_behaves_like 'a functioning auto encrypter'
    end

    context 'with Azure KMS providers' do
      include_context 'with Azure kms_providers'
      it_behaves_like 'a functioning auto encrypter'
    end

    context 'with GCP KMS providers' do
      include_context 'with GCP kms_providers'
      it_behaves_like 'a functioning auto encrypter'
    end

    context 'with GCP KMS providers and PEM key' do
      require_mri

      include_context 'with GCP kms_providers'

      let(:kms_providers) do
        {
          gcp: {
            email: SpecConfig.instance.fle_gcp_email,
            private_key: OpenSSL::PKey.read(
              Base64.decode64(SpecConfig.instance.fle_gcp_private_key)
            ).export,
          }
        }
      end

      it_behaves_like 'a functioning auto encrypter'
    end

    context 'with KMIP KMS providers' do
      include_context 'with KMIP kms_providers'
      it_behaves_like 'a functioning auto encrypter'
    end

    context 'with local KMS providers' do
      include_context 'with local kms_providers'
      it_behaves_like 'a functioning auto encrypter'
    end
  end

  shared_examples 'with no validator or client option' do
    include_context 'without jsonSchema validator'

    let(:auto_encryption_options) do
      {
        kms_providers: kms_providers,
        kms_tls_options: kms_tls_options,
        key_vault_namespace: key_vault_namespace,
      }
    end

    context 'with AWS KMS providers' do
      include_context 'with AWS kms_providers'

      describe '#encrypt' do
        it 'does not perform encryption' do
          result = auto_encrypter.encrypt(db_name, command, operation_context)
          expect(result).to eq(command)
        end
      end

      describe '#decrypt' do
        it 'still performs decryption' do
          result = auto_encrypter.decrypt(encrypted_command, operation_context)
          expect(result).to eq(command)
        end
      end
    end

    context 'with Azure KMS providers' do
      include_context 'with Azure kms_providers'

      describe '#encrypt' do
        it 'does not perform encryption' do
          result = auto_encrypter.encrypt(db_name, command, operation_context)
          expect(result).to eq(command)
        end
      end

      describe '#decrypt' do
        it 'still performs decryption' do
          result = auto_encrypter.decrypt(encrypted_command, operation_context)
          expect(result).to eq(command)
        end
      end
    end

    context 'with GCP KMS providers' do
      include_context 'with GCP kms_providers'

      describe '#encrypt' do
        it 'does not perform encryption' do
          result = auto_encrypter.encrypt(db_name, command, operation_context)
          expect(result).to eq(command)
        end
      end

      describe '#decrypt' do
        it 'still performs decryption' do
          result = auto_encrypter.decrypt(encrypted_command, operation_context)
          expect(result).to eq(command)
        end
      end
    end

    context 'with KMIP KMS providers' do
      include_context 'with KMIP kms_providers'

      describe '#encrypt' do
        it 'does not perform encryption' do
          result = auto_encrypter.encrypt(db_name, command, operation_context)
          expect(result).to eq(command)
        end
      end

      describe '#decrypt' do
        it 'still performs decryption' do
          result = auto_encrypter.decrypt(encrypted_command, operation_context)
          expect(result).to eq(command)
        end
      end
    end

    context 'with local KMS providers' do
      include_context 'with local kms_providers'

      describe '#encrypt' do
        it 'does not perform encryption' do
          result = auto_encrypter.encrypt(db_name, command, operation_context)
          expect(result).to eq(command)
        end
      end

      describe '#decrypt' do
        it 'still performs decryption' do
          result = auto_encrypter.decrypt(encrypted_command, operation_context)
          expect(result).to eq(command)
        end
      end
    end
  end

  context 'when using crypt shared library' do
    min_server_version '6.0.0'

    let(:auto_encrypter_extra_options) do
      {
        crypt_shared_lib_path: SpecConfig.instance.crypt_shared_lib_path
      }
    end

    let(:auto_encryption_options) do
      {
        kms_providers: kms_providers,
        kms_tls_options: kms_tls_options,
        key_vault_namespace: key_vault_namespace,
        schema_map: { "#{db_name}.#{collection_name}": schema_map },
      }
    end

    it_behaves_like 'with schema map in auto encryption commands'
    it_behaves_like 'with schema map file in auto encryption commands'
    it_behaves_like 'with schema map collection validator'
    it_behaves_like 'with no validator or client option'
  end

  context 'when using mongocryptd' do
    it_behaves_like 'with schema map in auto encryption commands'
    it_behaves_like 'with schema map file in auto encryption commands'
    it_behaves_like 'with schema map collection validator'
    it_behaves_like 'with no validator or client option'
  end
end