Search in sources :

Example 6 with TestCommandListener

use of com.mongodb.internal.connection.TestCommandListener in project mongo-java-driver by mongodb.

the class AbstractClientSideEncryptionDeadlockTest method shouldPassAllOutcomes.

@ParameterizedTest
@MethodSource("testArgumentProvider")
public void shouldPassAllOutcomes(final int maxPoolSize, final int expectedNumberOfClientsCreated, final boolean bypassAutoEncryption, final boolean externalKeyVaultClient, final List<ExpectedEvent> expectedEncryptingClientEvents, final List<ExpectedEvent> expectedExternalKeyVaultsClientEvents) {
    AutoEncryptionSettings.Builder autoEncryptionSettingsBuilder = AutoEncryptionSettings.builder().keyVaultNamespace("keyvault.datakeys").kmsProviders(kmsProviders).bypassAutoEncryption(bypassAutoEncryption);
    TestCommandListener externalKeyVaultClientCommandListener = new TestCommandListener(singletonList("commandStartedEvent"), emptyList());
    if (externalKeyVaultClient) {
        autoEncryptionSettingsBuilder.keyVaultMongoClientSettings(getKeyVaultClientSettings(externalKeyVaultClientCommandListener));
    }
    TestCommandListener encryptingClientCommandListener = new TestCommandListener(singletonList("commandStartedEvent"), emptyList());
    encryptingClient = createMongoClient(getClientSettings(maxPoolSize, encryptingClientCommandListener, autoEncryptionSettingsBuilder.build()));
    BsonDocument unencryptedDocument = new BsonDocument("_id", new BsonInt32(0)).append("encrypted", new BsonString("string0"));
    if (bypassAutoEncryption) {
        getMongoClient().getDatabase("db").getCollection("coll", BsonDocument.class).withWriteConcern(WriteConcern.MAJORITY).insertOne(new BsonDocument("_id", new BsonInt32(0)).append("encrypted", cipherText));
    } else {
        encryptingClient.getDatabase("db").getCollection("coll", BsonDocument.class).withWriteConcern(WriteConcern.MAJORITY).insertOne(unencryptedDocument);
    }
    BsonDocument result = encryptingClient.getDatabase("db").getCollection("coll", BsonDocument.class).find().filter(Filters.eq("_id", 0)).first();
    assertEquals(unencryptedDocument, result);
    assertEquals(expectedNumberOfClientsCreated, getNumUniqueClients(encryptingClientCommandListener), "Unique clients");
    assertEventEquality(encryptingClientCommandListener, expectedEncryptingClientEvents);
    assertEventEquality(externalKeyVaultClientCommandListener, expectedExternalKeyVaultsClientEvents);
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) TestCommandListener(com.mongodb.internal.connection.TestCommandListener) AutoEncryptionSettings(com.mongodb.AutoEncryptionSettings) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 7 with TestCommandListener

use of com.mongodb.internal.connection.TestCommandListener in project mongo-java-driver by mongodb.

the class AbstractClientSideEncryptionTest method setUp.

@Before
public void setUp() {
    assumeTrue("Client side encryption tests disabled", hasEncryptionTestsEnabled());
    assumeFalse("runOn requirements not satisfied", skipTest);
    assumeFalse("Skipping count tests", filename.startsWith("count."));
    assumeFalse(definition.getString("skipReason", new BsonString("")).getValue(), definition.containsKey("skipReason"));
    String databaseName = specDocument.getString("database_name").getValue();
    String collectionName = specDocument.getString("collection_name").getValue();
    collectionHelper = new CollectionHelper<BsonDocument>(new BsonDocumentCodec(), new MongoNamespace(databaseName, collectionName));
    MongoDatabase database = getMongoClient().getDatabase(databaseName);
    MongoCollection<BsonDocument> collection = database.getCollection(collectionName, BsonDocument.class);
    collection.drop();
    /* Create the collection for auto encryption. */
    if (specDocument.containsKey("json_schema")) {
        database.createCollection(collectionName, new CreateCollectionOptions().validationOptions(new ValidationOptions().validator(new BsonDocument("$jsonSchema", specDocument.getDocument("json_schema")))));
    }
    /* Insert data into the collection */
    List<BsonDocument> documents = new ArrayList<BsonDocument>();
    if (!data.isEmpty()) {
        for (BsonValue document : data) {
            documents.add(document.asDocument());
        }
        database.getCollection(collectionName, BsonDocument.class).insertMany(documents);
    }
    /* Insert data into the "keyvault.datakeys" key vault. */
    BsonArray data = specDocument.getArray("key_vault_data", new BsonArray());
    collection = getMongoClient().getDatabase("keyvault").getCollection("datakeys", BsonDocument.class).withWriteConcern(WriteConcern.MAJORITY);
    collection.drop();
    if (!data.isEmpty()) {
        documents = new ArrayList<BsonDocument>();
        for (BsonValue document : data) {
            documents.add(document.asDocument());
        }
        collection.insertMany(documents);
    }
    commandListener = new TestCommandListener();
    BsonDocument clientOptions = definition.getDocument("clientOptions");
    BsonDocument cryptOptions = clientOptions.getDocument("autoEncryptOpts");
    BsonDocument kmsProviders = cryptOptions.getDocument("kmsProviders");
    boolean bypassAutoEncryption = cryptOptions.getBoolean("bypassAutoEncryption", BsonBoolean.FALSE).getValue();
    Map<String, BsonDocument> namespaceToSchemaMap = new HashMap<String, BsonDocument>();
    if (cryptOptions.containsKey("schemaMap")) {
        BsonDocument autoEncryptMapDocument = cryptOptions.getDocument("schemaMap");
        for (Map.Entry<String, BsonValue> entries : autoEncryptMapDocument.entrySet()) {
            final BsonDocument autoEncryptOptionsDocument = entries.getValue().asDocument();
            namespaceToSchemaMap.put(entries.getKey(), autoEncryptOptionsDocument);
        }
    }
    Map<String, Object> extraOptions = new HashMap<String, Object>();
    if (cryptOptions.containsKey("extraOptions")) {
        BsonDocument extraOptionsDocument = cryptOptions.getDocument("extraOptions");
        if (extraOptionsDocument.containsKey("mongocryptdSpawnArgs")) {
            List<String> mongocryptdSpawnArgsValue = new ArrayList<String>();
            for (BsonValue cur : extraOptionsDocument.getArray("mongocryptdSpawnArgs")) {
                mongocryptdSpawnArgsValue.add(cur.asString().getValue());
            }
            extraOptions.put("mongocryptdSpawnArgs", mongocryptdSpawnArgsValue);
        }
        if (extraOptionsDocument.containsKey("mongocryptdBypassSpawn")) {
            extraOptions.put("mongocryptdBypassSpawn", extraOptionsDocument.getBoolean("mongocryptdBypassSpawn").getValue());
        }
        if (extraOptionsDocument.containsKey("mongocryptdURI")) {
            extraOptions.put("mongocryptdURI", extraOptionsDocument.getString("mongocryptdURI").getValue());
        }
    }
    Map<String, Map<String, Object>> kmsProvidersMap = new HashMap<>();
    for (String kmsProviderKey : kmsProviders.keySet()) {
        BsonDocument kmsProviderOptions = kmsProviders.get(kmsProviderKey).asDocument();
        Map<String, Object> kmsProviderMap = new HashMap<>();
        kmsProvidersMap.put(kmsProviderKey.startsWith("aws") ? "aws" : kmsProviderKey, kmsProviderMap);
        switch(kmsProviderKey) {
            case "aws":
                kmsProviderMap.put("accessKeyId", System.getProperty("org.mongodb.test.awsAccessKeyId"));
                kmsProviderMap.put("secretAccessKey", System.getProperty("org.mongodb.test.awsSecretAccessKey"));
                break;
            case "awsTemporary":
                kmsProviderMap.put("accessKeyId", System.getProperty("org.mongodb.test.tmpAwsAccessKeyId"));
                kmsProviderMap.put("secretAccessKey", System.getProperty("org.mongodb.test.tmpAwsSecretAccessKey"));
                kmsProviderMap.put("sessionToken", System.getProperty("org.mongodb.test.tmpAwsSessionToken"));
                break;
            case "awsTemporaryNoSessionToken":
                kmsProviderMap.put("accessKeyId", System.getProperty("org.mongodb.test.tmpAwsAccessKeyId"));
                kmsProviderMap.put("secretAccessKey", System.getProperty("org.mongodb.test.tmpAwsSecretAccessKey"));
                break;
            case "azure":
                kmsProviderMap.put("tenantId", System.getProperty("org.mongodb.test.azureTenantId"));
                kmsProviderMap.put("clientId", System.getProperty("org.mongodb.test.azureClientId"));
                kmsProviderMap.put("clientSecret", System.getProperty("org.mongodb.test.azureClientSecret"));
                break;
            case "gcp":
                kmsProviderMap.put("email", System.getProperty("org.mongodb.test.gcpEmail"));
                kmsProviderMap.put("privateKey", System.getProperty("org.mongodb.test.gcpPrivateKey"));
                break;
            case "kmip":
                kmsProviderMap.put("endpoint", System.getProperty("org.mongodb.test.kmipEndpoint", "localhost:5698"));
                break;
            case "local":
                kmsProviderMap.put("key", kmsProviderOptions.getBinary("key").getData());
                break;
            default:
                throw new UnsupportedOperationException("Unsupported KMS provider: " + kmsProviderKey);
        }
    }
    String keyVaultNamespace = "keyvault.datakeys";
    if (cryptOptions.containsKey("keyVaultNamespace")) {
        keyVaultNamespace = cryptOptions.getString("keyVaultNamespace").getValue();
    }
    createMongoClient(AutoEncryptionSettings.builder().keyVaultNamespace(keyVaultNamespace).kmsProviders(kmsProvidersMap).schemaMap(namespaceToSchemaMap).bypassAutoEncryption(bypassAutoEncryption).extraOptions(extraOptions).build(), commandListener);
    database = getDatabase(databaseName);
    helper = new JsonPoweredCrudTestHelper(description, database, database.getCollection("default", BsonDocument.class));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BsonString(org.bson.BsonString) ValidationOptions(com.mongodb.client.model.ValidationOptions) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) TestCommandListener(com.mongodb.internal.connection.TestCommandListener) MongoNamespace(com.mongodb.MongoNamespace) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) HashMap(java.util.HashMap) Map(java.util.Map) BsonValue(org.bson.BsonValue) Before(org.junit.Before)

Example 8 with TestCommandListener

use of com.mongodb.internal.connection.TestCommandListener in project mongo-java-driver by mongodb.

the class AbstractServerSelectionProseTest method operationCountBasedSelectionWithinLatencyWindow.

/**
 * <a href="https://github.com/mongodb/specifications/blob/master/source/server-selection/server-selection-tests.rst#prose-test">
 * {@code operationCount}-based Selection Within Latency Window</a>.
 */
@Test
@SuppressWarnings("try")
void operationCountBasedSelectionWithinLatencyWindow() throws InterruptedException, ExecutionException {
    assumeTrue(isSharded());
    ConnectionString multiMongosConnectionString = getMultiMongosConnectionString();
    assumeTrue(multiMongosConnectionString != null);
    assumeTrue(multiMongosConnectionString.getSslEnabled() == null || !multiMongosConnectionString.getSslEnabled());
    assertEquals(2, multiMongosConnectionString.getHosts().size());
    String appName = "loadBalancingTest";
    int timeoutSeconds = 60;
    int tasks = 10;
    int opsPerTask = 100;
    TestCommandListener commandListener = new TestCommandListener(singletonList("commandStartedEvent"), singletonList("drop"));
    MongoClientSettings clientSettings = getMongoClientSettingsBuilder().applicationName(appName).applyConnectionString(multiMongosConnectionString).applyToConnectionPoolSettings(builder -> builder.minSize(tasks)).addCommandListener(commandListener).build();
    BsonDocument configureFailPoint = new BsonDocument().append("configureFailPoint", new BsonString("failCommand")).append("mode", new BsonDocument().append("times", new BsonInt32(10_000))).append("data", new BsonDocument().append("failCommands", new BsonArray(singletonList(new BsonString("find")))).append("blockConnection", BsonBoolean.valueOf(true)).append("blockTimeMS", new BsonInt32(500)).append("appName", new BsonString(appName)));
    ServerAddress serverWithFailPoint = clientSettings.getClusterSettings().getHosts().get(0);
    ExecutorService executor = Executors.newFixedThreadPool(tasks);
    try (MongoClient client = createClient(clientSettings)) {
        MongoCollection<Document> collection = client.getDatabase(getDefaultDatabaseName()).getCollection("operationCountBasedSelectionWithinLatencyWindow");
        collection.drop();
        try (FailPoint ignored = FailPoint.enable(configureFailPoint, serverWithFailPoint)) {
            Map<ServerAddress, Double> selectionRates = doSelections(collection, commandListener, executor, tasks, opsPerTask, timeoutSeconds);
            double expectedServerWithFpSelectionRateUpperBound = 0.25;
            assertTrue(selectionRates.containsKey(serverWithFailPoint));
            assertTrue(selectionRates.get(serverWithFailPoint) < expectedServerWithFpSelectionRateUpperBound, selectionRates::toString);
            assertEquals(1, selectionRates.values().stream().mapToDouble(Double::doubleValue).sum(), 0.01, selectionRates::toString);
        }
        commandListener.reset();
        Map<ServerAddress, Double> selectionRates = doSelections(collection, commandListener, executor, tasks, opsPerTask, timeoutSeconds);
        selectionRates.values().forEach(rate -> assertEquals(0.5, rate, 0.1, selectionRates::toString));
    } finally {
        executor.shutdownNow();
        assertTrue(executor.awaitTermination(timeoutSeconds, SECONDS));
    }
}
Also used : TestCommandListener(com.mongodb.internal.connection.TestCommandListener) ServerAddress(com.mongodb.ServerAddress) BsonString(org.bson.BsonString) ClusterFixture.getMultiMongosConnectionString(com.mongodb.ClusterFixture.getMultiMongosConnectionString) ConnectionString(com.mongodb.ConnectionString) MongoClientSettings(com.mongodb.MongoClientSettings) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) ExecutorService(java.util.concurrent.ExecutorService) ClusterFixture.getMultiMongosConnectionString(com.mongodb.ClusterFixture.getMultiMongosConnectionString) ConnectionString(com.mongodb.ConnectionString) Test(org.junit.jupiter.api.Test)

Example 9 with TestCommandListener

use of com.mongodb.internal.connection.TestCommandListener in project mongo-java-driver by mongodb.

the class AtlasDataLakeKillCursorsProseTest method setUp.

@Before
public void setUp() {
    assumeTrue(isDataLakeTest());
    commandListener = new TestCommandListener();
    client = MongoClients.create(MongoClientSettings.builder().applyConnectionString(requireNonNull(getConnectionString())).addCommandListener(commandListener).build());
}
Also used : TestCommandListener(com.mongodb.internal.connection.TestCommandListener) Before(org.junit.Before)

Example 10 with TestCommandListener

use of com.mongodb.internal.connection.TestCommandListener in project mongo-java-driver by mongodb.

the class AbstractChangeStreamsTest method setUp.

@Before
public void setUp() {
    assumeFalse(skipTest);
    CollectionHelper.dropDatabase(namespace.getDatabaseName(), WriteConcern.MAJORITY);
    collectionHelper = new CollectionHelper<BsonDocument>(new BsonDocumentCodec(), namespace);
    collectionHelper.drop();
    collectionHelper.create();
    if (namespace2 != null) {
        CollectionHelper.dropDatabase(namespace2.getDatabaseName(), WriteConcern.MAJORITY);
        CollectionHelper<BsonDocument> collectionHelper2 = new CollectionHelper<BsonDocument>(new BsonDocumentCodec(), namespace2);
        collectionHelper2.drop();
        collectionHelper2.create();
    }
    if (definition.containsKey("failPoint")) {
        collectionHelper.runAdminCommand(definition.getDocument("failPoint"));
    }
    commandListener = new TestCommandListener();
    mongoClient = createMongoClient(getMongoClientSettingsBuilder().addCommandListener(commandListener).build());
}
Also used : BsonDocument(org.bson.BsonDocument) TestCommandListener(com.mongodb.internal.connection.TestCommandListener) CollectionHelper(com.mongodb.client.test.CollectionHelper) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) Before(org.junit.Before)

Aggregations

TestCommandListener (com.mongodb.internal.connection.TestCommandListener)16 BsonDocument (org.bson.BsonDocument)11 BsonString (org.bson.BsonString)10 Before (org.junit.Before)9 MongoClientSettings (com.mongodb.MongoClientSettings)5 HashMap (java.util.HashMap)5 BsonArray (org.bson.BsonArray)5 TestConnectionPoolListener (com.mongodb.internal.connection.TestConnectionPoolListener)4 Map (java.util.Map)4 BsonInt32 (org.bson.BsonInt32)4 BsonValue (org.bson.BsonValue)4 Fixture.getMongoClientSettingsBuilder (com.mongodb.client.Fixture.getMongoClientSettingsBuilder)3 ArrayList (java.util.ArrayList)3 BsonBoolean (org.bson.BsonBoolean)3 ClusterFixture.getMultiMongosConnectionString (com.mongodb.ClusterFixture.getMultiMongosConnectionString)2 ClusterFixture.isSharded (com.mongodb.ClusterFixture.isSharded)2 MongoNamespace (com.mongodb.MongoNamespace)2 CreateCollectionOptions (com.mongodb.client.model.CreateCollectionOptions)2 ValidationOptions (com.mongodb.client.model.ValidationOptions)2 ConnectionCheckOutFailedEvent (com.mongodb.event.ConnectionCheckOutFailedEvent)2