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(builder -> builder.addConnectionPoolListener(connectionPoolListener)).build();
collectionHelper = new CollectionHelper<>(new DocumentCodec(), new MongoNamespace(getDefaultDatabaseName(), COLLECTION_NAME));
client = MongoClients.create(settings);
MongoDatabase database = client.getDatabase(getDefaultDatabaseName());
collection = client.getDatabase(getDefaultDatabaseName()).getCollection(COLLECTION_NAME);
Mono.from(collection.withWriteConcern(WriteConcern.MAJORITY).drop()).block(TIMEOUT_DURATION);
Mono.from(database.withWriteConcern(WriteConcern.MAJORITY).createCollection(COLLECTION_NAME)).block(TIMEOUT_DURATION);
}
use of com.mongodb.MongoClientSettings in project mongo-java-driver by mongodb.
the class ClientSideEncryptionSessionTest method setUp.
@Before
public void setUp() throws Throwable {
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);
Mono.from(dataKeys.drop()).block(TIMEOUT_DURATION);
Mono.from(dataKeys.insertOne(bsonDocumentFromPath("external-key.json"))).block(TIMEOUT_DURATION);
/* 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"));
MongoClientSettings clientSettings = getMongoClientBuilderFromConnectionString().autoEncryptionSettings(AutoEncryptionSettings.builder().keyVaultNamespace("keyvault.datakeys").kmsProviders(kmsProviders).schemaMap(schemaMap).build()).build();
clientEncrypted = MongoClients.create(clientSettings);
CollectionHelper<BsonDocument> collectionHelper = new CollectionHelper<>(new BsonDocumentCodec(), new MongoNamespace(getDefaultDatabaseName(), COLLECTION_NAME));
collectionHelper.drop();
collectionHelper.create();
}
use of com.mongodb.MongoClientSettings in project mongo-java-driver by mongodb.
the class Crypts method createCrypt.
public static Crypt createCrypt(final MongoClientImpl client, final AutoEncryptionSettings options) {
MongoClient internalClient = null;
MongoClientSettings keyVaultMongoClientSettings = options.getKeyVaultMongoClientSettings();
if (keyVaultMongoClientSettings == null || !options.isBypassAutoEncryption()) {
MongoClientSettings settings = MongoClientSettings.builder(client.getSettings()).applyToConnectionPoolSettings(builder -> builder.minSize(0)).autoEncryptionSettings(null).build();
internalClient = MongoClients.create(settings);
}
MongoClient collectionInfoRetrieverClient = internalClient;
MongoClient keyVaultClient = keyVaultMongoClientSettings == null ? internalClient : MongoClients.create(keyVaultMongoClientSettings);
return new Crypt(MongoCrypts.create(createMongoCryptOptions(options.getKmsProviders(), options.getSchemaMap())), options.isBypassAutoEncryption() ? null : new CollectionInfoRetriever(collectionInfoRetrieverClient), new CommandMarker(options.isBypassAutoEncryption(), options.getExtraOptions()), new KeyRetriever(keyVaultClient, new MongoNamespace(options.getKeyVaultNamespace())), createKeyManagementService(options.getKmsProviderSslContextMap()), options.isBypassAutoEncryption(), internalClient);
}
use of com.mongodb.MongoClientSettings in project mongo-java-driver by mongodb.
the class AbstractRetryableReadsTest method setUp.
@Before
public void setUp() {
assumeFalse(skipTest);
assumeTrue("Skipping test: " + definition.getString("skipReason", new BsonString("")).getValue(), !definition.containsKey("skipReason"));
assumeFalse("Skipping count tests", filename.startsWith("count.") || filename.startsWith("count-"));
assumeFalse("Skipping list index names tests", filename.startsWith("listIndexNames"));
collectionHelper = new CollectionHelper<Document>(new DocumentCodec(), new MongoNamespace(databaseName, collectionName));
final BsonDocument clientOptions = definition.getDocument("clientOptions", new BsonDocument());
ConnectionString connectionString = getConnectionString();
useMultipleMongoses = definition.getBoolean("useMultipleMongoses", BsonBoolean.FALSE).getValue();
if (useMultipleMongoses) {
assumeTrue(isSharded());
connectionString = getMultiMongosConnectionString();
assumeTrue("The system property org.mongodb.test.multi.mongos.uri is not set.", connectionString != null);
}
MongoClientSettings settings = getMongoClientSettingsBuilder().applyConnectionString(connectionString).addCommandListener(commandListener).applyToSocketSettings(new Block<SocketSettings.Builder>() {
@Override
public void apply(final SocketSettings.Builder builder) {
builder.readTimeout(5, TimeUnit.SECONDS);
}
}).applyToServerSettings(new Block<ServerSettings.Builder>() {
@Override
public void apply(final ServerSettings.Builder builder) {
builder.heartbeatFrequency(5, TimeUnit.MILLISECONDS);
}
}).writeConcern(getWriteConcern(clientOptions)).readConcern(getReadConcern(clientOptions)).readPreference(getReadPreference(clientOptions)).retryWrites(clientOptions.getBoolean("retryWrites", BsonBoolean.FALSE).getValue()).retryReads(clientOptions.getBoolean("retryReads", BsonBoolean.TRUE).getValue()).build();
mongoClient = createMongoClient(settings);
if (data != null) {
List<BsonDocument> documents = new ArrayList<BsonDocument>();
for (BsonValue document : data) {
documents.add(document.asDocument());
}
collectionHelper.drop();
if (documents.size() > 0) {
collectionHelper.insertDocuments(documents);
}
}
MongoDatabase database = mongoClient.getDatabase(databaseName);
if (gridFSBucketName != null) {
setupGridFSBuckets(database);
commandListener.reset();
}
collection = database.getCollection(collectionName, BsonDocument.class);
helper = new JsonPoweredCrudTestHelper(description, database, collection, gridFSBucket, mongoClient);
if (definition.containsKey("failPoint")) {
collectionHelper.runAdminCommand(definition.getDocument("failPoint"));
}
}
use of com.mongodb.MongoClientSettings in project mongo-java-driver by mongodb.
the class Crypts method createCrypt.
public static Crypt createCrypt(final MongoClientImpl client, final AutoEncryptionSettings options) {
MongoClient internalClient = null;
MongoClientSettings keyVaultMongoClientSettings = options.getKeyVaultMongoClientSettings();
if (keyVaultMongoClientSettings == null || !options.isBypassAutoEncryption()) {
MongoClientSettings settings = MongoClientSettings.builder(client.getSettings()).applyToConnectionPoolSettings(builder -> builder.minSize(0)).autoEncryptionSettings(null).build();
internalClient = MongoClients.create(settings);
}
MongoClient collectionInfoRetrieverClient = internalClient;
MongoClient keyVaultClient = keyVaultMongoClientSettings == null ? internalClient : MongoClients.create(keyVaultMongoClientSettings);
return new Crypt(MongoCrypts.create(createMongoCryptOptions(options.getKmsProviders(), options.getSchemaMap())), options.isBypassAutoEncryption() ? null : new CollectionInfoRetriever(collectionInfoRetrieverClient), new CommandMarker(options.isBypassAutoEncryption(), options.getExtraOptions()), new KeyRetriever(keyVaultClient, new MongoNamespace(options.getKeyVaultNamespace())), createKeyManagementService(options.getKmsProviderSslContextMap()), options.isBypassAutoEncryption(), internalClient);
}
Aggregations