@@ -60,39 +60,49 @@ More information about libmongocrypt will soon be available from the official do
60
60
61
61
The following is a sample app that assumes key and schema have already been created in MongoDB. The example uses a local key,
62
62
however using AWS Key Management Service is also an option. The data in the ` encryptedField ` field is automatically encrypted on the
63
- insert and decrypted when using find on the client side:
63
+ insert and decrypted when using find on the client side. The following code snippet comes from the
64
+ [ ` ClientSideEncryptionSimpleTour.java ` ] ({{< srcref "driver-sync/src/examples/tour/ClientSideEncryptionSimpleTour.java">}}) example code
65
+ that can be found with the driver source on github:
64
66
65
67
``` java
66
68
import com.mongodb.AutoEncryptionSettings ;
67
69
import com.mongodb.MongoClientSettings ;
70
+ import com.mongodb.client.MongoClient ;
68
71
import com.mongodb.client.MongoClients ;
72
+ import com.mongodb.client.MongoCollection ;
69
73
import org.bson.Document ;
70
74
71
75
import java.security.SecureRandom ;
76
+ import java.util.HashMap ;
72
77
import java.util.Map ;
73
78
74
- public class ClientSideEncryptionSimpleTest {
79
+ public class ClientSideEncryptionSimpleTour {
75
80
76
- public static void main (String [] args ) {
81
+ public static void main (final String [] args ) {
77
82
78
83
// This would have to be the same master key as was used to create the encryption key
79
- var localMasterKey = new byte [96 ];
84
+ final byte [] localMasterKey = new byte [96 ];
80
85
new SecureRandom (). nextBytes(localMasterKey);
81
86
82
- var kmsProviders = Map . of(" local" , Map . < String , Object > of(" key" , localMasterKey));
83
- var keyVaultNamespace = " admin.datakeys" ;
87
+ Map<String , Map<String , Object > > kmsProviders = new HashMap<String , Map<String , Object > > () {{
88
+ put(" local" , new HashMap<String , Object > () {{
89
+ put(" key" , localMasterKey);
90
+ }});
91
+ }};
84
92
85
- var autoEncryptionSettings = AutoEncryptionSettings . builder()
86
- .keyVaultNamespace(keyVaultNamespace)
87
- .kmsProviders(kmsProviders)
88
- .build();
93
+ String keyVaultNamespace = " admin.datakeys" ;
89
94
90
- var clientSettings = MongoClientSettings . builder()
91
- .autoEncryptionSettings(autoEncryptionSettings)
92
- .build();
95
+ AutoEncryptionSettings autoEncryptionSettings = AutoEncryptionSettings . builder()
96
+ .keyVaultNamespace(keyVaultNamespace)
97
+ .kmsProviders(kmsProviders)
98
+ .build();
93
99
94
- var client = MongoClients . create(clientSettings);
95
- var collection = client. getDatabase(" test" ). getCollection(" coll" );
100
+ MongoClientSettings clientSettings = MongoClientSettings . builder()
101
+ .autoEncryptionSettings(autoEncryptionSettings)
102
+ .build();
103
+
104
+ MongoClient mongoClient = MongoClients . create(clientSettings);
105
+ MongoCollection<Document > collection = mongoClient. getDatabase(" test" ). getCollection(" coll" );
96
106
collection. drop(); // Clear old data
97
107
98
108
collection. insertOne(new Document (" encryptedField" , " 123456789" ));
@@ -106,54 +116,62 @@ public class ClientSideEncryptionSimpleTest {
106
116
Auto encryption is an ** enterprise** only feature.
107
117
{{% /note %}}
108
118
109
- The following example shows how to configure the ` AutoEncryptionSettings ` instance to create a new key and setting the json schema map:
119
+ The following example shows how to configure the ` AutoEncryptionSettings ` instance to create a new key and setting the json schema map.
120
+ The full code snippet can be found in
121
+ [ ` ClientSideEncryptionAutoEncryptionSettingsTour.java ` ] ({{< srcref "driver-sync/src/examples/tour/ClientSideEncryptionAutoEncryptionSettingsTour.java">}}):
110
122
111
123
``` java
112
- import com.mongodb.ConnectionString ;
113
124
import com.mongodb.ClientEncryptionSettings ;
125
+ import com.mongodb.ConnectionString ;
126
+ import com.mongodb.client.model.vault.DataKeyOptions ;
127
+ import com.mongodb.client.vault.ClientEncryption ;
114
128
import com.mongodb.client.vault.ClientEncryptions ;
129
+ import org.bson.BsonBinary ;
130
+ import org.bson.BsonDocument ;
115
131
116
- ...
132
+ import java.util.Base64 ;
117
133
134
+ ...
118
135
119
- var keyVaultNamespace = " admin.datakeys" ;
120
- var clientEncryptionSettings = ClientEncryptionSettings . builder()
136
+ String keyVaultNamespace = " admin.datakeys" ;
137
+ ClientEncryptionSettings clientEncryptionSettings = ClientEncryptionSettings . builder()
121
138
.keyVaultMongoClientSettings(MongoClientSettings . builder()
122
139
.applyConnectionString(new ConnectionString (" mongodb://localhost" ))
123
140
.build())
124
141
.keyVaultNamespace(keyVaultNamespace)
125
142
.kmsProviders(kmsProviders)
126
143
.build();
127
144
128
- var clientEncryption = ClientEncryptions . create(clientEncryptionSettings);
129
- var dataKeyId = keyVault. createDataKey(" local" , new DataKeyOptions ());
130
- var base64DataKeyId = Base64 . getEncoder(). encodeToString(dataKeyId. getData());
131
-
132
- var dbName = " test" ;
133
- var collName = " coll" ;
134
- var autoEncryptionSettings = AutoEncryptionSettings . builder()
135
- .keyVaultNamespace(keyVaultNamespace)
136
- .kmsProviders(kmsProviders)
137
- .namespaceToLocalSchemaDocumentMap(Map . of(dbName + " ." + collName,
138
- // Need a schema that references the new data key
139
- BsonDocument . parse(" {" +
140
- " properties: {" +
141
- " encryptedField: {" +
142
- " encrypt: {" +
143
- " keyId: [{" +
144
- " \" $binary\" : {" +
145
- " \" base64\" : \" " + base64DataKeyId + " \" ," +
146
- " \" subType\" : \" 04\" " +
147
- " }" +
148
- " }]," +
149
- " bsonType: \" string\" ," +
150
- " algorithm: \" AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic\" " +
151
- " }" +
152
- " }" +
153
- " }," +
154
- " \" bsonType\" : \" object\" " +
155
- " }" ))
156
- ). build();
145
+ ClientEncryption clientEncryption = ClientEncryptions . create(clientEncryptionSettings);
146
+ BsonBinary dataKeyId = clientEncryption. createDataKey(" local" , new DataKeyOptions ());
147
+ final String base64DataKeyId = Base64 . getEncoder(). encodeToString(dataKeyId. getData());
148
+
149
+ final String dbName = " test" ;
150
+ final String collName = " coll" ;
151
+ AutoEncryptionSettings autoEncryptionSettings = AutoEncryptionSettings . builder()
152
+ .keyVaultNamespace(keyVaultNamespace)
153
+ .kmsProviders(kmsProviders)
154
+ .schemaMap(new HashMap<String , BsonDocument > () {{
155
+ put(dbName + " ." + collName,
156
+ // Need a schema that references the new data key
157
+ BsonDocument . parse(" {"
158
+ + " properties: {"
159
+ + " encryptedField: {"
160
+ + " encrypt: {"
161
+ + " keyId: [{"
162
+ + " \" $binary\" : {"
163
+ + " \" base64\" : \" " + base64DataKeyId + " \" ,"
164
+ + " \" subType\" : \" 04\" "
165
+ + " }"
166
+ + " }],"
167
+ + " bsonType: \" string\" ,"
168
+ + " algorithm: \" AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic\" "
169
+ + " }"
170
+ + " }"
171
+ + " },"
172
+ + " \" bsonType\" : \" object\" "
173
+ + " }" ));
174
+ }}). build();
157
175
```
158
176
159
177
{{% note %}}
0 commit comments