Skip to content

Commit b74716b

Browse files
authored
fix: Add cacheing CMM examples (#187)
resolves #23 Add examples for both node and browser. Refactor the browser examples to be tested with karma. Update the browser html so that it still works.
1 parent de30b36 commit b74716b

25 files changed

+679
-38
lines changed

karma.conf.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Karma configuration
22
process.env.CHROME_BIN = require('puppeteer').executablePath()
3-
const webpack = require('webpack')
43

54
module.exports = function (config) {
65
config.set({

modules/cache-material/src/get_local_cryptographic_materials_cache.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ import {
3131
} from './cryptographic_materials_cache'
3232

3333
export function getLocalCryptographicMaterialsCache<S extends SupportedAlgorithmSuites> (
34-
maxSize: number,
34+
capacity: number,
3535
proactiveFrequency: number = 1000 * 60
3636
): CryptographicMaterialsCache<S> {
3737
const cache = new LRU<string, Entry<S>>({
38-
max: maxSize,
38+
max: capacity,
3939
dispose (_key, value) {
4040
/* Zero out the unencrypted dataKey, when the material is removed from the cache. */
4141
value.response.zeroUnencryptedDataKey()

modules/cache-material/test/get_local_cryptographic_materials_cache.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ describe('getLocalCryptographicMaterialsCache', () => {
158158
})
159159

160160
describe('cache eviction', () => {
161-
it('putDecryptionMaterial can exceed maxSize', () => {
161+
it('putDecryptionMaterial can exceed capacity', () => {
162162
const {
163163
getDecryptionMaterial,
164164
putDecryptionMaterial
@@ -207,7 +207,7 @@ describe('cache eviction', () => {
207207
expect(lost).to.equal(false)
208208
})
209209

210-
it('putEncryptionMaterial can exceed maxSize', () => {
210+
it('putEncryptionMaterial can exceed capacity', () => {
211211
const {
212212
getEncryptionMaterial,
213213
putEncryptionMaterial

modules/client-browser/Readme.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ const context = {
6666
const plainText = new Uint8Array([1, 2, 3, 4, 5])
6767

6868
/* Encrypt the string using the keyring and the encryption context
69-
* the Encryption SDK returns an "encrypted message" that includes the ciphertext,
69+
* the Encryption SDK returns an "encrypted message" (`result`) that includes the ciphertext,
7070
* the encryption context, and the encrypted data keys.
7171
*/
72-
const { ciphertext } = await encrypt(keyring, plainText, { encryptionContext: context })
72+
const { result } = await encrypt(keyring, plainText, { encryptionContext: context })
7373

74-
/* Decrypt the ciphertext using the same keyring */
75-
const { plaintext, messageHeader } = await decrypt(keyring, ciphertext)
74+
/* Decrypt the result using the same keyring */
75+
const { plaintext, messageHeader } = await decrypt(keyring, result)
7676

7777
/* Get the encryption context */
7878
const { encryptionContext } = messageHeader

modules/client-node/Readme.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ const context = {
5858
const cleartext = 'asdf'
5959

6060
/* Encrypt the string using the keyring and the encryption context
61-
* the Encryption SDK returns an "encrypted message" that includes the ciphertext,
61+
* the Encryption SDK returns an "encrypted message" (`result`) that includes the ciphertext
6262
* the encryption context, and the encrypted data keys.
6363
*/
64-
const { ciphertext } = await encrypt(keyring, cleartext, { context })
64+
const { result } = await encrypt(keyring, cleartext, { context })
6565

66-
/* Decrypt the ciphertext using the same keyring */
67-
const { plaintext, messageHeader } = await decrypt(keyring, ciphertext)
66+
/* Decrypt the result using the same keyring */
67+
const { plaintext, messageHeader } = await decrypt(keyring, result)
6868

6969
/* Get the encryption context */
7070
const { encryptionContext } = messageHeader

modules/example-browser/html/aes_simple.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@
77
<script src="../build/aes_simple_bundle.js"></script>
88
</head>
99
<body>
10+
<script>
11+
// This is exported via the webpack library setting
12+
test.testAES()
13+
</script>
14+
</body>
1015
</html>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE>
2+
3+
<html>
4+
<head>
5+
<meta charset="utf-8">
6+
<title>Client-Side Caching CMM Encryption Test</title>
7+
<script src="../build/caching_cmm_bundle.js"></script>
8+
</head>
9+
<body>
10+
<script>
11+
// This is exported via the webpack library setting
12+
test.testCachingMaterialsManagerExample()
13+
</script>
14+
</body>
15+
</html>

modules/example-browser/html/kms_simple.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@
77
<script src="../build/kms_simple_bundle.js"></script>
88
</head>
99
<body>
10+
<script>
11+
// This is exported via the webpack library setting
12+
test.testKmsSimpleExample()
13+
</script>
14+
</body>
1015
</html>

modules/example-browser/html/multi_keyring.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@
77
<script src="../build/multi_keyring_bundle.js"></script>
88
</head>
99
<body>
10+
<script>
11+
// This is exported via the webpack library setting
12+
test.testMultiKeyringExample()
13+
</script>
14+
</body>
1015
</html>

modules/example-browser/html/rsa_simple.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,9 @@
77
<script src="../build/rsa_simple_bundle.js"></script>
88
</head>
99
<body>
10+
<script>
11+
// This is exported via the webpack library setting
12+
test.testRSA()
13+
</script>
1014
</body>
1115
</html>

modules/example-browser/karma.conf.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
const webpack = require('webpack')
2+
const {defaultProvider} = require('@aws-sdk/credential-provider-node')
3+
4+
// Karma configuration
5+
process.env.CHROME_BIN = require('puppeteer').executablePath()
6+
7+
module.exports = function (config) {
8+
config.set({
9+
basePath: '',
10+
frameworks: ['mocha', 'chai'],
11+
files: [
12+
'test/**/*.ts',
13+
// 'src/**/*.ts'
14+
],
15+
preprocessors: {
16+
// 'src/**/*.ts': ['credentials'],
17+
'test/**/*.ts': ['webpack', 'credentials']
18+
},
19+
webpack: {
20+
resolve: {
21+
extensions: [ '.ts', '.js' ]
22+
},
23+
mode: 'development',
24+
module: {
25+
rules: [
26+
{
27+
test: /\.tsx?$/,
28+
use: [
29+
{
30+
loader: 'ts-loader',
31+
options: {
32+
configFile: 'tsconfig.module.json',
33+
compilerOptions: {
34+
rootDir: './'
35+
}
36+
}
37+
}
38+
],
39+
exclude: /node_modules/,
40+
},
41+
{
42+
test: /\.ts$/,
43+
exclude: [ /\/test\// ],
44+
enforce: 'post',
45+
use: {
46+
loader: 'istanbul-instrumenter-loader',
47+
options: { esModules: true }
48+
}
49+
}
50+
]
51+
},
52+
stats: {
53+
colors: true,
54+
modules: true,
55+
reasons: true,
56+
errorDetails: true
57+
},
58+
devtool: 'inline-source-map',
59+
node: {
60+
fs: 'empty'
61+
},
62+
},
63+
coverageIstanbulReporter: {
64+
reports: [ 'json' ],
65+
dir: '.karma_output',
66+
fixWebpackSourcePaths: true
67+
},
68+
plugins: [
69+
'@aws-sdk/karma-credential-loader',
70+
'karma-chrome-launcher',
71+
'karma-mocha',
72+
'karma-chai',
73+
'karma-webpack',
74+
'karma-coverage-istanbul-reporter'
75+
],
76+
reporters: ['progress', 'coverage-istanbul'],
77+
port: 9876,
78+
colors: true,
79+
logLevel: config.LOG_INFO,
80+
autoWatch: false,
81+
browsers: ['ChromeHeadlessDisableCors'],
82+
customLaunchers: {
83+
ChromeHeadlessDisableCors: {
84+
base: 'ChromeHeadless',
85+
flags: ['--disable-web-security']
86+
}
87+
},
88+
singleRun: true,
89+
concurrency: Infinity,
90+
exclude: ['**/*.d.ts']
91+
})
92+
}

modules/example-browser/package.json

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
"scripts": {
55
"prepublishOnly": "npm run build",
66
"build": "tsc -b tsconfig.json && tsc -b tsconfig.module.json",
7-
"test": "mocha --require ts-node/register tests/**/*tests.ts",
7+
"lint": "standard src/*.ts test/**/*.ts",
8+
"karma": "karma start karma.conf.js",
9+
"test": "npm run lint && npm run coverage",
10+
"coverage": "npm run karma && nyc report --exclude-after-remap false -t .karma_output --check-coverage",
811
"example-rsa": "webpack -d --config webpack_configs/rsa.webpack.config.js",
912
"example-aes": "webpack -d --config webpack_configs/aes.webpack.config.js",
1013
"example-kms": "webpack -d --config webpack_configs/kms.webpack.config.js",
11-
"example-multi-keyring": "webpack -d --config webpack_configs/multi_keyring.webpack.config.js"
14+
"example-multi-keyring": "webpack -d --config webpack_configs/multi_keyring.webpack.config.js",
15+
"example-caching-cmm": "webpack -d --config webpack_configs/caching_cmm.webpack.config.js"
1216
},
1317
"author": {
1418
"name": "AWS Crypto Tools Team",
@@ -25,11 +29,21 @@
2529
"@aws-sdk/credential-provider-node": "^0.1.0-preview.4",
2630
"@types/chai": "^4.1.4",
2731
"@types/mocha": "^5.2.5",
32+
"@typescript-eslint/eslint-plugin": "^1.9.0",
33+
"@typescript-eslint/parser": "^1.9.0",
2834
"chai": "^4.1.2",
2935
"mocha": "^5.2.0",
3036
"ts-loader": "^5.3.3",
3137
"ts-node": "^7.0.1",
3238
"typescript": "^3.5.0",
39+
"karma": "^4.1.0",
40+
"karma-chai": "^0.1.0",
41+
"karma-chrome-launcher": "^2.2.0",
42+
"karma-coverage-istanbul-reporter": "^2.0.4",
43+
"karma-mocha": "^1.3.0",
44+
"karma-webpack": "^3.0.5",
45+
"nyc": "^14.0.0",
46+
"standard": "^12.0.1",
3347
"webpack": "^4.30.0",
3448
"webpack-cli": "^3.3.0"
3549
},

modules/example-browser/src/aes_simple.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ import {
2727
} from '@aws-crypto/client-browser'
2828
import { toBase64 } from '@aws-sdk/util-base64-browser'
2929

30-
;(async function testAES () {
30+
/* This is done to facilitate testing. */
31+
export async function testAES () {
3132
/* You need to specify a name
3233
* and a namespace for raw encryption key providers.
3334
* The name and namespace that you use in the decryption keyring *must* be an exact,
@@ -105,4 +106,7 @@ import { toBase64 } from '@aws-sdk/util-base64-browser'
105106
*/
106107
document.write('</br>plaintext:' + plaintext)
107108
console.log(plaintext)
108-
})()
109+
110+
/* Return the values to make testing easy. */
111+
return { plainText, plaintext }
112+
}

0 commit comments

Comments
 (0)