Skip to content

Commit 9522eac

Browse files
committed
Rename classes to match spec, improve Javadoc.
JAVA-3310
1 parent 9e96684 commit 9522eac

File tree

11 files changed

+266
-83
lines changed

11 files changed

+266
-83
lines changed

driver-core/src/main/com/mongodb/AutoEncryptionSettings.java

Lines changed: 108 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,39 @@
2626
import 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
*/
3357
public 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: &lt;byte array of length 96&gt;</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 &lt;database name>.&lt;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
}

driver-core/src/main/com/mongodb/KeyVaultEncryptionSettings.java renamed to driver-core/src/main/com/mongodb/ClientEncryptionSettings.java

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,22 @@
2323
import static com.mongodb.assertions.Assertions.notNull;
2424

2525
/**
26-
* The key vault encryption settings
26+
* The client-side settings for data key creation and explicit encryption.
27+
*
28+
* <p>
29+
* Explicit encryption/decryption is a community feature, enabled with the new {@code com.mongodb.client.vault.ClientEncryption} type,
30+
* for which this is the settings.
31+
* </p>
2732
*
2833
* @since 3.11
2934
*/
30-
public final class KeyVaultEncryptionSettings {
35+
public final class ClientEncryptionSettings {
3136
private final MongoClientSettings keyVaultMongoClientSettings;
3237
private final String keyVaultNamespace;
3338
private final Map<String, Map<String, Object>> kmsProviders;
3439

3540
/**
36-
* A builder for {@code KeyVaultEncryptionSettings} so that {@code KeyVaultEncryptionSettings} can be immutable, and to support easier
41+
* A builder for {@code ClientEncryptionSettings} so that {@code ClientEncryptionSettings} can be immutable, and to support easier
3742
* construction through chaining.
3843
*/
3944
@NotThreadSafe
@@ -47,6 +52,7 @@ public static final class Builder {
4752
*
4853
* @param keyVaultMongoClientSettings the key vault mongo client settings, which may be null.
4954
* @return this
55+
* @see #getKeyVaultMongoClientSettings()
5056
*/
5157
public Builder keyVaultMongoClientSettings(final MongoClientSettings keyVaultMongoClientSettings) {
5258
this.keyVaultMongoClientSettings = keyVaultMongoClientSettings;
@@ -58,6 +64,7 @@ public Builder keyVaultMongoClientSettings(final MongoClientSettings keyVaultMon
5864
*
5965
* @param keyVaultNamespace the key vault namespace, which may not be null
6066
* @return this
67+
* @see #getKeyVaultNamespace()
6168
*/
6269
public Builder keyVaultNamespace(final String keyVaultNamespace) {
6370
this.keyVaultNamespace = notNull("keyVaultNamespace", keyVaultNamespace);
@@ -69,19 +76,20 @@ public Builder keyVaultNamespace(final String keyVaultNamespace) {
6976
*
7077
* @param kmsProviders the KMS providers map, which may not be null
7178
* @return this
79+
* @see #getKmsProviders()
7280
*/
7381
public Builder kmsProviders(final Map<String, Map<String, Object>> kmsProviders) {
7482
this.kmsProviders = notNull("kmsProviders", kmsProviders);
7583
return this;
7684
}
7785

7886
/**
79-
* Build an instance of {@code KeyVaultEncryptionSettings}.
87+
* Build an instance of {@code ClientEncryptionSettings}.
8088
*
8189
* @return the settings from this builder
8290
*/
83-
public KeyVaultEncryptionSettings build() {
84-
return new KeyVaultEncryptionSettings(this);
91+
public ClientEncryptionSettings build() {
92+
return new ClientEncryptionSettings(this);
8593
}
8694

8795
private Builder() {
@@ -98,33 +106,63 @@ public static Builder builder() {
98106
}
99107

100108
/**
101-
* Gets the key vault client settings
109+
* Gets the key vault settings.
102110
*
103-
* @return key vault client settings
111+
* <p>
112+
* The key vault collection is assumed to reside on the same MongoDB cluster as indicated by the connecting URI. But the optional
113+
* keyVaultMongoClientSettings can be used to route data key queries to a separate MongoDB cluster, or the same cluster but with a
114+
* different credential.
115+
* </p>
116+
* @return the key vault settings, which may be null to indicate that the same {@code MongoClient} should be used to access the key
117+
* vault collection as is used for the rest of the application.
104118
*/
105119
public MongoClientSettings getKeyVaultMongoClientSettings() {
106120
return keyVaultMongoClientSettings;
107121
}
108122

109123
/**
110-
* Gets the key vault namespace
124+
* Gets the key vault namespace.
125+
* <p>
126+
* The key vault namespace refers to a collection that contains all data keys used for encryption and decryption (aka the key vault
127+
* collection). Data keys are stored as documents in a special MongoDB collection. Data keys are protected with encryption by a KMS
128+
* provider (AWS KMS or a local master key).
129+
* </p>
111130
*
112-
* @return key vault namespace
131+
* @return the key vault namespace, which may not be null
113132
*/
133+
114134
public String getKeyVaultNamespace() {
115135
return keyVaultNamespace;
116136
}
117137

118138
/**
119-
* Gets the map of KMS provider properties
139+
* Gets the map of KMS provider properties.
140+
*
141+
* <p>
142+
* Multiple KMS providers may be specified. Initially, two KMS providers are supported: "aws" and "local". The kmsProviders map
143+
* values differ by provider:
144+
* </p>
145+
* <p>
146+
* For "aws", the properties are:
147+
* </p>
148+
* <ul>
149+
* <li>accessKeyId: a String containing the AWS access key identifier</li>
150+
* <li>secretAccessKey: a String the AWS secret access key</li>
151+
* </ul>
152+
* <p>
153+
* For "local", the properties are:
154+
* </p>
155+
* <ul>
156+
* <li>key: &lt;byte array of length 96&gt;</li>
157+
* </ul>
120158
*
121159
* @return map of KMS provider properties
122160
*/
123161
public Map<String, Map<String, Object>> getKmsProviders() {
124162
return kmsProviders;
125163
}
126164

127-
private KeyVaultEncryptionSettings(final Builder builder) {
165+
private ClientEncryptionSettings(final Builder builder) {
128166
this.keyVaultMongoClientSettings = builder.keyVaultMongoClientSettings;
129167
this.keyVaultNamespace = notNull("keyVaultNamespace", builder.keyVaultNamespace);
130168
this.kmsProviders = notNull("kmsProviders", builder.kmsProviders);

0 commit comments

Comments
 (0)