|
| 1 | +/* Copyright 2019-present MongoDB Inc. |
| 2 | +* |
| 3 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +* you may not use this file except in compliance with the License. |
| 5 | +* You may obtain a copy of the License at |
| 6 | +* |
| 7 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +* |
| 9 | +* Unless required by applicable law or agreed to in writing, software |
| 10 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +* See the License for the specific language governing permissions and |
| 13 | +* limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +using MongoDB.Driver.Core.Misc; |
| 17 | +using MongoDB.Driver.Core.TestHelpers.XunitExtensions; |
| 18 | +using System; |
| 19 | +using System.Collections.Generic; |
| 20 | +using System.Threading; |
| 21 | +using MongoDB.Bson; |
| 22 | +using MongoDB.Driver.Encryption; |
| 23 | +using Xunit; |
| 24 | +using Xunit.Abstractions; |
| 25 | + |
| 26 | +namespace MongoDB.Driver.Examples |
| 27 | +{ |
| 28 | + public class ClientEncryptionExamples |
| 29 | + { |
| 30 | + private const string LocalMasterKey = "Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk"; |
| 31 | + |
| 32 | + private readonly ITestOutputHelper _output; |
| 33 | + |
| 34 | + public ClientEncryptionExamples(ITestOutputHelper output) |
| 35 | + { |
| 36 | + _output = output; |
| 37 | + } |
| 38 | + |
| 39 | + [Fact] |
| 40 | + public void ClientSideEncryptionSimpleTour() |
| 41 | + { |
| 42 | + RequireServer.Check().Supports(Feature.ClientSideEncryption); |
| 43 | + |
| 44 | + var localMasterKey = Convert.FromBase64String(LocalMasterKey); |
| 45 | + |
| 46 | + var kmsProviders = new Dictionary<string, IReadOnlyDictionary<string, object>>(); |
| 47 | + var localKey = new Dictionary<string, object> |
| 48 | + { |
| 49 | + { "key", localMasterKey } |
| 50 | + }; |
| 51 | + kmsProviders.Add("local", localKey); |
| 52 | + |
| 53 | + var keyVaultNamespace = CollectionNamespace.FromFullName("admin.datakeys"); |
| 54 | + var autoEncryptionOptions = new AutoEncryptionOptions(keyVaultNamespace, kmsProviders); |
| 55 | + |
| 56 | + var mongoClientSettings = new MongoClientSettings |
| 57 | + { |
| 58 | + AutoEncryptionOptions = autoEncryptionOptions |
| 59 | + }; |
| 60 | + var client = new MongoClient(mongoClientSettings); |
| 61 | + var database = client.GetDatabase("test"); |
| 62 | + database.DropCollection("coll"); |
| 63 | + var collection = database.GetCollection<BsonDocument>("coll"); |
| 64 | + |
| 65 | + collection.InsertOne(new BsonDocument("encryptedField", "123456789")); |
| 66 | + |
| 67 | + var result = collection.Find(FilterDefinition<BsonDocument>.Empty).First(); |
| 68 | + _output.WriteLine(result.ToJson()); |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + public void ClientSideEncryptionAutoEncryptionSettingsTour() |
| 73 | + { |
| 74 | + RequireServer.Check().Supports(Feature.ClientSideEncryption); |
| 75 | + |
| 76 | + var localMasterKey = Convert.FromBase64String(LocalMasterKey); |
| 77 | + |
| 78 | + var kmsProviders = new Dictionary<string, IReadOnlyDictionary<string, object>>(); |
| 79 | + var localKey = new Dictionary<string, object> |
| 80 | + { |
| 81 | + { "key", localMasterKey } |
| 82 | + }; |
| 83 | + kmsProviders.Add("local", localKey); |
| 84 | + |
| 85 | + var keyVaultNamespace = CollectionNamespace.FromFullName("admin.datakeys"); |
| 86 | + var keyVaultMongoClient = new MongoClient(); |
| 87 | + var clientEncryptionSettings = new ClientEncryptionOptions( |
| 88 | + keyVaultMongoClient, |
| 89 | + keyVaultNamespace, |
| 90 | + kmsProviders); |
| 91 | + |
| 92 | + var clientEncryption = new ClientEncryption(clientEncryptionSettings); |
| 93 | + var dataKeyId = clientEncryption.CreateDataKey("local", new DataKeyOptions(), CancellationToken.None); |
| 94 | + var base64DataKeyId = Convert.ToBase64String(GuidConverter.ToBytes(dataKeyId, GuidRepresentation.Standard)); |
| 95 | + clientEncryption.Dispose(); |
| 96 | + |
| 97 | + var collectionNamespace = CollectionNamespace.FromFullName("test.coll"); |
| 98 | + |
| 99 | + var schemaMap = $@"{{ |
| 100 | + properties: {{ |
| 101 | + encryptedField: {{ |
| 102 | + encrypt: {{ |
| 103 | + keyId: [{{ |
| 104 | + '$binary' : {{ |
| 105 | + 'base64' : '{base64DataKeyId}', |
| 106 | + 'subType' : '04' |
| 107 | + }} |
| 108 | + }}], |
| 109 | + bsonType: 'string', |
| 110 | + algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic' |
| 111 | + }} |
| 112 | + }} |
| 113 | + }}, |
| 114 | + 'bsonType': 'object' |
| 115 | + }}"; |
| 116 | + var autoEncryptionSettings = new AutoEncryptionOptions( |
| 117 | + keyVaultNamespace, |
| 118 | + kmsProviders, |
| 119 | + schemaMap: new Dictionary<string, BsonDocument>() |
| 120 | + { |
| 121 | + { collectionNamespace.ToString(), BsonDocument.Parse(schemaMap) } |
| 122 | + }); |
| 123 | + var clientSettings = new MongoClientSettings |
| 124 | + { |
| 125 | + AutoEncryptionOptions = autoEncryptionSettings |
| 126 | + }; |
| 127 | + var client = new MongoClient(clientSettings); |
| 128 | + var database = client.GetDatabase("test"); |
| 129 | + database.DropCollection("coll"); |
| 130 | + var collection = database.GetCollection<BsonDocument>("coll"); |
| 131 | + |
| 132 | + collection.InsertOne(new BsonDocument("encryptedField", "123456789")); |
| 133 | + |
| 134 | + var result = collection.Find(FilterDefinition<BsonDocument>.Empty).First(); |
| 135 | + _output.WriteLine(result.ToJson()); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments