Skip to content

fix: Add cacheing CMM examples #187

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 68 commits into from
Sep 20, 2019
Merged

Conversation

seebees
Copy link
Contributor

@seebees seebees commented Aug 6, 2019

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.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Check any applicable:

  • Were any files moved? Moving files changes their URL, which breaks all hyperlinks to the files.

resolves aws#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.
@seebees seebees requested review from juneb and a team August 6, 2019 17:57
* Just because you can decrypt something does not mean it is what you expect.
* For example, if you are are only expecting data from 'us-west-2',
* the origin can identify a malicious actor.
* See: https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context
Copy link
Contributor

Choose a reason for hiding this comment

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

Better: The following doc describes the use of the encryption context in data key caching.
https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/data-caching-details.html#caching-encryption-context

Copy link
Contributor Author

@seebees seebees Aug 14, 2019

Choose a reason for hiding this comment

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

That link is not about cacheing, it is about encryption context. i.e. why you should include/use it.
Edit: But I see what you are saying. Added link

* Use any method you like to get credentials into the browser.
* See kms.webpack.config
*/
declare const credentials: {accessKeyId: string, secretAccessKey:string, sessionToken:string }
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you need to add the sessionToken to all browser examples, or just this one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All of them.

import { toBase64 } from '@aws-sdk/util-base64-browser'

/* This is injected by webpack.
* The webpack.DefinePlugin will replace the values when bundling.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Update comment, this is no longer accurate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

*/
const maxMessagesEncrypted = 100

const cmm = new WebCryptoCachingMaterialsManager({
Copy link
Contributor

Choose a reason for hiding this comment

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

Other ESDKs call this variable cachingCMM to make it very clear what it is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

kk

* This value is optional,
* but you should configure the lowest value possible.
*/
const maxMessagesEncrypted = 100
Copy link
Contributor

Choose a reason for hiding this comment

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

Other samples set this to 10, not 100.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

kk

*/

/* This is a simple example of using a KMS Keyring
* to encrypt and decrypt using the AWS Encryption SDK for Javascript in a browser.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nope. This is the caching example.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, it is

* This can be configure by passing a `proactiveFrequency`
* as the second paramter to however often you want to check in milliseconds.
*/
const cache = getLocalCryptographicMaterialsCache(100)
Copy link
Contributor

Choose a reason for hiding this comment

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

A capacity value of 100 is very high. The typical value is 10. Also, I would make this a variable so it's meaning is clear.

const maxSize = 10
const cache = getLocalCryptographicMaterialsCache(maxSize)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will update in both places but I will use capacity

Copy link
Contributor

@juneb juneb left a comment

Choose a reason for hiding this comment

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

I edited the browser example. Please apply the same suggestions to the node example.

* you must provide all of the plaintext to the encrypt function.
* Therefore, the encrypt function in the browser knows the length of the plaintext
* and does not accept a plaintextLength option.
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

Ryan, is there a reason to explain why you can't provide a value? This function doesn't have this parameter. I would just omit lines 148-153. (Feel free to overrule me if I've misinterpreted this.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you provide a value and it does not match, this just makes for confusion.

seebees and others added 13 commits September 19, 2019 19:53
to avoid issues with cmm being called caching material manager
@@ -61,10 +61,10 @@ const cleartext = 'asdf'
* the Encryption SDK returns an "encrypted message" that includes the ciphertext,
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above. Again, not required.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

/* You need to specify a name
* and a namespace for raw encryption key providers.
* The name and namespace that you use in the decryption keyring *must* be an exact,
* The name and namespace that you use in the decryption keyring *must* be an exact, case-sensitive match.
* *case-sensitive* match for the name and namespace in the encryption keyring.
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, I didn't see the line following. We now have a duplicate. I would revert the change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

* Access to kms:Encrypt is required for every CMK in keyIds.
* You might list several keys in different AWS Regions.
* This allows you to decrypt the data in any of the represented Regions.
* In this example, I am using the same CMK.
Copy link
Contributor

Choose a reason for hiding this comment

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

This should match the text in the browser example.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

const keyring = new KmsKeyringNode({ generatorKeyId, keyIds })

/* Create a cache to hold the material.
* In this case we use the local cache provided by the Encryption SDK.
Copy link
Contributor

Choose a reason for hiding this comment

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

Please make all text in the node example match the corresponding edited text in the browser example.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@seebees seebees merged commit b74716b into aws:master Sep 20, 2019
@seebees seebees deleted the cacheing-cmm-examples branch September 20, 2019 19:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Need data key caching example
2 participants