File: auto_encryption_command_monitoring_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 (306 lines) | stat: -rw-r--r-- 9,785 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
# frozen_string_literal: true
# rubocop:todo all

require 'spec_helper'

describe 'Auto Encryption' do
  require_libmongocrypt
  require_enterprise
  min_server_fcv '4.2'

  # Diagnostics of leaked background threads only, these tests do not
  # actually require a clean slate. https://jira.mongodb.org/browse/RUBY-2138
  clean_slate

  include_context 'define shared FLE helpers'
  include_context 'with local kms_providers'

  let(:subscriber) { Mrss::EventSubscriber.new }
  let(:db_name) { 'auto_encryption' }

  let(:encryption_client) do
    new_local_client(
      SpecConfig.instance.addresses,
      SpecConfig.instance.test_options.merge(
        auto_encryption_options: {
          kms_providers: kms_providers,
          key_vault_namespace: key_vault_namespace,
          schema_map: { "auto_encryption.users" => schema_map },
          # Spawn mongocryptd on non-default port for sharded cluster tests
          extra_options: extra_options,
        },
        database: db_name
      ),
    ).tap do |client|
      client.subscribe(Mongo::Monitoring::COMMAND, subscriber)
    end
  end

  before(:each) do
    key_vault_collection.drop
    key_vault_collection.insert_one(data_key)

    encryption_client['users'].drop
  end

  let(:started_event) do
    subscriber.single_command_started_event(command_name, database_name: db_name)
  end

  let(:succeeded_event) do
    subscriber.single_command_succeeded_event(command_name, database_name: db_name)
  end

  let(:key_vault_list_collections_event) do
    subscriber.started_events.find do |event|
      event.command_name == 'listCollections' && event.database_name == key_vault_db
    end
  end

  shared_examples 'it has a non-encrypted key_vault_client' do
    it 'does not register a listCollections event on the key vault client' do
      expect(key_vault_list_collections_event).to be_nil
    end
  end

  context 'when performing operations that need a document in the database' do
    before do
      result = encryption_client['users'].insert_one(ssn: ssn, age: 23)
    end

    describe '#aggregate' do
      let(:command_name) { 'aggregate' }

      before do
        encryption_client['users'].aggregate([{ '$match' => { 'ssn' => ssn } }]).first
      end

      it 'has encrypted data in command monitoring' do
        # Command started event occurs after ssn is encrypted
        expect(
          started_event.command["pipeline"].first["$match"]["ssn"]["$eq"]
        ).to be_ciphertext

        # Command succeeded event occurs before ssn is decrypted
        expect(succeeded_event.reply["cursor"]["firstBatch"].first["ssn"]).to be_ciphertext
      end

      it_behaves_like 'it has a non-encrypted key_vault_client'
    end

    describe '#count' do
      let(:command_name) { 'count' }

      before do
        encryption_client['users'].count(ssn: ssn)
      end

      it 'has encrypted data in command monitoring' do
        # Command started event occurs after ssn is encrypted
        # Command succeeded event does not contain any data to be decrypted
        expect(started_event.command["query"]["ssn"]["$eq"]).to be_ciphertext
      end

      it_behaves_like 'it has a non-encrypted key_vault_client'
    end

    describe '#distinct' do
      let(:command_name) { 'distinct' }

      before do
        encryption_client['users'].distinct(:ssn)
      end

      it 'has encrypted data in command monitoring' do
        # Command started event does not contain any data to be encrypted
        # Command succeeded event occurs before ssn is decrypted
        expect(succeeded_event.reply["values"].first).to be_ciphertext
      end

      it_behaves_like 'it has a non-encrypted key_vault_client'
    end

    describe '#delete_one' do
      let(:command_name) { 'delete' }

      before do
        encryption_client['users'].delete_one(ssn: ssn)
      end

      it 'has encrypted data in command monitoring' do
        # Command started event occurs after ssn is encrypted
        # Command succeeded event does not contain any data to be decrypted
        expect(started_event.command["deletes"].first["q"]["ssn"]["$eq"]).to be_ciphertext
      end

      it_behaves_like 'it has a non-encrypted key_vault_client'
    end

    describe '#delete_many' do
      let(:command_name) { 'delete' }

      before do
        encryption_client['users'].delete_many(ssn: ssn)
      end

      it 'has encrypted data in command monitoring' do
        # Command started event occurs after ssn is encrypted
        # Command succeeded event does not contain any data to be decrypted
        expect(started_event.command["deletes"].first["q"]["ssn"]["$eq"]).to be_ciphertext
      end

      it_behaves_like 'it has a non-encrypted key_vault_client'
    end

    describe '#find' do
      let(:command_name) { 'find' }

      before do
        encryption_client['users'].find(ssn: ssn).first
      end

      it 'has encrypted data in command monitoring' do
        # Command started event occurs after ssn is encrypted
        expect(started_event.command["filter"]["ssn"]["$eq"]).to be_ciphertext

        # Command succeeded event occurs before ssn is decrypted
        expect(succeeded_event.reply["cursor"]["firstBatch"].first["ssn"]).to be_ciphertext
      end

      it_behaves_like 'it has a non-encrypted key_vault_client'
    end

    describe '#find_one_and_delete' do
      let(:command_name) { 'findAndModify' }

      before do
        encryption_client['users'].find_one_and_delete(ssn: ssn)
      end

      it 'has encrypted data in command monitoring' do
        # Command started event occurs after ssn is encrypted
        expect(started_event.command["query"]["ssn"]["$eq"]).to be_ciphertext

        # Command succeeded event occurs before ssn is decrypted
        expect(succeeded_event.reply["value"]["ssn"]).to be_ciphertext
      end

      it_behaves_like 'it has a non-encrypted key_vault_client'
    end

    describe '#find_one_and_replace' do
      let(:command_name) { 'findAndModify' }

      before do
        encryption_client['users'].find_one_and_replace(
          { ssn: ssn },
          { ssn: '555-555-5555' }
        )
      end

      it 'has encrypted data in command monitoring' do
        # Command started event occurs after ssn is encrypted
        expect(started_event.command["query"]["ssn"]["$eq"]).to be_ciphertext
        expect(started_event.command["update"]["ssn"]).to be_ciphertext

        # Command succeeded event occurs before ssn is decrypted
        expect(succeeded_event.reply["value"]["ssn"]).to be_ciphertext
      end

      it_behaves_like 'it has a non-encrypted key_vault_client'
    end

    describe '#find_one_and_update' do
      let(:command_name) { 'findAndModify' }

      before do
        encryption_client['users'].find_one_and_update(
          { ssn: ssn },
          { ssn: '555-555-5555' }
        )
      end

      it 'has encrypted data in command monitoring' do

        # Command started event occurs after ssn is encrypted
        expect(started_event.command["query"]["ssn"]["$eq"]).to be_ciphertext
        expect(started_event.command["update"]["ssn"]).to be_ciphertext

        # Command succeeded event occurs before ssn is decrypted
        expect(succeeded_event.reply["value"]["ssn"]).to be_ciphertext
      end

      it_behaves_like 'it has a non-encrypted key_vault_client'
    end

    describe '#replace_one' do
      let(:command_name) { 'update' }

      before do
        encryption_client['users'].replace_one(
          { ssn: ssn },
          { ssn: '555-555-5555' }
        )
      end

      it 'has encrypted data in command monitoring' do
        # Command started event occurs after ssn is encrypted
        # Command succeeded event does not contain any data to be decrypted
        expect(started_event.command["updates"].first["q"]["ssn"]["$eq"]).to be_ciphertext
        expect(started_event.command["updates"].first["u"]["ssn"]).to be_ciphertext
      end

      it_behaves_like 'it has a non-encrypted key_vault_client'
    end

    describe '#update_one' do
      let(:command_name) { 'update' }

      before do
        encryption_client['users'].replace_one({ ssn: ssn }, { ssn: '555-555-5555' })
      end

      it 'has encrypted data in command monitoring' do
        # Command started event occurs after ssn is encrypted
        # Command succeeded event does not contain any data to be decrypted
        expect(started_event.command["updates"].first["q"]["ssn"]["$eq"]).to be_ciphertext
        expect(started_event.command["updates"].first["u"]["ssn"]).to be_ciphertext
      end

      it_behaves_like 'it has a non-encrypted key_vault_client'
    end

    describe '#update_many' do
      let(:command_name) { 'update' }

      before do
        # update_many does not support replacement-style updates
        encryption_client['users'].update_many({ ssn: ssn }, { "$inc" => { :age => 1 } })
      end

      it 'has encrypted data in command monitoring' do
        # Command started event occurs after ssn is encrypted
        # Command succeeded event does not contain any data to be decrypted
        expect(started_event.command["updates"].first["q"]["ssn"]["$eq"]).to be_ciphertext
      end

      it_behaves_like 'it has a non-encrypted key_vault_client'
    end
  end

  describe '#insert_one' do
    let(:command_name) { 'insert' }

    before do
      encryption_client['users'].insert_one(ssn: ssn)
    end

    it 'has encrypted data in command monitoring' do
      # Command started event occurs after ssn is encrypted
      # Command succeeded event does not contain any data to be decrypted
      expect(started_event.command["documents"].first["ssn"]).to be_ciphertext
    end

    it_behaves_like 'it has a non-encrypted key_vault_client'
  end
end