Skip to content

Commit 58c9bb0

Browse files
author
Chris Cho
authored
DOCSP-7658: Java CSFLE fixes (#572)
* Fixes to CSFLE Guide for the Java sections
1 parent 24bae56 commit 58c9bb0

File tree

4 files changed

+89
-74
lines changed

4 files changed

+89
-74
lines changed

source/includes/steps-fle-configure-the-mongodb-client.yaml

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ content: |
4040
:tabid: java-sync
4141
4242
.. code-block:: java
43-
:emphasize-lines: 2,3
43+
:emphasize-lines: 2,5
44+
45+
Map<String, Object> keyMap = new HashMap<String, Object>();
46+
keyMap.put("key", localMasterKey);
4447
45-
Map<String, Map<String, Object>> kmsProviders = new HashMap<String, Map<String, Object>>() {{
46-
put("local", new HashMap<String, Object>() {{
47-
put("key", localMasterKey);
48-
}});
49-
}};
48+
Map<String, Map<String, Object>> kmsProviders = new HashMap<String, Map<String, Object>>();
49+
kmsProviders.put("local", keyMap);
5050
.. tab::
5151
:tabid: nodejs
5252
@@ -86,9 +86,8 @@ content: |
8686
.. code-block:: java
8787
:emphasize-lines: 2
8888
89-
HashMap<String, BsonDocument> schemaMap = new HashMap<String, BsonDocument>() {{
90-
put("medicalRecords.patients", BsonDocument.parse(jsonSchema));
91-
}}
89+
HashMap<String, BsonDocument> schemaMap = new HashMap<String, BsonDocument>();
90+
schemaMap.put("medicalRecords.patients", BsonDocument.parse(jsonSchema));
9291
.. tab::
9392
:tabid: nodejs
9493
@@ -124,9 +123,8 @@ content: |
124123
.. code-block:: java
125124
:emphasize-lines: 2
126125
127-
final Map<String, Object> extraOptions = new HashMap<String, Object>() {{
128-
put("mongocryptdSpawnPath", "/usr/local/bin/mongocryptd");
129-
}};
126+
Map<String, Object> extraOptions = new HashMap<String, Object>();
127+
extraOptions.put("mongocryptdSpawnPath", "/usr/local/bin/mongocryptd");
130128
131129
.. admonition:: Encryption Binary Daemon
132130
:class: note
@@ -199,14 +197,14 @@ content: |
199197
:emphasize-lines: 3-8
200198
201199
MongoClientSettings clientSettings = MongoClientSettings.builder()
202-
.applyConnectionString(new ConnectionString("mongodb://localhost:27017"))
203-
.autoEncryptionSettings(AutoEncryptionSettings.builder()
204-
.keyVaultNamespace(keyVaultNamespace)
205-
.kmsProviders(kmsProviders)
206-
.schemaMap(schemaMap)
207-
.extraOptions(extraOptions)
208-
.build())
209-
.build();
200+
.applyConnectionString(new ConnectionString("mongodb://localhost:27017"))
201+
.autoEncryptionSettings(AutoEncryptionSettings.builder()
202+
.keyVaultNamespace(keyVaultNamespace)
203+
.kmsProviders(kmsProviders)
204+
.schemaMap(schemaMap)
205+
.extraOptions(extraOptions)
206+
.build())
207+
.build();
210208
211209
MongoClient mongoClient = MongoClients.create(clientSettings);
212210
.. tab::

source/includes/steps-fle-convert-to-a-remote-master-key.yaml

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,19 @@ content: |
4646
:tabid: java-sync
4747
4848
.. code-block:: java
49-
:emphasize-lines: 5-8
50-
51-
final BsonString masterKeyRegion = new BsonString("<Master Key AWS Region>"); // e.g. "us-east-2"
52-
final BsonString awsAccessKeyId = new BsonString("<IAM User Access Key ID>");
53-
final BsonString awsSecretAccessKey = new BsonString("<IAM User Secret Access Key>");
54-
Map<String, Map<String, Object>> kmsProviders = new HashMap<String, Map<String, Object>>() {{
55-
put("aws", new HashMap<String, Object>() {{
56-
put("region", masterKeyRegion);
57-
put("accessKeyId", awsAccessKeyId);
58-
put("secretAccessKey", awsSecretAccessKey);
59-
}});
60-
}};
49+
:emphasize-lines: 7-9, 11
50+
51+
BsonString masterKeyRegion = new BsonString("<Master Key AWS Region>"); // e.g. "us-east-2"
52+
BsonString awsAccessKeyId = new BsonString("<IAM User Access Key ID>");
53+
BsonString awsSecretAccessKey = new BsonString("<IAM User Secret Access Key>");
54+
Map<String, Map<String, Object>> kmsProviders = new HashMap<String, Map<String, Object>>();
55+
Map<String, Object> providerDetails = new HashMap<String, Object>();
56+
57+
providerDetails.put("accessKeyId", awsAccessKeyId);
58+
providerDetails.put("secretAccessKey", awsSecretAccessKey);
59+
providerDetails.put("region", masterKeyRegion);
60+
61+
kmsProviders.put("aws", providerDetails);
6162
.. tab::
6263
:tabid: nodejs
6364
@@ -100,7 +101,7 @@ content: |
100101
:tabid: java-sync
101102
102103
.. code-block:: Java
103-
:emphasize-lines: 9-14, 17
104+
:emphasize-lines: 9-14, 16-17
104105
105106
ClientEncryption clientEncryption = ClientEncryptions.create(ClientEncryptionSettings.builder()
106107
.keyVaultMongoClientSettings(MongoClientSettings.builder()
@@ -110,12 +111,12 @@ content: |
110111
.kmsProviders(kmsProviders)
111112
.build());
112113
113-
final BsonString masterKeyRegion = new BsonString("<Master Key AWS Region>"); // e.g. "us-east-2"
114-
final BsonString masterKeyArn = new BsonString("<Master Key ARN>"); // e.g. "arn:aws:kms:us-east-2:111122223333:alias/test-key"
115-
DataKeyOptions dataKeyOptions = new DataKeyOptions().masterKey(new BsonDocument() {{
116-
put("region", masterKeyRegion);
117-
put("key", masterKeyArn);
118-
}})
114+
BsonString masterKeyRegion = new BsonString("<Master Key AWS Region>"); // e.g. "us-east-2"
115+
BsonString masterKeyArn = new BsonString("<Master Key ARN>"); // e.g. "arn:aws:kms:us-east-2:111122223333:alias/test-key"
116+
DataKeyOptions dataKeyOptions = new DataKeyOptions().masterKey(
117+
new BsonDocument()
118+
.append("region", masterKeyRegion)
119+
.append("key", masterKeyArn));
119120
120121
BsonBinary dataKeyId = clientEncryption.createDataKey("aws", dataKeyOptions);
121122
String base64DataKeyId = Base64.getEncoder().encodeToString(dataKeyId.getData());

source/includes/steps-fle-create-data-encryption-key.yaml

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,23 @@ content: |
1111
:tabid: java-sync
1212
1313
.. code-block:: java
14-
:emphasize-lines: 11
14+
:emphasize-lines: 6
1515
1616
String path = "master-key.txt";
1717
18-
byte[] fileBytes = new byte[96];
18+
byte[] localMasterKey= new byte[96];
1919
2020
try (FileInputStream fis = new FileInputStream(path)) {
21-
fileBytes = fis.readAllBytes();
22-
} catch (IOException e) {
23-
e.printStackTrace();
21+
fis.readNBytes(localMasterKey, 0, 96);
2422
}
23+
.. note::
2524
26-
final byte[] localMasterKey = Arrays.copyOf(fileBytes, 96);
25+
The `FileInputStream#readNBytes <https://docs.oracle.com/javase/9/docs/api/java/io/InputStream.html#readNBytes-byte:A-int-int->`_
26+
method was introduced in Java 9. The helper method is used in
27+
this guide to keep the implementation concise. If you are using
28+
JDK 8, you may consider
29+
`implementing a custom solution <https://stackoverflow.com/questions/858980/file-to-byte-in-java>`_
30+
to read a file into a byte array.
2731
.. tab::
2832
:tabid: nodejs
2933
@@ -62,15 +66,13 @@ content: |
6266
for the ClientEncryptionSettings Builder.
6367
6468
.. code-block:: java
65-
:emphasize-lines: 4,5
69+
:emphasize-lines: 2,5
6670
67-
String kmsProvider = "local";
71+
Map<String, Object> keyMap = new HashMap<String, Object>();
72+
keyMap.put("key", localMasterKey);
6873
69-
Map<String, Map<String, Object>> kmsProviders = new HashMap<String, Map<String, Object>>() {{
70-
put(kmsProvider, new HashMap<String, Object>() {{
71-
put("key", localMasterKey);
72-
}});
73-
}};
74+
Map<String, Map<String, Object>> kmsProviders = new HashMap<String, Map<String, Object>>();
75+
kmsProviders.put("local", keyMap);
7476
.. tab::
7577
:tabid: nodejs
7678
@@ -115,18 +117,18 @@ content: |
115117
String keyVaultNamespace = "encryption.__keyVault";
116118
117119
ClientEncryptionSettings clientEncryptionSettings = ClientEncryptionSettings.builder()
118-
.keyVaultMongoClientSettings(MongoClientSettings.builder()
119-
.applyConnectionString(new ConnectionString(connectionString))
120-
.build())
121-
.keyVaultNamespace(keyVaultNamespace)
122-
.kmsProviders(kmsProviders)
123-
.build();
120+
.keyVaultMongoClientSettings(MongoClientSettings.builder()
121+
.applyConnectionString(new ConnectionString(connectionString))
122+
.build())
123+
.keyVaultNamespace(keyVaultNamespace)
124+
.kmsProviders(kmsProviders)
125+
.build();
124126
125127
ClientEncryption clientEncryption = ClientEncryptions.create(clientEncryptionSettings);
126128
BsonBinary dataKeyId = clientEncryption.createDataKey(kmsProvider, new DataKeyOptions());
127129
System.out.println("DataKeyId [UUID]: " + dataKeyId.asUuid());
128130
129-
final String base64DataKeyId = Base64.getEncoder().encodeToString(dataKeyId.getData());
131+
String base64DataKeyId = Base64.getEncoder().encodeToString(dataKeyId.getData());
130132
System.out.println("DataKeyId [base64]: " + base64DataKeyId);
131133
132134
The ``createDataKey()`` method returns a :java-docs-latest:`BsonBinary
@@ -190,7 +192,7 @@ content: |
190192
191193
client = MongoClient(connection_string)
192194
client_encryption = ClientEncryption(
193-
kms_providers, # pass in the kms_providers variable from the previous step
195+
kms_providers, # pass in the kms_providers variable from the previous step
194196
key_vault_namespace,
195197
client,
196198
CodecOptions(uuid_representation=STANDARD)
@@ -338,7 +340,7 @@ content: |
338340
_bsontype: 'Binary',
339341
sub_type: 0,
340342
position: 160,
341-
buffer: <Buffer f1 4a 9f bd aa ac c9 89 e9 b3 da 48 72 8e a8 62 97 2a 4a a0 d2 d4 2d a8 f0 74 9c 16 4d 2c 95 34 19 22 05 05 84 0e 41 42 12 1e e3 b5 f0 b1 c5 a8 37 b8 ... 110 more bytes>
343+
buffer: <Buffer f1 4a 9f bd aa ac c9 89 e9 b3 da 48 72 8e a8 62 97 2a 4a a0 d2 d4 2d a8 f0 74 9c 16 4d 2c 95 34 19 22 05 05 84 0e 41 42 12 1e e3 b5 f0 b1 c5 a8 37 b8 ... 110 more bytes>
342344
},
343345
creationDate: 2019-09-25T22:22:54.017Z,
344346
updateDate: 2019-09-25T22:22:54.017Z,

source/use-cases/client-side-field-level-encryption-guide.txt

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,21 @@ Additional Dependencies
181181
.. tab::
182182
:tabid: java-sync
183183

184-
No additional dependencies are required for the Java driver.
184+
.. list-table::
185+
:header-rows: 1
185186

187+
* - Dependency Name
188+
- Description
189+
190+
* - `JDK 8 or later
191+
<https://www.oracle.com/technetwork/java/javase/downloads/index.html>`_
192+
- While the current driver is compatible with older versions of
193+
the JDK, the CSFLE feature is only compatible with JDK 8
194+
and later.
195+
196+
* - :java-docs-latest:`libmongocrypt <driver/tutorials/client-side-encryption/>`
197+
- The `libmongocrypt` library contains bindings to communicate
198+
with the native library that manages the encryption.
186199
.. tab::
187200
:tabid: nodejs
188201

@@ -251,17 +264,15 @@ To begin development, MedcoMD engineers generate a locally-managed master key:
251264
import java.security.SecureRandom;
252265

253266
public class CreateMasterKeyFile {
254-
public static void main(final String[] args) {
267+
public static void main(String[] args) throws IOException {
255268

256-
final byte[] localMasterKey = new byte[96];
257-
new SecureRandom().nextBytes(localMasterKey);
269+
byte[] localMasterKey = new byte[96];
270+
new SecureRandom().nextBytes(localMasterKey);
258271

259-
try (FileOutputStream stream = new FileOutputStream("master-key.txt")) {
260-
stream.write(localMasterKey);
261-
} catch (IOException e) {
262-
e.printStackTrace();
272+
try (FileOutputStream stream = new FileOutputStream("master-key.txt")) {
273+
stream.write(localMasterKey);
274+
}
263275
}
264-
}
265276
}
266277
.. tab::
267278
:tabid: nodejs
@@ -597,15 +608,18 @@ MedcoMD engineers write a function to create a new patient record:
597608
int policyNumber,
598609
String provider
599610
) {
611+
612+
Document insurance = new Document()
613+
.append("policyNumber", policyNumber)
614+
.append("provider", provider);
615+
600616
Document patient = new Document()
601617
.append("name", name)
602618
.append("ssn", ssn)
603619
.append("bloodType", bloodType)
604-
.append("medicalRecords", medicalRecords);
605-
Document insurance = new Document()
606-
.append("policyNumber", policyNumber)
607-
.append("provider", provider)
620+
.append("medicalRecords", medicalRecords)
608621
.append("insurance", insurance);
622+
609623
collection.insertOne(patient);
610624
}
611625
.. tab::

0 commit comments

Comments
 (0)