@@ -5,6 +5,7 @@ mod encrypt;
5
5
6
6
use mongocrypt:: { ctx:: KmsProvider , Crypt } ;
7
7
use serde:: { Deserialize , Serialize } ;
8
+ use typed_builder:: TypedBuilder ;
8
9
9
10
use crate :: {
10
11
bson:: { doc, spec:: BinarySubtype , Binary , RawBinaryRef , RawDocumentBuf } ,
@@ -193,57 +194,169 @@ impl ClientEncryption {
193
194
#[ non_exhaustive]
194
195
#[ allow( missing_docs) ]
195
196
pub enum MasterKey {
196
- #[ serde( rename_all = "camelCase" ) ]
197
- Aws {
198
- region : String ,
199
- /// The Amazon Resource Name (ARN) to the AWS customer master key (CMK).
200
- key : String ,
201
- /// An alternate host identifier to send KMS requests to. May include port number. Defaults
202
- /// to "kms.REGION.amazonaws.com"
203
- endpoint : Option < String > ,
204
- } ,
205
- #[ serde( rename_all = "camelCase" ) ]
206
- Azure {
207
- /// Host with optional port. Example: "example.vault.azure.net".
208
- key_vault_endpoint : String ,
209
- key_name : String ,
210
- /// A specific version of the named key, defaults to using the key's primary version.
211
- key_version : Option < String > ,
212
- } ,
213
- #[ serde( rename_all = "camelCase" ) ]
214
- Gcp {
215
- project_id : String ,
216
- location : String ,
217
- key_ring : String ,
218
- key_name : String ,
219
- /// A specific version of the named key, defaults to using the key's primary version.
220
- key_version : Option < String > ,
221
- /// Host with optional port. Defaults to "cloudkms.googleapis.com".
222
- endpoint : Option < String > ,
223
- } ,
224
- /// Master keys are not applicable to `KmsProvider::Local`.
225
- Local ,
226
- #[ serde( rename_all = "camelCase" ) ]
227
- Kmip {
228
- /// keyId is the KMIP Unique Identifier to a 96 byte KMIP Secret Data managed object. If
229
- /// keyId is omitted, the driver creates a random 96 byte KMIP Secret Data managed object.
230
- key_id : Option < String > ,
231
- /// If true (recommended), the KMIP server must decrypt this key. Defaults to false.
232
- delegated : Option < bool > ,
233
- /// Host with optional port.
234
- endpoint : Option < String > ,
235
- } ,
197
+ Aws ( AwsMasterKey ) ,
198
+ Azure ( AzureMasterKey ) ,
199
+ Gcp ( GcpMasterKey ) ,
200
+ Kmip ( KmipMasterKey ) ,
201
+ Local ( LocalMasterKey ) ,
202
+ }
203
+
204
+ /// An AWS master key.
205
+ #[ serde_with:: skip_serializing_none]
206
+ #[ derive( Debug , Clone , Serialize , Deserialize , TypedBuilder ) ]
207
+ #[ builder( field_defaults( default , setter( into) ) ) ]
208
+ #[ serde( rename_all = "camelCase" ) ]
209
+ #[ non_exhaustive]
210
+ pub struct AwsMasterKey {
211
+ /// The name for the key. The value for this field must be the same as the corresponding
212
+ /// [`KmsProvider`](mongocrypt::ctx::KmsProvider)'s name.
213
+ #[ serde( skip) ]
214
+ pub name : Option < String > ,
215
+
216
+ /// The region.
217
+ pub region : String ,
218
+
219
+ /// The Amazon Resource Name (ARN) to the AWS customer master key (CMK).
220
+ pub key : String ,
221
+
222
+ /// An alternate host identifier to send KMS requests to. May include port number. Defaults to
223
+ /// "kms.<region>.amazonaws.com".
224
+ pub endpoint : Option < String > ,
225
+ }
226
+
227
+ impl From < AwsMasterKey > for MasterKey {
228
+ fn from ( aws_master_key : AwsMasterKey ) -> Self {
229
+ Self :: Aws ( aws_master_key)
230
+ }
231
+ }
232
+
233
+ /// An Azure master key.
234
+ #[ serde_with:: skip_serializing_none]
235
+ #[ derive( Debug , Clone , Serialize , Deserialize , TypedBuilder ) ]
236
+ #[ builder( field_defaults( default , setter( into) ) ) ]
237
+ #[ serde( rename_all = "camelCase" ) ]
238
+ #[ non_exhaustive]
239
+ pub struct AzureMasterKey {
240
+ /// The name for the key. The value for this field must be the same as the corresponding
241
+ /// [`KmsProvider`](mongocrypt::ctx::KmsProvider)'s name.
242
+ #[ serde( skip) ]
243
+ pub name : Option < String > ,
244
+
245
+ /// Host with optional port. Example: "example.vault.azure.net".
246
+ pub key_vault_endpoint : String ,
247
+
248
+ /// The key name.
249
+ pub key_name : String ,
250
+
251
+ /// A specific version of the named key, defaults to using the key's primary version.
252
+ pub key_version : Option < String > ,
253
+ }
254
+
255
+ impl From < AzureMasterKey > for MasterKey {
256
+ fn from ( azure_master_key : AzureMasterKey ) -> Self {
257
+ Self :: Azure ( azure_master_key)
258
+ }
259
+ }
260
+
261
+ /// A GCP master key.
262
+ #[ serde_with:: skip_serializing_none]
263
+ #[ derive( Debug , Clone , Serialize , Deserialize , TypedBuilder ) ]
264
+ #[ builder( field_defaults( default , setter( into) ) ) ]
265
+ #[ serde( rename_all = "camelCase" ) ]
266
+ #[ non_exhaustive]
267
+ pub struct GcpMasterKey {
268
+ /// The name for the key. The value for this field must be the same as the corresponding
269
+ /// [`KmsProvider`](mongocrypt::ctx::KmsProvider)'s name.
270
+ #[ serde( skip) ]
271
+ pub name : Option < String > ,
272
+
273
+ /// The project ID.
274
+ pub project_id : String ,
275
+
276
+ /// The location.
277
+ pub location : String ,
278
+
279
+ /// The key ring.
280
+ pub key_ring : String ,
281
+
282
+ /// The key name.
283
+ pub key_name : String ,
284
+
285
+ /// A specific version of the named key. Defaults to using the key's primary version.
286
+ pub key_version : Option < String > ,
287
+
288
+ /// Host with optional port. Defaults to "cloudkms.googleapis.com".
289
+ pub endpoint : Option < String > ,
290
+ }
291
+
292
+ impl From < GcpMasterKey > for MasterKey {
293
+ fn from ( gcp_master_key : GcpMasterKey ) -> Self {
294
+ Self :: Gcp ( gcp_master_key)
295
+ }
296
+ }
297
+
298
+ /// A local master key.
299
+ #[ serde_with:: skip_serializing_none]
300
+ #[ derive( Debug , Clone , Serialize , Deserialize , TypedBuilder ) ]
301
+ #[ builder( field_defaults( default , setter( into) ) ) ]
302
+ #[ serde( rename_all = "camelCase" ) ]
303
+ #[ non_exhaustive]
304
+ pub struct LocalMasterKey {
305
+ /// The name for the key. The value for this field must be the same as the corresponding
306
+ /// [`KmsProvider`](mongocrypt::ctx::KmsProvider)'s name.
307
+ #[ serde( skip) ]
308
+ pub name : Option < String > ,
309
+ }
310
+
311
+ impl From < LocalMasterKey > for MasterKey {
312
+ fn from ( local_master_key : LocalMasterKey ) -> Self {
313
+ Self :: Local ( local_master_key)
314
+ }
315
+ }
316
+
317
+ /// A KMIP master key.
318
+ #[ serde_with:: skip_serializing_none]
319
+ #[ derive( Debug , Clone , Serialize , Deserialize , TypedBuilder ) ]
320
+ #[ builder( field_defaults( default , setter( into) ) ) ]
321
+ #[ serde( rename_all = "camelCase" ) ]
322
+ #[ non_exhaustive]
323
+ pub struct KmipMasterKey {
324
+ /// The name for the key. The value for this field must be the same as the corresponding
325
+ /// [`KmsProvider`](mongocrypt::ctx::KmsProvider)'s name.
326
+ #[ serde( skip) ]
327
+ pub name : Option < String > ,
328
+
329
+ /// The KMIP Unique Identifier to a 96 byte KMIP Secret Data managed object. If this field is
330
+ /// not specified, the driver creates a random 96 byte KMIP Secret Data managed object.
331
+ pub key_id : Option < String > ,
332
+
333
+ /// If true (recommended), the KMIP server must decrypt this key. Defaults to false.
334
+ pub delegated : Option < bool > ,
335
+
336
+ /// Host with optional port.
337
+ pub endpoint : Option < String > ,
338
+ }
339
+
340
+ impl From < KmipMasterKey > for MasterKey {
341
+ fn from ( kmip_master_key : KmipMasterKey ) -> Self {
342
+ Self :: Kmip ( kmip_master_key)
343
+ }
236
344
}
237
345
238
346
impl MasterKey {
239
347
/// Returns the `KmsProvider` associated with this key.
240
348
pub fn provider ( & self ) -> KmsProvider {
241
- match self {
242
- MasterKey :: Aws { .. } => KmsProvider :: Aws { name : None } ,
243
- MasterKey :: Azure { .. } => KmsProvider :: Azure { name : None } ,
244
- MasterKey :: Gcp { .. } => KmsProvider :: Gcp { name : None } ,
245
- MasterKey :: Kmip { .. } => KmsProvider :: Kmip { name : None } ,
246
- MasterKey :: Local => KmsProvider :: Local { name : None } ,
349
+ let ( provider, name) = match self {
350
+ MasterKey :: Aws ( AwsMasterKey { name, .. } ) => ( KmsProvider :: aws ( ) , name. clone ( ) ) ,
351
+ MasterKey :: Azure ( AzureMasterKey { name, .. } ) => ( KmsProvider :: azure ( ) , name. clone ( ) ) ,
352
+ MasterKey :: Gcp ( GcpMasterKey { name, .. } ) => ( KmsProvider :: gcp ( ) , name. clone ( ) ) ,
353
+ MasterKey :: Kmip ( KmipMasterKey { name, .. } ) => ( KmsProvider :: kmip ( ) , name. clone ( ) ) ,
354
+ MasterKey :: Local ( LocalMasterKey { name, .. } ) => ( KmsProvider :: local ( ) , name. clone ( ) ) ,
355
+ } ;
356
+ if let Some ( name) = name {
357
+ provider. with_name ( name)
358
+ } else {
359
+ provider
247
360
}
248
361
}
249
362
}
0 commit comments