Search in sources :

Example 1 with MongoCryptException

use of com.mongodb.crypt.capi.MongoCryptException 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 MongoCryptException

use of com.mongodb.crypt.capi.MongoCryptException 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 MongoCryptException

use of com.mongodb.crypt.capi.MongoCryptException 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)

Aggregations

MongoCryptContext (com.mongodb.crypt.capi.MongoCryptContext)3 MongoCryptException (com.mongodb.crypt.capi.MongoCryptException)3 MongoExplicitEncryptOptions (com.mongodb.crypt.capi.MongoExplicitEncryptOptions)1 BsonDocument (org.bson.BsonDocument)1 RawBsonDocument (org.bson.RawBsonDocument)1