Skip to content

Fix documentation of ClientEncryptionSettings#keyVaultMongoClientSett… #1087

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions config/spotbugs/exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -240,18 +240,4 @@
<Method name="acquirePermitOrGetAvailableOpenedConnection"/>
<Bug pattern="NS_NON_SHORT_CIRCUIT"/>
</Match>

<!-- Can actually be null, but is not annotated as `@Nullable`. Annotating it as such causes warnings
in other places where `null` is not handled, see https://jira.mongodb.org/browse/JAVA-4861.
When the aforementioned ticket is done, it will be clear what to do with the warnings suppressed here. -->
<Match>
<Class name="com.mongodb.client.internal.ClientEncryptionImpl"/>
<Method name="createEncryptedCollection"/>
<Bug pattern="RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE"/>
</Match>
<Match>
<Class name="com.mongodb.reactivestreams.client.internal.vault.ClientEncryptionImpl"/>
<Method name="~.*createEncryptedCollection.*"/>
<Bug pattern="RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE"/>
</Match>
</FindBugsFilter>
18 changes: 6 additions & 12 deletions driver-core/src/main/com/mongodb/ClientEncryptionSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ public static final class Builder {
private Map<String, SSLContext> kmsProviderSslContextMap = new HashMap<>();

/**
* Sets the key vault settings.
* Sets the {@link MongoClientSettings} that will be used to access the key vault.
*
* @param keyVaultMongoClientSettings the key vault mongo client settings, which may be null.
* @param keyVaultMongoClientSettings the key vault mongo client settings, which may not be null.
* @return this
* @see #getKeyVaultMongoClientSettings()
*/
public Builder keyVaultMongoClientSettings(final MongoClientSettings keyVaultMongoClientSettings) {
this.keyVaultMongoClientSettings = keyVaultMongoClientSettings;
this.keyVaultMongoClientSettings = notNull("keyVaultMongoClientSettings", keyVaultMongoClientSettings);
return this;
}

Expand Down Expand Up @@ -143,15 +143,9 @@ public static Builder builder() {
}

/**
* Gets the key vault settings.
* Gets the {@link MongoClientSettings} that will be used to access the key vault.
*
* <p>
* The key vault collection is assumed to reside on the same MongoDB cluster as indicated by the connecting URI. But the optional
* keyVaultMongoClientSettings can be used to route data key queries to a separate MongoDB cluster, or the same cluster but with a
* different credential.
* </p>
* @return the key vault settings, which may be null to indicate that the same {@code MongoClient} should be used to access the key
* vault collection as is used for the rest of the application.
* @return the key vault settings, which may be not be null
*/
public MongoClientSettings getKeyVaultMongoClientSettings() {
return keyVaultMongoClientSettings;
Expand Down Expand Up @@ -260,7 +254,7 @@ public Map<String, SSLContext> getKmsProviderSslContextMap() {
}

private ClientEncryptionSettings(final Builder builder) {
this.keyVaultMongoClientSettings = builder.keyVaultMongoClientSettings;
this.keyVaultMongoClientSettings = notNull("keyVaultMongoClientSettings", builder.keyVaultMongoClientSettings);
this.keyVaultNamespace = notNull("keyVaultNamespace", builder.keyVaultNamespace);
this.kmsProviders = notNull("kmsProviders", builder.kmsProviders);
this.kmsProviderPropertySuppliers = notNull("kmsProviderPropertySuppliers", builder.kmsProviderPropertySuppliers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.mongodb.AutoEncryptionSettings;
import com.mongodb.ClientEncryptionSettings;
import com.mongodb.MongoClientException;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.model.vault.RewrapManyDataKeyOptions;
import com.mongodb.crypt.capi.MongoCryptOptions;
import org.bson.BsonDocument;
Expand Down Expand Up @@ -52,6 +53,7 @@ public void createsExpectedMongoCryptOptionsUsingClientEncryptionSettings() {
ClientEncryptionSettings settings = ClientEncryptionSettings
.builder()
.kmsProviders(kmsProvidersRaw)
.keyVaultMongoClientSettings(MongoClientSettings.builder().build())
.keyVaultNamespace("a.b")
.build();
MongoCryptOptions mongoCryptOptions = MongoCryptHelper.createMongoCryptOptions(settings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.mongodb.reactivestreams.client.internal.vault;

import com.mongodb.ClientEncryptionSettings;
import com.mongodb.MongoClientSettings;
import com.mongodb.MongoConfigurationException;
import com.mongodb.MongoNamespace;
import com.mongodb.MongoUpdatedEncryptedFieldsException;
Expand All @@ -33,6 +32,7 @@
import com.mongodb.client.model.vault.RewrapManyDataKeyOptions;
import com.mongodb.client.model.vault.RewrapManyDataKeyResult;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.internal.VisibleForTesting;
import com.mongodb.reactivestreams.client.FindPublisher;
import com.mongodb.reactivestreams.client.MongoClient;
import com.mongodb.reactivestreams.client.MongoClients;
Expand All @@ -59,6 +59,7 @@
import java.util.stream.Collectors;

import static com.mongodb.assertions.Assertions.notNull;
import static com.mongodb.internal.VisibleForTesting.AccessModifier.PRIVATE;
import static com.mongodb.internal.capi.MongoCryptHelper.validateRewrapManyDataKeyOptions;
import static java.lang.String.format;
import static java.util.Arrays.asList;
Expand All @@ -78,6 +79,7 @@ public ClientEncryptionImpl(final ClientEncryptionSettings options) {
this(MongoClients.create(options.getKeyVaultMongoClientSettings()), options);
}

@VisibleForTesting(otherwise = PRIVATE)
public ClientEncryptionImpl(final MongoClient keyVaultClient, final ClientEncryptionSettings options) {
this.keyVaultClient = keyVaultClient;
this.crypt = Crypts.create(keyVaultClient, options);
Expand Down Expand Up @@ -208,9 +210,7 @@ public Publisher<BsonDocument> createEncryptedCollection(final MongoDatabase dat
if (rawEncryptedFields == null) {
throw new MongoConfigurationException(format("`encryptedFields` is not configured for the collection %s.", namespace));
}
CodecRegistry codecRegistry = options.getKeyVaultMongoClientSettings() == null
? MongoClientSettings.getDefaultCodecRegistry()
: options.getKeyVaultMongoClientSettings().getCodecRegistry();
CodecRegistry codecRegistry = options.getKeyVaultMongoClientSettings().getCodecRegistry();
BsonDocument encryptedFields = rawEncryptedFields.toBsonDocument(BsonDocument.class, codecRegistry);
BsonValue fields = encryptedFields.get("fields");
if (fields != null && fields.isArray()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.mongodb.client.internal;

import com.mongodb.ClientEncryptionSettings;
import com.mongodb.MongoClientSettings;
import com.mongodb.MongoConfigurationException;
import com.mongodb.MongoNamespace;
import com.mongodb.MongoUpdatedEncryptedFieldsException;
Expand All @@ -39,6 +38,7 @@
import com.mongodb.client.model.vault.RewrapManyDataKeyResult;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.vault.ClientEncryption;
import com.mongodb.internal.VisibleForTesting;
import org.bson.BsonArray;
import org.bson.BsonBinary;
import org.bson.BsonDocument;
Expand All @@ -54,6 +54,7 @@
import java.util.stream.Collectors;

import static com.mongodb.assertions.Assertions.notNull;
import static com.mongodb.internal.VisibleForTesting.AccessModifier.PRIVATE;
import static com.mongodb.internal.capi.MongoCryptHelper.validateRewrapManyDataKeyOptions;
import static java.lang.String.format;
import static java.util.Arrays.asList;
Expand All @@ -73,6 +74,7 @@ public ClientEncryptionImpl(final ClientEncryptionSettings options) {
this(MongoClients.create(options.getKeyVaultMongoClientSettings()), options);
}

@VisibleForTesting(otherwise = PRIVATE)
public ClientEncryptionImpl(final MongoClient keyVaultClient, final ClientEncryptionSettings options) {
this.keyVaultClient = keyVaultClient;
this.crypt = Crypts.create(keyVaultClient, options);
Expand Down Expand Up @@ -195,9 +197,7 @@ public BsonDocument createEncryptedCollection(final MongoDatabase database, fina
if (rawEncryptedFields == null) {
throw new MongoConfigurationException(format("`encryptedFields` is not configured for the collection %s.", namespace));
}
CodecRegistry codecRegistry = options.getKeyVaultMongoClientSettings() == null
? MongoClientSettings.getDefaultCodecRegistry()
: options.getKeyVaultMongoClientSettings().getCodecRegistry();
CodecRegistry codecRegistry = options.getKeyVaultMongoClientSettings().getCodecRegistry();
BsonDocument encryptedFields = rawEncryptedFields.toBsonDocument(BsonDocument.class, codecRegistry);
BsonValue fields = encryptedFields.get("fields");
if (fields != null && fields.isArray()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,8 @@ private void initClientEncryption(final BsonDocument entity, final String id,

MongoClient mongoClient = null;
ClientEncryptionSettings.Builder builder = ClientEncryptionSettings.builder();
// this is ignored in preference to the keyVaultClient, but required to be non-null in the ClientEncryptionSettings constructor
builder.keyVaultMongoClientSettings(MongoClientSettings.builder().build());
for (Map.Entry<String, BsonValue> entry : clientEncryptionOpts.entrySet()) {
switch (entry.getKey()) {
case "keyVaultClient":
Expand Down