-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathclient_side_encryption.prose.14.decryption_events.test.ts
More file actions
269 lines (247 loc) · 9.53 KB
/
client_side_encryption.prose.14.decryption_events.test.ts
File metadata and controls
269 lines (247 loc) · 9.53 KB
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
import { expect } from 'chai';
import {
Binary,
BSON,
ClientEncryption,
type CommandFailedEvent,
type CommandSucceededEvent,
type MongoClient,
MongoNetworkError
} from '../../mongodb';
import { getEncryptExtraOptions } from '../../tools/utils';
const metadata: MongoDBMetadataUI = {
requires: {
clientSideEncryption: true,
topology: '!load-balanced'
}
};
const LOCAL_KEY = Buffer.from(
'Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk',
'base64'
);
describe('14. Decryption Events', metadata, function () {
let setupClient: MongoClient;
let clientEncryption;
let keyId: Binary;
let cipherText: Binary;
let malformedCiphertext: Binary;
let encryptedClient: MongoClient;
let aggregateSucceeded: CommandSucceededEvent | undefined;
let aggregateFailed: CommandFailedEvent | undefined;
beforeEach(async function () {
// Create a MongoClient named ``setupClient``.
setupClient = this.configuration.newClient();
// Drop and create the collection ``db.decryption_events``.
const db = setupClient.db('db');
await setupClient
.db('db')
.collection('decryption_events')
.deleteMany({})
.catch(() => null);
await db.dropCollection('decryption_events');
await db.createCollection('decryption_events');
// Create a ClientEncryption object named ``clientEncryption`` with these options:
// ClientEncryptionOpts {
// keyVaultClient: <setupClient>,
// keyVaultNamespace: "keyvault.datakeys",
// kmsProviders: { "local": { "key": <base64 decoding of LOCAL_MASTERKEY> } }
// }
clientEncryption = new ClientEncryption(setupClient, {
keyVaultNamespace: 'keyvault.datakeys',
kmsProviders: { local: { key: LOCAL_KEY } },
bson: BSON,
extraOptions: getEncryptExtraOptions()
});
// Create a data key with the "local" KMS provider.
// Storing the result in a variable named ``keyID``.
keyId = await clientEncryption.createDataKey('local');
// Use ``clientEncryption`` to encrypt the string "hello" with the following ``EncryptOpts``:
// EncryptOpts {
// keyId: <keyID>,
// algorithm: "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic"
// }
// Store the result in a variable named ``ciphertext``.
cipherText = await clientEncryption.encrypt('hello', {
keyId: keyId,
algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic'
});
// Copy ``ciphertext`` into a variable named ``malformedCiphertext``. Change the
// last byte to a different value. This will produce an invalid HMAC tag.
const buffer = Buffer.from(cipherText.buffer);
const lastByte = buffer.readUInt8(buffer.length - 1);
const replacementByte = lastByte === 0 ? 1 : 0;
buffer.writeUInt8(replacementByte, buffer.length - 1);
malformedCiphertext = new Binary(buffer, 6);
// Create a MongoClient named ``encryptedClient`` with these ``AutoEncryptionOpts``:
// AutoEncryptionOpts {
// keyVaultNamespace: "keyvault.datakeys";
// kmsProviders: { "local": { "key": <base64 decoding of LOCAL_MASTERKEY> } }
// }
// Configure ``encryptedClient`` with "retryReads=false".
encryptedClient = this.configuration.newClient(
{},
{
writeConcern: { w: 'majority' },
retryReads: false,
monitorCommands: true,
autoEncryption: {
keyVaultNamespace: 'keyvault.datakeys',
kmsProviders: { local: { key: LOCAL_KEY } },
extraOptions: getEncryptExtraOptions()
}
}
);
// Register a listener for CommandSucceeded events on ``encryptedClient``.
encryptedClient.on('commandSucceeded', event => {
if (event.commandName === 'aggregate') {
aggregateSucceeded = event;
}
});
// The listener must store the most recent CommandFailedEvent error for the "aggregate" command.
encryptedClient.on('commandFailed', event => {
if (event.commandName === 'aggregate') {
aggregateFailed = event;
}
});
});
afterEach(async function () {
aggregateSucceeded = undefined;
aggregateFailed = undefined;
await setupClient
.db('db')
.collection('decryption_events')
.deleteMany({})
.catch(() => null);
await setupClient.close();
await encryptedClient.close();
});
context('Case 1: Command Error', metadata, function () {
beforeEach(async function () {
// Use ``setupClient`` to configure the following failpoint:
// {
// "configureFailPoint": "failCommand",
// "mode": {
// "times": 1
// },
// "data": {
// "errorCode": 123,
// "failCommands": [
// "aggregate"
// ]
// }
// }
await setupClient
.db()
.admin()
.command({
configureFailPoint: 'failCommand',
mode: {
times: 1
},
data: {
errorCode: 123,
failCommands: ['aggregate']
}
});
});
it('expects an error and a command failed event', async function () {
// Use ``encryptedClient`` to run an aggregate on ``db.decryption_events``.
// Expect an exception to be thrown from the command error. Expect a CommandFailedEvent.
const collection = encryptedClient.db('db').collection('decryption_events');
const error = await collection
.aggregate([])
.toArray()
.catch(error => error);
expect(error).to.have.property('code', 123);
expect(aggregateFailed).to.have.nested.property('failure.code', 123);
});
});
context('Case 2: Network Error', metadata, function () {
beforeEach(async function () {
// Use ``setupClient`` to configure the following failpoint:
// {
// "configureFailPoint": "failCommand",
// "mode": {
// "times": 1
// },
// "data": {
// "errorCode": 123,
// "closeConnection": true,
// "failCommands": [
// "aggregate"
// ]
// }
// }
await setupClient
.db()
.admin()
.command({
configureFailPoint: 'failCommand',
mode: {
times: 1
},
data: {
errorCode: 123,
closeConnection: true,
failCommands: ['aggregate']
}
});
});
it('expects an error and a command failed event', async function () {
// Use ``encryptedClient`` to run an aggregate on ``db.decryption_events``.
// Expect an exception to be thrown from the network error. Expect a CommandFailedEvent.
const collection = encryptedClient.db('db').collection('decryption_events');
const error = await collection
.aggregate([])
.toArray()
.catch(error => error);
expect(error).to.be.instanceOf(MongoNetworkError);
expect(aggregateFailed).to.have.nested.property('failure.message').to.include('closed');
});
});
context('Case 3: Decrypt Error', metadata, function () {
it('errors on decryption but command succeeds', async function () {
// Use ``encryptedClient`` to insert the document ``{ "encrypted": <malformedCiphertext> }``
// into ``db.decryption_events``.
// Use ``encryptedClient`` to run an aggregate on ``db.decryption_events``.
// Expect an exception to be thrown from the decryption error.
// Expect a CommandSucceededEvent. Expect the CommandSucceededEvent.reply
// to contain BSON binary for the field
// ``cursor.firstBatch.encrypted``.
const collection = encryptedClient.db('db').collection('decryption_events');
await collection.insertOne(
{ encrypted: malformedCiphertext },
{ writeConcern: { w: 'majority' } }
);
/// Verify the malformedCiphertext was inserted with a plain client
const docs = await setupClient.db('db').collection('decryption_events').find({}).toArray();
expect(docs).to.have.lengthOf(1);
expect(docs).to.have.deep.nested.property('[0].encrypted', malformedCiphertext);
const error = await collection
.aggregate([])
.toArray()
.catch(error => error);
expect(error).to.have.property('message').to.include('HMAC validation failure');
expect(aggregateSucceeded)
.to.have.nested.property('reply.cursor.firstBatch[0].encrypted')
.to.be.instanceOf(Binary);
});
});
context('Case 4: Decrypt Success', metadata, function () {
it('succeeds on decryption and command succeeds', async function () {
// Use ``encryptedClient`` to insert the document ``{ "encrypted": <ciphertext> }``
// into ``db.decryption_events``.
// Use ``encryptedClient`` to run an aggregate on ``db.decryption_events``.
// Expect no exception.
// Expect a CommandSucceededEvent. Expect the CommandSucceededEvent.reply
// to contain BSON binary for the field ``cursor.firstBatch.encrypted``.
const collection = encryptedClient.db('db').collection('decryption_events');
await collection.insertOne({ encrypted: cipherText });
const result = await collection.aggregate([]).toArray();
expect(result).to.have.nested.property('[0].encrypted', 'hello');
expect(aggregateSucceeded)
.to.have.nested.property('reply.cursor.firstBatch[0].encrypted')
.to.be.instanceOf(Binary);
});
});
});