Search in sources :

Example 1 with MongoCryptContext

use of com.mongodb.crypt.capi.MongoCryptContext in project mongo-java-driver by mongodb.

the class Crypt method encryptExplicitly.

/**
 * Encrypt the given value with the given options
 *
 * @param value the value to encrypt
 * @param options the options
 * @return the encrypted value
 */
BsonBinary encryptExplicitly(final BsonValue value, final EncryptOptions options) {
    notNull("value", value);
    notNull("options", options);
    try {
        MongoExplicitEncryptOptions.Builder encryptOptionsBuilder = MongoExplicitEncryptOptions.builder().algorithm(options.getAlgorithm());
        if (options.getKeyId() != null) {
            encryptOptionsBuilder.keyId(options.getKeyId());
        }
        if (options.getKeyAltName() != null) {
            encryptOptionsBuilder.keyAltName(options.getKeyAltName());
        }
        MongoCryptContext encryptionContext = mongoCrypt.createExplicitEncryptionContext(new BsonDocument("v", value), encryptOptionsBuilder.build());
        try {
            return executeStateMachine(encryptionContext, null).getBinary("v");
        } finally {
            encryptionContext.close();
        }
    } catch (MongoCryptException e) {
        throw wrapInClientException(e);
    }
}
Also used : MongoExplicitEncryptOptions(com.mongodb.crypt.capi.MongoExplicitEncryptOptions) MongoCryptException(com.mongodb.crypt.capi.MongoCryptException) RawBsonDocument(org.bson.RawBsonDocument) BsonDocument(org.bson.BsonDocument) MongoCryptContext(com.mongodb.crypt.capi.MongoCryptContext)

Example 2 with MongoCryptContext

use of com.mongodb.crypt.capi.MongoCryptContext in project mongo-java-driver by mongodb.

the class Crypt method createDataKey.

/**
 * Create a data key.
 *
 * @param kmsProvider the KMS provider to create the data key for
 * @param options     the data key options
 * @return the document representing the data key to be added to the key vault
 */
BsonDocument createDataKey(final String kmsProvider, final DataKeyOptions options) {
    notNull("kmsProvider", kmsProvider);
    notNull("options", options);
    try {
        MongoCryptContext dataKeyCreationContext = mongoCrypt.createDataKeyContext(kmsProvider, MongoDataKeyOptions.builder().keyAltNames(options.getKeyAltNames()).masterKey(options.getMasterKey()).build());
        try {
            return executeStateMachine(dataKeyCreationContext, null);
        } finally {
            dataKeyCreationContext.close();
        }
    } catch (MongoCryptException e) {
        throw wrapInClientException(e);
    }
}
Also used : MongoCryptException(com.mongodb.crypt.capi.MongoCryptException) MongoCryptContext(com.mongodb.crypt.capi.MongoCryptContext)

Example 3 with MongoCryptContext

use of com.mongodb.crypt.capi.MongoCryptContext in project mongo-java-driver by mongodb.

the class Crypt method encrypt.

/**
 * Encrypt the given command
 *
 * @param databaseName the namespace
 * @param command   the unencrypted command
 * @return the encrypted command
 */
public RawBsonDocument encrypt(final String databaseName, final RawBsonDocument command) {
    notNull("databaseName", databaseName);
    notNull("command", command);
    if (bypassAutoEncryption) {
        return command;
    }
    try {
        MongoCryptContext encryptionContext = mongoCrypt.createEncryptionContext(databaseName, command);
        try {
            return executeStateMachine(encryptionContext, databaseName);
        } finally {
            encryptionContext.close();
        }
    } catch (MongoCryptException e) {
        throw wrapInClientException(e);
    }
}
Also used : MongoCryptException(com.mongodb.crypt.capi.MongoCryptException) MongoCryptContext(com.mongodb.crypt.capi.MongoCryptContext)

Example 4 with MongoCryptContext

use of com.mongodb.crypt.capi.MongoCryptContext in project mongo-java-driver by mongodb.

the class Crypt method fetchKeys.

private void fetchKeys(final MongoCryptContext cryptContext, @Nullable final String databaseName, final MonoSink<RawBsonDocument> sink) {
    keyRetriever.find(cryptContext.getMongoOperation()).doOnSuccess(results -> {
        for (BsonDocument result : results) {
            cryptContext.addMongoOperationResult(result);
        }
        cryptContext.completeMongoOperation();
        executeStateMachineWithSink(cryptContext, databaseName, sink);
    }).doOnError(t -> sink.error(MongoException.fromThrowableNonNull(t))).subscribe();
}
Also used : MongoInternalException(com.mongodb.MongoInternalException) MongoClientException(com.mongodb.MongoClientException) EncryptOptions(com.mongodb.client.model.vault.EncryptOptions) MonoSink(reactor.core.publisher.MonoSink) MongoCryptException(com.mongodb.crypt.capi.MongoCryptException) Supplier(java.util.function.Supplier) DataKeyOptions(com.mongodb.client.model.vault.DataKeyOptions) MongoDataKeyOptions(com.mongodb.crypt.capi.MongoDataKeyOptions) MongoClient(com.mongodb.reactivestreams.client.MongoClient) BsonDocument(org.bson.BsonDocument) BsonValue(org.bson.BsonValue) MongoExplicitEncryptOptions(com.mongodb.crypt.capi.MongoExplicitEncryptOptions) MongoCryptContext(com.mongodb.crypt.capi.MongoCryptContext) MongoCrypt(com.mongodb.crypt.capi.MongoCrypt) Logger(com.mongodb.diagnostics.logging.Logger) MongoException(com.mongodb.MongoException) RawBsonDocument(org.bson.RawBsonDocument) MongoKeyDecryptor(com.mongodb.crypt.capi.MongoKeyDecryptor) State(com.mongodb.crypt.capi.MongoCryptContext.State) Mono(reactor.core.publisher.Mono) String.format(java.lang.String.format) Assertions.notNull(com.mongodb.assertions.Assertions.notNull) Loggers(com.mongodb.diagnostics.logging.Loggers) Closeable(java.io.Closeable) Nullable(com.mongodb.lang.Nullable) BsonBinary(org.bson.BsonBinary) BsonDocument(org.bson.BsonDocument) RawBsonDocument(org.bson.RawBsonDocument)

Aggregations

MongoCryptContext (com.mongodb.crypt.capi.MongoCryptContext)4 MongoCryptException (com.mongodb.crypt.capi.MongoCryptException)4 MongoExplicitEncryptOptions (com.mongodb.crypt.capi.MongoExplicitEncryptOptions)2 BsonDocument (org.bson.BsonDocument)2 RawBsonDocument (org.bson.RawBsonDocument)2 MongoClientException (com.mongodb.MongoClientException)1 MongoException (com.mongodb.MongoException)1 MongoInternalException (com.mongodb.MongoInternalException)1 Assertions.notNull (com.mongodb.assertions.Assertions.notNull)1 DataKeyOptions (com.mongodb.client.model.vault.DataKeyOptions)1 EncryptOptions (com.mongodb.client.model.vault.EncryptOptions)1 MongoCrypt (com.mongodb.crypt.capi.MongoCrypt)1 State (com.mongodb.crypt.capi.MongoCryptContext.State)1 MongoDataKeyOptions (com.mongodb.crypt.capi.MongoDataKeyOptions)1 MongoKeyDecryptor (com.mongodb.crypt.capi.MongoKeyDecryptor)1 Logger (com.mongodb.diagnostics.logging.Logger)1 Loggers (com.mongodb.diagnostics.logging.Loggers)1 Nullable (com.mongodb.lang.Nullable)1 MongoClient (com.mongodb.reactivestreams.client.MongoClient)1 Closeable (java.io.Closeable)1