|
| 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.async.client; |
| 18 | + |
| 19 | +import com.mongodb.ClientEncryptionSettings; |
| 20 | +import com.mongodb.AutoEncryptionSettings; |
| 21 | +import com.mongodb.MongoClientSettings; |
| 22 | +import com.mongodb.MongoCredential; |
| 23 | +import com.mongodb.MongoSecurityException; |
| 24 | +import com.mongodb.async.FutureResultCallback; |
| 25 | +import com.mongodb.async.client.vault.ClientEncryption; |
| 26 | +import com.mongodb.async.client.vault.ClientEncryptions; |
| 27 | +import com.mongodb.client.model.vault.EncryptOptions; |
| 28 | +import org.bson.BsonBinary; |
| 29 | +import org.bson.BsonBinarySubType; |
| 30 | +import org.bson.BsonDocument; |
| 31 | +import org.bson.BsonString; |
| 32 | +import org.junit.After; |
| 33 | +import org.junit.Before; |
| 34 | +import org.junit.Test; |
| 35 | +import org.junit.runner.RunWith; |
| 36 | +import org.junit.runners.Parameterized; |
| 37 | + |
| 38 | +import java.io.File; |
| 39 | +import java.io.IOException; |
| 40 | +import java.net.URISyntaxException; |
| 41 | +import java.util.Arrays; |
| 42 | +import java.util.Base64; |
| 43 | +import java.util.Collection; |
| 44 | +import java.util.HashMap; |
| 45 | +import java.util.Map; |
| 46 | + |
| 47 | +import static com.mongodb.ClusterFixture.isNotAtLeastJava8; |
| 48 | +import static com.mongodb.ClusterFixture.serverVersionAtLeast; |
| 49 | +import static com.mongodb.async.client.Fixture.getMongoClientBuilderFromConnectionString; |
| 50 | +import static com.mongodb.async.client.Fixture.getMongoClient; |
| 51 | +import static org.junit.Assert.assertEquals; |
| 52 | +import static org.junit.Assume.assumeFalse; |
| 53 | +import static org.junit.Assume.assumeTrue; |
| 54 | +import static util.JsonPoweredTestHelper.getTestDocument; |
| 55 | + |
| 56 | +@RunWith(Parameterized.class) |
| 57 | +public class ClientSideEncryptionExternalKeyVaultTest { |
| 58 | + private MongoClient client, clientEncrypted; |
| 59 | + private ClientEncryption clientEncryption; |
| 60 | + private final boolean withExternalKeyVault; |
| 61 | + |
| 62 | + public ClientSideEncryptionExternalKeyVaultTest(final boolean withExternalKeyVault) { |
| 63 | + this.withExternalKeyVault = withExternalKeyVault; |
| 64 | + } |
| 65 | + |
| 66 | + @Before |
| 67 | + public void setUp() throws IOException, URISyntaxException { |
| 68 | + assumeFalse(isNotAtLeastJava8()); |
| 69 | + assumeTrue(serverVersionAtLeast(4, 1)); |
| 70 | + assumeTrue("Encryption test with external keyVault is disabled", |
| 71 | + System.getProperty("org.mongodb.test.awsAccessKeyId") != null |
| 72 | + && !System.getProperty("org.mongodb.test.awsAccessKeyId").isEmpty()); |
| 73 | + |
| 74 | + /* Step 1: get unencrypted client and recreate keys collection */ |
| 75 | + client = getMongoClient(); |
| 76 | + MongoDatabase admin = client.getDatabase("admin"); |
| 77 | + MongoCollection<BsonDocument> datakeys = admin.getCollection("datakeys", BsonDocument.class); |
| 78 | + FutureResultCallback<Void> voidCallback = new FutureResultCallback<Void>(); |
| 79 | + datakeys.drop(voidCallback); |
| 80 | + voidCallback.get(); |
| 81 | + |
| 82 | + voidCallback = new FutureResultCallback<Void>(); |
| 83 | + datakeys.insertOne(bsonDocumentFromPath("external-key.json"), voidCallback); |
| 84 | + voidCallback.get(); |
| 85 | + |
| 86 | + /* Step 2: create encryption objects. */ |
| 87 | + Map<String, Map<String, Object>> kmsProviders = new HashMap<String, Map<String, Object>>(); |
| 88 | + Map<String, Object> localMasterkey = new HashMap<String, Object>(); |
| 89 | + Map<String, BsonDocument> schemaMap = new HashMap<String, BsonDocument>(); |
| 90 | + |
| 91 | + byte[] localMasterkeyBytes = Base64.getDecoder().decode("Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBM" |
| 92 | + + "UN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk"); |
| 93 | + localMasterkey.put("key", localMasterkeyBytes); |
| 94 | + kmsProviders.put("local", localMasterkey); |
| 95 | + schemaMap.put("db.coll", bsonDocumentFromPath("external-schema.json")); |
| 96 | + |
| 97 | + AutoEncryptionSettings.Builder autoEncryptionSettingsBuilder = AutoEncryptionSettings.builder() |
| 98 | + .keyVaultNamespace("admin.datakeys") |
| 99 | + .kmsProviders(kmsProviders) |
| 100 | + .schemaMap(schemaMap); |
| 101 | + |
| 102 | + MongoClientSettings externalClientSettings = null; |
| 103 | + if (withExternalKeyVault) { |
| 104 | + externalClientSettings = getMongoClientBuilderFromConnectionString() |
| 105 | + .credential(MongoCredential.createCredential("fake-user", "admin", "fake-pwd".toCharArray())) |
| 106 | + .build(); |
| 107 | + autoEncryptionSettingsBuilder.keyVaultMongoClientSettings(externalClientSettings); |
| 108 | + } |
| 109 | + |
| 110 | + AutoEncryptionSettings autoEncryptionSettings = autoEncryptionSettingsBuilder.build(); |
| 111 | + |
| 112 | + MongoClientSettings clientSettings = getMongoClientBuilderFromConnectionString() |
| 113 | + .autoEncryptionSettings(autoEncryptionSettings) |
| 114 | + .build(); |
| 115 | + clientEncrypted = MongoClients.create(clientSettings); |
| 116 | + |
| 117 | + ClientEncryptionSettings.Builder clientEncryptionSettingsBuilder = ClientEncryptionSettings.builder(). |
| 118 | + keyVaultMongoClientSettings(getMongoClientBuilderFromConnectionString().build()) |
| 119 | + .kmsProviders(kmsProviders) |
| 120 | + .keyVaultNamespace("admin.datakeys"); |
| 121 | + |
| 122 | + if (withExternalKeyVault) { |
| 123 | + clientEncryptionSettingsBuilder.keyVaultMongoClientSettings(externalClientSettings); |
| 124 | + } |
| 125 | + |
| 126 | + ClientEncryptionSettings clientEncryptionSettings = clientEncryptionSettingsBuilder.build(); |
| 127 | + clientEncryption = ClientEncryptions.create(clientEncryptionSettings); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void testExternal() { |
| 132 | + boolean authExceptionThrown = false; |
| 133 | + MongoCollection<BsonDocument> coll = clientEncrypted |
| 134 | + .getDatabase("db") |
| 135 | + .getCollection("coll", BsonDocument.class); |
| 136 | + FutureResultCallback<Void> voidCallback; |
| 137 | + try { |
| 138 | + voidCallback = new FutureResultCallback<Void>(); |
| 139 | + coll.insertOne(new BsonDocument().append("encrypted", new BsonString("test")), voidCallback); |
| 140 | + voidCallback.get(); |
| 141 | + } catch (MongoSecurityException mse) { |
| 142 | + authExceptionThrown = true; |
| 143 | + } |
| 144 | + assertEquals(authExceptionThrown, withExternalKeyVault); |
| 145 | + |
| 146 | + EncryptOptions encryptOptions = new EncryptOptions("AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic") |
| 147 | + .keyId(new BsonBinary(BsonBinarySubType.UUID_STANDARD, Base64.getDecoder().decode("LOCALAAAAAAAAAAAAAAAAA=="))); |
| 148 | + authExceptionThrown = false; |
| 149 | + try { |
| 150 | + FutureResultCallback<BsonBinary> bsonBinaryCallback = new FutureResultCallback<BsonBinary>(); |
| 151 | + clientEncryption.encrypt(new BsonString("test"), encryptOptions, bsonBinaryCallback); |
| 152 | + bsonBinaryCallback.get(); |
| 153 | + } catch (MongoSecurityException mse) { |
| 154 | + authExceptionThrown = true; |
| 155 | + } |
| 156 | + assertEquals(authExceptionThrown, withExternalKeyVault); |
| 157 | + } |
| 158 | + |
| 159 | + private static BsonDocument bsonDocumentFromPath(final String path) throws IOException, URISyntaxException { |
| 160 | + return getTestDocument(new File(ClientSideEncryptionExternalKeyVaultTest.class |
| 161 | + .getResource("/client-side-encryption-external/" + path).toURI())); |
| 162 | + } |
| 163 | + |
| 164 | + @Parameterized.Parameters(name = "withExternalKeyVault: {0}") |
| 165 | + public static Collection<Object[]> data() { |
| 166 | + return Arrays.asList(new Object[]{true}, new Object[]{false}); |
| 167 | + } |
| 168 | + |
| 169 | + @After |
| 170 | + public void after() { |
| 171 | + if (clientEncrypted != null) { |
| 172 | + try { |
| 173 | + clientEncrypted.close(); |
| 174 | + } catch (Exception e) { |
| 175 | + // ignore |
| 176 | + } |
| 177 | + } |
| 178 | + if (clientEncryption != null) { |
| 179 | + try { |
| 180 | + clientEncryption.close(); |
| 181 | + } catch (Exception e) { |
| 182 | + // ignore |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments