26
26
import static com .mongodb .assertions .Assertions .notNull ;
27
27
28
28
/**
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>
30
54
*
31
55
* @since 3.11
32
56
*/
33
57
public final class AutoEncryptionSettings {
34
58
private final MongoClientSettings keyVaultMongoClientSettings ;
35
59
private final String keyVaultNamespace ;
36
60
private final Map <String , Map <String , Object >> kmsProviders ;
37
- private final Map <String , BsonDocument > namespaceToLocalSchemaDocumentMap ;
61
+ private final Map <String , BsonDocument > schemaMap ;
38
62
private final Map <String , Object > extraOptions ;
39
63
private final boolean bypassAutoEncryption ;
40
64
@@ -47,7 +71,7 @@ public static final class Builder {
47
71
private MongoClientSettings keyVaultMongoClientSettings ;
48
72
private String keyVaultNamespace ;
49
73
private Map <String , Map <String , Object >> kmsProviders ;
50
- private Map <String , BsonDocument > namespaceToLocalSchemaDocumentMap = Collections .emptyMap ();
74
+ private Map <String , BsonDocument > schemaMap = Collections .emptyMap ();
51
75
private Map <String , Object > extraOptions = Collections .emptyMap ();
52
76
private boolean bypassAutoEncryption ;
53
77
@@ -56,6 +80,7 @@ public static final class Builder {
56
80
*
57
81
* @param keyVaultMongoClientSettings the key vault mongo client settings, which may be null.
58
82
* @return this
83
+ * @see #getKeyVaultMongoClientSettings()
59
84
*/
60
85
public Builder keyVaultMongoClientSettings (final MongoClientSettings keyVaultMongoClientSettings ) {
61
86
this .keyVaultMongoClientSettings = keyVaultMongoClientSettings ;
@@ -67,6 +92,7 @@ public Builder keyVaultMongoClientSettings(final MongoClientSettings keyVaultMon
67
92
*
68
93
* @param keyVaultNamespace the key vault namespace, which may not be null
69
94
* @return this
95
+ * @see #getKeyVaultNamespace()
70
96
*/
71
97
public Builder keyVaultNamespace (final String keyVaultNamespace ) {
72
98
this .keyVaultNamespace = notNull ("keyVaultNamespace" , keyVaultNamespace );
@@ -78,6 +104,7 @@ public Builder keyVaultNamespace(final String keyVaultNamespace) {
78
104
*
79
105
* @param kmsProviders the KMS providers map, which may not be null
80
106
* @return this
107
+ * @see #getKmsProviders()
81
108
*/
82
109
public Builder kmsProviders (final Map <String , Map <String , Object >> kmsProviders ) {
83
110
this .kmsProviders = notNull ("kmsProviders" , kmsProviders );
@@ -87,11 +114,12 @@ public Builder kmsProviders(final Map<String, Map<String, Object>> kmsProviders)
87
114
/**
88
115
* Sets the map from namespace to local schema document
89
116
*
90
- * @param namespaceToLocalSchemaDocumentMap the map from namespace to local schema document
117
+ * @param schemaMap the map from namespace to local schema document
91
118
* @return this
119
+ * @see #getSchemaMap()
92
120
*/
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 );
95
123
return this ;
96
124
}
97
125
@@ -100,6 +128,7 @@ public Builder namespaceToLocalSchemaDocumentMap(final Map<String, BsonDocument>
100
128
*
101
129
* @param extraOptions the extra options, which may not be null
102
130
* @return this
131
+ * @see #getExtraOptions()
103
132
*/
104
133
public Builder extraOptions (final Map <String , Object > extraOptions ) {
105
134
this .extraOptions = notNull ("extraOptions" , extraOptions );
@@ -111,6 +140,7 @@ public Builder extraOptions(final Map<String, Object> extraOptions) {
111
140
*
112
141
* @param bypassAutoEncryption whether auto-encryption should be bypassed
113
142
* @return this
143
+ * @see #isBypassAutoEncryption()
114
144
*/
115
145
public Builder bypassAutoEncryption (final boolean bypassAutoEncryption ) {
116
146
this .bypassAutoEncryption = bypassAutoEncryption ;
@@ -142,6 +172,11 @@ public static Builder builder() {
142
172
/**
143
173
* Gets the key vault settings.
144
174
*
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>
145
180
* @return the key vault settings, which may be null to indicate that the same {@code MongoClient} should be used to access the key
146
181
* vault collection as is used for the rest of the application.
147
182
*/
@@ -153,14 +188,38 @@ public MongoClientSettings getKeyVaultMongoClientSettings() {
153
188
/**
154
189
* Gets the key vault namespace.
155
190
*
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
+ *
156
197
* @return the key vault namespace, which may not be null
157
198
*/
158
199
public String getKeyVaultNamespace () {
159
200
return keyVaultNamespace ;
160
201
}
161
202
162
203
/**
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>
164
223
*
165
224
* @return map of KMS provider properties
166
225
*/
@@ -169,26 +228,61 @@ public Map<String, Map<String, Object>> getKmsProviders() {
169
228
}
170
229
171
230
/**
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>
174
250
* @return map of namespace to local JSON schema
175
251
*/
176
- public Map <String , BsonDocument > getNamespaceToLocalSchemaDocumentMap () {
177
- return namespaceToLocalSchemaDocumentMap ;
252
+ public Map <String , BsonDocument > getSchemaMap () {
253
+ return schemaMap ;
178
254
}
179
255
180
256
/**
181
257
* 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>
182
271
*
183
- * @return the extra options
272
+ * @return the extra options map
184
273
*/
185
274
public Map <String , Object > getExtraOptions () {
186
275
return extraOptions ;
187
276
}
188
277
189
278
/**
190
279
* 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>
192
286
* @return true if auto-encryption should be bypassed
193
287
*/
194
288
public boolean isBypassAutoEncryption () {
@@ -199,7 +293,7 @@ private AutoEncryptionSettings(final Builder builder) {
199
293
this .keyVaultMongoClientSettings = builder .keyVaultMongoClientSettings ;
200
294
this .keyVaultNamespace = notNull ("keyVaultNamespace" , builder .keyVaultNamespace );
201
295
this .kmsProviders = notNull ("kmsProviders" , builder .kmsProviders );
202
- this .namespaceToLocalSchemaDocumentMap = notNull ("namespaceToLocalSchemaDocumentMap " , builder .namespaceToLocalSchemaDocumentMap );
296
+ this .schemaMap = notNull ("schemaMap " , builder .schemaMap );
203
297
this .extraOptions = notNull ("extraOptions" , builder .extraOptions );
204
298
this .bypassAutoEncryption = builder .bypassAutoEncryption ;
205
299
}
0 commit comments