|
| 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.ClientEncryptionSettings |
| 21 | +import com.mongodb.MongoNamespace |
| 22 | +import com.mongodb.MongoWriteException |
| 23 | +import com.mongodb.async.FutureResultCallback |
| 24 | +import com.mongodb.async.client.vault.ClientEncryption |
| 25 | +import com.mongodb.async.client.vault.ClientEncryptions |
| 26 | +import com.mongodb.client.test.CollectionHelper |
| 27 | +import com.mongodb.internal.connection.TestCommandListener |
| 28 | +import org.bson.BsonDocument |
| 29 | +import org.bson.BsonString |
| 30 | +import org.bson.codecs.BsonDocumentCodec |
| 31 | + |
| 32 | +import static com.mongodb.ClusterFixture.isNotAtLeastJava8 |
| 33 | +import static com.mongodb.ClusterFixture.serverVersionAtLeast |
| 34 | +import static com.mongodb.async.client.Fixture.getDefaultDatabaseName |
| 35 | +import static com.mongodb.async.client.Fixture.getMongoClientBuilderFromConnectionString |
| 36 | +import static com.mongodb.async.client.Fixture.getMongoClientSettings |
| 37 | +import static java.util.Collections.singletonMap |
| 38 | +import static org.junit.Assume.assumeFalse |
| 39 | +import static org.junit.Assume.assumeTrue |
| 40 | +import static util.JsonPoweredTestHelper.getTestDocument |
| 41 | + |
| 42 | +class ClientSideEncryptionBsonSizeLimitsSpecification extends FunctionalSpecification { |
| 43 | + |
| 44 | + private final MongoNamespace keyVaultNamespace = new MongoNamespace('test.datakeys') |
| 45 | + private final MongoNamespace autoEncryptingCollectionNamespace = new MongoNamespace(getDefaultDatabaseName(), |
| 46 | + 'ClientSideEncryptionProseTestSpecification') |
| 47 | + private final TestCommandListener commandListener = new TestCommandListener() |
| 48 | + |
| 49 | + private MongoClient autoEncryptingClient |
| 50 | + private ClientEncryption clientEncryption |
| 51 | + private MongoCollection<BsonDocument> autoEncryptingDataCollection |
| 52 | + |
| 53 | + def setup() { |
| 54 | + assumeFalse(isNotAtLeastJava8()) |
| 55 | + assumeTrue(serverVersionAtLeast(4, 2)) |
| 56 | + assumeTrue('Key vault tests disabled', |
| 57 | + System.getProperty('org.mongodb.test.awsAccessKeyId') != null |
| 58 | + && !System.getProperty('org.mongodb.test.awsAccessKeyId').isEmpty()) |
| 59 | + Fixture.drop(keyVaultNamespace) |
| 60 | + Fixture.drop(autoEncryptingCollectionNamespace) |
| 61 | + |
| 62 | + new CollectionHelper<>(new BsonDocumentCodec(), keyVaultNamespace).insertDocuments( |
| 63 | + getTestDocument('/client-side-encryption-limits/limits-key.json')) |
| 64 | + |
| 65 | + def providerProperties = |
| 66 | + ['local': ['key': Base64.getDecoder().decode('Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN' |
| 67 | + + '3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk')] |
| 68 | + ] |
| 69 | + |
| 70 | + autoEncryptingClient = MongoClients.create(getMongoClientBuilderFromConnectionString() |
| 71 | + .autoEncryptionSettings(AutoEncryptionSettings.builder() |
| 72 | + .keyVaultNamespace(keyVaultNamespace.fullName) |
| 73 | + .kmsProviders(providerProperties) |
| 74 | + .schemaMap(singletonMap(autoEncryptingCollectionNamespace.fullName, |
| 75 | + getTestDocument('/client-side-encryption-limits/limits-schema.json'))) |
| 76 | + .build()) |
| 77 | + .addCommandListener(commandListener) |
| 78 | + .build()) |
| 79 | + |
| 80 | + autoEncryptingDataCollection = autoEncryptingClient.getDatabase(autoEncryptingCollectionNamespace.databaseName) |
| 81 | + .getCollection(autoEncryptingCollectionNamespace.collectionName, BsonDocument) |
| 82 | + |
| 83 | + clientEncryption = ClientEncryptions.create(ClientEncryptionSettings.builder() |
| 84 | + .keyVaultMongoClientSettings(getMongoClientSettings()) |
| 85 | + .keyVaultNamespace(keyVaultNamespace.fullName) |
| 86 | + .kmsProviders(providerProperties) |
| 87 | + .build()) |
| 88 | + } |
| 89 | + |
| 90 | + def 'test BSON size limits'() { |
| 91 | + when: |
| 92 | + def callback = new FutureResultCallback() |
| 93 | + autoEncryptingDataCollection.insertOne( |
| 94 | + new BsonDocument('_id', new BsonString('over_2mib_under_16mib')) |
| 95 | + .append('unencrypted', new BsonString('a' * 2097152)), callback) |
| 96 | + callback.get() |
| 97 | + |
| 98 | + then: |
| 99 | + noExceptionThrown() |
| 100 | + |
| 101 | + when: |
| 102 | + callback = new FutureResultCallback() |
| 103 | + autoEncryptingDataCollection.insertOne(getTestDocument('/client-side-encryption-limits/limits-doc.json') |
| 104 | + .append('_id', new BsonString('encryption_exceeds_2mib')) |
| 105 | + .append('unencrypted', new BsonString('a' * (2097152 - 2000))), callback) |
| 106 | + callback.get() |
| 107 | + |
| 108 | + then: |
| 109 | + noExceptionThrown() |
| 110 | + |
| 111 | + when: |
| 112 | + commandListener.reset() |
| 113 | + callback = new FutureResultCallback() |
| 114 | + autoEncryptingDataCollection.insertMany( |
| 115 | + [ |
| 116 | + new BsonDocument('_id', new BsonString('over_2mib_1')) |
| 117 | + .append('unencrypted', new BsonString('a' * 2097152)), |
| 118 | + new BsonDocument('_id', new BsonString('over_2mib_2')) |
| 119 | + .append('unencrypted', new BsonString('a' * 2097152)) |
| 120 | + ], callback) |
| 121 | + callback.get() |
| 122 | + |
| 123 | + then: |
| 124 | + noExceptionThrown() |
| 125 | + countStartedEvents('insert') == 2 |
| 126 | + |
| 127 | + when: |
| 128 | + commandListener.reset() |
| 129 | + callback = new FutureResultCallback() |
| 130 | + autoEncryptingDataCollection.insertMany( |
| 131 | + [ |
| 132 | + getTestDocument('/client-side-encryption-limits/limits-doc.json') |
| 133 | + .append('_id', new BsonString('encryption_exceeds_2mib_1')) |
| 134 | + .append('unencrypted', new BsonString('a' * (2097152 - 2000))), |
| 135 | + getTestDocument('/client-side-encryption-limits/limits-doc.json') |
| 136 | + .append('_id', new BsonString('encryption_exceeds_2mib_2')) |
| 137 | + .append('unencrypted', new BsonString('a' * (2097152 - 2000))), |
| 138 | + ], callback) |
| 139 | + callback.get() |
| 140 | + |
| 141 | + then: |
| 142 | + noExceptionThrown() |
| 143 | + countStartedEvents('insert') == 2 |
| 144 | + |
| 145 | + when: |
| 146 | + callback = new FutureResultCallback() |
| 147 | + autoEncryptingDataCollection.insertOne( |
| 148 | + new BsonDocument('_id', new BsonString('under_16mib')) |
| 149 | + .append('unencrypted', new BsonString('a' * (16777216 - 2000))), callback) |
| 150 | + callback.get() |
| 151 | + |
| 152 | + then: |
| 153 | + noExceptionThrown() |
| 154 | + |
| 155 | + when: |
| 156 | + callback = new FutureResultCallback() |
| 157 | + autoEncryptingDataCollection.insertOne(getTestDocument('/client-side-encryption-limits/limits-doc.json') |
| 158 | + .append('_id', new BsonString('encryption_exceeds_16mib')) |
| 159 | + .append('unencrypted', new BsonString('a' * (16777216 - 2000))), callback) |
| 160 | + callback.get() |
| 161 | + |
| 162 | + then: |
| 163 | + thrown(MongoWriteException) |
| 164 | + } |
| 165 | + |
| 166 | + private int countStartedEvents(String name) { |
| 167 | + int count = 0 |
| 168 | + for (def cur : commandListener.commandStartedEvents) { |
| 169 | + if (cur.commandName == name) { |
| 170 | + count++ |
| 171 | + } |
| 172 | + } |
| 173 | + count |
| 174 | + } |
| 175 | +} |
0 commit comments