Skip to content

Support auto encryption in unified tests #1727

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ public void create(final WriteConcern writeConcern, final BsonDocument createOpt
case "size":
createCollectionOptions.sizeInBytes(createOptions.getNumber("size").longValue());
break;
case "encryptedFields":
createCollectionOptions.encryptedFields(createOptions.getDocument("encryptedFields"));
break;
case "validator":
ValidationOptions validationOptions = new ValidationOptions();
validationOptions.validator(createOptions.getDocument("validator"));
createCollectionOptions.validationOptions(validationOptions);
break;
default:
throw new UnsupportedOperationException("Unsupported create collection option: " + option);
}
Expand All @@ -195,6 +203,10 @@ public void create(final String collectionName, final CreateCollectionOptions op
if (indexOptionDefaults.getStorageEngine() != null) {
operation.indexOptionDefaults(new BsonDocument("storageEngine", toBsonDocument(indexOptionDefaults.getStorageEngine())));
}
Bson encryptedFields = options.getEncryptedFields();
if (encryptedFields != null) {
operation.encryptedFields(encryptedFields.toBsonDocument());
}
ValidationOptions validationOptions = options.getValidationOptions();
if (validationOptions.getValidator() != null) {
operation.validator(toBsonDocument(validationOptions.getValidator()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Predicate;

import static com.mongodb.assertions.Assertions.isTrue;
import static com.mongodb.assertions.Assertions.notNull;
import static com.mongodb.internal.Locks.withLock;

Expand All @@ -48,15 +47,17 @@ public final class TestClusterListener implements ClusterListener {

@Override
public void clusterOpening(final ClusterOpeningEvent event) {
isTrue("clusterOpeningEvent is null", clusterOpeningEvent == null);
clusterOpeningEvent = event;
if(clusterOpeningEvent == null) {
clusterOpeningEvent = event;
}
}

@Override
public void clusterClosed(final ClusterClosedEvent event) {
isTrue("clusterClosingEvent is null", clusterClosingEvent == null);
closedLatch.countDown();
clusterClosingEvent = event;
if(clusterClosingEvent == null) {
closedLatch.countDown();
clusterClosingEvent = event;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.mongodb.client.unified;

import com.mongodb.AutoEncryptionSettings;
import com.mongodb.ClientEncryptionSettings;
import com.mongodb.ClientSessionOptions;
import com.mongodb.ConnectionString;
Expand Down Expand Up @@ -111,7 +112,7 @@
public final class Entities {
private static final Set<String> SUPPORTED_CLIENT_ENTITY_OPTIONS = new HashSet<>(
asList(
"id", "uriOptions", "serverApi", "useMultipleMongoses", "storeEventsAsEntities",
"id", "autoEncryptOpts", "uriOptions", "serverApi", "useMultipleMongoses", "storeEventsAsEntities",
"observeEvents", "observeLogMessages", "observeSensitiveCommands", "ignoreCommandMonitoringEvents"));
private final Set<String> entityNames = new HashSet<>();
private final Map<String, ExecutorService> threads = new HashMap<>();
Expand Down Expand Up @@ -604,6 +605,59 @@ private void initClient(final BsonDocument entity, final String id,
}
clientSettingsBuilder.serverApi(serverApiBuilder.build());
}
if (entity.containsKey("autoEncryptOpts")) {
AutoEncryptionSettings.Builder builder = AutoEncryptionSettings.builder();
for (Map.Entry<String, BsonValue> entry : entity.getDocument("autoEncryptOpts").entrySet()) {
switch (entry.getKey()) {
case "bypassAutoEncryption":
builder.bypassAutoEncryption(entry.getValue().asBoolean().getValue());
break;
case "bypassQueryAnalysis":
builder.bypassQueryAnalysis(entry.getValue().asBoolean().getValue());
break;
case "schemaMap":
Map<String, BsonDocument> schemaMap = new HashMap<>();
for (Map.Entry<String, BsonValue> entries : entry.getValue().asDocument().entrySet()) {
schemaMap.put(entries.getKey(), entries.getValue().asDocument());
}
builder.schemaMap(schemaMap);
break;
case "encryptedFieldsMap":
Map<String, BsonDocument> encryptedFieldsMap = new HashMap<>();
for (Map.Entry<String, BsonValue> entries : entry.getValue().asDocument().entrySet()) {
encryptedFieldsMap.put(entries.getKey(), entries.getValue().asDocument());
}
builder.encryptedFieldsMap(encryptedFieldsMap);
break;
case "extraOptions":
Map<String, Object> extraOptions = new HashMap<>();
for (Map.Entry<String, BsonValue> extraOptionsEntry : entry.getValue().asDocument().entrySet()) {
switch (extraOptionsEntry.getKey()) {
case "mongocryptdBypassSpawn":
extraOptions.put(extraOptionsEntry.getKey(), extraOptionsEntry.getValue().asBoolean().getValue());
break;
default:
throw new UnsupportedOperationException("Unsupported extra encryption option: " + extraOptionsEntry.getKey());
}
}
builder.extraOptions(extraOptions);
break;
case "keyVaultNamespace":
builder.keyVaultNamespace(entry.getValue().asString().getValue());
break;
case "kmsProviders":
builder.kmsProviders(createKmsProvidersMap(entry.getValue().asDocument()));
break;
case "keyExpirationMS":
builder.keyExpiration(entry.getValue().asNumber().longValue(), TimeUnit.MILLISECONDS);
break;
default:
throw new UnsupportedOperationException("Unsupported client encryption option: " + entry.getKey());
}
}
clientSettingsBuilder.autoEncryptionSettings(builder.build());
}

MongoClientSettings clientSettings = clientSettingsBuilder.build();

if (entity.containsKey("observeLogMessages")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import com.mongodb.client.model.DeleteManyModel;
import com.mongodb.client.model.DeleteOneModel;
import com.mongodb.client.model.DeleteOptions;
import com.mongodb.client.model.DropCollectionOptions;
import com.mongodb.client.model.DropIndexOptions;
import com.mongodb.client.model.EstimatedDocumentCountOptions;
import com.mongodb.client.model.FindOneAndDeleteOptions;
Expand All @@ -77,6 +78,7 @@
import com.mongodb.client.model.UpdateManyModel;
import com.mongodb.client.model.UpdateOneModel;
import com.mongodb.client.model.UpdateOptions;
import com.mongodb.client.model.ValidationOptions;
import com.mongodb.client.model.WriteModel;
import com.mongodb.client.model.bulk.ClientBulkWriteOptions;
import com.mongodb.client.model.bulk.ClientBulkWriteResult;
Expand Down Expand Up @@ -1364,12 +1366,21 @@ public OperationResult executeDropCollection(final BsonDocument operation) {
BsonDocument arguments = operation.getDocument("arguments", new BsonDocument());
String collectionName = arguments.getString("collection").getValue();

if (operation.getDocument("arguments").size() > 1) {
throw new UnsupportedOperationException("Unexpected arguments " + operation.get("arguments"));
DropCollectionOptions dropCollectionOptions = new DropCollectionOptions();
for (Map.Entry<String, BsonValue> entry : arguments.entrySet()) {
switch (entry.getKey()) {
case "collection":
break;
case "encryptedFields":
dropCollectionOptions.encryptedFields(entry.getValue().asDocument());
break;
default:
throw new UnsupportedOperationException("Unsupported drop collections option: " + entry.getKey());
}
}

return resultOf(() -> {
database.getCollection(collectionName).drop();
database.getCollection(collectionName).drop(dropCollectionOptions);
return null;
});
}
Expand Down Expand Up @@ -1429,6 +1440,11 @@ public OperationResult executeCreateCollection(final BsonDocument operation) {
case "clusteredIndex":
options.clusteredIndexOptions(createClusteredIndexOptions(cur.getValue().asDocument()));
break;
case "validator":
ValidationOptions validationOptions = new ValidationOptions();
validationOptions.validator(cur.getValue().asDocument());
options.validationOptions(validationOptions);
break;
default:
throw new UnsupportedOperationException("Unsupported argument: " + cur.getKey());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public abstract class UnifiedTest {
private static final Set<String> PRESTART_POOL_ASYNC_WORK_MANAGER_FILE_DESCRIPTIONS = Collections.singleton(
"wait queue timeout errors include details about checked out connections");

private static final String MAX_SUPPORTED_SCHEMA_VERSION = "1.22";
private static final String MAX_SUPPORTED_SCHEMA_VERSION = "1.23";
private static final List<Integer> MAX_SUPPORTED_SCHEMA_VERSION_COMPONENTS = Arrays.stream(MAX_SUPPORTED_SCHEMA_VERSION.split("\\."))
.map(Integer::parseInt)
.collect(Collectors.toList());
Expand Down