Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.mongodb.client;

import com.mongodb.ClientEncryptionSettings;
import com.mongodb.MongoClientException;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.model.vault.DataKeyOptions;
import com.mongodb.client.model.vault.EncryptOptions;
Expand All @@ -26,7 +27,10 @@
import org.bson.BsonBinary;
import org.bson.BsonDocument;
import org.bson.BsonString;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -43,6 +47,7 @@
import static com.mongodb.client.Fixture.getMongoClient;
import static com.mongodb.client.Fixture.getMongoClientSettingsBuilder;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* See <a href="https://github.com/mongodb/specifications/tree/master/source/client-side-encryption/tests#rewrap">
Expand Down Expand Up @@ -109,12 +114,19 @@ public static Collection<Arguments> data() {
return data;
}

@ParameterizedTest
@MethodSource("data")
public void rewrapWithSeparateClientEncryption(final String srcProvider, final String dstProvider) {
protected AbstractClientEncryptionRewrapManyDataKeyProseTest() {
Assumptions.assumeTrue(serverVersionAtLeast(4, 2));
Assumptions.assumeTrue(hasEncryptionTestsEnabled(), "Custom Endpoint tests disables");
}

@AfterEach
void cleanUp(){
getMongoClient().getDatabase("keyvault").getCollection("datakeys").drop();
}

@ParameterizedTest
@MethodSource("data")
public void rewrapWithSeparateClientEncryption(final String srcProvider, final String dstProvider) {
BsonDocument srcKey = MASTER_KEYS_BY_PROVIDER.get(srcProvider);
BsonDocument dstKey = MASTER_KEYS_BY_PROVIDER.get(dstProvider);
BsonString testString = new BsonString("test");
Expand Down Expand Up @@ -147,4 +159,26 @@ public void rewrapWithSeparateClientEncryption(final String srcProvider, final S
assertEquals(testString, clientEncryption1.decrypt(ciphertext));
assertEquals(testString, clientEncryption2.decrypt(ciphertext));
}

@Test
public void shouldThrowClientErrorWhenProviderIsNotSpecified() {
//given
String expectedErrorMessage = "Missing the provider but supplied a master key in the RewrapManyDataKeyOptions";
BsonDocument dstKey = MASTER_KEYS_BY_PROVIDER.get("aws");

ClientEncryption clientEncryption = getClientEncryption(ClientEncryptionSettings.builder()
.keyVaultMongoClientSettings(getMongoClientSettingsBuilder().build())
.keyVaultNamespace("keyvault.datakeys")
.kmsProviders(KMS_PROVIDERS)
.build());

RewrapManyDataKeyOptions rewrapManyDataKeyOptions = new RewrapManyDataKeyOptions().masterKey(dstKey);

//when
Executable executable = () -> clientEncryption.rewrapManyDataKey(new BsonDocument(), rewrapManyDataKeyOptions);

//then
MongoClientException mongoClientException = assertThrows(MongoClientException.class, executable);
assertEquals(expectedErrorMessage, mongoClientException.getMessage());
}
}