Skip to content

Commit d1b937a

Browse files
DOCSP-31527 Add byte length check to QE quick starts (#5515)
* Check byte length when reading local key * fix emphasized lines * update byte length check to equality
1 parent bb5445c commit d1b937a

File tree

7 files changed

+35
-8
lines changed

7 files changed

+35
-8
lines changed

source/core/queryable-encryption/quick-start.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ Procedure
367367
.. literalinclude:: /includes/qe-tutorials/mongosh/queryable-encryption-helpers.js
368368
:start-after: start-get-local-key
369369
:end-before: end-get-local-key
370-
:emphasize-lines: 3-7
370+
:emphasize-lines: 10-14
371371
:language: javascript
372372
:dedent:
373373

@@ -377,7 +377,7 @@ Procedure
377377
.. literalinclude:: /includes/qe-tutorials/node/queryable-encryption-helpers.js
378378
:start-after: start-get-local-key
379379
:end-before: end-get-local-key
380-
:emphasize-lines: 3-7
380+
:emphasize-lines: 10-14
381381
:language: javascript
382382
:dedent:
383383

@@ -388,7 +388,7 @@ Procedure
388388
:start-after: start-get-local-key
389389
:end-before: end-get-local-key
390390
:language: python
391-
:emphasize-lines: 4-8
391+
:emphasize-lines: 6-10
392392
:dedent:
393393

394394
.. tab::
@@ -408,7 +408,7 @@ Procedure
408408
:start-after: start-get-local-key
409409
:end-before: end-get-local-key
410410
:language: go
411-
:emphasize-lines: 6
411+
:emphasize-lines: 8
412412
:dedent:
413413

414414
.. tab::
@@ -418,7 +418,7 @@ Procedure
418418
:start-after: start-get-local-key
419419
:end-before: end-get-local-key
420420
:language: csharp
421-
:emphasize-lines: 8-13
421+
:emphasize-lines: 13-18
422422
:dedent:
423423

424424
.. step:: Set Your Automatic Encryption Options

source/includes/qe-tutorials/csharp/QueryableEncryptionHelpers.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ public Dictionary<string, IReadOnlyDictionary<string, object>> GetKmsProviderCre
100100
var localCustomerMasterKeyBase64 = File.ReadAllText("customer-master-key.txt");
101101
var localCustomerMasterKeyBytes = Convert.FromBase64String(localCustomerMasterKeyBase64);
102102

103+
if (localCustomerMasterKeyBytes.Length != 96)
104+
{
105+
throw new Exception("Expected the customer master key file to be 96 bytes.");
106+
}
107+
103108
var localOptions = new Dictionary<string, object>
104109
{
105110
{ "key", localCustomerMasterKeyBytes }

source/includes/qe-tutorials/go/queryable_encryption_helpers.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ func GetKmsProviderCredentials(kmsProviderName string) map[string]map[string]int
8484
if err != nil {
8585
panic(fmt.Sprintf("Could not read the Customer Master Key: %v", err))
8686
}
87+
if len(key) != 96 {
88+
panic(fmt.Sprintf("Expected the customer master key file to be 96 bytes."))
89+
}
8790
kmsProviderCredentials := map[string]map[string]interface{}{"local": {"key": key}}
8891
// end-get-local-key
8992
return kmsProviderCredentials

source/includes/qe-tutorials/java/src/main/java/com/mongodb/tutorials/qe/util/QueryableEncryptionHelpers.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public static Map<String, Map<String, Object>> getKmsProviderCredentials(String
4545
byte[] localCustomerMasterKey = new byte[96];
4646

4747
try (FileInputStream fis = new FileInputStream("customer-master-key.txt")) {
48-
if (fis.read(localCustomerMasterKey) < 96)
49-
throw new Exception("Expected to read 96 bytes from the customer master key file");
48+
if (fis.read(localCustomerMasterKey) != 96)
49+
throw new Exception("Expected the customer master key file to be 96 bytes.");
5050
} catch (Exception e) {
5151
throw new Exception("Unable to read the Customer Master Key due to the following error: " + e.getMessage());
5252
}

source/includes/qe-tutorials/mongosh/queryable-encryption-helpers.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ function getKMSProviderCredentials(kmsProviderName) {
6868
// start-get-local-key
6969
// WARNING: Do not use a local key file in a production application
7070
const localMasterKey = fs.readFileSync("./customer-master-key.txt");
71+
72+
if (localMasterKey.length !== 96) {
73+
throw new Error(
74+
"Expected the customer master key file to be 96 bytes."
75+
);
76+
}
77+
7178
kmsProviderCredentials = {
7279
local: {
7380
key: localMasterKey,

source/includes/qe-tutorials/node/queryable-encryption-helpers.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ export function getKMSProviderCredentials(kmsProviderName) {
6969
// start-get-local-key
7070
// WARNING: Do not use a local key file in a production application
7171
const localMasterKey = readFileSync("./customer-master-key.txt");
72+
73+
if (localMasterKey.length !== 96) {
74+
throw new Error(
75+
"Expected the customer master key file to be 96 bytes."
76+
);
77+
}
78+
7279
kmsProviders = {
7380
local: {
7481
key: localMasterKey,
@@ -183,7 +190,10 @@ function getKmipTlsOptions() {
183190

184191
export function getClientEncryption(encryptedClient, autoEncryptionOptions) {
185192
// start-client-encryption
186-
const clientEncryption = new ClientEncryption(encryptedClient, autoEncryptionOptions);
193+
const clientEncryption = new ClientEncryption(
194+
encryptedClient,
195+
autoEncryptionOptions
196+
);
187197
// end-client-encryption
188198
return clientEncryption;
189199
}

source/includes/qe-tutorials/python/queryable_encryption_helpers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ def get_kms_provider_credentials(kms_provider_string):
6565
path = "./customer-master-key.txt"
6666
with open(path, "rb") as f:
6767
local_master_key = f.read()
68+
if len(local_master_key) != 96:
69+
raise Exception("Expected the customer master key file to be 96 bytes.")
6870
kms_provider_credentials = {
6971
"local": {
7072
"key": local_master_key

0 commit comments

Comments
 (0)