Search in sources :

Example 56 with MongoClientSettings

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

the class ClientSideEncryptionViewAreProhibitedTest method setUp.

@Before
public void setUp() {
    assumeTrue(serverVersionAtLeast(4, 2));
    assumeTrue("Encryption test with external keyVault is disabled", isClientSideEncryptionTest());
    MongoClient client = getMongoClient();
    MongoDatabase db = client.getDatabase("db");
    db.getCollection("view").drop();
    db.createView("view", "coll", Collections.<BsonDocument>emptyList());
    Map<String, Map<String, Object>> kmsProviders = new HashMap<String, Map<String, Object>>();
    Map<String, Object> localMasterkey = new HashMap<String, Object>();
    byte[] localMasterkeyBytes = Base64.getDecoder().decode("Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBM" + "UN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk");
    localMasterkey.put("key", localMasterkeyBytes);
    kmsProviders.put("local", localMasterkey);
    AutoEncryptionSettings.Builder autoEncryptionSettingsBuilder = AutoEncryptionSettings.builder().keyVaultNamespace("keyvault.datakeys").kmsProviders(kmsProviders);
    AutoEncryptionSettings autoEncryptionSettings = autoEncryptionSettingsBuilder.build();
    MongoClientSettings.Builder clientSettingsBuilder = getMongoClientSettingsBuilder();
    MongoClientSettings clientSettings = clientSettingsBuilder.autoEncryptionSettings(autoEncryptionSettings).build();
    clientEncrypted = MongoClients.create(clientSettings);
}
Also used : HashMap(java.util.HashMap) BsonString(org.bson.BsonString) MongoClientSettings(com.mongodb.MongoClientSettings) Fixture.getMongoClient(com.mongodb.client.Fixture.getMongoClient) AutoEncryptionSettings(com.mongodb.AutoEncryptionSettings) HashMap(java.util.HashMap) Map(java.util.Map) Before(org.junit.Before)

Example 57 with MongoClientSettings

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

the class ConnectionsSurvivePrimaryStepDownProseTest method setUp.

@Before
public void setUp() {
    assumeTrue(isDiscoverableReplicaSet() && serverVersionAtLeast(4, 0));
    connectionPoolListener = new TestConnectionPoolListener();
    MongoClientSettings settings = MongoClientSettings.builder(getMongoClientSettings()).retryWrites(false).applyToConnectionPoolSettings(new Block<ConnectionPoolSettings.Builder>() {

        @Override
        public void apply(final ConnectionPoolSettings.Builder builder) {
            builder.addConnectionPoolListener(connectionPoolListener);
        }
    }).build();
    collectionHelper = new CollectionHelper<Document>(new DocumentCodec(), new MongoNamespace(getDefaultDatabaseName(), COLLECTION_NAME));
    client = MongoClients.create(settings);
    MongoDatabase database = client.getDatabase(getDefaultDatabaseName());
    collection = client.getDatabase(getDefaultDatabaseName()).getCollection(COLLECTION_NAME);
    collection.withWriteConcern(WriteConcern.MAJORITY).drop();
    database.withWriteConcern(WriteConcern.MAJORITY).createCollection(COLLECTION_NAME);
}
Also used : TestConnectionPoolListener(com.mongodb.internal.connection.TestConnectionPoolListener) DocumentCodec(org.bson.codecs.DocumentCodec) Block(com.mongodb.Block) Fixture.getMongoClientSettings(com.mongodb.client.Fixture.getMongoClientSettings) MongoClientSettings(com.mongodb.MongoClientSettings) Document(org.bson.Document) MongoNamespace(com.mongodb.MongoNamespace) ConnectionPoolSettings(com.mongodb.connection.ConnectionPoolSettings) Before(org.junit.Before)

Example 58 with MongoClientSettings

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

the class InitialDnsSeedlistDiscoveryTest method shouldResolveTxtRecord.

@Test
public void shouldResolveTxtRecord() throws InterruptedException {
    if (isError) {
        MongoClient client = null;
        try {
            final AtomicReference<MongoException> exceptionReference = new AtomicReference<MongoException>();
            final CountDownLatch latch = new CountDownLatch(1);
            ConnectionString connectionString;
            MongoClientSettings settings;
            try {
                connectionString = new ConnectionString(uri);
                final SslSettings sslSettings = getSslSettings(connectionString);
                assumeTrue("SSL settings don't match", getSslSettings().isEnabled() == sslSettings.isEnabled());
                settings = MongoClientSettings.builder().applyConnectionString(connectionString).applyToSslSettings(new Block<SslSettings.Builder>() {

                    @Override
                    public void apply(final SslSettings.Builder builder) {
                        builder.applySettings(sslSettings);
                        builder.invalidHostNameAllowed(true);
                    }
                }).applyToClusterSettings(new Block<ClusterSettings.Builder>() {

                    @Override
                    public void apply(final ClusterSettings.Builder builder) {
                        builder.serverSelectionTimeout(5, TimeUnit.SECONDS);
                        builder.addClusterListener(new ClusterListener() {

                            @Override
                            public void clusterDescriptionChanged(final ClusterDescriptionChangedEvent event) {
                                if (event.getNewDescription().getSrvResolutionException() != null) {
                                    exceptionReference.set(event.getNewDescription().getSrvResolutionException());
                                    latch.countDown();
                                }
                            }
                        });
                    }
                }).build();
            } catch (MongoClientException | IllegalArgumentException e) {
                // all good
                return;
            }
            client = createMongoClient(settings);
            // Instead we just try to execute an operation and assert that it throws
            if (settings.getClusterSettings().getMode() == ClusterConnectionMode.LOAD_BALANCED) {
                try {
                    client.getDatabase("admin").runCommand(new Document("ping", 1));
                } catch (MongoClientException e) {
                // all good
                }
            } else {
                if (!latch.await(5, TimeUnit.SECONDS)) {
                    fail("Failed to capture SRV resolution exception");
                }
                try {
                    throw exceptionReference.get();
                } catch (MongoClientException e) {
                // all good
                }
            }
        } finally {
            if (client != null) {
                client.close();
            }
        }
    } else {
        ConnectionString connectionString = new ConnectionString(this.uri);
        for (Map.Entry<String, BsonValue> entry : options.entrySet()) {
            if (entry.getKey().equals("replicaSet")) {
                assertEquals(entry.getValue().asString().getValue(), connectionString.getRequiredReplicaSetName());
            } else if (entry.getKey().equals("ssl")) {
                assertEquals(entry.getValue().asBoolean().getValue(), connectionString.getSslEnabled());
            } else if (entry.getKey().equals("authSource")) {
                // ignoring authSource for now, because without at least a userName also in the connection string,
                // the authSource is ignored.  If the test gets this far, at least we know that a TXT record
                // containing in authSource doesn't blow up.  We just don't test that it's actually used.
                assertTrue(true);
            } else if (entry.getKey().equals("directConnection")) {
                assertEquals(entry.getValue().asBoolean().getValue(), connectionString.isDirectConnection());
            } else if (entry.getKey().equals("loadBalanced")) {
                assertEquals(entry.getValue().asBoolean().getValue(), connectionString.isLoadBalanced());
            } else if (entry.getKey().equals("srvMaxHosts")) {
                assertEquals(Integer.valueOf(entry.getValue().asInt32().getValue()), connectionString.getSrvMaxHosts());
            } else if (entry.getKey().equals("srvServiceName")) {
                assertEquals(entry.getValue().asString().getValue(), connectionString.getSrvServiceName());
            } else {
                throw new UnsupportedOperationException("No support configured yet for " + entry.getKey());
            }
        }
    }
}
Also used : ClusterSettings(com.mongodb.connection.ClusterSettings) ConnectionString(com.mongodb.ConnectionString) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) MongoException(com.mongodb.MongoException) MongoClientException(com.mongodb.MongoClientException) AtomicReference(java.util.concurrent.atomic.AtomicReference) MongoClientSettings(com.mongodb.MongoClientSettings) CountDownLatch(java.util.concurrent.CountDownLatch) ClusterFixture.getSslSettings(com.mongodb.ClusterFixture.getSslSettings) SslSettings(com.mongodb.connection.SslSettings) ClusterDescriptionChangedEvent(com.mongodb.event.ClusterDescriptionChangedEvent) ClusterListener(com.mongodb.event.ClusterListener) Block(com.mongodb.Block) ConnectionString(com.mongodb.ConnectionString) Map(java.util.Map) BsonValue(org.bson.BsonValue) ClusterFixture.isServerlessTest(com.mongodb.ClusterFixture.isServerlessTest) Test(org.junit.Test)

Example 59 with MongoClientSettings

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

the class ClientSideEncryptionBypassAutoEncryptionTest method setUp.

@Before
public void setUp() throws Throwable {
    assumeTrue(serverVersionAtLeast(4, 2));
    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);
                }
            });
        }
    };
    MongoNamespace keyVaultNamespace = new MongoNamespace(Fixture.getDefaultDatabaseName(), "testKeyVault");
    Fixture.dropDatabase(Fixture.getDefaultDatabaseName());
    ClientEncryptionSettings clientEncryptionSettings = ClientEncryptionSettings.builder().keyVaultMongoClientSettings(Fixture.getMongoClientSettings()).keyVaultNamespace(keyVaultNamespace.getFullName()).kmsProviders(kmsProviders).build();
    clientEncryption = ClientEncryptions.create(clientEncryptionSettings);
    AutoEncryptionSettings autoEncryptionSettings = AutoEncryptionSettings.builder().keyVaultNamespace(keyVaultNamespace.getFullName()).kmsProviders(kmsProviders).bypassAutoEncryption(true).build();
    MongoClientSettings clientSettings = Fixture.getMongoClientSettingsBuilder().autoEncryptionSettings(autoEncryptionSettings).build();
    clientEncrypted = MongoClients.create(clientSettings);
}
Also used : ClientEncryptionSettings(com.mongodb.ClientEncryptionSettings) HashMap(java.util.HashMap) SecureRandom(java.security.SecureRandom) AutoEncryptionSettings(com.mongodb.AutoEncryptionSettings) BsonString(org.bson.BsonString) MongoClientSettings(com.mongodb.MongoClientSettings) MongoNamespace(com.mongodb.MongoNamespace) HashMap(java.util.HashMap) Map(java.util.Map) Before(org.junit.Before)

Example 60 with MongoClientSettings

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

the class ClientSideEncryptionViewAreProhibitedTest method setUp.

@Before
public void setUp() {
    assumeTrue(serverVersionAtLeast(4, 2));
    assumeTrue("Encryption test with external keyVault is disabled", isClientSideEncryptionTest());
    MongoClient client = getMongoClient();
    MongoDatabase db = client.getDatabase("db");
    Mono.from(db.getCollection("view").drop()).block(TIMEOUT_DURATION);
    Mono.from(db.createView("view", "coll", Collections.<BsonDocument>emptyList())).block(TIMEOUT_DURATION);
    Map<String, Map<String, Object>> kmsProviders = new HashMap<String, Map<String, Object>>();
    Map<String, Object> localMasterkey = new HashMap<String, Object>();
    byte[] localMasterkeyBytes = Base64.getDecoder().decode("Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBM" + "UN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk");
    localMasterkey.put("key", localMasterkeyBytes);
    kmsProviders.put("local", localMasterkey);
    AutoEncryptionSettings.Builder autoEncryptionSettingsBuilder = AutoEncryptionSettings.builder().keyVaultNamespace("admin.datakeys").kmsProviders(kmsProviders);
    AutoEncryptionSettings autoEncryptionSettings = autoEncryptionSettingsBuilder.build();
    MongoClientSettings.Builder clientSettingsBuilder = getMongoClientBuilderFromConnectionString();
    MongoClientSettings clientSettings = clientSettingsBuilder.autoEncryptionSettings(autoEncryptionSettings).build();
    clientEncrypted = MongoClients.create(clientSettings);
}
Also used : HashMap(java.util.HashMap) BsonString(org.bson.BsonString) Fixture.getMongoClientBuilderFromConnectionString(com.mongodb.reactivestreams.client.Fixture.getMongoClientBuilderFromConnectionString) MongoClientSettings(com.mongodb.MongoClientSettings) Fixture.getMongoClient(com.mongodb.reactivestreams.client.Fixture.getMongoClient) AutoEncryptionSettings(com.mongodb.AutoEncryptionSettings) HashMap(java.util.HashMap) Map(java.util.Map) 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