Skip to content

Commit 6d47acd

Browse files
committed
lint and import
Browsers import a crypto key
1 parent 9d2dc69 commit 6d47acd

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

modules/kms-keyring-browser/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"private": true,
44
"version": "0.0.1",
55
"scripts": {
6-
"prepublishOnly": "tsc -p tsconfig.json && tsc -p tsconfig.module.json",
6+
"prepublishOnly": "npm run build",
7+
"build": "tsc -b tsconfig.json && tsc -b tsconfig.module.json",
78
"lint": "standard src/*.ts test/**/*.ts",
89
"mocha": "mocha --require ts-node/register test/**/*test.ts",
910
"test": "npm run lint && npm run coverage",
@@ -18,13 +19,14 @@
1819
"dependencies": {
1920
"@aws-crypto/material-management-browser": "^0.0.1",
2021
"@aws-crypto/kms-keyring": "^0.0.1",
22+
"@aws-crypto/web-crypto-backend": "^0.0.1",
2123
"@aws-sdk/types": "0.1.0-preview.1",
2224
"tslib": "^1.9.3"
2325
},
2426
"devDependencies": {
2527
"@types/chai": "^4.1.4",
2628
"@types/mocha": "^5.2.5",
27-
"@types/node": "^8.10.40",
29+
"@types/node": "^11.11.4",
2830
"@typescript-eslint/eslint-plugin": "^1.4.2",
2931
"@typescript-eslint/parser": "^1.4.2",
3032
"aws-sdk": "^2.412.0",

modules/kms-keyring-browser/src/index.ts

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,67 @@
1515

1616
import {
1717
KmsKeyring,
18-
KmsKeyringInput,
19-
KMSConstructible,
20-
KmsClientSupplier,
18+
KmsKeyringInput, // eslint-disable-line no-unused-vars
19+
KMSConstructible, // eslint-disable-line no-unused-vars
20+
KmsClientSupplier, // eslint-disable-line no-unused-vars
2121
getClient,
2222
limitRegions,
2323
excludeRegions,
2424
cacheClients
2525
} from '@aws-crypto/kms-keyring'
2626
import {
27-
WebCryptoAlgorithmSuite,
28-
immutableClass
27+
WebCryptoAlgorithmSuite, // eslint-disable-line no-unused-vars
28+
WebCryptoEncryptionMaterial, // eslint-disable-line no-unused-vars
29+
WebCryptoDecryptionMaterial, // eslint-disable-line no-unused-vars
30+
EncryptionContext, // eslint-disable-line no-unused-vars
31+
EncryptedDataKey, // eslint-disable-line no-unused-vars
32+
immutableClass,
33+
importCryptoKey
2934
} from '@aws-crypto/material-management-browser'
30-
import {KMS, KMSConfiguration} from '@aws-sdk/client-kms-browser'
35+
import { getWebCryptoBackend } from '@aws-crypto/web-crypto-backend'
36+
import { KMS, KMSConfiguration } from '@aws-sdk/client-kms-browser' // eslint-disable-line no-unused-vars
3137

3238
export type KmsKeyringWebCryptoInput = KmsKeyringInput<KMS>
3339
export type KMSWebCryptoConstructible = KMSConstructible<KMS, KMSConfiguration>
3440
export type KmsWebCryptoClientSupplier = KmsClientSupplier<KMS>
3541

3642
export class KmsKeyringNode extends KmsKeyring<WebCryptoAlgorithmSuite, KMS> {
37-
constructor(input: KmsKeyringWebCryptoInput){
38-
super(input)
43+
async _onEncrypt (material: WebCryptoEncryptionMaterial, context?: EncryptionContext) {
44+
const _material = await super._onEncrypt(material, context)
45+
46+
/* Check for early return (Postcondition): If a cryptoKey has already been imported, return. */
47+
if (_material.hasUnencryptedDataKey && _material.hasCryptoKey) {
48+
return _material
49+
}
50+
51+
const backend = await getWebCryptoBackend()
52+
const cryptoKey = await importCryptoKey(backend, _material)
53+
// The trace is only set when the material does not already have
54+
// an hasUnencryptedDataKey. This is an implementation detail :(
55+
const [trace] = _material.keyringTrace
56+
57+
return _material.setCryptoKey(cryptoKey, trace)
58+
}
59+
60+
async _onDecrypt (material: WebCryptoDecryptionMaterial, encryptedDataKeys: EncryptedDataKey[], context?: EncryptionContext) {
61+
const _material = await super._onDecrypt(material, encryptedDataKeys, context)
62+
63+
/* Check for early return (Postcondition): If a cryptoKey has already been imported, return. */
64+
if (_material.hasUnencryptedDataKey && _material.hasCryptoKey) {
65+
return _material
66+
}
67+
68+
const backend = await getWebCryptoBackend()
69+
const cryptoKey = await importCryptoKey(backend, _material)
70+
// Now that a cryptoKey has been imported, the unencrypted data key can be zeroed.
71+
_material.zeroUnencryptedDataKey()
72+
// The trace is only set when the material does not already have
73+
// an hasUnencryptedDataKey. This is an implementation detail :(
74+
const [trace] = _material.keyringTrace
75+
76+
return _material.setCryptoKey(cryptoKey, trace)
3977
}
4078
}
4179
immutableClass(KmsKeyringNode)
4280

43-
export {getClient, limitRegions, excludeRegions, cacheClients}
81+
export { getClient, limitRegions, excludeRegions, cacheClients }

0 commit comments

Comments
 (0)