Skip to content

Add second case for the RewrapManyDataKeyOpts prose test. #1138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 9, 2023
Merged
Changes from all commits
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,25 @@ public void rewrapWithSeparateClientEncryption(final String srcProvider, final S
assertEquals(testString, clientEncryption1.decrypt(ciphertext));
assertEquals(testString, clientEncryption2.decrypt(ciphertext));
}

@Test
public void shouldThrowClientErrorWhenProviderIsNotSpecified() {
//given
ClientEncryption clientEncryption = getClientEncryption(ClientEncryptionSettings.builder()
.keyVaultMongoClientSettings(getMongoClientSettingsBuilder().build())
.keyVaultNamespace("keyvault.datakeys")
.kmsProviders(KMS_PROVIDERS)
.build());

RewrapManyDataKeyOptions rewrapManyDataKeyOptions = new RewrapManyDataKeyOptions().masterKey(BsonDocument.parse("{}"));

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

//then
MongoClientException mongoClientException = assertThrows(MongoClientException.class, executable);

assertEquals("Missing the provider but supplied a master key in the RewrapManyDataKeyOptions",
mongoClientException.getMessage());
}
}