use of com.mongodb.internal.connection.TestCommandListener in project mongo-java-driver by mongodb.
the class AbstractClientSideEncryptionDeadlockTest method setUp.
@BeforeEach
public void setUp() throws IOException, URISyntaxException {
assumeTrue(serverVersionAtLeast(4, 2));
assumeTrue(isClientSideEncryptionTest());
MongoDatabase keyVaultDatabase = getMongoClient().getDatabase("keyvault");
MongoCollection<BsonDocument> dataKeysCollection = keyVaultDatabase.getCollection("datakeys", BsonDocument.class).withWriteConcern(WriteConcern.MAJORITY);
dataKeysCollection.drop();
dataKeysCollection.insertOne(bsonDocumentFromPath("external-key.json"));
MongoDatabase encryptedDatabase = getMongoClient().getDatabase("db");
MongoCollection<BsonDocument> encryptedCollection = encryptedDatabase.getCollection("coll", BsonDocument.class).withWriteConcern(WriteConcern.MAJORITY);
encryptedCollection.drop();
encryptedDatabase.createCollection("coll", new CreateCollectionOptions().validationOptions(new ValidationOptions().validator(new BsonDocument("$jsonSchema", bsonDocumentFromPath("external-schema.json")))));
kmsProviders = new HashMap<>();
Map<String, Object> localProviderMap = new HashMap<>();
localProviderMap.put("key", Base64.getDecoder().decode("Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZ" + "GJkTXVyZG9uSjFk"));
kmsProviders.put("local", localProviderMap);
ClientEncryption clientEncryption = ClientEncryptions.create(ClientEncryptionSettings.builder().keyVaultMongoClientSettings(getKeyVaultClientSettings(new TestCommandListener())).keyVaultNamespace("keyvault.datakeys").kmsProviders(kmsProviders).build());
cipherText = clientEncryption.encrypt(new BsonString("string0"), new EncryptOptions("AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic").keyAltName("local"));
clientEncryption.close();
}
use of com.mongodb.internal.connection.TestCommandListener in project mongo-java-driver by mongodb.
the class ClientEncryptionDataKeyAndDoubleEncryptionTest method setUp.
@Before
public void setUp() {
assumeTrue(serverVersionAtLeast(4, 2));
assumeTrue("Has encryption tests", hasEncryptionTestsEnabled());
// Step 1: create unencrypted client
commandListener = new TestCommandListener();
client = MongoClients.create(getMongoClientSettingsBuilder().addCommandListener(commandListener).build());
client.getDatabase("keyvault").getCollection("datakeys").drop();
client.getDatabase("db").getCollection("coll").drop();
// Step 2: Create encrypted client and client encryption
Map<String, Map<String, Object>> kmsProviders = new HashMap<String, Map<String, Object>>() {
{
put("aws", new HashMap<String, Object>() {
{
put("accessKeyId", System.getProperty("org.mongodb.test.awsAccessKeyId"));
put("secretAccessKey", System.getProperty("org.mongodb.test.awsSecretAccessKey"));
}
});
put("azure", new HashMap<String, Object>() {
{
put("tenantId", System.getProperty("org.mongodb.test.azureTenantId"));
put("clientId", System.getProperty("org.mongodb.test.azureClientId"));
put("clientSecret", System.getProperty("org.mongodb.test.azureClientSecret"));
}
});
put("gcp", new HashMap<String, Object>() {
{
put("email", System.getProperty("org.mongodb.test.gcpEmail"));
put("privateKey", System.getProperty("org.mongodb.test.gcpPrivateKey"));
}
});
put("local", new HashMap<String, Object>() {
{
put("key", "Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBM" + "UN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk");
}
});
}
};
HashMap<String, BsonDocument> schemaMap = new HashMap<String, BsonDocument>() {
{
put("db.coll", BsonDocument.parse("{" + " \"bsonType\": \"object\"," + " \"properties\": {" + " \"encrypted_placeholder\": {" + " \"encrypt\": {" + " \"keyId\": \"/placeholder\"," + " \"bsonType\": \"string\"," + " \"algorithm\": \"AEAD_AES_256_CBC_HMAC_SHA_512-Random\"" + " }" + " }" + " }" + "}"));
}
};
String keyVaultNamespace = "keyvault.datakeys";
clientEncrypted = MongoClients.create(getMongoClientSettingsBuilder().autoEncryptionSettings(AutoEncryptionSettings.builder().keyVaultNamespace(keyVaultNamespace).kmsProviders(kmsProviders).schemaMap(schemaMap).build()).build());
clientEncryption = ClientEncryptions.create(ClientEncryptionSettings.builder().keyVaultMongoClientSettings(getMongoClientSettingsBuilder().addCommandListener(commandListener).build()).keyVaultNamespace(keyVaultNamespace).kmsProviders(kmsProviders).build());
}
use of com.mongodb.internal.connection.TestCommandListener in project mongo-java-driver by mongodb.
the class UnifiedTest method compareEvents.
private void compareEvents(final BsonDocument operation) {
for (BsonValue cur : operation.getArray("expectEvents")) {
BsonDocument curClientEvents = cur.asDocument();
String client = curClientEvents.getString("client").getValue();
String eventType = curClientEvents.getString("eventType", new BsonString("command")).getValue();
if (eventType.equals("command")) {
TestCommandListener listener = entities.getClientCommandListener(client);
eventMatcher.assertCommandEventsEquality(client, curClientEvents.getArray("events"), listener.getEvents());
} else if (eventType.equals("cmap")) {
TestConnectionPoolListener listener = entities.getConnectionPoolListener(client);
eventMatcher.assertConnectionPoolEventsEquality(client, curClientEvents.getArray("events"), listener.getEvents());
} else {
throw new UnsupportedOperationException("Unexpected event type: " + eventType);
}
}
}
use of com.mongodb.internal.connection.TestCommandListener in project mongo-java-driver by mongodb.
the class Entities method initClient.
private void initClient(final BsonDocument entity, final String id, final Function<MongoClientSettings, MongoClient> mongoClientSupplier, final boolean waitForPoolAsyncWorkManagerStart) {
if (!SUPPORTED_CLIENT_ENTITY_OPTIONS.containsAll(entity.keySet())) {
throw new UnsupportedOperationException("Client entity contains unsupported options: " + entity.keySet() + ". Supported options are " + SUPPORTED_CLIENT_ENTITY_OPTIONS);
}
MongoClientSettings.Builder clientSettingsBuilder;
if (entity.getBoolean("useMultipleMongoses", BsonBoolean.FALSE).getValue() && (isSharded() || isLoadBalanced())) {
assumeTrue("Multiple mongos connection string not available for sharded cluster", !isSharded() || getMultiMongosConnectionString() != null);
assumeTrue("Multiple mongos connection string not available for load-balanced cluster", !isLoadBalanced() || getMultiMongosConnectionString() != null);
clientSettingsBuilder = getMultiMongosMongoClientSettingsBuilder();
} else {
clientSettingsBuilder = getMongoClientSettingsBuilder();
}
if (entity.containsKey("observeEvents")) {
List<String> ignoreCommandMonitoringEvents = entity.getArray("ignoreCommandMonitoringEvents", new BsonArray()).stream().map(type -> type.asString().getValue()).collect(Collectors.toList());
ignoreCommandMonitoringEvents.add("configureFailPoint");
TestCommandListener testCommandListener = new TestCommandListener(entity.getArray("observeEvents").stream().map(type -> type.asString().getValue()).collect(Collectors.toList()), ignoreCommandMonitoringEvents, entity.getBoolean("observeSensitiveCommands", BsonBoolean.FALSE).getValue());
clientSettingsBuilder.addCommandListener(testCommandListener);
putEntity(id + "-command-listener", testCommandListener, clientCommandListeners);
TestConnectionPoolListener testConnectionPoolListener = new TestConnectionPoolListener(entity.getArray("observeEvents").stream().map(type -> type.asString().getValue()).collect(Collectors.toList()));
clientSettingsBuilder.applyToConnectionPoolSettings(builder -> builder.addConnectionPoolListener(testConnectionPoolListener));
putEntity(id + "-connection-pool-listener", testConnectionPoolListener, clientConnectionPoolListeners);
} else {
// Regardless of whether events are observed, we still need to track some info about the pool in order to implement
// the assertNumberConnectionsCheckedOut operation
TestConnectionPoolListener testConnectionPoolListener = new TestConnectionPoolListener();
clientSettingsBuilder.applyToConnectionPoolSettings(builder -> builder.addConnectionPoolListener(testConnectionPoolListener));
putEntity(id + "-connection-pool-listener", testConnectionPoolListener, clientConnectionPoolListeners);
}
if (entity.containsKey("storeEventsAsEntities")) {
BsonArray storeEventsAsEntitiesArray = entity.getArray("storeEventsAsEntities");
for (BsonValue eventValue : storeEventsAsEntitiesArray) {
BsonDocument eventDocument = eventValue.asDocument();
String key = eventDocument.getString("id").getValue();
BsonArray eventList = eventDocument.getArray("events");
List<BsonDocument> eventDocumentList = synchronizedList(new ArrayList<>());
putEntity(key, eventDocumentList, eventsMap);
if (eventList.stream().map(value -> value.asString().getValue()).anyMatch(value -> value.startsWith("Command"))) {
clientSettingsBuilder.addCommandListener(new EntityCommandListener(eventList.stream().map(value -> value.asString().getValue()).collect(Collectors.toSet()), eventDocumentList));
}
if (eventList.stream().map(value -> value.asString().getValue()).anyMatch(value -> value.startsWith("Pool") || value.startsWith("Connection"))) {
clientSettingsBuilder.applyToConnectionPoolSettings(builder -> builder.addConnectionPoolListener(new EntityConnectionPoolListener(eventList.stream().map(value -> value.asString().getValue()).collect(Collectors.toSet()), eventDocumentList)));
}
}
}
clientSettingsBuilder.applyToServerSettings(builder -> {
builder.heartbeatFrequency(50, TimeUnit.MILLISECONDS);
builder.minHeartbeatFrequency(50, TimeUnit.MILLISECONDS);
});
if (entity.containsKey("uriOptions")) {
entity.getDocument("uriOptions").forEach((key, value) -> {
switch(key) {
case "retryReads":
clientSettingsBuilder.retryReads(value.asBoolean().getValue());
break;
case "retryWrites":
clientSettingsBuilder.retryWrites(value.asBoolean().getValue());
break;
case "readConcernLevel":
clientSettingsBuilder.readConcern(new ReadConcern(ReadConcernLevel.fromString(value.asString().getValue())));
break;
case "w":
clientSettingsBuilder.writeConcern(new WriteConcern(value.asInt32().intValue()));
break;
case "maxPoolSize":
clientSettingsBuilder.applyToConnectionPoolSettings(builder -> builder.maxSize(value.asNumber().intValue()));
break;
case "waitQueueTimeoutMS":
clientSettingsBuilder.applyToConnectionPoolSettings(builder -> builder.maxWaitTime(value.asNumber().longValue(), TimeUnit.MILLISECONDS));
break;
case "loadBalanced":
if (value.asBoolean().getValue()) {
clientSettingsBuilder.applyToClusterSettings(builder -> builder.mode(ClusterConnectionMode.LOAD_BALANCED));
}
break;
case "appname":
clientSettingsBuilder.applicationName(value.asString().getValue());
break;
default:
throw new UnsupportedOperationException("Unsupported uri option: " + key);
}
});
}
if (entity.containsKey("serverApi")) {
BsonDocument serverApiDocument = entity.getDocument("serverApi");
String apiVersion = serverApiDocument.getString("version").getValue();
ServerApi.Builder serverApiBuilder = ServerApi.builder().version(ServerApiVersion.findByValue(apiVersion));
if (serverApiDocument.containsKey("deprecationErrors")) {
serverApiBuilder.deprecationErrors(serverApiDocument.getBoolean("deprecationErrors").getValue());
}
if (serverApiDocument.containsKey("strict")) {
serverApiBuilder.strict(serverApiDocument.getBoolean("strict").getValue());
}
clientSettingsBuilder.serverApi(serverApiBuilder.build());
}
putEntity(id, mongoClientSupplier.apply(clientSettingsBuilder.build()), clients);
if (waitForPoolAsyncWorkManagerStart) {
waitForPoolAsyncWorkManagerStart();
}
}
use of com.mongodb.internal.connection.TestCommandListener in project mongo-java-driver by mongodb.
the class RetryableWritesProseTest method poolClearedExceptionMustBeRetryable.
@SuppressWarnings("try")
public static <R> void poolClearedExceptionMustBeRetryable(final Function<MongoClientSettings, MongoClient> clientCreator, final Function<MongoCollection<Document>, R> operation, final String operationName, final boolean write) throws InterruptedException, ExecutionException, TimeoutException {
assumeTrue(serverVersionAtLeast(4, 3) && !(write && isStandalone()));
assumeFalse(isServerlessTest());
TestConnectionPoolListener connectionPoolListener = new TestConnectionPoolListener(asList("connectionCheckedOutEvent", "poolClearedEvent", "connectionCheckOutFailedEvent"));
TestCommandListener commandListener = new TestCommandListener(singletonList("commandStartedEvent"), asList("configureFailPoint", "drop"));
MongoClientSettings clientSettings = getMongoClientSettingsBuilder().applyToConnectionPoolSettings(builder -> builder.maxSize(1).addConnectionPoolListener(connectionPoolListener)).applyToServerSettings(builder -> builder.minHeartbeatFrequency(50, TimeUnit.MILLISECONDS).heartbeatFrequency(50, TimeUnit.MILLISECONDS)).retryReads(true).retryWrites(true).addCommandListener(commandListener).build();
BsonDocument configureFailPoint = new BsonDocument().append("configureFailPoint", new BsonString("failCommand")).append("mode", new BsonDocument().append("times", new BsonInt32(1))).append("data", new BsonDocument().append("failCommands", new BsonArray(singletonList(new BsonString(operationName)))).append("errorCode", new BsonInt32(91)).append("errorLabels", write ? new BsonArray(singletonList(new BsonString("RetryableWriteError"))) : new BsonArray()).append("blockConnection", BsonBoolean.valueOf(true)).append("blockTimeMS", new BsonInt32(1000)));
int timeoutSeconds = 5;
try (MongoClient client = clientCreator.apply(clientSettings);
FailPoint ignored = FailPoint.enable(configureFailPoint, client)) {
MongoCollection<Document> collection = client.getDatabase(getDefaultDatabaseName()).getCollection("poolClearedExceptionMustBeRetryable");
collection.drop();
ExecutorService ex = Executors.newFixedThreadPool(2);
try {
Future<R> result1 = ex.submit(() -> operation.apply(collection));
Future<R> result2 = ex.submit(() -> operation.apply(collection));
connectionPoolListener.waitForEvent(ConnectionCheckedOutEvent.class, 1, timeoutSeconds, SECONDS);
connectionPoolListener.waitForEvent(ConnectionPoolClearedEvent.class, 1, timeoutSeconds, SECONDS);
connectionPoolListener.waitForEvent(ConnectionCheckOutFailedEvent.class, 1, timeoutSeconds, SECONDS);
result1.get(timeoutSeconds, SECONDS);
result2.get(timeoutSeconds, SECONDS);
} finally {
ex.shutdownNow();
}
assertEquals(3, commandListener.getCommandStartedEvents().size());
commandListener.getCommandStartedEvents().forEach(event -> assertEquals(operationName, event.getCommandName()));
}
}
Aggregations