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);
}
}
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);
}
}
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);
}
}
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();
}
Aggregations