@@ -43,24 +43,46 @@ type CryptOptions struct {
43
43
BypassAutoEncryption bool
44
44
}
45
45
46
- // Crypt consumes the libmongocrypt.MongoCrypt type to iterate the mongocrypt state machine and perform encryption
46
+ // Crypt is an interface implemented by types that can encrypt and decrypt instances of
47
+ // bsoncore.Document.
48
+ //
49
+ // Users should rely on the driver's crypt type (used by default) for encryption and decryption
50
+ // unless they are perfectly confident in another implementation of Crypt.
51
+ type Crypt interface {
52
+ // Encrypt encrypts the given command.
53
+ Encrypt (ctx context.Context , db string , cmd bsoncore.Document ) (bsoncore.Document , error )
54
+ // Decrypt decrypts the given command response.
55
+ Decrypt (ctx context.Context , cmdResponse bsoncore.Document ) (bsoncore.Document , error )
56
+ // CreateDataKey creates a data key using the given KMS provider and options.
57
+ CreateDataKey (ctx context.Context , kmsProvider string , opts * options.DataKeyOptions ) (bsoncore.Document , error )
58
+ // EncryptExplicit encrypts the given value with the given options.
59
+ EncryptExplicit (ctx context.Context , val bsoncore.Value , opts * options.ExplicitEncryptionOptions ) (byte , []byte , error )
60
+ // DecryptExplicit decrypts the given encrypted value.
61
+ DecryptExplicit (ctx context.Context , subtype byte , data []byte ) (bsoncore.Value , error )
62
+ // Close cleans up any resources associated with the Crypt instance.
63
+ Close ()
64
+ // BypassAutoEncryption returns true if auto-encryption should be bypassed.
65
+ BypassAutoEncryption () bool
66
+ }
67
+
68
+ // crypt consumes the libmongocrypt.MongoCrypt type to iterate the mongocrypt state machine and perform encryption
47
69
// and decryption.
48
- type Crypt struct {
70
+ type crypt struct {
49
71
mongoCrypt * mongocrypt.MongoCrypt
50
72
collInfoFn CollectionInfoFn
51
73
keyFn KeyRetrieverFn
52
74
markFn MarkCommandFn
53
75
54
- BypassAutoEncryption bool
76
+ bypassAutoEncryption bool
55
77
}
56
78
57
79
// NewCrypt creates a new Crypt instance configured with the given AutoEncryptionOptions.
58
- func NewCrypt (opts * CryptOptions ) (* Crypt , error ) {
59
- c := & Crypt {
80
+ func NewCrypt (opts * CryptOptions ) (Crypt , error ) {
81
+ c := & crypt {
60
82
collInfoFn : opts .CollInfoFn ,
61
83
keyFn : opts .KeyFn ,
62
84
markFn : opts .MarkFn ,
63
- BypassAutoEncryption : opts .BypassAutoEncryption ,
85
+ bypassAutoEncryption : opts .BypassAutoEncryption ,
64
86
}
65
87
66
88
mongocryptOpts := options .MongoCrypt ().SetKmsProviders (opts .KmsProviders ).SetLocalSchemaMap (opts .SchemaMap )
@@ -74,8 +96,8 @@ func NewCrypt(opts *CryptOptions) (*Crypt, error) {
74
96
}
75
97
76
98
// Encrypt encrypts the given command.
77
- func (c * Crypt ) Encrypt (ctx context.Context , db string , cmd bsoncore.Document ) (bsoncore.Document , error ) {
78
- if c .BypassAutoEncryption {
99
+ func (c * crypt ) Encrypt (ctx context.Context , db string , cmd bsoncore.Document ) (bsoncore.Document , error ) {
100
+ if c .bypassAutoEncryption {
79
101
return cmd , nil
80
102
}
81
103
@@ -89,7 +111,7 @@ func (c *Crypt) Encrypt(ctx context.Context, db string, cmd bsoncore.Document) (
89
111
}
90
112
91
113
// Decrypt decrypts the given command response.
92
- func (c * Crypt ) Decrypt (ctx context.Context , cmdResponse bsoncore.Document ) (bsoncore.Document , error ) {
114
+ func (c * crypt ) Decrypt (ctx context.Context , cmdResponse bsoncore.Document ) (bsoncore.Document , error ) {
93
115
cryptCtx , err := c .mongoCrypt .CreateDecryptionContext (cmdResponse )
94
116
if err != nil {
95
117
return nil , err
@@ -100,7 +122,7 @@ func (c *Crypt) Decrypt(ctx context.Context, cmdResponse bsoncore.Document) (bso
100
122
}
101
123
102
124
// CreateDataKey creates a data key using the given KMS provider and options.
103
- func (c * Crypt ) CreateDataKey (ctx context.Context , kmsProvider string , opts * options.DataKeyOptions ) (bsoncore.Document , error ) {
125
+ func (c * crypt ) CreateDataKey (ctx context.Context , kmsProvider string , opts * options.DataKeyOptions ) (bsoncore.Document , error ) {
104
126
cryptCtx , err := c .mongoCrypt .CreateDataKeyContext (kmsProvider , opts )
105
127
if err != nil {
106
128
return nil , err
@@ -111,7 +133,7 @@ func (c *Crypt) CreateDataKey(ctx context.Context, kmsProvider string, opts *opt
111
133
}
112
134
113
135
// EncryptExplicit encrypts the given value with the given options.
114
- func (c * Crypt ) EncryptExplicit (ctx context.Context , val bsoncore.Value , opts * options.ExplicitEncryptionOptions ) (byte , []byte , error ) {
136
+ func (c * crypt ) EncryptExplicit (ctx context.Context , val bsoncore.Value , opts * options.ExplicitEncryptionOptions ) (byte , []byte , error ) {
115
137
idx , doc := bsoncore .AppendDocumentStart (nil )
116
138
doc = bsoncore .AppendValueElement (doc , "v" , val )
117
139
doc , _ = bsoncore .AppendDocumentEnd (doc , idx )
@@ -132,7 +154,7 @@ func (c *Crypt) EncryptExplicit(ctx context.Context, val bsoncore.Value, opts *o
132
154
}
133
155
134
156
// DecryptExplicit decrypts the given encrypted value.
135
- func (c * Crypt ) DecryptExplicit (ctx context.Context , subtype byte , data []byte ) (bsoncore.Value , error ) {
157
+ func (c * crypt ) DecryptExplicit (ctx context.Context , subtype byte , data []byte ) (bsoncore.Value , error ) {
136
158
idx , doc := bsoncore .AppendDocumentStart (nil )
137
159
doc = bsoncore .AppendBinaryElement (doc , "v" , subtype , data )
138
160
doc , _ = bsoncore .AppendDocumentEnd (doc , idx )
@@ -152,11 +174,15 @@ func (c *Crypt) DecryptExplicit(ctx context.Context, subtype byte, data []byte)
152
174
}
153
175
154
176
// Close cleans up any resources associated with the Crypt instance.
155
- func (c * Crypt ) Close () {
177
+ func (c * crypt ) Close () {
156
178
c .mongoCrypt .Close ()
157
179
}
158
180
159
- func (c * Crypt ) executeStateMachine (ctx context.Context , cryptCtx * mongocrypt.Context , db string ) (bsoncore.Document , error ) {
181
+ func (c * crypt ) BypassAutoEncryption () bool {
182
+ return c .bypassAutoEncryption
183
+ }
184
+
185
+ func (c * crypt ) executeStateMachine (ctx context.Context , cryptCtx * mongocrypt.Context , db string ) (bsoncore.Document , error ) {
160
186
var err error
161
187
for {
162
188
state := cryptCtx .State ()
@@ -180,7 +206,7 @@ func (c *Crypt) executeStateMachine(ctx context.Context, cryptCtx *mongocrypt.Co
180
206
}
181
207
}
182
208
183
- func (c * Crypt ) collectionInfo (ctx context.Context , cryptCtx * mongocrypt.Context , db string ) error {
209
+ func (c * crypt ) collectionInfo (ctx context.Context , cryptCtx * mongocrypt.Context , db string ) error {
184
210
op , err := cryptCtx .NextOperation ()
185
211
if err != nil {
186
212
return err
@@ -199,7 +225,7 @@ func (c *Crypt) collectionInfo(ctx context.Context, cryptCtx *mongocrypt.Context
199
225
return cryptCtx .CompleteOperation ()
200
226
}
201
227
202
- func (c * Crypt ) markCommand (ctx context.Context , cryptCtx * mongocrypt.Context , db string ) error {
228
+ func (c * crypt ) markCommand (ctx context.Context , cryptCtx * mongocrypt.Context , db string ) error {
203
229
op , err := cryptCtx .NextOperation ()
204
230
if err != nil {
205
231
return err
@@ -216,7 +242,7 @@ func (c *Crypt) markCommand(ctx context.Context, cryptCtx *mongocrypt.Context, d
216
242
return cryptCtx .CompleteOperation ()
217
243
}
218
244
219
- func (c * Crypt ) retrieveKeys (ctx context.Context , cryptCtx * mongocrypt.Context ) error {
245
+ func (c * crypt ) retrieveKeys (ctx context.Context , cryptCtx * mongocrypt.Context ) error {
220
246
op , err := cryptCtx .NextOperation ()
221
247
if err != nil {
222
248
return err
@@ -236,7 +262,7 @@ func (c *Crypt) retrieveKeys(ctx context.Context, cryptCtx *mongocrypt.Context)
236
262
return cryptCtx .CompleteOperation ()
237
263
}
238
264
239
- func (c * Crypt ) decryptKeys (ctx context.Context , cryptCtx * mongocrypt.Context ) error {
265
+ func (c * crypt ) decryptKeys (ctx context.Context , cryptCtx * mongocrypt.Context ) error {
240
266
for {
241
267
kmsCtx := cryptCtx .NextKmsContext ()
242
268
if kmsCtx == nil {
@@ -251,7 +277,7 @@ func (c *Crypt) decryptKeys(ctx context.Context, cryptCtx *mongocrypt.Context) e
251
277
return cryptCtx .FinishKmsContexts ()
252
278
}
253
279
254
- func (c * Crypt ) decryptKey (ctx context.Context , kmsCtx * mongocrypt.KmsContext ) error {
280
+ func (c * crypt ) decryptKey (ctx context.Context , kmsCtx * mongocrypt.KmsContext ) error {
255
281
host , err := kmsCtx .HostName ()
256
282
if err != nil {
257
283
return err
0 commit comments