-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Telemetry] update crypto packages #62469
Conversation
Pinging @elastic/pulse (Team:Pulse) |
…ate_crypto_packages
…ate_crypto_packages
@elasticmachine merge upstream |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
attributeValue, | ||
encryptionAAD | ||
); | ||
)) as string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation @afharo!
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this error message should be wrapped in i18n.translate
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
@@ -6,6 +6,10 @@ | |||
|
|||
import nodeCrypto from '@elastic/node-crypto'; | |||
|
|||
export function cryptoFactory(encryptionKey: string | undefined) { | |||
export function cryptoFactory(encryptionKey?: string) { | |||
if (typeof encryptionKey !== 'string') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
@@ -116,10 +116,6 @@ export interface ConditionalHeadersConditions { | |||
basePath: string; | |||
} | |||
|
|||
export interface CryptoFactory { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not needed anymore since Crypto
is typed
@@ -58,7 +58,13 @@ export const executeJobFactory: ExecuteJobFactory<ImmediateExecuteFn< | |||
let decryptedHeaders: Record<string, unknown>; | |||
const serializedEncryptedHeaders = job.headers; | |||
try { | |||
decryptedHeaders = await crypto.decrypt(serializedEncryptedHeaders); | |||
if (typeof serializedEncryptedHeaders !== 'string') { | |||
throw new Error('Job headers are missing'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like we'd want this wrapped in i18n.translate too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrap the error message in i18n ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.ts
since the package provides the types.Removed
@ts-ignore
from@elastic/node-crypto
imports since this package is now typed as well.Closes #58023