Skip to content

fix: caching cmm export and material #186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions modules/cache-material/src/clone_cryptographic_material.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ export function cloneMaterial<M extends Material> (source: M): M {
? new WebCryptoEncryptionMaterial(suite, encryptionContext)
: new WebCryptoDecryptionMaterial(suite, encryptionContext)

const udk = new Uint8Array(source.getUnencryptedDataKey())
clone.setUnencryptedDataKey(udk, source.keyringTrace[0])
if (source.hasUnencryptedDataKey) {
const udk = new Uint8Array(source.getUnencryptedDataKey())
clone.setUnencryptedDataKey(udk, source.keyringTrace[0])
}

if ((<WebCryptoDecryptionMaterial>source).hasCryptoKey) {
const cryptoKey = (<WebCryptoDecryptionMaterial>source).getCryptoKey()
;(<WebCryptoDecryptionMaterial>clone)
.setCryptoKey(cryptoKey, clone.keyringTrace[0])
.setCryptoKey(cryptoKey, source.keyringTrace[0])
}

if (isEncryptionMaterial(source) && isEncryptionMaterial(clone)) {
Expand Down
1 change: 1 addition & 0 deletions modules/cache-material/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ export * from './cryptographic_materials_cache'
export * from './caching_cryptographic_materials_decorators'
export * from './build_cryptographic_materials_cache_key_helpers'
export * from './clone_cryptographic_material'
export * from './get_local_cryptographic_materials_cache'
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,12 @@ describe('cloneMaterial', () => {
})

it('clone WebCryptoDecryptionMaterial', () => {
/* WebCryptoDecryptionMaterial do not have an unencrypted data key. */
const material = new WebCryptoDecryptionMaterial(webCryptoSuite, { some: 'context' })
.setUnencryptedDataKey(udk128, trace)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this change fixes the cloning so that it handles the WebCryptoDecryptionMaterials case, but is setting an unencryptedDataKey on WebCryptoDecryptionMaterials something that should be allowed in the first place?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be true. I'd have to think about it. But it would also be a VERY large change. Having it should not hurt anything...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is a potential for this being a sharp edge, can we either document this somewhere (maybe CryptoKey or WebCryptoDecryptionMaterial) or create an issue for further investigation?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed offline, we do not see this as a sharp edge. Investigation for how to make this easy to maintain is part of #74

.setCryptoKey(cryptoKey, trace)

const test = cloneMaterial(material)
expect(test).to.be.instanceOf(WebCryptoDecryptionMaterial)
expect(test.getUnencryptedDataKey()).to.deep.equal(udk128)
expect(test.getCryptoKey()).to.deep.equal(cryptoKey)
expect(test.keyringTrace).to.deep.equal(material.keyringTrace)
expect(test.encryptionContext).to.deep.equal(material.encryptionContext)
Expand Down
1 change: 1 addition & 0 deletions modules/caching-materials-manager-browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
*/

export * from './caching_materials_manager_browser'
export { getLocalCryptographicMaterialsCache } from '@aws-crypto/cache-material'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are these exports being used? Should they be included in a PR containing changes which include them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can not create a local cache easily without these. The should always have been exported... I found this "bug" in writing the examples. I can put it somewhere else, but I felt like they were in the same world.

1 change: 1 addition & 0 deletions modules/caching-materials-manager-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
*/

export * from './caching_materials_manager_node'
export { getLocalCryptographicMaterialsCache } from '@aws-crypto/cache-material'