@@ -2,6 +2,7 @@ use std::collections::HashMap;
22
33use bson:: Array ;
44use mongocrypt:: ctx:: KmsProvider ;
5+ use serde:: Deserialize ;
56
67use crate :: {
78 bson:: { Bson , Document } ,
@@ -19,13 +20,15 @@ use crate::{
1920/// https://github.com/mongodb/specifications/blob/master/source/client-side-encryption/client-side-encryption.rst#libmongocrypt-auto-encryption-allow-list
2021/// )). To bypass automatic encryption for all operations, set bypassAutoEncryption=true in
2122/// AutoEncryptionOpts.
22- #[ derive( Debug , Clone ) ]
23- #[ non_exhaustive ]
23+ #[ derive( Debug , Clone , Deserialize ) ]
24+ #[ serde ( rename_all = "camelCase" , deny_unknown_fields ) ]
2425pub ( crate ) struct AutoEncryptionOptions {
2526 /// Used for data key queries. Will default to an internal client if not set.
27+ #[ serde( skip) ]
2628 pub ( crate ) key_vault_client : Option < crate :: Client > ,
2729 /// A collection that contains all data keys used for encryption and decryption (aka the key
2830 /// vault collection).
31+ #[ serde( default = "default_key_vault_namespace" ) ]
2932 pub ( crate ) key_vault_namespace : Namespace ,
3033 /// Options individual to each KMS provider.
3134 pub ( crate ) kms_providers : KmsProviders ,
@@ -54,9 +57,17 @@ pub(crate) struct AutoEncryptionOptions {
5457 pub ( crate ) bypass_query_analysis : Option < bool > ,
5558 /// Disable loading crypt_shared.
5659 #[ cfg( test) ]
60+ #[ serde( skip) ]
5761 pub ( crate ) disable_crypt_shared : Option < bool > ,
5862}
5963
64+ fn default_key_vault_namespace ( ) -> Namespace {
65+ Namespace {
66+ db : "keyvault" . to_string ( ) ,
67+ coll : "datakeys" . to_string ( ) ,
68+ }
69+ }
70+
6071impl AutoEncryptionOptions {
6172 pub ( crate ) fn new ( key_vault_namespace : Namespace , kms_providers : KmsProviders ) -> Self {
6273 Self {
@@ -74,9 +85,11 @@ impl AutoEncryptionOptions {
7485 }
7586}
7687
77- #[ derive( Debug , Clone ) ]
88+ #[ derive( Deserialize , Debug , Clone ) ]
7889pub ( crate ) struct KmsProviders {
90+ #[ serde( flatten) ]
7991 credentials : HashMap < KmsProvider , Document > ,
92+ #[ serde( skip) ]
8093 tls_options : Option < KmsProvidersTlsOptions > ,
8194}
8295
@@ -105,13 +118,36 @@ impl KmsProviders {
105118 } )
106119 }
107120
108- pub ( crate ) fn credentials ( & self ) -> Result < Document > {
121+ pub ( crate ) fn credentials_doc ( & self ) -> Result < Document > {
109122 Ok ( bson:: to_document ( & self . credentials ) ?)
110123 }
111124
112125 pub ( crate ) fn tls_options ( & self ) -> & Option < KmsProvidersTlsOptions > {
113126 & self . tls_options
114127 }
128+
129+ #[ cfg( test) ]
130+ pub ( crate ) fn credentials ( & self ) -> & HashMap < KmsProvider , Document > {
131+ & self . credentials
132+ }
133+
134+ #[ cfg( test) ]
135+ pub ( crate ) fn set ( & mut self , provider : KmsProvider , creds : Document , tls : Option < TlsOptions > ) {
136+ self . credentials . insert ( provider. clone ( ) , creds) ;
137+ if let Some ( tls) = tls {
138+ self . tls_options
139+ . get_or_insert_with ( KmsProvidersTlsOptions :: new)
140+ . insert ( provider, tls) ;
141+ }
142+ }
143+
144+ #[ cfg( test) ]
145+ pub ( crate ) fn clear ( & mut self , provider : & KmsProvider ) {
146+ self . credentials . remove ( provider) ;
147+ if let Some ( tls_opts) = & mut self . tls_options {
148+ tls_opts. remove ( provider) ;
149+ }
150+ }
115151}
116152
117153impl AutoEncryptionOptions {
0 commit comments