Skip to content

Commit edf38c7

Browse files
committed
Add RewrapManyDataKeyProseTest
1 parent 8ce235f commit edf38c7

File tree

3 files changed

+219
-0
lines changed

3 files changed

+219
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb.reactivestreams.client;
18+
19+
import com.mongodb.ClientEncryptionSettings;
20+
import com.mongodb.MongoClientSettings;
21+
import com.mongodb.client.AbstractRewrapManyDataKeyProseTest;
22+
import com.mongodb.client.MongoClient;
23+
import com.mongodb.client.vault.ClientEncryption;
24+
import com.mongodb.client.vault.ClientEncryptions;
25+
import com.mongodb.reactivestreams.client.syncadapter.SyncMongoClient;
26+
27+
public class RewrapManyDataKeyProseTest extends AbstractRewrapManyDataKeyProseTest {
28+
29+
@Override
30+
protected MongoClient createMongoClient(final MongoClientSettings settings) {
31+
return new SyncMongoClient(MongoClients.create(settings));
32+
}
33+
34+
@Override
35+
public ClientEncryption getClientEncryption(final ClientEncryptionSettings settings) {
36+
return ClientEncryptions.create(settings);
37+
}
38+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb.client;
18+
19+
import com.mongodb.ClientEncryptionSettings;
20+
import com.mongodb.MongoClientSettings;
21+
import com.mongodb.client.model.vault.DataKeyOptions;
22+
import com.mongodb.client.model.vault.EncryptOptions;
23+
import com.mongodb.client.model.vault.RewrapManyDataKeyOptions;
24+
import com.mongodb.client.model.vault.RewrapManyDataKeyResult;
25+
import com.mongodb.client.vault.ClientEncryption;
26+
import org.bson.BsonBinary;
27+
import org.bson.BsonDocument;
28+
import org.bson.BsonString;
29+
import org.junit.jupiter.api.Assumptions;
30+
import org.junit.jupiter.params.ParameterizedTest;
31+
import org.junit.jupiter.params.provider.Arguments;
32+
import org.junit.jupiter.params.provider.MethodSource;
33+
34+
import java.util.ArrayList;
35+
import java.util.Collection;
36+
import java.util.HashMap;
37+
import java.util.List;
38+
import java.util.Map;
39+
import java.util.Set;
40+
41+
import static com.mongodb.ClusterFixture.hasEncryptionTestsEnabled;
42+
import static com.mongodb.ClusterFixture.serverVersionAtLeast;
43+
import static com.mongodb.client.Fixture.getMongoClient;
44+
import static com.mongodb.client.Fixture.getMongoClientSettingsBuilder;
45+
import static org.junit.jupiter.api.Assertions.assertEquals;
46+
47+
public abstract class AbstractRewrapManyDataKeyProseTest {
48+
49+
private static final Map<String, BsonDocument> MASTER_KEYS_BY_PROVIDER = new HashMap<>();
50+
static {
51+
MASTER_KEYS_BY_PROVIDER.put("aws", BsonDocument.parse("{\n"
52+
+ " \"region\": \"us-east-1\",\n"
53+
+ " \"key\": \"arn:aws:kms:us-east-1:579766882180:key/89fcc2c4-08b0-4bd9-9f25-e30687b580d0\"\n"
54+
+ "}"));
55+
MASTER_KEYS_BY_PROVIDER.put("azure", BsonDocument.parse("{\n"
56+
+ " \"keyVaultEndpoint\": \"key-vault-csfle.vault.azure.net\",\n"
57+
+ " \"keyName\": \"key-name-csfle\"\n"
58+
+ "}"));
59+
MASTER_KEYS_BY_PROVIDER.put("gcp", BsonDocument.parse("{\n"
60+
+ " \"projectId\": \"devprod-drivers\",\n"
61+
+ " \"location\": \"global\",\n"
62+
+ " \"keyRing\": \"key-ring-csfle\",\n"
63+
+ " \"keyName\": \"key-name-csfle\"\n"
64+
+ "}"));
65+
MASTER_KEYS_BY_PROVIDER.put("kmip", BsonDocument.parse("{}"));
66+
MASTER_KEYS_BY_PROVIDER.put("local", null);
67+
}
68+
69+
private static final Map<String, Map<String, Object>> KMS_PROVIDERS = new HashMap<String, Map<String, Object>>() {{
70+
put("aws", new HashMap<String, Object>() {{
71+
put("accessKeyId", System.getProperty("org.mongodb.test.awsAccessKeyId"));
72+
put("secretAccessKey", System.getProperty("org.mongodb.test.awsSecretAccessKey"));
73+
}});
74+
put("azure", new HashMap<String, Object>() {{
75+
put("tenantId", System.getProperty("org.mongodb.test.azureTenantId"));
76+
put("clientId", System.getProperty("org.mongodb.test.azureClientId"));
77+
put("clientSecret", System.getProperty("org.mongodb.test.azureClientSecret"));
78+
put("identityPlatformEndpoint", "login.microsoftonline.com:443");
79+
}});
80+
put("gcp", new HashMap<String, Object>() {{
81+
put("email", System.getProperty("org.mongodb.test.gcpEmail"));
82+
put("privateKey", System.getProperty("org.mongodb.test.gcpPrivateKey"));
83+
put("endpoint", "oauth2.googleapis.com:443");
84+
}});
85+
put("kmip", new HashMap<String, Object>() {{
86+
put("endpoint", "localhost:5698");
87+
}});
88+
put("local", new HashMap<String, Object>() {{
89+
put("key", "Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBM"
90+
+ "UN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk");
91+
}});
92+
}};
93+
94+
protected abstract MongoClient createMongoClient(MongoClientSettings settings);
95+
public abstract ClientEncryption getClientEncryption(ClientEncryptionSettings settings);
96+
97+
public static Collection<Arguments> data() {
98+
List<Arguments> data = new ArrayList<>();
99+
Set<String> types = MASTER_KEYS_BY_PROVIDER.keySet();
100+
for (String srcProvider : types) {
101+
for (String dstProvider : types) {
102+
data.add(Arguments.of(srcProvider, dstProvider));
103+
}
104+
}
105+
return data;
106+
}
107+
108+
@ParameterizedTest
109+
@MethodSource("data")
110+
public void rewrapWithSeparateClientEncryption(final String srcProvider, final String dstProvider) {
111+
Assumptions.assumeTrue(serverVersionAtLeast(4, 2));
112+
Assumptions.assumeTrue(hasEncryptionTestsEnabled(), "Custom Endpoint tests disables");
113+
114+
BsonDocument srcKey = MASTER_KEYS_BY_PROVIDER.get(srcProvider);
115+
BsonDocument dstKey = MASTER_KEYS_BY_PROVIDER.get(dstProvider);
116+
BsonString testString = new BsonString("test");
117+
118+
getMongoClient().getDatabase("keyvault").getCollection("datakeys").drop();
119+
120+
ClientEncryption clientEncryption1 = getClientEncryption(ClientEncryptionSettings.builder()
121+
.keyVaultMongoClientSettings(getMongoClientSettingsBuilder().build())
122+
.keyVaultNamespace("keyvault.datakeys")
123+
.kmsProviders(KMS_PROVIDERS)
124+
.build());
125+
126+
BsonBinary keyId = clientEncryption1.createDataKey(srcProvider, new DataKeyOptions().masterKey(srcKey));
127+
128+
BsonBinary ciphertext = clientEncryption1.encrypt(
129+
testString,
130+
new EncryptOptions("AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic").keyId(keyId));
131+
132+
ClientEncryption clientEncryption2 = getClientEncryption(ClientEncryptionSettings.builder()
133+
.keyVaultMongoClientSettings(getMongoClientSettingsBuilder().build())
134+
.keyVaultNamespace("keyvault.datakeys")
135+
.kmsProviders(KMS_PROVIDERS)
136+
.build());
137+
138+
RewrapManyDataKeyResult result = clientEncryption2.rewrapManyDataKey(
139+
new BsonDocument(),
140+
new RewrapManyDataKeyOptions().provider(dstProvider).masterKey(dstKey));
141+
assertEquals(1, result.getBulkWriteResult().getModifiedCount());
142+
143+
assertEquals(testString, clientEncryption1.decrypt(ciphertext));
144+
assertEquals(testString, clientEncryption2.decrypt(ciphertext));
145+
}
146+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb.client;
18+
19+
import com.mongodb.ClientEncryptionSettings;
20+
import com.mongodb.MongoClientSettings;
21+
import com.mongodb.client.vault.ClientEncryption;
22+
import com.mongodb.client.vault.ClientEncryptions;
23+
24+
public class RewrapManyDataKeyProseTest extends AbstractRewrapManyDataKeyProseTest {
25+
26+
@Override
27+
protected MongoClient createMongoClient(final MongoClientSettings settings) {
28+
return MongoClients.create(settings);
29+
}
30+
31+
@Override
32+
public ClientEncryption getClientEncryption(final ClientEncryptionSettings settings) {
33+
return ClientEncryptions.create(settings);
34+
}
35+
}

0 commit comments

Comments
 (0)