Search in sources :

Example 1 with MongoClientException

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);
    }
}
Also used : MongoClientException(com.mongodb.MongoClientException) File(java.io.File)

Example 2 with MongoClientException

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());
}
Also used : ClusterSettings(com.mongodb.connection.ClusterSettings) MongoClientException(com.mongodb.MongoClientException) ClusterId(com.mongodb.connection.ClusterId) ServerAddress(com.mongodb.ServerAddress) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 3 with MongoClientException

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)));
}
Also used : MongoException(com.mongodb.MongoException) MongoClientException(com.mongodb.MongoClientException) ClusterDescription(com.mongodb.connection.ClusterDescription) Test(org.junit.Test)

Example 4 with MongoClientException

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));
    }
}
Also used : ClientEncryptionSettings(com.mongodb.ClientEncryptionSettings) MongoClientException(com.mongodb.MongoClientException) HashMap(java.util.HashMap) ClientEncryption(com.mongodb.client.vault.ClientEncryption) DataKeyOptions(com.mongodb.client.model.vault.DataKeyOptions) Test(org.junit.jupiter.api.Test)

Example 5 with MongoClientException

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());
}
Also used : MongoClientException(com.mongodb.MongoClientException) SplittablePayloadBsonWriter(com.mongodb.internal.connection.SplittablePayloadBsonWriter) RawBsonDocumentCodec(org.bson.codecs.RawBsonDocumentCodec) BsonBinaryReader(org.bson.BsonBinaryReader) SplittablePayloadBsonWriter(com.mongodb.internal.connection.SplittablePayloadBsonWriter) BsonWriter(org.bson.BsonWriter) RawBsonDocument(org.bson.RawBsonDocument) BsonBinaryWriter(org.bson.BsonBinaryWriter) BsonBinaryWriterSettings(org.bson.BsonBinaryWriterSettings) BsonWriterSettings(org.bson.BsonWriterSettings) BasicOutputBuffer(org.bson.io.BasicOutputBuffer)

Aggregations

MongoClientException (com.mongodb.MongoClientException)19 MongoException (com.mongodb.MongoException)4 BsonDocument (org.bson.BsonDocument)4 MongoInternalException (com.mongodb.MongoInternalException)3 ClusterSettings (com.mongodb.connection.ClusterSettings)3 MongoQueryException (com.mongodb.MongoQueryException)2 MongoSocketException (com.mongodb.MongoSocketException)2 ReadPreference (com.mongodb.ReadPreference)2 RequestContext (com.mongodb.RequestContext)2 ServerAddress (com.mongodb.ServerAddress)2 WriteConcern (com.mongodb.WriteConcern)2 ClusterId (com.mongodb.connection.ClusterId)2 ConnectionDescription (com.mongodb.connection.ConnectionDescription)2 Nullable (com.mongodb.lang.Nullable)2 MongoOperationPublisher.sinkToCallback (com.mongodb.reactivestreams.client.internal.MongoOperationPublisher.sinkToCallback)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 BsonString (org.bson.BsonString)2 BsonValue (org.bson.BsonValue)2 Test (org.junit.jupiter.api.Test)2