|
| 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 tour; |
| 18 | + |
| 19 | +import com.mongodb.AutoEncryptionSettings; |
| 20 | +import com.mongodb.ClientEncryptionSettings; |
| 21 | +import com.mongodb.ConnectionString; |
| 22 | +import com.mongodb.MongoClientSettings; |
| 23 | +import com.mongodb.MongoNamespace; |
| 24 | +import com.mongodb.client.MongoClient; |
| 25 | +import com.mongodb.client.MongoClients; |
| 26 | +import com.mongodb.client.MongoCollection; |
| 27 | +import com.mongodb.client.model.Filters; |
| 28 | +import com.mongodb.client.model.IndexOptions; |
| 29 | +import com.mongodb.client.model.Indexes; |
| 30 | +import com.mongodb.client.model.vault.DataKeyOptions; |
| 31 | +import com.mongodb.client.model.vault.EncryptOptions; |
| 32 | +import com.mongodb.client.vault.ClientEncryption; |
| 33 | +import com.mongodb.client.vault.ClientEncryptions; |
| 34 | +import org.bson.BsonBinary; |
| 35 | +import org.bson.BsonString; |
| 36 | +import org.bson.Document; |
| 37 | + |
| 38 | +import java.security.SecureRandom; |
| 39 | +import java.util.HashMap; |
| 40 | +import java.util.Map; |
| 41 | + |
| 42 | +/** |
| 43 | + * ClientSideEncryption explicit encryption and decryption tour |
| 44 | + */ |
| 45 | +public class ClientSideEncryptionExplicitEncryptionOnlyTour { |
| 46 | + |
| 47 | + /** |
| 48 | + * Run this main method to see the output of this quick example. |
| 49 | + * |
| 50 | + * @param args ignored args |
| 51 | + */ |
| 52 | + public static void main(final String[] args) { |
| 53 | + |
| 54 | + // This would have to be the same master key as was used to create the encryption key |
| 55 | + final byte[] localMasterKey = new byte[96]; |
| 56 | + new SecureRandom().nextBytes(localMasterKey); |
| 57 | + |
| 58 | + Map<String, Map<String, Object>> kmsProviders = new HashMap<String, Map<String, Object>>() {{ |
| 59 | + put("local", new HashMap<String, Object>() {{ |
| 60 | + put("key", localMasterKey); |
| 61 | + }}); |
| 62 | + }}; |
| 63 | + |
| 64 | + MongoNamespace keyVaultNamespace = new MongoNamespace("encryption.testKeyVault"); |
| 65 | + |
| 66 | + MongoClientSettings clientSettings = MongoClientSettings.builder() |
| 67 | + .autoEncryptionSettings(AutoEncryptionSettings.builder() |
| 68 | + .keyVaultNamespace(keyVaultNamespace.getFullName()) |
| 69 | + .kmsProviders(kmsProviders) |
| 70 | + .bypassAutoEncryption(true) |
| 71 | + .build()) |
| 72 | + .build(); |
| 73 | + MongoClient mongoClient = MongoClients.create(clientSettings); |
| 74 | + |
| 75 | + // Set up the key vault for this example |
| 76 | + MongoCollection<Document> keyVaultCollection = mongoClient.getDatabase(keyVaultNamespace.getDatabaseName()) |
| 77 | + .getCollection(keyVaultNamespace.getCollectionName()); |
| 78 | + keyVaultCollection.drop(); |
| 79 | + |
| 80 | + // Ensure that two data keys cannot share the same keyAltName. |
| 81 | + keyVaultCollection.createIndex(Indexes.ascending("keyAltNames"), |
| 82 | + new IndexOptions().unique(true) |
| 83 | + .partialFilterExpression(Filters.exists("keyAltNames"))); |
| 84 | + |
| 85 | + MongoCollection<Document> collection = mongoClient.getDatabase("test").getCollection("coll"); |
| 86 | + collection.drop(); // Clear old data |
| 87 | + |
| 88 | + // Create the ClientEncryption instance |
| 89 | + ClientEncryptionSettings clientEncryptionSettings = ClientEncryptionSettings.builder() |
| 90 | + .keyVaultMongoClientSettings(MongoClientSettings.builder() |
| 91 | + .applyConnectionString(new ConnectionString("mongodb://localhost")) |
| 92 | + .build()) |
| 93 | + .keyVaultNamespace(keyVaultNamespace.getFullName()) |
| 94 | + .kmsProviders(kmsProviders) |
| 95 | + .build(); |
| 96 | + |
| 97 | + ClientEncryption clientEncryption = ClientEncryptions.create(clientEncryptionSettings); |
| 98 | + |
| 99 | + BsonBinary dataKeyId = clientEncryption.createDataKey("local", new DataKeyOptions()); |
| 100 | + |
| 101 | + // Explicitly encrypt a field |
| 102 | + BsonBinary encryptedFieldValue = clientEncryption.encrypt(new BsonString("123456789"), |
| 103 | + new EncryptOptions("AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic").keyId(dataKeyId)); |
| 104 | + |
| 105 | + collection.insertOne(new Document("encryptedField", encryptedFieldValue)); |
| 106 | + |
| 107 | + // Automatically decrypts the encrypted field. |
| 108 | + Document doc = collection.find().first(); |
| 109 | + System.out.println(doc.toJson()); |
| 110 | + System.out.println(doc.get("encryptedField")); |
| 111 | + |
| 112 | + // release resources |
| 113 | + clientEncryption.close(); |
| 114 | + mongoClient.close(); |
| 115 | + } |
| 116 | +} |
0 commit comments