use of com.mongodb.MongoClientException in project mongo-java-driver by mongodb.
the class MongoCryptHelper method startProcess.
public static void startProcess(final ProcessBuilder processBuilder) {
try {
processBuilder.redirectErrorStream(true);
processBuilder.redirectOutput(new File(System.getProperty("os.name").startsWith("Windows") ? "NUL" : "/dev/null"));
processBuilder.start();
} catch (Throwable t) {
throw new MongoClientException("Exception starting mongocryptd process. Is `mongocryptd` on the system path?", t);
}
}
use of com.mongodb.MongoClientException in project mongo-java-driver by mongodb.
the class LoadBalancedClusterTest method shouldFailSelectServerWhenThereIsSRVMisconfiguration.
@Test
public void shouldFailSelectServerWhenThereIsSRVMisconfiguration() {
// given
String srvHostName = "foo.bar.com";
ClusterSettings clusterSettings = ClusterSettings.builder().mode(ClusterConnectionMode.LOAD_BALANCED).srvHost(srvHostName).build();
ClusterableServerFactory serverFactory = mockServerFactory();
DnsSrvRecordMonitorFactory dnsSrvRecordMonitorFactory = mock(DnsSrvRecordMonitorFactory.class);
when(dnsSrvRecordMonitorFactory.create(eq(srvHostName), eq(clusterSettings.getSrvServiceName()), any())).thenAnswer(invocation -> new TestDnsSrvRecordMonitor(invocation.getArgument(2)).hosts(Arrays.asList(new ServerAddress("host1"), new ServerAddress("host2"))));
cluster = new LoadBalancedCluster(new ClusterId(), clusterSettings, serverFactory, dnsSrvRecordMonitorFactory);
MongoClientException exception = assertThrows(MongoClientException.class, () -> cluster.selectServer(mock(ServerSelector.class)));
assertEquals("In load balancing mode, the host must resolve to a single SRV record, but instead it resolved to multiple hosts", exception.getMessage());
}
use of com.mongodb.MongoClientException in project mongo-java-driver by mongodb.
the class EventHelperTest method testClusterDescriptionEquivalence.
@Test
public void testClusterDescriptionEquivalence() {
assertTrue(wouldDescriptionsGenerateEquivalentEvents(new ClusterDescription(SINGLE, STANDALONE, singletonList(createBuilder().build())), new ClusterDescription(SINGLE, STANDALONE, singletonList(createBuilder().build()))));
assertTrue(wouldDescriptionsGenerateEquivalentEvents(new ClusterDescription(MULTIPLE, REPLICA_SET, asList(createBuilder("localhost:27017").build(), createBuilder("localhost:27018").build())), new ClusterDescription(MULTIPLE, REPLICA_SET, asList(createBuilder("localhost:27017").build(), createBuilder("localhost:27018").build()))));
assertTrue(wouldDescriptionsGenerateEquivalentEvents(new ClusterDescription(MULTIPLE, REPLICA_SET, asList(createBuilder("localhost:27017").build(), createBuilder("localhost:27018").build())), new ClusterDescription(MULTIPLE, REPLICA_SET, asList(createBuilder("localhost:27018").build(), createBuilder("localhost:27017").build()))));
assertFalse(wouldDescriptionsGenerateEquivalentEvents(new ClusterDescription(SINGLE, STANDALONE, singletonList(createBuilder().build())), new ClusterDescription(SINGLE, STANDALONE, singletonList(createBuilder().maxWireVersion(4).build()))));
assertFalse(wouldDescriptionsGenerateEquivalentEvents(new ClusterDescription(MULTIPLE, REPLICA_SET, asList(createBuilder("localhost:27017").build(), createBuilder("localhost:27018").build())), new ClusterDescription(MULTIPLE, REPLICA_SET, singletonList(createBuilder("localhost:27017").build()))));
assertFalse(wouldDescriptionsGenerateEquivalentEvents(new ClusterDescription(MULTIPLE, REPLICA_SET, asList(createBuilder("localhost:27017").build(), createBuilder("localhost:27018").build())), new ClusterDescription(MULTIPLE, REPLICA_SET, asList(createBuilder("localhost:27017").build(), createBuilder("localhost:27018").maxWireVersion(4).build()))));
assertFalse(wouldDescriptionsGenerateEquivalentEvents(new ClusterDescription(MULTIPLE, REPLICA_SET, asList(createBuilder("localhost:27017").build(), createBuilder("localhost:27018").build())), new ClusterDescription(MULTIPLE, REPLICA_SET, asList(createBuilder("localhost:27018").build(), createBuilder("localhost:27017").maxWireVersion(4).build()))));
assertTrue(wouldDescriptionsGenerateEquivalentEvents(new ClusterDescription(SINGLE, STANDALONE, new MongoException("msg1"), emptyList(), null, null), new ClusterDescription(SINGLE, STANDALONE, new MongoException("msg1"), emptyList(), null, null)));
assertFalse(wouldDescriptionsGenerateEquivalentEvents(new ClusterDescription(SINGLE, STANDALONE, new MongoException("msg1"), emptyList(), null, null), new ClusterDescription(SINGLE, STANDALONE, null, emptyList(), null, null)));
assertFalse(wouldDescriptionsGenerateEquivalentEvents(new ClusterDescription(SINGLE, STANDALONE, new MongoException("msg1"), emptyList(), null, null), new ClusterDescription(SINGLE, STANDALONE, new MongoException("msg2"), emptyList(), null, null)));
assertFalse(wouldDescriptionsGenerateEquivalentEvents(new ClusterDescription(SINGLE, STANDALONE, new MongoException("msg1"), emptyList(), null, null), new ClusterDescription(SINGLE, STANDALONE, new MongoClientException("msg1"), emptyList(), null, null)));
}
use of com.mongodb.MongoClientException in project mongo-java-driver by mongodb.
the class AbstractClientSideEncryptionKmsTlsTest method testInvalidKmsCertificate.
@Test
public void testInvalidKmsCertificate() {
assumeTrue(System.getProperties().containsKey(SYSTEM_PROPERTY_KEY));
TlsErrorType expectedKmsTlsError = TlsErrorType.fromSystemPropertyValue(System.getProperty(SYSTEM_PROPERTY_KEY));
ClientEncryptionSettings clientEncryptionSettings = ClientEncryptionSettings.builder().keyVaultMongoClientSettings(getMongoClientSettings()).keyVaultNamespace("keyvault.datakeys").kmsProviders(new HashMap<String, Map<String, Object>>() {
{
put("aws", new HashMap<String, Object>() {
{
put("accessKeyId", "fakeAccessKeyId");
put("secretAccessKey", "fakeSecretAccessKey");
}
});
}
}).build();
try (ClientEncryption clientEncryption = getClientEncryption(clientEncryptionSettings)) {
clientEncryption.createDataKey("aws", new DataKeyOptions().masterKey(BsonDocument.parse("{" + "region: \"us-east-1\", " + "key: \"arn:aws:kms:us-east-1:579766882180:key/89fcc2c4-08b0-4bd9-9f25-e30687b580d0\"," + "endpoint: \"mongodb://127.0.0.1:8000\"}")));
fail();
} catch (MongoClientException e) {
assertNotNull(expectedKmsTlsError.getCauseOfExpectedClass(e));
assertTrue(expectedKmsTlsError.causeContainsExpectedMessage(e));
}
}
use of com.mongodb.MongoClientException in project mongo-java-driver by mongodb.
the class CryptConnection method command.
@Override
public <T> T command(final String database, final BsonDocument command, final FieldNameValidator commandFieldNameValidator, final ReadPreference readPreference, final Decoder<T> commandResultDecoder, final SessionContext sessionContext, @Nullable final ServerApi serverApi, final RequestContext requestContext, final boolean responseExpected, @Nullable final SplittablePayload payload, @Nullable final FieldNameValidator payloadFieldNameValidator) {
if (serverIsLessThanVersionFourDotTwo(wrapped.getDescription())) {
throw new MongoClientException("Auto-encryption requires a minimum MongoDB version of 4.2");
}
BasicOutputBuffer bsonOutput = new BasicOutputBuffer();
BsonBinaryWriter bsonBinaryWriter = new BsonBinaryWriter(new BsonWriterSettings(), new BsonBinaryWriterSettings(getDescription().getMaxDocumentSize()), bsonOutput, getFieldNameValidator(payload, commandFieldNameValidator, payloadFieldNameValidator));
BsonWriter writer = payload == null ? bsonBinaryWriter : new SplittablePayloadBsonWriter(bsonBinaryWriter, bsonOutput, createSplittablePayloadMessageSettings(), payload, MAX_SPLITTABLE_DOCUMENT_SIZE);
getEncoder(command).encode(writer, command, EncoderContext.builder().build());
RawBsonDocument encryptedCommand = crypt.encrypt(database, new RawBsonDocument(bsonOutput.getInternalBuffer(), 0, bsonOutput.getSize()));
RawBsonDocument encryptedResponse = wrapped.command(database, encryptedCommand, commandFieldNameValidator, readPreference, new RawBsonDocumentCodec(), sessionContext, serverApi, requestContext, responseExpected, null, null);
RawBsonDocument decryptedResponse = crypt.decrypt(encryptedResponse);
BsonBinaryReader reader = new BsonBinaryReader(decryptedResponse.getByteBuffer().asNIO());
return commandResultDecoder.decode(reader, DecoderContext.builder().build());
}
Aggregations