|
| 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.MongoClientException; |
| 21 | +import com.mongodb.async.FutureResultCallback; |
| 22 | +import com.mongodb.async.client.vault.ClientEncryption; |
| 23 | +import com.mongodb.async.client.vault.ClientEncryptions; |
| 24 | +import com.mongodb.client.model.vault.DataKeyOptions; |
| 25 | +import com.mongodb.client.model.vault.EncryptOptions; |
| 26 | +import com.mongodb.crypt.capi.MongoCryptException; |
| 27 | +import com.mongodb.lang.Nullable; |
| 28 | +import org.bson.BsonBinary; |
| 29 | +import org.bson.BsonDocument; |
| 30 | +import org.bson.BsonString; |
| 31 | +import org.junit.After; |
| 32 | +import org.junit.Before; |
| 33 | +import org.junit.Test; |
| 34 | +import org.junit.runner.RunWith; |
| 35 | +import org.junit.runners.Parameterized; |
| 36 | + |
| 37 | +import java.net.ConnectException; |
| 38 | +import java.util.ArrayList; |
| 39 | +import java.util.Collection; |
| 40 | +import java.util.HashMap; |
| 41 | +import java.util.List; |
| 42 | +import java.util.Map; |
| 43 | +import java.util.concurrent.TimeUnit; |
| 44 | + |
| 45 | +import static com.mongodb.ClusterFixture.TIMEOUT; |
| 46 | +import static com.mongodb.ClusterFixture.isNotAtLeastJava8; |
| 47 | +import static com.mongodb.ClusterFixture.serverVersionAtLeast; |
| 48 | +import static org.junit.Assert.assertEquals; |
| 49 | +import static org.junit.Assert.assertNull; |
| 50 | +import static org.junit.Assert.assertTrue; |
| 51 | +import static org.junit.Assume.assumeFalse; |
| 52 | +import static org.junit.Assume.assumeTrue; |
| 53 | + |
| 54 | +@RunWith(Parameterized.class) |
| 55 | +public class ClientEncryptionCustomEndpointTest { |
| 56 | + |
| 57 | + private ClientEncryption clientEncryption; |
| 58 | + private BsonDocument masterKey; |
| 59 | + private final Class<? extends RuntimeException> exceptionClass; |
| 60 | + private final Class<? extends RuntimeException> wrappedExceptionClass; |
| 61 | + private final String messageContainedInException; |
| 62 | + |
| 63 | + public ClientEncryptionCustomEndpointTest(@SuppressWarnings("unused") final String name, |
| 64 | + final BsonDocument masterKey, |
| 65 | + @Nullable final Class<? extends RuntimeException> exceptionClass, |
| 66 | + @Nullable final Class<? extends RuntimeException> wrappedExceptionClass, |
| 67 | + @Nullable final String messageContainedInException) { |
| 68 | + this.masterKey = masterKey; |
| 69 | + this.exceptionClass = exceptionClass; |
| 70 | + this.wrappedExceptionClass = wrappedExceptionClass; |
| 71 | + this.messageContainedInException = messageContainedInException; |
| 72 | + } |
| 73 | + |
| 74 | + @Before |
| 75 | + public void setUp() { |
| 76 | + assumeFalse(isNotAtLeastJava8()); |
| 77 | + assumeTrue(serverVersionAtLeast(4, 1)); |
| 78 | + assumeTrue("Encryption test with external keyVault is disabled", |
| 79 | + System.getProperty("org.mongodb.test.awsAccessKeyId") != null |
| 80 | + && !System.getProperty("org.mongodb.test.awsAccessKeyId").isEmpty()); |
| 81 | + |
| 82 | + Map<String, Map<String, Object>> kmsProviders = new HashMap<String, Map<String, Object>>(); |
| 83 | + Map<String, Object> awsCreds = new HashMap<String, Object>(); |
| 84 | + awsCreds.put("accessKeyId", System.getProperty("org.mongodb.test.awsAccessKeyId")); |
| 85 | + awsCreds.put("secretAccessKey", System.getProperty("org.mongodb.test.awsSecretAccessKey")); |
| 86 | + kmsProviders.put("aws", awsCreds); |
| 87 | + |
| 88 | + ClientEncryptionSettings.Builder clientEncryptionSettingsBuilder = ClientEncryptionSettings.builder(). |
| 89 | + keyVaultMongoClientSettings(Fixture.getMongoClientSettings()) |
| 90 | + .kmsProviders(kmsProviders) |
| 91 | + .keyVaultNamespace("admin.datakeys"); |
| 92 | + |
| 93 | + ClientEncryptionSettings clientEncryptionSettings = clientEncryptionSettingsBuilder.build(); |
| 94 | + clientEncryption = ClientEncryptions.create(clientEncryptionSettings); |
| 95 | + } |
| 96 | + |
| 97 | + @After |
| 98 | + public void after() { |
| 99 | + if (clientEncryption != null) { |
| 100 | + try { |
| 101 | + clientEncryption.close(); |
| 102 | + } catch (Exception e) { |
| 103 | + // ignore |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testEndpoint() throws Exception { |
| 110 | + try { |
| 111 | + FutureResultCallback<BsonBinary> dataKeyCreationCallback = new FutureResultCallback<BsonBinary>(); |
| 112 | + clientEncryption.createDataKey("aws", new DataKeyOptions() |
| 113 | + .masterKey(masterKey), dataKeyCreationCallback); |
| 114 | + |
| 115 | + BsonBinary dataKeyId = dataKeyCreationCallback.get(TIMEOUT, TimeUnit.SECONDS); |
| 116 | + |
| 117 | + assertNull("Expected exception, but encryption succeeded", exceptionClass); |
| 118 | + |
| 119 | + FutureResultCallback<BsonBinary> encryptCallback = new FutureResultCallback<BsonBinary>(); |
| 120 | + clientEncryption.encrypt(new BsonString("test"), new EncryptOptions("AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic") |
| 121 | + .keyId(dataKeyId), encryptCallback); |
| 122 | + encryptCallback.get(); |
| 123 | + } catch (Exception e) { |
| 124 | + if (exceptionClass == null) { |
| 125 | + throw e; |
| 126 | + } |
| 127 | + try { |
| 128 | + assertEquals(exceptionClass, e.getClass()); |
| 129 | + assertEquals(wrappedExceptionClass, e.getCause().getClass()); |
| 130 | + } catch (AssertionError ae) { |
| 131 | + throw e; |
| 132 | + } |
| 133 | + if (messageContainedInException != null) { |
| 134 | + assertTrue(e.getCause().getMessage().contains(messageContainedInException)); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + @Parameterized.Parameters(name = "{0}") |
| 140 | + public static Collection<Object[]> data() { |
| 141 | + List<Object[]> data = new ArrayList<Object[]>(); |
| 142 | + |
| 143 | + data.add(new Object[]{"default endpoint", |
| 144 | + getDefaultMasterKey(), |
| 145 | + null, null, null}); |
| 146 | + data.add(new Object[]{"valid endpoint", |
| 147 | + getDefaultMasterKey().append("endpoint", new BsonString("kms.us-east-1.amazonaws.com")), |
| 148 | + null, null, null}); |
| 149 | + data.add(new Object[]{"valid endpoint port", |
| 150 | + getDefaultMasterKey().append("endpoint", new BsonString("kms.us-east-1.amazonaws.com:443")), |
| 151 | + null, null, null}); |
| 152 | + data.add(new Object[]{"invalid endpoint port", |
| 153 | + getDefaultMasterKey().append("endpoint", new BsonString("kms.us-east-1.amazonaws.com:12345")), |
| 154 | + MongoClientException.class, ConnectException.class, "Connection refused"}); |
| 155 | + data.add(new Object[]{"invalid amazon region in endpoint", |
| 156 | + getDefaultMasterKey().append("endpoint", new BsonString("kms.us-east-2.amazonaws.com")), |
| 157 | + MongoClientException.class, MongoCryptException.class, "us-east-1"}); |
| 158 | + data.add(new Object[]{"invalid endpoint host", |
| 159 | + getDefaultMasterKey().append("endpoint", new BsonString("example.com")), |
| 160 | + MongoClientException.class, MongoCryptException.class, "parse error"}); |
| 161 | + |
| 162 | + return data; |
| 163 | + } |
| 164 | + |
| 165 | + private static BsonDocument getDefaultMasterKey() { |
| 166 | + return new BsonDocument() |
| 167 | + .append("region", new BsonString("us-east-1")) |
| 168 | + .append("key", new BsonString("arn:aws:kms:us-east-1:579766882180:key/89fcc2c4-08b0-4bd9-9f25-e30687b580d0")); |
| 169 | + } |
| 170 | +} |
0 commit comments