Repository navigation
Expand file tree
/
Copy pathclient_encryption.go
More file actions
559 lines (489 loc) · 18.4 KB
/
Copy pathclient_encryption.go
File metadata and controls
559 lines (489 loc) · 18.4 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
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package mongo
import (
"bytes"
"context"
"errors"
"fmt"
"strings"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/internal/credutil"
"go.mongodb.org/mongo-driver/v2/internal/mongoutil"
"go.mongodb.org/mongo-driver/v2/mongo/options"
"go.mongodb.org/mongo-driver/v2/x/bsonx/bsoncore"
"go.mongodb.org/mongo-driver/v2/x/mongo/driver"
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/mongocrypt"
mcopts "go.mongodb.org/mongo-driver/v2/x/mongo/driver/mongocrypt/options"
)
// ClientEncryption is used to create data keys and explicitly encrypt and decrypt BSON values.
type ClientEncryption struct {
crypt driver.Crypt
keyVaultClient *Client
keyVaultColl *Collection
closed bool
}
// NewClientEncryption creates a new ClientEncryption instance configured with the given options.
func NewClientEncryption(keyVaultClient *Client, opts ...options.Lister[options.ClientEncryptionOptions]) (*ClientEncryption, error) {
if keyVaultClient == nil {
return nil, errors.New("keyVaultClient must not be nil")
}
ce := &ClientEncryption{
keyVaultClient: keyVaultClient,
}
cea, err := mongoutil.NewOptions(opts...)
if err != nil {
return nil, err
}
// create keyVaultColl
db, coll := splitNamespace(cea.KeyVaultNamespace)
ce.keyVaultColl = ce.keyVaultClient.Database(db).Collection(coll, keyVaultCollOpts)
kmsProviders, err := marshal(cea.KmsProviders, nil, nil)
if err != nil {
return nil, fmt.Errorf("error creating KMS providers map: %w", err)
}
cryptOpts := &mcopts.MongoCryptOptions{
KmsProviders: kmsProviders,
// Explicitly disable loading the crypt_shared library for the Crypt used for
// ClientEncryption because it's only needed for AutoEncryption and we don't expect users to
// have the crypt_shared library installed if they're using ClientEncryption.
CryptSharedLibDisabled: true,
HTTPClient: cea.HTTPClient,
KeyExpiration: cea.KeyExpiration,
}
if cea.AWSCredentialsProvider != nil {
cryptOpts.AWSCredentialsProvider = credutil.AWSOptionsProvider{Provider: cea.AWSCredentialsProvider}
}
mc, err := mongocrypt.NewMongoCrypt(cryptOpts)
if err != nil {
return nil, err
}
// create Crypt
kr := keyRetriever{coll: ce.keyVaultColl}
cir := collInfoRetriever{client: ce.keyVaultClient}
ce.crypt = driver.NewCrypt(&driver.CryptOptions{
MongoCrypt: mc,
KeyFn: kr.cryptKeys,
CollInfoFn: cir.cryptCollInfo,
TLSConfig: cea.TLSConfig,
})
return ce, nil
}
// CreateEncryptedCollection creates a new collection for Queryable Encryption with the help of automatic generation of new encryption data keys for null keyIds.
// It returns the created collection and the encrypted fields document used to create it.
func (ce *ClientEncryption) CreateEncryptedCollection(ctx context.Context,
db *Database, coll string, createOpts options.Lister[options.CreateCollectionOptions],
kmsProvider string, masterKey any,
) (*Collection, bson.M, error) {
if ce.closed {
return nil, nil, ErrClientDisconnected
}
if createOpts == nil {
return nil, nil, errors.New("nil CreateCollectionOptions")
}
createArgs, err := mongoutil.NewOptions[options.CreateCollectionOptions](createOpts)
if err != nil {
return nil, nil, fmt.Errorf("failed to construct options from builder: %w", err)
}
ef := createArgs.EncryptedFields
if ef == nil {
return nil, nil, errors.New("no EncryptedFields defined for the collection")
}
efBSON, err := marshal(ef, db.bsonOpts, db.registry)
if err != nil {
return nil, nil, err
}
r := bson.NewDocumentReader(bytes.NewReader(efBSON))
dec := bson.NewDecoder(r)
dec.DefaultDocumentM()
var m bson.M
err = dec.Decode(&m)
if err != nil {
return nil, nil, err
}
if v, ok := m["fields"]; ok {
if fields, ok := v.(bson.A); ok {
for _, field := range fields {
if f, ok := field.(bson.M); !ok {
continue
} else if v, ok := f["keyId"]; ok && v == nil {
dkOpts := options.DataKey()
if masterKey != nil {
dkOpts.SetMasterKey(masterKey)
}
keyid, err := ce.CreateDataKey(ctx, kmsProvider, dkOpts)
if err != nil {
createArgs.EncryptedFields = m
return nil, m, err
}
f["keyId"] = keyid
}
}
createArgs.EncryptedFields = m
}
}
updatedCreateOpts := mongoutil.NewOptionsLister(createArgs, nil)
err = db.CreateCollection(ctx, coll, updatedCreateOpts)
if err != nil {
return nil, m, err
}
return db.Collection(coll), m, nil
}
// AddKeyAltName adds a keyAltName to the keyAltNames array of the key document in the key vault collection with the
// given UUID (BSON binary subtype 0x04). Returns the previous version of the key document.
func (ce *ClientEncryption) AddKeyAltName(ctx context.Context, id bson.Binary, keyAltName string) *SingleResult {
if ce.closed {
return &SingleResult{err: ErrClientDisconnected}
}
filter := bsoncore.NewDocumentBuilder().AppendBinary("_id", id.Subtype, id.Data).Build()
keyAltNameDoc := bsoncore.NewDocumentBuilder().AppendString("keyAltNames", keyAltName).Build()
update := bsoncore.NewDocumentBuilder().AppendDocument("$addToSet", keyAltNameDoc).Build()
return ce.keyVaultColl.FindOneAndUpdate(ctx, filter, update)
}
// CreateDataKey creates a new key document and inserts into the key vault collection. Returns the _id of the created
// document as a UUID (BSON binary subtype 0x04).
func (ce *ClientEncryption) CreateDataKey(
ctx context.Context,
kmsProvider string,
opts ...options.Lister[options.DataKeyOptions],
) (bson.Binary, error) {
if ce.closed {
return bson.Binary{}, ErrClientDisconnected
}
args, err := mongoutil.NewOptions[options.DataKeyOptions](opts...)
if err != nil {
return bson.Binary{}, fmt.Errorf("failed to construct options from builder: %w", err)
}
co := &mcopts.DataKeyOptions{
KeyAltNames: args.KeyAltNames,
KeyMaterial: args.KeyMaterial,
}
if args.MasterKey != nil {
keyDoc, err := marshal(
args.MasterKey,
ce.keyVaultClient.bsonOpts,
ce.keyVaultClient.registry)
if err != nil {
return bson.Binary{}, err
}
co.MasterKey = keyDoc
}
// create data key document
dataKeyDoc, err := ce.crypt.CreateDataKey(ctx, kmsProvider, co)
if err != nil {
return bson.Binary{}, err
}
// insert key into key vault
_, err = ce.keyVaultColl.InsertOne(ctx, dataKeyDoc)
if err != nil {
return bson.Binary{}, err
}
subtype, data := bson.Raw(dataKeyDoc).Lookup("_id").Binary()
return bson.Binary{Subtype: subtype, Data: data}, nil
}
// transformExplicitEncryptionOptions creates explicit encryption options to be passed to libmongocrypt.
func transformExplicitEncryptionOptions(opts ...options.Lister[options.EncryptOptions]) (*mcopts.ExplicitEncryptionOptions, error) {
args, err := mongoutil.NewOptions[options.EncryptOptions](opts...)
if err != nil {
return nil, err
}
transformed := &mcopts.ExplicitEncryptionOptions{
KeyID: args.KeyID,
KeyAltName: args.KeyAltName,
Algorithm: args.Algorithm,
QueryType: args.QueryType,
ContentionFactor: args.ContentionFactor,
}
if args.RangeOptions != nil {
rangeArgs, err := mongoutil.NewOptions[options.RangeOptions](args.RangeOptions)
if err != nil {
return nil, err
}
var transformedRange mcopts.ExplicitRangeOptions
if rangeArgs.Min != nil {
transformedRange.Min = &bsoncore.Value{Type: bsoncore.Type(rangeArgs.Min.Type), Data: rangeArgs.Min.Value}
}
if rangeArgs.Max != nil {
transformedRange.Max = &bsoncore.Value{Type: bsoncore.Type(rangeArgs.Max.Type), Data: rangeArgs.Max.Value}
}
if rangeArgs.Precision != nil {
transformedRange.Precision = rangeArgs.Precision
}
if rangeArgs.Sparsity != nil {
transformedRange.Sparsity = rangeArgs.Sparsity
}
if rangeArgs.TrimFactor != nil {
transformedRange.TrimFactor = rangeArgs.TrimFactor
}
transformed.RangeOptions = &transformedRange
}
if args.StringOptions != nil {
stringArgs, err := mongoutil.NewOptions[options.StringOptions](args.StringOptions)
if err != nil {
return nil, err
}
transformedString := mcopts.ExplicitStringOptions{
CaseSensitive: stringArgs.CaseSensitive,
DiacriticSensitive: stringArgs.DiacriticSensitive,
}
if stringArgs.Substring != nil {
substringOpts := mcopts.SubstringOptions(*stringArgs.Substring)
transformedString.Substring = &substringOpts
}
if stringArgs.Prefix != nil {
prefixOpts := mcopts.PrefixOptions(*stringArgs.Prefix)
transformedString.Prefix = &prefixOpts
}
if stringArgs.Suffix != nil {
suffixOpts := mcopts.SuffixOptions(*stringArgs.Suffix)
transformedString.Suffix = &suffixOpts
}
transformed.SetStringOptions(transformedString)
}
return transformed, nil
}
// Encrypt encrypts a BSON value with the given key and algorithm. Returns an encrypted value (BSON binary of subtype 6).
func (ce *ClientEncryption) Encrypt(
ctx context.Context,
val bson.RawValue,
opts ...options.Lister[options.EncryptOptions],
) (bson.Binary, error) {
if ce.closed {
return bson.Binary{}, ErrClientDisconnected
}
transformed, err := transformExplicitEncryptionOptions(opts...)
if err != nil {
return bson.Binary{}, err
}
subtype, data, err := ce.crypt.EncryptExplicit(ctx, bsoncore.Value{Type: bsoncore.Type(val.Type), Data: val.Value}, transformed)
if err != nil {
return bson.Binary{}, err
}
return bson.Binary{Subtype: subtype, Data: data}, nil
}
// EncryptExpression encrypts an expression to query a range index.
// On success, `result` is populated with the resulting BSON document.
// `expr` is expected to be a BSON document of one of the following forms:
// 1. A Match Expression of this form:
// {$and: [{<field>: {$gt: <value1>}}, {<field>: {$lt: <value2> }}]}
// 2. An Aggregate Expression of this form:
// {$and: [{$gt: [<fieldpath>, <value1>]}, {$lt: [<fieldpath>, <value2>]}]
// $gt may also be $gte. $lt may also be $lte.
// Only supported for queryType "range"
func (ce *ClientEncryption) EncryptExpression(ctx context.Context, expr any, result any, opts ...options.Lister[options.EncryptOptions]) error {
if ce.closed {
return ErrClientDisconnected
}
transformed, err := transformExplicitEncryptionOptions(opts...)
if err != nil {
return err
}
exprDoc, err := marshal(expr, nil, nil)
if err != nil {
return err
}
encryptedExprDoc, err := ce.crypt.EncryptExplicitExpression(ctx, exprDoc, transformed)
if err != nil {
return err
}
if raw, ok := result.(*bson.Raw); ok {
// Avoid the cost of Unmarshal.
*raw = bson.Raw(encryptedExprDoc)
return nil
}
err = bson.Unmarshal([]byte(encryptedExprDoc), result)
if err != nil {
return err
}
return nil
}
// Decrypt decrypts an encrypted value (BSON binary of subtype 6) and returns the original BSON value.
func (ce *ClientEncryption) Decrypt(ctx context.Context, val bson.Binary) (bson.RawValue, error) {
if ce.closed {
return bson.RawValue{}, ErrClientDisconnected
}
decrypted, err := ce.crypt.DecryptExplicit(ctx, val.Subtype, val.Data)
if err != nil {
return bson.RawValue{}, err
}
return bson.RawValue{Type: bson.Type(decrypted.Type), Value: decrypted.Data}, nil
}
// Close cleans up any resources associated with the ClientEncryption instance. This includes disconnecting the
// key-vault Client instance.
func (ce *ClientEncryption) Close(ctx context.Context) error {
if ce.closed {
return ErrClientDisconnected
}
ce.crypt.Close()
err := ce.keyVaultClient.Disconnect(ctx)
if err == nil {
ce.closed = true
}
return err
}
// DeleteKey removes the key document with the given UUID (BSON binary subtype 0x04) from the key vault collection.
// Returns the result of the internal deleteOne() operation on the key vault collection.
func (ce *ClientEncryption) DeleteKey(ctx context.Context, id bson.Binary) (*DeleteResult, error) {
if ce.closed {
return nil, ErrClientDisconnected
}
filter := bsoncore.NewDocumentBuilder().AppendBinary("_id", id.Subtype, id.Data).Build()
return ce.keyVaultColl.DeleteOne(ctx, filter)
}
// GetKeyByAltName returns a key document in the key vault collection with the given keyAltName.
func (ce *ClientEncryption) GetKeyByAltName(ctx context.Context, keyAltName string) *SingleResult {
if ce.closed {
return &SingleResult{err: ErrClientDisconnected}
}
filter := bsoncore.NewDocumentBuilder().AppendString("keyAltNames", keyAltName).Build()
return ce.keyVaultColl.FindOne(ctx, filter)
}
// GetKey finds a single key document with the given UUID (BSON binary subtype 0x04). Returns the result of the
// internal find() operation on the key vault collection.
func (ce *ClientEncryption) GetKey(ctx context.Context, id bson.Binary) *SingleResult {
if ce.closed {
return &SingleResult{err: ErrClientDisconnected}
}
filter := bsoncore.NewDocumentBuilder().AppendBinary("_id", id.Subtype, id.Data).Build()
return ce.keyVaultColl.FindOne(ctx, filter)
}
// GetKeys finds all documents in the key vault collection. Returns the result of the internal find() operation on the
// key vault collection.
func (ce *ClientEncryption) GetKeys(ctx context.Context) (*Cursor, error) {
if ce.closed {
return nil, ErrClientDisconnected
}
return ce.keyVaultColl.Find(ctx, bson.D{})
}
// RemoveKeyAltName removes a keyAltName from the keyAltNames array of the key document in the key vault collection with
// the given UUID (BSON binary subtype 0x04). Returns the previous version of the key document.
func (ce *ClientEncryption) RemoveKeyAltName(ctx context.Context, id bson.Binary, keyAltName string) *SingleResult {
if ce.closed {
return &SingleResult{err: ErrClientDisconnected}
}
filter := bsoncore.NewDocumentBuilder().AppendBinary("_id", id.Subtype, id.Data).Build()
update := bson.A{bson.D{{"$set", bson.D{{"keyAltNames", bson.D{{"$cond", bson.A{bson.D{{
"$eq",
bson.A{"$keyAltNames", bson.A{keyAltName}},
}}, "$$REMOVE", bson.D{{
"$filter",
bson.D{{"input", "$keyAltNames"}, {"cond", bson.D{{"$ne", bson.A{"$$this", keyAltName}}}}},
}}}}}}}}}}
return ce.keyVaultColl.FindOneAndUpdate(ctx, filter, update)
}
// setRewrapManyDataKeyWriteModels will prepare the WriteModel slice for a bulk updating rewrapped documents.
func setRewrapManyDataKeyWriteModels(rewrappedDocuments []bsoncore.Document, writeModels *[]WriteModel) error {
const idKey = "_id"
const keyMaterial = "keyMaterial"
const masterKey = "masterKey"
if writeModels == nil {
return fmt.Errorf("writeModels pointer not set for location referenced")
}
// Append a slice of WriteModel with the update document per each rewrappedDoc _id filter.
for _, rewrappedDocument := range rewrappedDocuments {
// Prepare the new master key for update.
masterKeyValue, err := rewrappedDocument.LookupErr(masterKey)
if err != nil {
return err
}
masterKeyDoc := masterKeyValue.Document()
// Prepare the new material key for update.
keyMaterialValue, err := rewrappedDocument.LookupErr(keyMaterial)
if err != nil {
return err
}
keyMaterialSubtype, keyMaterialData := keyMaterialValue.Binary()
keyMaterialBinary := bson.Binary{Subtype: keyMaterialSubtype, Data: keyMaterialData}
// Prepare the _id filter for documents to update.
id, err := rewrappedDocument.LookupErr(idKey)
if err != nil {
return err
}
idSubtype, idData, ok := id.BinaryOK()
if !ok {
return fmt.Errorf("expected to assert %q as binary, got type %s", idKey, id.Type)
}
binaryID := bson.Binary{Subtype: idSubtype, Data: idData}
// Append the mutable document to the slice for bulk update.
*writeModels = append(*writeModels, NewUpdateOneModel().
SetFilter(bson.D{{idKey, binaryID}}).
SetUpdate(
bson.D{
{"$set", bson.D{{keyMaterial, keyMaterialBinary}, {masterKey, masterKeyDoc}}},
{"$currentDate", bson.D{{"updateDate", true}}},
},
))
}
return nil
}
// RewrapManyDataKey decrypts and encrypts all matching data keys with a possibly new masterKey value. For all
// matching documents, this method will overwrite the "masterKey", "updateDate", and "keyMaterial". On error, some
// matching data keys may have been rewrapped.
// libmongocrypt 1.5.2 is required. An error is returned if the detected version of libmongocrypt is less than 1.5.2.
func (ce *ClientEncryption) RewrapManyDataKey(
ctx context.Context,
filter any,
opts ...options.Lister[options.RewrapManyDataKeyOptions],
) (*RewrapManyDataKeyResult, error) {
// libmongocrypt versions 1.5.0 and 1.5.1 have a severe bug in RewrapManyDataKey.
// Check if the version string starts with 1.5.0 or 1.5.1. This accounts for pre-release versions, like 1.5.0-rc0.
if ce.closed {
return nil, ErrClientDisconnected
}
libmongocryptVersion := mongocrypt.Version()
if strings.HasPrefix(libmongocryptVersion, "1.5.0") || strings.HasPrefix(libmongocryptVersion, "1.5.1") {
return nil, fmt.Errorf("RewrapManyDataKey requires libmongocrypt 1.5.2 or newer. Detected version: %v", libmongocryptVersion)
}
if ctx == nil {
ctx = context.Background()
}
args, err := mongoutil.NewOptions[options.RewrapManyDataKeyOptions](opts...)
if err != nil {
return nil, fmt.Errorf("failed to construct options from builder: %w", err)
}
// Transfer rmdko options to /x/ package options to publish the mongocrypt feed.
co := &mcopts.RewrapManyDataKeyOptions{
Provider: args.Provider,
}
if args.MasterKey != nil {
keyDoc, err := marshal(
args.MasterKey,
ce.keyVaultClient.bsonOpts,
ce.keyVaultClient.registry)
if err != nil {
return nil, err
}
co.MasterKey = keyDoc
}
// Prepare the filters and rewrap the data key using mongocrypt.
filterdoc, err := marshal(filter, ce.keyVaultClient.bsonOpts, ce.keyVaultClient.registry)
if err != nil {
return nil, err
}
rewrappedDocuments, err := ce.crypt.RewrapDataKey(ctx, filterdoc, co)
if err != nil {
return nil, err
}
if len(rewrappedDocuments) == 0 {
// If there are no documents to rewrap, then do nothing.
return new(RewrapManyDataKeyResult), nil
}
// Prepare the WriteModel slice for bulk updating the rewrapped data keys.
models := []WriteModel{}
if err := setRewrapManyDataKeyWriteModels(rewrappedDocuments, &models); err != nil {
return nil, err
}
bulkWriteResults, err := ce.keyVaultColl.BulkWrite(ctx, models)
return &RewrapManyDataKeyResult{BulkWriteResult: bulkWriteResults}, err
}
// splitNamespace takes a namespace in the form "database.collection" and returns (database name, collection name)
func splitNamespace(ns string) (string, string) {
firstDot := strings.Index(ns, ".")
if firstDot == -1 {
return "", ns
}
return ns[:firstDot], ns[firstDot+1:]
}