-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathkms_mrk_discovery_keyring_browser.test.ts
More file actions
105 lines (89 loc) · 3.45 KB
/
Copy pathkms_mrk_discovery_keyring_browser.test.ts
File metadata and controls
105 lines (89 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
/* eslint-env mocha */
import * as chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
import {
AwsKmsMrkAwareSymmetricDiscoveryKeyringBrowser,
AwsKmsMrkAwareSymmetricKeyringBrowser,
} from '../src/index'
import {
KeyringWebCrypto,
WebCryptoEncryptionMaterial,
WebCryptoAlgorithmSuite,
AlgorithmSuiteIdentifier,
WebCryptoDecryptionMaterial,
} from '@aws-crypto/material-management-browser'
import { KMS as V3KMS } from '@aws-sdk/client-kms'
chai.use(chaiAsPromised)
const { expect } = chai
describe('AwsKmsMrkAwareSymmetricDiscoveryKeyringBrowser::constructor', () => {
it('constructor decorates', async () => {
const discoveryFilter = { accountIDs: ['658956600833'], partition: 'aws' }
const grantTokens = ['grant']
const client: any = { config: { region: 'us-west-2' } }
const test = new AwsKmsMrkAwareSymmetricDiscoveryKeyringBrowser({
client,
discoveryFilter,
grantTokens,
})
expect(test.discoveryFilter).to.deep.equal(discoveryFilter)
expect(test.client).to.equal(client)
expect(test.grantTokens).to.equal(grantTokens)
})
it('instance of KeyringWebCrypto', () => {
const discoveryFilter = { accountIDs: ['658956600833'], partition: 'aws' }
const grantTokens = ['grant']
const client: any = { config: { region: 'us-west-2' } }
const test = new AwsKmsMrkAwareSymmetricDiscoveryKeyringBrowser({
client,
discoveryFilter,
grantTokens,
})
expect(test instanceof KeyringWebCrypto).to.equal(true)
})
})
/* Injected from @aws-sdk/karma-credential-loader. */
declare const credentials: any
describe('AwsKmsMrkAwareSymmetricKeyringBrowser can encrypt/decrypt with AWS SDK v3 client', () => {
const discoveryFilter = { accountIDs: ['658956600833'], partition: 'aws' }
const eastKeyId =
'arn:aws:kms:us-east-1:658956600833:key/mrk-80bd8ecdcd4342aebd84b7dc9da498a7'
const grantTokens = ['grant']
const encryptionContext = { some: 'context' }
const suite = new WebCryptoAlgorithmSuite(
AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA256
)
const keyring = new AwsKmsMrkAwareSymmetricDiscoveryKeyringBrowser({
// Note the difference in the region from the keyId
client: new V3KMS({ region: 'us-west-2', credentials }),
discoveryFilter,
grantTokens,
})
it('throws an error on encrypt', async () => {
const material = new WebCryptoEncryptionMaterial(suite, encryptionContext)
return expect(keyring.onEncrypt(material)).to.rejectedWith(
Error,
'AwsKmsMrkAwareSymmetricDiscoveryKeyring cannot be used to encrypt'
)
})
it('can decrypt an EncryptedDataKey', async () => {
const encryptKeyring = new AwsKmsMrkAwareSymmetricKeyringBrowser({
client: new V3KMS({ region: 'us-east-1', credentials }),
keyId: eastKeyId,
grantTokens,
})
const encryptMaterial = await encryptKeyring.onEncrypt(
new WebCryptoEncryptionMaterial(suite, encryptionContext)
)
const [edk] = encryptMaterial.encryptedDataKeys
const material = await keyring.onDecrypt(
new WebCryptoDecryptionMaterial(suite, encryptionContext),
[edk]
)
const test = await keyring.onDecrypt(material, [edk])
expect(test.hasValidKey()).to.equal(true)
// The UnencryptedDataKey should be zeroed, because the cryptoKey has been set
expect(() => test.getUnencryptedDataKey()).to.throw()
})
})