|
| 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.AutoEncryptionSettings; |
| 20 | +import com.mongodb.MongoClientSettings; |
| 21 | +import com.mongodb.MongoNamespace; |
| 22 | +import com.mongodb.WriteConcern; |
| 23 | +import com.mongodb.async.FutureResultCallback; |
| 24 | +import com.mongodb.client.test.CollectionHelper; |
| 25 | +import org.bson.BsonDocument; |
| 26 | +import org.bson.BsonString; |
| 27 | +import org.bson.codecs.BsonDocumentCodec; |
| 28 | +import org.junit.After; |
| 29 | +import org.junit.Before; |
| 30 | +import org.junit.Test; |
| 31 | +import org.junit.runner.RunWith; |
| 32 | +import org.junit.runners.Parameterized; |
| 33 | + |
| 34 | +import java.io.File; |
| 35 | +import java.io.IOException; |
| 36 | +import java.net.URISyntaxException; |
| 37 | +import java.util.Arrays; |
| 38 | +import java.util.Base64; |
| 39 | +import java.util.Collection; |
| 40 | +import java.util.HashMap; |
| 41 | +import java.util.Map; |
| 42 | + |
| 43 | +import static com.mongodb.ClusterFixture.isNotAtLeastJava8; |
| 44 | +import static com.mongodb.ClusterFixture.isStandalone; |
| 45 | +import static com.mongodb.ClusterFixture.serverVersionAtLeast; |
| 46 | +import static com.mongodb.async.client.Fixture.getDefaultDatabaseName; |
| 47 | +import static com.mongodb.async.client.Fixture.getMongoClient; |
| 48 | +import static com.mongodb.async.client.Fixture.getMongoClientBuilderFromConnectionString; |
| 49 | +import static org.junit.Assert.assertEquals; |
| 50 | +import static org.junit.Assert.assertTrue; |
| 51 | +import static org.junit.Assume.assumeFalse; |
| 52 | +import static org.junit.Assume.assumeTrue; |
| 53 | +import static util.JsonPoweredTestHelper.getTestDocument; |
| 54 | + |
| 55 | +@RunWith(Parameterized.class) |
| 56 | +public class ClientSideEncryptionSessionTest { |
| 57 | + private static final String COLLECTION_NAME = "clientSideEncryptionSessionsTest"; |
| 58 | + |
| 59 | + private MongoClient client = getMongoClient(); |
| 60 | + private MongoClient clientEncrypted; |
| 61 | + private final boolean useTransaction; |
| 62 | + |
| 63 | + @Parameterized.Parameters(name = "useTransaction: {0}") |
| 64 | + public static Collection<Object[]> data() { |
| 65 | + return Arrays.asList(new Object[]{true}, new Object[]{false}); |
| 66 | + } |
| 67 | + |
| 68 | + public ClientSideEncryptionSessionTest(final boolean useTransaction) { |
| 69 | + this.useTransaction = useTransaction; |
| 70 | + } |
| 71 | + |
| 72 | + @Before |
| 73 | + public void setUp() throws Throwable { |
| 74 | + assumeFalse(isNotAtLeastJava8()); |
| 75 | + assumeTrue(serverVersionAtLeast(4, 2)); |
| 76 | + assumeFalse(isStandalone()); |
| 77 | + |
| 78 | + /* Step 1: get unencrypted client and recreate keys collection */ |
| 79 | + client = getMongoClient(); |
| 80 | + MongoDatabase keyVaultDatabase = client.getDatabase("keyvault"); |
| 81 | + MongoCollection<BsonDocument> dataKeys = keyVaultDatabase.getCollection("datakeys", BsonDocument.class) |
| 82 | + .withWriteConcern(WriteConcern.MAJORITY); |
| 83 | + FutureResultCallback<Void> voidCallback = new FutureResultCallback<Void>(); |
| 84 | + dataKeys.drop(voidCallback); |
| 85 | + voidCallback.get(); |
| 86 | + |
| 87 | + voidCallback = new FutureResultCallback<Void>(); |
| 88 | + dataKeys.insertOne(bsonDocumentFromPath("external-key.json"), voidCallback); |
| 89 | + voidCallback.get(); |
| 90 | + |
| 91 | + /* Step 2: create encryption objects. */ |
| 92 | + Map<String, Map<String, Object>> kmsProviders = new HashMap<String, Map<String, Object>>(); |
| 93 | + Map<String, Object> localMasterkey = new HashMap<String, Object>(); |
| 94 | + Map<String, BsonDocument> schemaMap = new HashMap<String, BsonDocument>(); |
| 95 | + |
| 96 | + byte[] localMasterKeyBytes = Base64.getDecoder().decode("Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBM" |
| 97 | + + "UN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk"); |
| 98 | + localMasterkey.put("key", localMasterKeyBytes); |
| 99 | + kmsProviders.put("local", localMasterkey); |
| 100 | + schemaMap.put(getDefaultDatabaseName() + "." + COLLECTION_NAME, bsonDocumentFromPath("external-schema.json")); |
| 101 | + |
| 102 | + MongoClientSettings clientSettings = getMongoClientBuilderFromConnectionString() |
| 103 | + .autoEncryptionSettings(AutoEncryptionSettings.builder() |
| 104 | + .keyVaultNamespace("keyvault.datakeys") |
| 105 | + .kmsProviders(kmsProviders) |
| 106 | + .schemaMap(schemaMap).build()) |
| 107 | + .build(); |
| 108 | + clientEncrypted = MongoClients.create(clientSettings); |
| 109 | + |
| 110 | + CollectionHelper<BsonDocument> collectionHelper = |
| 111 | + new CollectionHelper<BsonDocument>(new BsonDocumentCodec(), new MongoNamespace(getDefaultDatabaseName(), COLLECTION_NAME)); |
| 112 | + collectionHelper.drop(); |
| 113 | + collectionHelper.create(); |
| 114 | + } |
| 115 | + |
| 116 | + @After |
| 117 | + public void after() { |
| 118 | + if (clientEncrypted != null) { |
| 119 | + try { |
| 120 | + clientEncrypted.close(); |
| 121 | + } catch (Exception e) { |
| 122 | + // ignore |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void testWithExplicitSession() throws Throwable { |
| 129 | + BsonString unencryptedValue = new BsonString("test"); |
| 130 | + |
| 131 | + FutureResultCallback<ClientSession> clientSessionCallback = new FutureResultCallback<ClientSession>(); |
| 132 | + clientEncrypted.startSession(clientSessionCallback); |
| 133 | + ClientSession clientSession = clientSessionCallback.get(); |
| 134 | + try { |
| 135 | + if (useTransaction) { |
| 136 | + clientSession.startTransaction(); |
| 137 | + } |
| 138 | + MongoCollection<BsonDocument> autoEncryptedCollection = clientEncrypted.getDatabase(getDefaultDatabaseName()) |
| 139 | + .getCollection(COLLECTION_NAME, BsonDocument.class); |
| 140 | + FutureResultCallback<Void> insertCallback = new FutureResultCallback<Void>(); |
| 141 | + autoEncryptedCollection.insertOne(clientSession, new BsonDocument().append("encrypted", new BsonString("test")), |
| 142 | + insertCallback); |
| 143 | + insertCallback.get(); |
| 144 | + |
| 145 | + FutureResultCallback<BsonDocument> findCallback = new FutureResultCallback<BsonDocument>(); |
| 146 | + autoEncryptedCollection.find(clientSession).first(findCallback); |
| 147 | + BsonDocument unencryptedDocument = findCallback.get(); |
| 148 | + assertEquals(unencryptedValue, unencryptedDocument.getString("encrypted")); |
| 149 | + |
| 150 | + if (useTransaction) { |
| 151 | + FutureResultCallback<Void> commitCallback = new FutureResultCallback<Void>(); |
| 152 | + clientSession.commitTransaction(commitCallback); |
| 153 | + commitCallback.get(); |
| 154 | + } |
| 155 | + } finally { |
| 156 | + clientSession.close(); |
| 157 | + } |
| 158 | + |
| 159 | + MongoCollection<BsonDocument> encryptedCollection = client.getDatabase(getDefaultDatabaseName()) |
| 160 | + .getCollection(COLLECTION_NAME, BsonDocument.class); |
| 161 | + FutureResultCallback<BsonDocument> findCallback = new FutureResultCallback<BsonDocument>(); |
| 162 | + encryptedCollection.find().first(findCallback); |
| 163 | + BsonDocument encryptedDocument = findCallback.get(); |
| 164 | + assertTrue(encryptedDocument.isBinary("encrypted")); |
| 165 | + assertEquals(6, encryptedDocument.getBinary("encrypted").getType()); |
| 166 | + } |
| 167 | + |
| 168 | + private static BsonDocument bsonDocumentFromPath(final String path) throws IOException, URISyntaxException { |
| 169 | + return getTestDocument(new File(ClientSideEncryptionSessionTest.class |
| 170 | + .getResource("/client-side-encryption-external/" + path).toURI())); |
| 171 | + } |
| 172 | +} |
0 commit comments