Conversation
|
Pinging @elastic/pulse (Team:Pulse) |
…ate_crypto_packages
…ate_crypto_packages
|
@elasticmachine merge upstream |
| attributeValue, | ||
| encryptionAAD | ||
| ); | ||
| )) as string; |
There was a problem hiding this comment.
nit: Any chance we can update the types for node-crypto to allow decrypt to accept a generic argument, like the old interface supported?
decrypt<T>(valueToDecrypt: string, aad?: string): Promise<T>;There was a problem hiding this comment.
@legrego we actually had a heated discussion about the same in the node-crypto repo but we decided not to do so for a reason:
The valueToDecrypt is not validated in any way before decryption (apart from the fact that it's a string when encrypted). It means that we have no control over the output of the decrypt method.
i.e.: You might be expecting the decrypted value to be a string, but that doesn't stop anyone to provide a valueToDecrypt that gets decrypted into anything else (string[], object, boolean, ...).
In my opinion, accepting a generic argument would allow (and encourage) devs to set the expected output, defeating the purpose of TS as a best practice advisor. With the current implementation (no generic argument), TS reminds you might get an unexpected output and you should implement type-guards. If you are sure about the exact output because you have full control over the input, you can cast it.
| try { | ||
| const decryptedHeaders: Record<string, string> = await crypto.decrypt(job.headers); | ||
| if (typeof job.headers !== 'string') { | ||
| throw new Error('Job headers are missing'); |
There was a problem hiding this comment.
nodeCrypto will throw an error when calling Buffer.from if it was not passed a string or a buffer https://github.com/elastic/node-crypto/blob/master/src/crypto.ts#L133
This will throw a more readable error and safe-guard the type
There was a problem hiding this comment.
Maybe this error message should be wrapped in i18n.translate ?
There was a problem hiding this comment.
I havent wrapped them in i18n.translate because this is the actual error payload and not the error message we are showing to the user.
The error message to the user is already i18n wrapped:
'Failed to decrypt report job data. Please ensure that {encryptionKey} is set and re-generate this report. {err}'.
I've wrapped them with i18n.translation; just explaining my thought process about that one.
|
|
||
| export function cryptoFactory(encryptionKey: string | undefined) { | ||
| export function cryptoFactory(encryptionKey?: string) { | ||
| if (typeof encryptionKey !== 'string') { |
There was a problem hiding this comment.
nodeCrypto will throw an error if encryptionKey is undefined: https://github.com/elastic/node-crypto/blob/master/src/crypto.ts#L37
This will type guard the parameter.
| basePath: string; | ||
| } | ||
|
|
||
| export interface CryptoFactory { |
There was a problem hiding this comment.
Not needed anymore since Crypto is typed
| try { | ||
| decryptedHeaders = await crypto.decrypt(serializedEncryptedHeaders); | ||
| if (typeof serializedEncryptedHeaders !== 'string') { | ||
| throw new Error('Job headers are missing'); |
There was a problem hiding this comment.
looks like we'd want this wrapped in i18n.translate too
joelgriffith
left a comment
There was a problem hiding this comment.
LGTM, thanks for the typeof checks on the headers (though those might be handled in joi schema's elsewhere, probably not a bad thing to double check).
| try { | ||
| decryptedHeaders = await crypto.decrypt(headers); | ||
| if (typeof headers !== 'string') { | ||
| throw new Error('Job headers are missing'); |
There was a problem hiding this comment.
wrap the error message in i18n ?
tsullivan
left a comment
There was a problem hiding this comment.
On the reporting side, I think the new error messages should be wrapped in i18n.translate, as other error strings are done in the files that have been changed.
I also want to check with you that the crypto import in x-pack/legacy/plugins/reporting/server/config/index.ts is still OK as it is.
|
@joelgriffith @tsullivan thanks for the review! I've updated the PR based on your feedback. @tsullivan the We actually do something similar in request-crypto which is used to encrypt telemetry payloads in kibana if you wish to grab that instead. Here we generate 32 random bytes instead of 16 since it is considered a better practice to do so. This way the method can be used to create salt keys as well (16 bytes will be used if only 16 are needed). |
💚 Build SucceededHistory
To update your PR or re-run it, just comment with: |
tsullivan
left a comment
There was a problem hiding this comment.
LGTM, and thank you for the write-up about generating encryption keys. :)
* master: (36 commits) [data.search.aggs] Remove service getters from agg types (elastic#61628) fixing APM internationalization (elastic#62757) fix: 🐛 correctly create error on no_matching_indices (elastic#61257) [Lens] Remove all legacy imports (elastic#62596) Add label for ace editor (elastic#62588) [ML] Show better file structure finder explanations (elastic#62316) Fix old pathes in eslintrc (elastic#62580) [Uptime] Improve Telemetry test (elastic#62428) [SIEM] Adds sort rules Cypress test (elastic#62700) [Uptime]Abstracted 'access:uptime-read' tag into a wrapper for… (elastic#62576) fixing bug (elastic#62577) [Maps] Allow updating requestType for ESGeoGridSource (elastic#62365) [Maps] do not show circle border when symbol size is zero (elastic#62644) [Maps] Always show current zoom level (elastic#62684) bc5 siem rules merge (elastic#62679) Revert "[Monitoring] Cluster state watch to Kibana alerting (elastic#61685)" Fix visual tests (elastic#62660) [Telemetry] update crypto packages (elastic#62469) [DOCS] Removed references to left (elastic#60807) [Maps] Move layers to np maps (elastic#61877) ...
|
Friendly reminder: Looks like this PR hasn’t been backported yet. |
5 similar comments
|
Friendly reminder: Looks like this PR hasn’t been backported yet. |
|
Friendly reminder: Looks like this PR hasn’t been backported yet. |
|
Friendly reminder: Looks like this PR hasn’t been backported yet. |
|
Friendly reminder: Looks like this PR hasn’t been backported yet. |
|
Friendly reminder: Looks like this PR hasn’t been backported yet. |
|
PR was backported in this PR: #62919 It was backported the same day this PR was merged to master. Not sure what happened to trigger these kibanamachine pings |
|
Pinging @elastic/kibana-core (Team:Core) |
Update crypto packages, no breaking changes just converted the libraries to TS for typings support.
Removed
typings/elastic__node_crypto.d.tssince the package provides the types.Removed
@ts-ignorefrom@elastic/node-cryptoimports since this package is now typed as well.Closes #58023