2626import static com .mongodb .assertions .Assertions .notNull ;
2727
2828/**
29- * The auto-encryption options.
29+ * The client-side automatic encryption settings. Client side encryption enables an application to specify what fields in a collection
30+ * must be encrypted, and the driver automatically encrypts commands sent to MongoDB and decrypts responses.
31+ * <p>
32+ * Automatic encryption is an enterprise only feature that only applies to operations on a collection. Automatic encryption is not
33+ * supported for operations on a database or view and will result in error. To bypass automatic encryption,
34+ * set bypassAutoEncryption=true in {@code AutoEncryptionSettings}.
35+ * </p>
36+ * <p>
37+ * Explicit encryption/decryption and automatic decryption is a community feature, enabled with the new
38+ * {@code com.mongodb.client.vault.ClientEncryption} type.
39+ * </p>
40+ * <p>
41+ * A MongoClient configured with bypassAutoEncryption=true will still automatically decrypt.
42+ * </p>
43+ * <p>
44+ * If automatic encryption fails on an operation, use a MongoClient configured with bypassAutoEncryption=true and use
45+ * ClientEncryption#encrypt to manually encrypt values.
46+ * </p>
47+ * <p>
48+ * Enabling client side encryption reduces the maximum document and message size (using a maxBsonObjectSize of 2MiB and
49+ * maxMessageSizeBytes of 6MB) and may have a negative performance impact.
50+ * </p>
51+ * <p>
52+ * Automatic encryption requires the authenticated user to have the listCollections privilege action.
53+ * </p>
3054 *
3155 * @since 3.11
3256 */
3357public final class AutoEncryptionSettings {
3458 private final MongoClientSettings keyVaultMongoClientSettings ;
3559 private final String keyVaultNamespace ;
3660 private final Map <String , Map <String , Object >> kmsProviders ;
37- private final Map <String , BsonDocument > namespaceToLocalSchemaDocumentMap ;
61+ private final Map <String , BsonDocument > schemaMap ;
3862 private final Map <String , Object > extraOptions ;
3963 private final boolean bypassAutoEncryption ;
4064
@@ -47,7 +71,7 @@ public static final class Builder {
4771 private MongoClientSettings keyVaultMongoClientSettings ;
4872 private String keyVaultNamespace ;
4973 private Map <String , Map <String , Object >> kmsProviders ;
50- private Map <String , BsonDocument > namespaceToLocalSchemaDocumentMap = Collections .emptyMap ();
74+ private Map <String , BsonDocument > schemaMap = Collections .emptyMap ();
5175 private Map <String , Object > extraOptions = Collections .emptyMap ();
5276 private boolean bypassAutoEncryption ;
5377
@@ -56,6 +80,7 @@ public static final class Builder {
5680 *
5781 * @param keyVaultMongoClientSettings the key vault mongo client settings, which may be null.
5882 * @return this
83+ * @see #getKeyVaultMongoClientSettings()
5984 */
6085 public Builder keyVaultMongoClientSettings (final MongoClientSettings keyVaultMongoClientSettings ) {
6186 this .keyVaultMongoClientSettings = keyVaultMongoClientSettings ;
@@ -67,6 +92,7 @@ public Builder keyVaultMongoClientSettings(final MongoClientSettings keyVaultMon
6792 *
6893 * @param keyVaultNamespace the key vault namespace, which may not be null
6994 * @return this
95+ * @see #getKeyVaultNamespace()
7096 */
7197 public Builder keyVaultNamespace (final String keyVaultNamespace ) {
7298 this .keyVaultNamespace = notNull ("keyVaultNamespace" , keyVaultNamespace );
@@ -78,6 +104,7 @@ public Builder keyVaultNamespace(final String keyVaultNamespace) {
78104 *
79105 * @param kmsProviders the KMS providers map, which may not be null
80106 * @return this
107+ * @see #getKmsProviders()
81108 */
82109 public Builder kmsProviders (final Map <String , Map <String , Object >> kmsProviders ) {
83110 this .kmsProviders = notNull ("kmsProviders" , kmsProviders );
@@ -87,11 +114,12 @@ public Builder kmsProviders(final Map<String, Map<String, Object>> kmsProviders)
87114 /**
88115 * Sets the map from namespace to local schema document
89116 *
90- * @param namespaceToLocalSchemaDocumentMap the map from namespace to local schema document
117+ * @param schemaMap the map from namespace to local schema document
91118 * @return this
119+ * @see #getSchemaMap()
92120 */
93- public Builder namespaceToLocalSchemaDocumentMap (final Map <String , BsonDocument > namespaceToLocalSchemaDocumentMap ) {
94- this .namespaceToLocalSchemaDocumentMap = notNull ("namespaceToLocalSchemaDocumentMap " , namespaceToLocalSchemaDocumentMap );
121+ public Builder schemaMap (final Map <String , BsonDocument > schemaMap ) {
122+ this .schemaMap = notNull ("schemaMap " , schemaMap );
95123 return this ;
96124 }
97125
@@ -100,6 +128,7 @@ public Builder namespaceToLocalSchemaDocumentMap(final Map<String, BsonDocument>
100128 *
101129 * @param extraOptions the extra options, which may not be null
102130 * @return this
131+ * @see #getExtraOptions()
103132 */
104133 public Builder extraOptions (final Map <String , Object > extraOptions ) {
105134 this .extraOptions = notNull ("extraOptions" , extraOptions );
@@ -111,6 +140,7 @@ public Builder extraOptions(final Map<String, Object> extraOptions) {
111140 *
112141 * @param bypassAutoEncryption whether auto-encryption should be bypassed
113142 * @return this
143+ * @see #isBypassAutoEncryption()
114144 */
115145 public Builder bypassAutoEncryption (final boolean bypassAutoEncryption ) {
116146 this .bypassAutoEncryption = bypassAutoEncryption ;
@@ -142,6 +172,11 @@ public static Builder builder() {
142172 /**
143173 * Gets the key vault settings.
144174 *
175+ * <p>
176+ * The key vault collection is assumed to reside on the same MongoDB cluster as the encrypted collections. But the optional
177+ * keyVaultMongoClientSettings can be used to route data key queries to a separate MongoDB cluster, or the same cluster but using a
178+ * different credential.
179+ * </p>
145180 * @return the key vault settings, which may be null to indicate that the same {@code MongoClient} should be used to access the key
146181 * vault collection as is used for the rest of the application.
147182 */
@@ -153,14 +188,38 @@ public MongoClientSettings getKeyVaultMongoClientSettings() {
153188 /**
154189 * Gets the key vault namespace.
155190 *
191+ * <p>
192+ * The key vault namespace refers to a collection that contains all data keys used for encryption and decryption (aka the key vault
193+ * collection). Data keys are stored as documents in a special MongoDB collection. Data keys are protected with encryption by a KMS
194+ * provider (AWS KMS or a local master key).
195+ * </p>
196+ *
156197 * @return the key vault namespace, which may not be null
157198 */
158199 public String getKeyVaultNamespace () {
159200 return keyVaultNamespace ;
160201 }
161202
162203 /**
163- * Gets the map of KMS provider properties
204+ * Gets the map of KMS provider properties.
205+ *
206+ * <p>
207+ * Multiple KMS providers may be specified. Initially, two KMS providers are supported: "aws" and "local". The kmsProviders map
208+ * values differ by provider:
209+ * </p>
210+ * <p>
211+ * For "aws", the properties are:
212+ * </p>
213+ * <ul>
214+ * <li>accessKeyId: a String containing the AWS access key identifier</li>
215+ * <li>secretAccessKey: a String the AWS secret access key</li>
216+ * </ul>
217+ * <p>
218+ * For "local", the properties are:
219+ * </p>
220+ * <ul>
221+ * <li>key: <byte array of length 96></li>
222+ * </ul>
164223 *
165224 * @return map of KMS provider properties
166225 */
@@ -169,26 +228,61 @@ public Map<String, Map<String, Object>> getKmsProviders() {
169228 }
170229
171230 /**
172- * Gets the map of namespace to local JSON schema
173- *
231+ * Gets the map of namespace to local JSON schema.
232+ * <p>
233+ * Automatic encryption is configured with an "encrypt" field in a collection's JSONSchema. By default, a collection's JSONSchema is
234+ * periodically polled with the listCollections command. But a JSONSchema may be specified locally with the schemaMap option.
235+ * </p>
236+ * <p>
237+ * The key into the map is the full namespace of the collection, which is {@code <database name>.<collection name>}. For
238+ * example, if the database name is {@code "test"} and the collection name is {@code "users"}, then the namesspace is
239+ * {@code "test.users"}.
240+ * </p>
241+ * <p>
242+ * Supplying a schemaMap provides more security than relying on JSON Schemas obtained from the server. It protects against a
243+ * malicious server advertising a false JSON Schema, which could trick the client into sending unencrypted data that should be
244+ * encrypted.
245+ * </p>
246+ * <p>
247+ * Schemas supplied in the schemaMap only apply to configuring automatic encryption for client side encryption. Other validation
248+ * rules in the JSON schema will not be enforced by the driver and will result in an error.
249+ * </p>
174250 * @return map of namespace to local JSON schema
175251 */
176- public Map <String , BsonDocument > getNamespaceToLocalSchemaDocumentMap () {
177- return namespaceToLocalSchemaDocumentMap ;
252+ public Map <String , BsonDocument > getSchemaMap () {
253+ return schemaMap ;
178254 }
179255
180256 /**
181257 * Gets the extra options that control the behavior of auto-encryption components.
258+ * <p>
259+ * The extraOptions currently only relate to the mongocryptd process. The following options keys are supported:
260+ * </p>
261+ * <ul>
262+ * <li>mongocryptdURI: a String which defaults to "mongodb://%2Fvar%2Fmongocryptd.sock" if domain sockets are available or
263+ * "mongodb://localhost:27020" otherwise.</li>
264+ * <li>mongocryptdBypassSpawn: a boolean which defaults to false. If true, the driver will not attempt to automatically spawn a
265+ * mongocryptd process</li>
266+ * <li>mongocryptdSpawnPath: specifies the full path to the mongocryptd executable. By default the driver spawns mongocryptd from
267+ * the system path.</li>
268+ * <li>mongocryptdSpawnArgs: Used to control the behavior of mongocryptd when the driver spawns it. By default, the driver spawns
269+ * mongocryptd with the single command line argument {@code "--idleShutdownTimeoutSecs=60"}</li>
270+ * </ul>
182271 *
183- * @return the extra options
272+ * @return the extra options map
184273 */
185274 public Map <String , Object > getExtraOptions () {
186275 return extraOptions ;
187276 }
188277
189278 /**
190279 * Gets whether auto-encryption should be bypassed. Even when this option is true, auto-decryption is still enabled.
191- *
280+ * <p>
281+ * This option is useful for cases where the driver throws an exception because it is unable to prove that the command does not
282+ * contain any fields that should be automatically encrypted, but the application is able to determine that it does not. For these
283+ * cases, the application can construct a {@code MongoClient} with {@code AutoEncryptionSettings} with {@code bypassAutoEncryption}
284+ * enabled.
285+ * </p>
192286 * @return true if auto-encryption should be bypassed
193287 */
194288 public boolean isBypassAutoEncryption () {
@@ -199,7 +293,7 @@ private AutoEncryptionSettings(final Builder builder) {
199293 this .keyVaultMongoClientSettings = builder .keyVaultMongoClientSettings ;
200294 this .keyVaultNamespace = notNull ("keyVaultNamespace" , builder .keyVaultNamespace );
201295 this .kmsProviders = notNull ("kmsProviders" , builder .kmsProviders );
202- this .namespaceToLocalSchemaDocumentMap = notNull ("namespaceToLocalSchemaDocumentMap " , builder .namespaceToLocalSchemaDocumentMap );
296+ this .schemaMap = notNull ("schemaMap " , builder .schemaMap );
203297 this .extraOptions = notNull ("extraOptions" , builder .extraOptions );
204298 this .bypassAutoEncryption = builder .bypassAutoEncryption ;
205299 }
0 commit comments