Search in sources :

Example 51 with MongoClientSettings

use of com.mongodb.MongoClientSettings in project mongo-java-driver by mongodb.

the class ClientSideEncryptionSimpleTour method main.

/**
 * Run this main method to see the output of this quick example.
 *
 * Requires the mongodb-crypt library in the class path and mongocryptd on the system path.
 * Assumes the schema has already been created in MongoDB.
 *
 * @param args ignored args
 */
public static void main(final String[] args) {
    // This would have to be the same master key as was used to create the encryption key
    final byte[] localMasterKey = new byte[96];
    new SecureRandom().nextBytes(localMasterKey);
    Map<String, Map<String, Object>> kmsProviders = new HashMap<String, Map<String, Object>>() {

        {
            put("local", new HashMap<String, Object>() {

                {
                    put("key", localMasterKey);
                }
            });
        }
    };
    String keyVaultNamespace = "admin.datakeys";
    AutoEncryptionSettings autoEncryptionSettings = AutoEncryptionSettings.builder().keyVaultNamespace(keyVaultNamespace).kmsProviders(kmsProviders).build();
    MongoClientSettings clientSettings = MongoClientSettings.builder().autoEncryptionSettings(autoEncryptionSettings).build();
    MongoClient mongoClient = MongoClients.create(clientSettings);
    MongoCollection<Document> collection = mongoClient.getDatabase("test").getCollection("coll");
    ObservableSubscriber<Void> successSubscriber = new OperationSubscriber<>();
    collection.drop().subscribe(successSubscriber);
    successSubscriber.await();
    ObservableSubscriber<InsertOneResult> insertOneSubscriber = new OperationSubscriber<>();
    collection.insertOne(new Document("encryptedField", "123456789")).subscribe(insertOneSubscriber);
    insertOneSubscriber.await();
    ObservableSubscriber<Document> documentSubscriber = new PrintDocumentSubscriber();
    collection.find().first().subscribe(documentSubscriber);
    documentSubscriber.await();
    // release resources
    mongoClient.close();
}
Also used : PrintDocumentSubscriber(reactivestreams.helpers.SubscriberHelpers.PrintDocumentSubscriber) HashMap(java.util.HashMap) OperationSubscriber(reactivestreams.helpers.SubscriberHelpers.OperationSubscriber) SecureRandom(java.security.SecureRandom) MongoClientSettings(com.mongodb.MongoClientSettings) Document(org.bson.Document) MongoClient(com.mongodb.reactivestreams.client.MongoClient) AutoEncryptionSettings(com.mongodb.AutoEncryptionSettings) InsertOneResult(com.mongodb.client.result.InsertOneResult) HashMap(java.util.HashMap) Map(java.util.Map)

Example 52 with MongoClientSettings

use of com.mongodb.MongoClientSettings 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();
    }
}
Also used : GridFSBucket(com.mongodb.client.gridfs.GridFSBucket) MongoDatabase(com.mongodb.client.MongoDatabase) ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) ConnectionPoolCreatedEvent(com.mongodb.event.ConnectionPoolCreatedEvent) BsonValue(org.bson.BsonValue) CommandListener(com.mongodb.event.CommandListener) ClusterFixture.isSharded(com.mongodb.ClusterFixture.isSharded) Fixture.getMongoClientSettingsBuilder(com.mongodb.client.Fixture.getMongoClientSettingsBuilder) Arrays.asList(java.util.Arrays.asList) CommandSucceededEvent(com.mongodb.event.CommandSucceededEvent) BsonArray(org.bson.BsonArray) Map(java.util.Map) ClusterFixture.getMultiMongosConnectionString(com.mongodb.ClusterFixture.getMultiMongosConnectionString) ReadConcern(com.mongodb.ReadConcern) ConnectionCheckedOutEvent(com.mongodb.event.ConnectionCheckedOutEvent) CommandEvent(com.mongodb.event.CommandEvent) UnifiedCrudHelper.asReadPreference(com.mongodb.client.unified.UnifiedCrudHelper.asReadPreference) Collections.synchronizedList(java.util.Collections.synchronizedList) Set(java.util.Set) ConnectionPoolClearedEvent(com.mongodb.event.ConnectionPoolClearedEvent) Collectors(java.util.stream.Collectors) List(java.util.List) EventMatcher.getReasonString(com.mongodb.client.unified.EventMatcher.getReasonString) TestConnectionPoolListener(com.mongodb.internal.connection.TestConnectionPoolListener) Assume.assumeTrue(org.junit.Assume.assumeTrue) MongoClientSettings(com.mongodb.MongoClientSettings) NotNull(org.jetbrains.annotations.NotNull) ConnectionClosedEvent(com.mongodb.event.ConnectionClosedEvent) ConnectionCheckedInEvent(com.mongodb.event.ConnectionCheckedInEvent) MongoClient(com.mongodb.client.MongoClient) MongoCollection(com.mongodb.client.MongoCollection) ServerApi(com.mongodb.ServerApi) ClusterFixture.isLoadBalanced(com.mongodb.ClusterFixture.isLoadBalanced) BsonBoolean(org.bson.BsonBoolean) HashMap(java.util.HashMap) AbstractConnectionPoolTest.waitForPoolAsyncWorkManagerStart(com.mongodb.internal.connection.AbstractConnectionPoolTest.waitForPoolAsyncWorkManagerStart) BsonString(org.bson.BsonString) Function(java.util.function.Function) Fixture.getMultiMongosMongoClientSettingsBuilder(com.mongodb.client.Fixture.getMultiMongosMongoClientSettingsBuilder) BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) TransactionOptions(com.mongodb.TransactionOptions) HashSet(java.util.HashSet) TestCommandListener(com.mongodb.internal.connection.TestCommandListener) MongoCursor(com.mongodb.client.MongoCursor) ClusterConnectionMode(com.mongodb.connection.ClusterConnectionMode) BsonInt64(org.bson.BsonInt64) ConnectionCreatedEvent(com.mongodb.event.ConnectionCreatedEvent) ConnectionReadyEvent(com.mongodb.event.ConnectionReadyEvent) ConnectionPoolListener(com.mongodb.event.ConnectionPoolListener) ServerApiVersion(com.mongodb.ServerApiVersion) ServerId(com.mongodb.connection.ServerId) UnifiedCrudHelper.asReadConcern(com.mongodb.client.unified.UnifiedCrudHelper.asReadConcern) BsonDouble(org.bson.BsonDouble) CommandFailedEvent(com.mongodb.event.CommandFailedEvent) TimeUnit(java.util.concurrent.TimeUnit) ConnectionPoolClosedEvent(com.mongodb.event.ConnectionPoolClosedEvent) ConnectionPoolReadyEvent(com.mongodb.event.ConnectionPoolReadyEvent) ClientSessionOptions(com.mongodb.ClientSessionOptions) ReadConcernLevel(com.mongodb.ReadConcernLevel) ConnectionCheckOutStartedEvent(com.mongodb.event.ConnectionCheckOutStartedEvent) UnifiedCrudHelper.asWriteConcern(com.mongodb.client.unified.UnifiedCrudHelper.asWriteConcern) ClientSession(com.mongodb.client.ClientSession) BsonInt32(org.bson.BsonInt32) WriteConcern(com.mongodb.WriteConcern) ConnectionId(com.mongodb.connection.ConnectionId) ConnectionCheckOutFailedEvent(com.mongodb.event.ConnectionCheckOutFailedEvent) CommandStartedEvent(com.mongodb.event.CommandStartedEvent) TestConnectionPoolListener(com.mongodb.internal.connection.TestConnectionPoolListener) TestCommandListener(com.mongodb.internal.connection.TestCommandListener) MongoClientSettings(com.mongodb.MongoClientSettings) ClusterFixture.getMultiMongosConnectionString(com.mongodb.ClusterFixture.getMultiMongosConnectionString) EventMatcher.getReasonString(com.mongodb.client.unified.EventMatcher.getReasonString) BsonString(org.bson.BsonString) ReadConcern(com.mongodb.ReadConcern) UnifiedCrudHelper.asReadConcern(com.mongodb.client.unified.UnifiedCrudHelper.asReadConcern) BsonDocument(org.bson.BsonDocument) UnifiedCrudHelper.asWriteConcern(com.mongodb.client.unified.UnifiedCrudHelper.asWriteConcern) WriteConcern(com.mongodb.WriteConcern) BsonArray(org.bson.BsonArray) ServerApi(com.mongodb.ServerApi) BsonValue(org.bson.BsonValue)

Example 53 with MongoClientSettings

use of com.mongodb.MongoClientSettings 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()));
    }
}
Also used : Document(org.bson.Document) Fixture.getDefaultDatabaseName(com.mongodb.client.Fixture.getDefaultDatabaseName) ClusterFixture.isDiscoverableReplicaSet(com.mongodb.ClusterFixture.isDiscoverableReplicaSet) MongoClientException(com.mongodb.MongoClientException) BsonBoolean(org.bson.BsonBoolean) Assume.assumeFalse(org.junit.Assume.assumeFalse) TimeoutException(java.util.concurrent.TimeoutException) Function(com.mongodb.Function) BsonString(org.bson.BsonString) BsonDocument(org.bson.BsonDocument) Collections.singletonList(java.util.Collections.singletonList) TestCommandListener(com.mongodb.internal.connection.TestCommandListener) Future(java.util.concurrent.Future) ClusterFixture.isSharded(com.mongodb.ClusterFixture.isSharded) Fixture.getMongoClientSettingsBuilder(com.mongodb.client.Fixture.getMongoClientSettingsBuilder) Arrays.asList(java.util.Arrays.asList) BsonArray(org.bson.BsonArray) ClusterFixture.isStandalone(com.mongodb.ClusterFixture.isStandalone) ConnectionCheckedOutEvent(com.mongodb.event.ConnectionCheckedOutEvent) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) ClusterFixture.isServerlessTest(com.mongodb.ClusterFixture.isServerlessTest) MongoException(com.mongodb.MongoException) ClusterFixture.serverVersionLessThan(com.mongodb.ClusterFixture.serverVersionLessThan) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) ConnectionPoolClearedEvent(com.mongodb.event.ConnectionPoolClearedEvent) Executors(java.util.concurrent.Executors) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) ClusterFixture.getServerStatus(com.mongodb.ClusterFixture.getServerStatus) ClusterFixture.serverVersionAtLeast(com.mongodb.ClusterFixture.serverVersionAtLeast) TestConnectionPoolListener(com.mongodb.internal.connection.TestConnectionPoolListener) Assume.assumeTrue(org.junit.Assume.assumeTrue) MongoClientSettings(com.mongodb.MongoClientSettings) BsonInt32(org.bson.BsonInt32) ConnectionCheckOutFailedEvent(com.mongodb.event.ConnectionCheckOutFailedEvent) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Assert.assertEquals(org.junit.Assert.assertEquals) TestConnectionPoolListener(com.mongodb.internal.connection.TestConnectionPoolListener) TestCommandListener(com.mongodb.internal.connection.TestCommandListener) 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)

Example 54 with MongoClientSettings

use of com.mongodb.MongoClientSettings in project mongo-java-driver by mongodb.

the class ServerDiscoveryAndMonitoringProseTests method monitorsSleepAtLeastMinHeartbeatFreqencyMSBetweenChecks.

/**
 * See
 * <a href="https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring-tests.rst#monitors-sleep-at-least-minheartbeatfreqencyms-between-checks">
 * Monitors sleep at least minHeartbeatFreqencyMS between checks</a>.
 */
@Test
@SuppressWarnings("try")
public void monitorsSleepAtLeastMinHeartbeatFreqencyMSBetweenChecks() {
    assumeTrue(serverVersionAtLeast(4, 3));
    assumeFalse(isServerlessTest());
    long defaultMinHeartbeatIntervalMillis = MongoClientSettings.builder().build().getServerSettings().getMinHeartbeatFrequency(MILLISECONDS);
    assertEquals(500, defaultMinHeartbeatIntervalMillis);
    String appName = "SDAMMinHeartbeatFrequencyTest";
    MongoClientSettings clientSettings = getMongoClientSettingsBuilder().applicationName(appName).applyToClusterSettings(ClusterFixture::setDirectConnection).applyToClusterSettings(builder -> builder.serverSelectionTimeout(5000, MILLISECONDS)).applyToServerSettings(builder -> builder.minHeartbeatFrequency(defaultMinHeartbeatIntervalMillis, MILLISECONDS)).build();
    BsonDocument configureFailPoint = new BsonDocument().append("configureFailPoint", new BsonString("failCommand")).append("mode", new BsonDocument().append("times", new BsonInt32(5))).append("data", new BsonDocument().append("failCommands", new BsonArray(asList(new BsonString("hello"), new BsonString("isMaster")))).append("errorCode", new BsonInt32(1234)).append("appName", new BsonString(appName)));
    try (FailPoint ignored = FailPoint.enable(configureFailPoint, clientSettings.getClusterSettings().getHosts().get(0));
        MongoClient client = MongoClients.create(clientSettings)) {
        long startNanos = System.nanoTime();
        client.getDatabase(getDefaultDatabaseName()).runCommand(new BsonDocument("ping", BsonNull.VALUE));
        long durationMillis = NANOSECONDS.toMillis(System.nanoTime() - startNanos);
        String msg = durationMillis + " ms";
        assertTrue(msg, durationMillis >= 2000);
        assertTrue(msg, durationMillis <= 3500);
    }
}
Also used : Document(org.bson.Document) ServerListener(com.mongodb.event.ServerListener) Fixture.getMongoClientSettingsBuilder(com.mongodb.client.Fixture.getMongoClientSettingsBuilder) Collections.singleton(java.util.Collections.singleton) Arrays.asList(java.util.Arrays.asList) BsonArray(org.bson.BsonArray) ClusterFixture.isStandalone(com.mongodb.ClusterFixture.isStandalone) Assert.fail(org.junit.Assert.fail) ServerDescriptionChangedEvent(com.mongodb.event.ServerDescriptionChangedEvent) ServerHeartbeatSucceededEvent(com.mongodb.event.ServerHeartbeatSucceededEvent) ClusterFixture.isServerlessTest(com.mongodb.ClusterFixture.isServerlessTest) BsonNull(org.bson.BsonNull) Collections.synchronizedList(java.util.Collections.synchronizedList) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) ConnectionPoolClearedEvent(com.mongodb.event.ConnectionPoolClearedEvent) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) String.format(java.lang.String.format) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Block(com.mongodb.Block) Assert.assertFalse(org.junit.Assert.assertFalse) Optional(java.util.Optional) ClusterFixture.configureFailPoint(com.mongodb.ClusterFixture.configureFailPoint) Assume.assumeTrue(org.junit.Assume.assumeTrue) MongoClientSettings(com.mongodb.MongoClientSettings) Fixture.getDefaultDatabaseName(com.mongodb.client.Fixture.getDefaultDatabaseName) BsonDocument.parse(org.bson.BsonDocument.parse) NANOSECONDS(java.util.concurrent.TimeUnit.NANOSECONDS) Assume.assumeFalse(org.junit.Assume.assumeFalse) BsonString(org.bson.BsonString) ServerSettings(com.mongodb.connection.ServerSettings) BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ClusterFixture(com.mongodb.ClusterFixture) ServerHeartbeatFailedEvent(com.mongodb.event.ServerHeartbeatFailedEvent) Logger(com.mongodb.diagnostics.logging.Logger) MongoInterruptedException(com.mongodb.MongoInterruptedException) ConnectionPoolListener(com.mongodb.event.ConnectionPoolListener) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Timeout(com.mongodb.internal.Timeout) TimeUnit(java.util.concurrent.TimeUnit) ConnectionPoolReadyEvent(com.mongodb.event.ConnectionPoolReadyEvent) ServerMonitorListener(com.mongodb.event.ServerMonitorListener) Ignore(org.junit.Ignore) Loggers(com.mongodb.diagnostics.logging.Loggers) ClusterFixture.serverVersionAtLeast(com.mongodb.ClusterFixture.serverVersionAtLeast) Nullable(com.mongodb.lang.Nullable) BsonInt32(org.bson.BsonInt32) ClusterFixture.disableFailPoint(com.mongodb.ClusterFixture.disableFailPoint) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Assert.assertEquals(org.junit.Assert.assertEquals) BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) BsonString(org.bson.BsonString) MongoClientSettings(com.mongodb.MongoClientSettings) ClusterFixture.configureFailPoint(com.mongodb.ClusterFixture.configureFailPoint) ClusterFixture.disableFailPoint(com.mongodb.ClusterFixture.disableFailPoint) ClusterFixture.isServerlessTest(com.mongodb.ClusterFixture.isServerlessTest) Test(org.junit.Test)

Example 55 with MongoClientSettings

use of com.mongodb.MongoClientSettings in project mongo-java-driver by mongodb.

the class ClientSideEncryptionSessionTest method setUp.

@Before
public void setUp() throws IOException, URISyntaxException {
    assumeTrue(serverVersionAtLeast(4, 2));
    assumeTrue(isClientSideEncryptionTest());
    assumeFalse(isStandalone());
    /* Step 1: get unencrypted client and recreate keys collection */
    client = getMongoClient();
    MongoDatabase keyvaultDatabase = client.getDatabase("keyvault");
    MongoCollection<BsonDocument> datakeys = keyvaultDatabase.getCollection("datakeys", BsonDocument.class).withWriteConcern(WriteConcern.MAJORITY);
    datakeys.drop();
    datakeys.insertOne(bsonDocumentFromPath("external-key.json"));
    /* Step 2: create encryption objects. */
    Map<String, Map<String, Object>> kmsProviders = new HashMap<>();
    Map<String, Object> localMasterkey = new HashMap<>();
    Map<String, BsonDocument> schemaMap = new HashMap<>();
    byte[] localMasterkeyBytes = Base64.getDecoder().decode("Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBM" + "UN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk");
    localMasterkey.put("key", localMasterkeyBytes);
    kmsProviders.put("local", localMasterkey);
    schemaMap.put(getDefaultDatabaseName() + "." + COLLECTION_NAME, bsonDocumentFromPath("external-schema.json"));
    AutoEncryptionSettings autoEncryptionSettings = AutoEncryptionSettings.builder().keyVaultNamespace("keyvault.datakeys").kmsProviders(kmsProviders).schemaMap(schemaMap).build();
    MongoClientSettings clientSettings = getMongoClientSettingsBuilder().autoEncryptionSettings(autoEncryptionSettings).build();
    clientEncrypted = MongoClients.create(clientSettings);
    CollectionHelper<BsonDocument> collectionHelper = new CollectionHelper<>(new BsonDocumentCodec(), new MongoNamespace(getDefaultDatabaseName(), COLLECTION_NAME));
    collectionHelper.drop();
    collectionHelper.create();
}
Also used : HashMap(java.util.HashMap) BsonString(org.bson.BsonString) MongoClientSettings(com.mongodb.MongoClientSettings) MongoNamespace(com.mongodb.MongoNamespace) BsonDocument(org.bson.BsonDocument) AutoEncryptionSettings(com.mongodb.AutoEncryptionSettings) CollectionHelper(com.mongodb.client.test.CollectionHelper) HashMap(java.util.HashMap) Map(java.util.Map) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) Before(org.junit.Before)

Aggregations

MongoClientSettings (com.mongodb.MongoClientSettings)75 Test (org.junit.jupiter.api.Test)24 BsonString (org.bson.BsonString)21 Map (java.util.Map)20 Test (org.junit.Test)20 HashMap (java.util.HashMap)19 Document (org.bson.Document)19 BsonDocument (org.bson.BsonDocument)18 ServerAddress (com.mongodb.ServerAddress)17 AutoEncryptionSettings (com.mongodb.AutoEncryptionSettings)14 Before (org.junit.Before)14 MongoNamespace (com.mongodb.MongoNamespace)13 ClientEncryptionSettings (com.mongodb.ClientEncryptionSettings)12 ConnectionString (com.mongodb.ConnectionString)12 MongoClientFactoryBean (org.springframework.data.mongodb.core.MongoClientFactoryBean)10 Block (com.mongodb.Block)8 Fixture.getMongoClientSettingsBuilder (com.mongodb.client.Fixture.getMongoClientSettingsBuilder)8 SecureRandom (java.security.SecureRandom)8 TimeUnit (java.util.concurrent.TimeUnit)8 ClusterFixture.isServerlessTest (com.mongodb.ClusterFixture.isServerlessTest)7