Skip to content
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

Add RewrapManyDataKeyProseTest #1083

Merged
merged 2 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
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
@@ -0,0 +1,38 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.mongodb.reactivestreams.client;

import com.mongodb.ClientEncryptionSettings;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.AbstractRewrapManyDataKeyProseTest;
import com.mongodb.client.MongoClient;
import com.mongodb.client.vault.ClientEncryption;
import com.mongodb.client.vault.ClientEncryptions;
import com.mongodb.reactivestreams.client.syncadapter.SyncMongoClient;

public class RewrapManyDataKeyProseTest extends AbstractRewrapManyDataKeyProseTest {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to ClientEncryptionRewrapManyDataKeyProseTest to aid future discovery


@Override
protected MongoClient createMongoClient(final MongoClientSettings settings) {
return new SyncMongoClient(MongoClients.create(settings));
}

@Override
public ClientEncryption getClientEncryption(final ClientEncryptionSettings settings) {
return ClientEncryptions.create(settings);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.mongodb.client;

import com.mongodb.ClientEncryptionSettings;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.model.vault.DataKeyOptions;
import com.mongodb.client.model.vault.EncryptOptions;
import com.mongodb.client.model.vault.RewrapManyDataKeyOptions;
import com.mongodb.client.model.vault.RewrapManyDataKeyResult;
import com.mongodb.client.vault.ClientEncryption;
import org.bson.BsonBinary;
import org.bson.BsonDocument;
import org.bson.BsonString;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.mongodb.ClusterFixture.hasEncryptionTestsEnabled;
import static com.mongodb.ClusterFixture.serverVersionAtLeast;
import static com.mongodb.client.Fixture.getMongoClient;
import static com.mongodb.client.Fixture.getMongoClientSettingsBuilder;
import static org.junit.jupiter.api.Assertions.assertEquals;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets add a comment to the prose test in the spec

public abstract class AbstractRewrapManyDataKeyProseTest {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to AbstractClientEncryptionRewrapManyDataKeyProseTest to aid future discovery

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many tests are named AbstractClientSideEncryption... - is this just a minor inconsistency, or is there some difference?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the weirdness that I hope JAVA-4879 will clean up.

Probably a mix of inconsistent naming and grouping for the CSFLE Prose tests - its not clear which ones map to which prose tests.


private static final Map<String, BsonDocument> MASTER_KEYS_BY_PROVIDER = new HashMap<>();
static {
MASTER_KEYS_BY_PROVIDER.put("aws", BsonDocument.parse("{\n"
+ " \"region\": \"us-east-1\",\n"
+ " \"key\": \"arn:aws:kms:us-east-1:579766882180:key/89fcc2c4-08b0-4bd9-9f25-e30687b580d0\"\n"
+ "}"));
MASTER_KEYS_BY_PROVIDER.put("azure", BsonDocument.parse("{\n"
+ " \"keyVaultEndpoint\": \"key-vault-csfle.vault.azure.net\",\n"
+ " \"keyName\": \"key-name-csfle\"\n"
+ "}"));
MASTER_KEYS_BY_PROVIDER.put("gcp", BsonDocument.parse("{\n"
+ " \"projectId\": \"devprod-drivers\",\n"
+ " \"location\": \"global\",\n"
+ " \"keyRing\": \"key-ring-csfle\",\n"
+ " \"keyName\": \"key-name-csfle\"\n"
+ "}"));
MASTER_KEYS_BY_PROVIDER.put("kmip", BsonDocument.parse("{}"));
MASTER_KEYS_BY_PROVIDER.put("local", null);
}

private static final Map<String, Map<String, Object>> KMS_PROVIDERS = new HashMap<String, Map<String, Object>>() {{
put("aws", new HashMap<String, Object>() {{
put("accessKeyId", System.getProperty("org.mongodb.test.awsAccessKeyId"));
put("secretAccessKey", System.getProperty("org.mongodb.test.awsSecretAccessKey"));
}});
put("azure", new HashMap<String, Object>() {{
put("tenantId", System.getProperty("org.mongodb.test.azureTenantId"));
put("clientId", System.getProperty("org.mongodb.test.azureClientId"));
put("clientSecret", System.getProperty("org.mongodb.test.azureClientSecret"));
put("identityPlatformEndpoint", "login.microsoftonline.com:443");
}});
put("gcp", new HashMap<String, Object>() {{
put("email", System.getProperty("org.mongodb.test.gcpEmail"));
put("privateKey", System.getProperty("org.mongodb.test.gcpPrivateKey"));
put("endpoint", "oauth2.googleapis.com:443");
}});
put("kmip", new HashMap<String, Object>() {{
put("endpoint", "localhost:5698");
}});
put("local", new HashMap<String, Object>() {{
put("key", "Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBM"
+ "UN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk");
}});
}};

protected abstract MongoClient createMongoClient(MongoClientSettings settings);
public abstract ClientEncryption getClientEncryption(ClientEncryptionSettings settings);

public static Collection<Arguments> data() {
List<Arguments> data = new ArrayList<>();
Set<String> types = MASTER_KEYS_BY_PROVIDER.keySet();
for (String srcProvider : types) {
for (String dstProvider : types) {
data.add(Arguments.of(srcProvider, dstProvider));
}
}
return data;
}

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

BsonDocument srcKey = MASTER_KEYS_BY_PROVIDER.get(srcProvider);
BsonDocument dstKey = MASTER_KEYS_BY_PROVIDER.get(dstProvider);
BsonString testString = new BsonString("test");

getMongoClient().getDatabase("keyvault").getCollection("datakeys").drop();

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

BsonBinary keyId = clientEncryption1.createDataKey(srcProvider, new DataKeyOptions().masterKey(srcKey));

BsonBinary ciphertext = clientEncryption1.encrypt(
testString,
new EncryptOptions("AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic").keyId(keyId));

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

RewrapManyDataKeyResult result = clientEncryption2.rewrapManyDataKey(
new BsonDocument(),
new RewrapManyDataKeyOptions().provider(dstProvider).masterKey(dstKey));
assertEquals(1, result.getBulkWriteResult().getModifiedCount());

assertEquals(testString, clientEncryption1.decrypt(ciphertext));
assertEquals(testString, clientEncryption2.decrypt(ciphertext));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.mongodb.client;

import com.mongodb.ClientEncryptionSettings;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.vault.ClientEncryption;
import com.mongodb.client.vault.ClientEncryptions;

public class RewrapManyDataKeyProseTest extends AbstractRewrapManyDataKeyProseTest {

@Override
protected MongoClient createMongoClient(final MongoClientSettings settings) {
return MongoClients.create(settings);
}

@Override
public ClientEncryption getClientEncryption(final ClientEncryptionSettings settings) {
return ClientEncryptions.create(settings);
}
}