Skip to content

Commit 4d798b6

Browse files
nvobilisseebees
authored andcommitted
comment the demo code
1 parent 8aa6bfa commit 4d798b6

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

modules/example-node/hkr-demo/hkr.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
import {
25
buildClient,
36
CommitmentPolicy,
@@ -19,12 +22,14 @@ const YELLO_LOG = '\x1b[33m%s\x1b[0m'
1922
const GREEN_LOG = '\x1b[32m%s\x1b[0m'
2023
const RED_LOG = '\x1b[31m%s\x1b[0m'
2124

25+
// function to generate a random string
2226
export function generateRandomString(minLength: number, maxLength: number) {
2327
const randomLength =
2428
Math.floor(Math.random() * (maxLength - minLength + 1)) + minLength
2529
return randomBytes(randomLength).toString('hex').slice(0, randomLength)
2630
}
2731

32+
// function to encrypt, decrypt, and verify
2833
export async function roundtrip(
2934
keyring: KeyringNode,
3035
context: EncryptionContext,
@@ -47,10 +52,12 @@ export async function roundtrip(
4752
return { plaintext, result, cleartext, messageHeader }
4853
}
4954

55+
// run the roundtrips on the specified keyring
5056
export async function runRoundTrips(
5157
keyring: KeyringNode,
5258
numRoundTrips: number
5359
) {
60+
// set up spies to monitor network call volume
5461
const kmsSpy = sinon.spy(KMSClient.prototype, 'send')
5562
const ddbSpy = sinon.spy(DynamoDBClient.prototype, 'send')
5663
const padding = String(numRoundTrips).length
@@ -60,15 +67,19 @@ export async function runRoundTrips(
6067
console.log(YELLO_LOG, `${keyring.constructor.name} Roundtrips`) // Print constructor name in yellow
6168
console.time('Total runtime') // Start the timer
6269

70+
// for each roundtrip
6371
for (let i = 0; i < numRoundTrips; i++) {
72+
// create an encryption context
6473
const encryptionContext = {
6574
roundtrip: i.toString(),
6675
}
76+
// generate a random string
6777
const encryptionInput = generateRandomString(
6878
MIN_INPUT_LENGTH,
6979
MAX_INPUT_LENGTH
7080
)
7181

82+
// try to do the roundtrip. If any error arises, log it properly
7283
let decryptionOutput: string
7384
try {
7485
const { plaintext } = await roundtrip(
@@ -88,21 +99,24 @@ export async function runRoundTrips(
8899
MAX_INPUT_LENGTH - decryptionOutput.length
89100
)
90101

102+
// log message
91103
const logMessage = `Roundtrip ${String(i + 1).padStart(
92104
padding,
93105
' '
94106
)}: ${encryptionInput}${encryptionInputPadding} ----encrypt & decrypt----> ${decryptionOutput}${decryptionOutputPadding}`
95107

108+
// print the log green if successful. Otherwise, red
96109
let logColor: string
97110
if (encryptionInput === decryptionOutput) {
98111
logColor = GREEN_LOG
99112
successes += 1
100113
} else {
101114
logColor = RED_LOG
102115
}
103-
console.log(logColor, logMessage) // Print log message in green
116+
console.log(logColor, logMessage)
104117
}
105118

119+
// print metrics for runtime and call volume
106120
console.log()
107121
console.log(YELLO_LOG, `${keyring.constructor.name} metrics`) // Print constructor name in yellow
108122
console.timeEnd('Total runtime')

modules/example-node/hkr-demo/hkr_vs_regular.demo.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// import chalk from 'chalk'
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
24
import {
35
BranchKeyStoreNode,
46
SrkCompatibilityKmsConfig,
@@ -8,9 +10,11 @@ import {
810
import { runRoundTrips } from './hkr'
911
import minimist from 'minimist'
1012

13+
// get cli args
1114
const args = minimist(process.argv.slice(2))
1215
const NUM_ROUNDTRIPS = args.numRoundTrips || 10
1316

17+
// function to run the KMS keyring roundtrips
1418
async function runKmsKeyring() {
1519
const generatorKeyId =
1620
'arn:aws:kms:us-west-2:658956600833:key/b3537ef1-d8dc-4780-9f5a-55776cbb2f7f'
@@ -19,6 +23,7 @@ async function runKmsKeyring() {
1923
await runRoundTrips(keyring, NUM_ROUNDTRIPS)
2024
}
2125

26+
// function to run the H-keyring roundtrips
2227
async function runKmsHkrKeyring() {
2328
const branchKeyArn =
2429
'arn:aws:kms:us-west-2:370957321024:key/9d989aa2-2f9c-438c-a745-cc57d3ad0126'

modules/example-node/hkr-demo/interop.demo.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
import * as fs from 'fs'
25
import {
36
BranchKeyStoreNode,
@@ -12,6 +15,7 @@ const { encrypt, decrypt } = buildClient(
1215
CommitmentPolicy.REQUIRE_ENCRYPT_REQUIRE_DECRYPT
1316
)
1417

18+
// create H-Keyring
1519
const branchKeyArn =
1620
'arn:aws:kms:us-west-2:370957321024:key/9d989aa2-2f9c-438c-a745-cc57d3ad0126'
1721
const branchKeyId = '38853b56-19c6-4345-9cb5-afc2a25dcdd1'
@@ -28,6 +32,7 @@ const keyring = new KmsHierarchicalKeyRingNode({
2832
cacheLimitTtl: 60,
2933
})
3034

35+
// function to decrypt with H-Keyring
3136
async function decryptEncryptedData(encryptedData: Buffer) {
3237
const { plaintext: decryptedData, messageHeader } = await decrypt(
3338
keyring,
@@ -45,6 +50,7 @@ async function decryptEncryptedData(encryptedData: Buffer) {
4550
return decryptedData
4651
}
4752

53+
// function to encrypt with H-Keyring
4854
async function encryptData(data: Buffer) {
4955
const { result } = await encrypt(keyring, data, {
5056
encryptionContext: { successful: 'demo' },
@@ -54,11 +60,13 @@ async function encryptData(data: Buffer) {
5460
}
5561

5662
async function main() {
63+
// read CLI args
5764
const args = process.argv.slice(2)
5865
const operation = args[0]
5966
const inFile = args[1]
6067
const outFile = args[2]
6168

69+
// read from input file
6270
let inData = Buffer.alloc(0)
6371
try {
6472
inData = fs.readFileSync(inFile)
@@ -67,6 +75,7 @@ async function main() {
6775
exit(1)
6876
}
6977

78+
// encrypt/decrypt input file
7079
let outData: Buffer
7180
let msg: string
7281
if (operation === 'encrypt') {
@@ -79,13 +88,15 @@ async function main() {
7988
msg = 'JS has completed decryption'
8089
}
8190

91+
// write to output file
8292
try {
8393
fs.writeFileSync(outFile, outData)
8494
} catch (err) {
8595
console.error(err)
8696
exit(1)
8797
}
8898

99+
// log completion message
89100
console.log(msg)
90101
}
91102

modules/example-node/hkr-demo/multi_tenant.demo.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
14
import {
25
KmsHierarchicalKeyRingNode,
36
BranchKeyStoreNode,
@@ -12,13 +15,16 @@ import minimist from 'minimist'
1215
import * as fs from 'fs'
1316
import { exit } from 'process'
1417

18+
// read CLI args
1519
const args = minimist(process.argv.slice(2))
1620

21+
// map A and B to respective branch IDs
1722
const tenantMap: { [key: string]: string } = {
1823
A: '38853b56-19c6-4345-9cb5-afc2a25dcdd1',
1924
B: '2c583585-5770-467d-8f59-b346d0ed1994',
2025
}
2126

27+
// preprocess CLI args and return them under an object with named fields
2228
function getCliArgs() {
2329
const operation = args.operation
2430
if (!operation) {
@@ -45,12 +51,15 @@ function getCliArgs() {
4551
return { operation, inFile, outFile, tenant }
4652
}
4753

54+
// a dummy branch key id supplier which looks for a field with key "branchKeyId"
55+
// inside the EC
4856
class ExampleBranchKeyIdSupplier implements BranchKeyIdSupplier {
4957
getBranchKeyId(encryptionContext: EncryptionContext): string {
5058
return encryptionContext.branchKeyId
5159
}
5260
}
5361

62+
// configure the keystore
5463
const branchKeyArn =
5564
'arn:aws:kms:us-west-2:370957321024:key/9d989aa2-2f9c-438c-a745-cc57d3ad0126'
5665

@@ -60,6 +69,7 @@ const keyStore = new BranchKeyStoreNode({
6069
kmsConfiguration: new SrkCompatibilityKmsConfig(branchKeyArn),
6170
})
6271

72+
// function to read input from a file
6373
function readInputData(inFile: string) {
6474
let inData = Buffer.alloc(0)
6575
try {
@@ -72,6 +82,7 @@ function readInputData(inFile: string) {
7282
return inData
7383
}
7484

85+
// a function to write output to a file
7586
function dumpOutputData(outFile: string, outData: Buffer) {
7687
try {
7788
fs.writeFileSync(outFile, outData)
@@ -85,6 +96,7 @@ const { encrypt, decrypt } = buildClient(
8596
CommitmentPolicy.REQUIRE_ENCRYPT_REQUIRE_DECRYPT
8697
)
8798

99+
// function to decrypt with the H-keyring
88100
async function decryptEncryptedData(
89101
encryptedData: Buffer,
90102
keyring: KeyringNode
@@ -105,6 +117,7 @@ async function decryptEncryptedData(
105117
return decryptedData
106118
}
107119

120+
// function to encrypt with the H-Keyring
108121
async function encryptData(
109122
data: Buffer,
110123
keyring: KeyringNode,
@@ -115,13 +128,18 @@ async function encryptData(
115128
}
116129

117130
async function main() {
131+
// read cli args
118132
const { operation, inFile, outFile, tenant } = getCliArgs()
133+
// based on CLI tenant arg, find the branch key id
119134
const branchKeyId: string = tenantMap[tenant]
135+
// read input from input file
120136
const inData = readInputData(inFile)
121137

122138
let outData: Buffer = Buffer.alloc(0)
123139
let msg: string
140+
// if cli arg operation field is encrypt
124141
if (operation === 'encrypt') {
142+
// create a dynamic keyring and encrypt
125143
const keyring = new KmsHierarchicalKeyRingNode({
126144
branchKeyIdSupplier: new ExampleBranchKeyIdSupplier(),
127145
keyStore,
@@ -131,6 +149,7 @@ async function main() {
131149
outData = await encryptData(data, keyring, { branchKeyId })
132150
msg = `Tenant ${tenant} has completed encryption`
133151
} else {
152+
// otherwise, create a static keyring and decrypt
134153
const keyring = new KmsHierarchicalKeyRingNode({
135154
branchKeyId,
136155
keyStore,
@@ -147,6 +166,7 @@ async function main() {
147166
msg = `Tenant ${tenant} has completed decryption`
148167
}
149168

169+
// write output to output file
150170
dumpOutputData(outFile, outData)
151171
console.log(msg)
152172
}

0 commit comments

Comments
 (0)