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);
}
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);
}
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());
}
}
}
}
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);
}
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);
}
Aggregations