|
17 | 17 | package com.mongodb.client;
|
18 | 18 |
|
19 | 19 | import com.mongodb.ClientEncryptionSettings;
|
| 20 | +import com.mongodb.MongoClientException; |
20 | 21 | import com.mongodb.MongoClientSettings;
|
21 | 22 | import com.mongodb.client.model.vault.DataKeyOptions;
|
22 | 23 | import com.mongodb.client.model.vault.EncryptOptions;
|
|
26 | 27 | import org.bson.BsonBinary;
|
27 | 28 | import org.bson.BsonDocument;
|
28 | 29 | import org.bson.BsonString;
|
| 30 | +import org.junit.jupiter.api.AfterEach; |
29 | 31 | import org.junit.jupiter.api.Assumptions;
|
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | +import org.junit.jupiter.api.function.Executable; |
30 | 34 | import org.junit.jupiter.params.ParameterizedTest;
|
31 | 35 | import org.junit.jupiter.params.provider.Arguments;
|
32 | 36 | import org.junit.jupiter.params.provider.MethodSource;
|
|
43 | 47 | import static com.mongodb.client.Fixture.getMongoClient;
|
44 | 48 | import static com.mongodb.client.Fixture.getMongoClientSettingsBuilder;
|
45 | 49 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
| 50 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
46 | 51 |
|
47 | 52 | /**
|
48 | 53 | * See <a href="https://github.com/mongodb/specifications/tree/master/source/client-side-encryption/tests#rewrap">
|
@@ -109,12 +114,19 @@ public static Collection<Arguments> data() {
|
109 | 114 | return data;
|
110 | 115 | }
|
111 | 116 |
|
112 |
| - @ParameterizedTest |
113 |
| - @MethodSource("data") |
114 |
| - public void rewrapWithSeparateClientEncryption(final String srcProvider, final String dstProvider) { |
| 117 | + protected AbstractClientEncryptionRewrapManyDataKeyProseTest() { |
115 | 118 | Assumptions.assumeTrue(serverVersionAtLeast(4, 2));
|
116 | 119 | Assumptions.assumeTrue(hasEncryptionTestsEnabled(), "Custom Endpoint tests disables");
|
| 120 | + } |
| 121 | + |
| 122 | + @AfterEach |
| 123 | + void cleanUp(){ |
| 124 | + getMongoClient().getDatabase("keyvault").getCollection("datakeys").drop(); |
| 125 | + } |
117 | 126 |
|
| 127 | + @ParameterizedTest |
| 128 | + @MethodSource("data") |
| 129 | + public void rewrapWithSeparateClientEncryption(final String srcProvider, final String dstProvider) { |
118 | 130 | BsonDocument srcKey = MASTER_KEYS_BY_PROVIDER.get(srcProvider);
|
119 | 131 | BsonDocument dstKey = MASTER_KEYS_BY_PROVIDER.get(dstProvider);
|
120 | 132 | BsonString testString = new BsonString("test");
|
@@ -147,4 +159,25 @@ public void rewrapWithSeparateClientEncryption(final String srcProvider, final S
|
147 | 159 | assertEquals(testString, clientEncryption1.decrypt(ciphertext));
|
148 | 160 | assertEquals(testString, clientEncryption2.decrypt(ciphertext));
|
149 | 161 | }
|
| 162 | + |
| 163 | + @Test |
| 164 | + public void shouldThrowClientErrorWhenProviderIsNotSpecified() { |
| 165 | + //given |
| 166 | + ClientEncryption clientEncryption = getClientEncryption(ClientEncryptionSettings.builder() |
| 167 | + .keyVaultMongoClientSettings(getMongoClientSettingsBuilder().build()) |
| 168 | + .keyVaultNamespace("keyvault.datakeys") |
| 169 | + .kmsProviders(KMS_PROVIDERS) |
| 170 | + .build()); |
| 171 | + |
| 172 | + RewrapManyDataKeyOptions rewrapManyDataKeyOptions = new RewrapManyDataKeyOptions().masterKey(BsonDocument.parse("{}")); |
| 173 | + |
| 174 | + //when |
| 175 | + Executable executable = () -> clientEncryption.rewrapManyDataKey(new BsonDocument(), rewrapManyDataKeyOptions); |
| 176 | + |
| 177 | + //then |
| 178 | + MongoClientException mongoClientException = assertThrows(MongoClientException.class, executable); |
| 179 | + |
| 180 | + assertEquals("Missing the provider but supplied a master key in the RewrapManyDataKeyOptions", |
| 181 | + mongoClientException.getMessage()); |
| 182 | + } |
150 | 183 | }
|
0 commit comments