Skip to content
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

Merged
merged 10 commits into from
Apr 6, 2020
Merged

Conversation

Bamieh
Copy link
Member

@Bamieh Bamieh commented Apr 3, 2020

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

@Bamieh Bamieh requested a review from a team April 3, 2020 15:45
@Bamieh Bamieh requested a review from a team as a code owner April 3, 2020 15:45
@elasticmachine
Copy link
Contributor

Pinging @elastic/pulse (Team:Pulse)

@Bamieh Bamieh added v7.8.0 release_note:skip Skip the PR/issue when compiling release notes labels Apr 3, 2020
@Bamieh
Copy link
Member Author

Bamieh commented Apr 6, 2020

@elasticmachine merge upstream

Copy link
Member

@afharo afharo left a comment

Choose a reason for hiding this comment

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

LGTM!

@legrego legrego self-requested a review April 6, 2020 12:22
attributeValue,
encryptionAAD
);
)) as string;
Copy link
Member

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>;

Copy link
Member

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.

Copy link
Member

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');
Copy link
Member Author

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

Copy link
Member

@tsullivan tsullivan Apr 6, 2020

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 ?

Copy link
Member Author

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') {
Copy link
Member Author

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 {
Copy link
Member Author

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

@Bamieh Bamieh requested a review from legrego April 6, 2020 13:57
@@ -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');
Copy link
Member

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

Copy link
Contributor

@joelgriffith joelgriffith left a 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');
Copy link
Member

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 ?

Copy link
Member

@tsullivan tsullivan left a 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.

@Bamieh
Copy link
Member Author

Bamieh commented Apr 6, 2020

@joelgriffith @tsullivan thanks for the review!

I've updated the PR based on your feedback. @tsullivan the crytpo is a native module in node so using it to generate the encryption key is still OK and preferred.

We actually do something similar in request-crypto which is used to encrypt telemetry payloads in kibana if you wish to grab that instead.

import { generatePassphrase } from '@elastic/request-crypto';
const encryptionKey = generatePassphrase.toString('hex');

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).

@Bamieh Bamieh requested a review from tsullivan April 6, 2020 16:53
@kibanamachine
Copy link
Contributor

💚 Build Succeeded

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

Copy link
Member

@tsullivan tsullivan left a 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. :)

@Bamieh Bamieh merged commit e16885c into master Apr 6, 2020
@Bamieh Bamieh deleted the telemetry/update_crypto_packages branch April 6, 2020 21:36
gmmorris added a commit to gmmorris/kibana that referenced this pull request Apr 7, 2020
* 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)
  ...
@kibanamachine kibanamachine added the backport missing Added to PRs automatically when the are determined to be missing a backport. label Apr 8, 2020
@kibanamachine
Copy link
Contributor

Friendly reminder: Looks like this PR hasn’t been backported yet.
To create backports run node scripts/backport --pr 62469 or prevent reminders by adding the backport:skip label.

5 similar comments
@kibanamachine
Copy link
Contributor

Friendly reminder: Looks like this PR hasn’t been backported yet.
To create backports run node scripts/backport --pr 62469 or prevent reminders by adding the backport:skip label.

@kibanamachine
Copy link
Contributor

Friendly reminder: Looks like this PR hasn’t been backported yet.
To create backports run node scripts/backport --pr 62469 or prevent reminders by adding the backport:skip label.

@kibanamachine
Copy link
Contributor

Friendly reminder: Looks like this PR hasn’t been backported yet.
To create backports run node scripts/backport --pr 62469 or prevent reminders by adding the backport:skip label.

@kibanamachine
Copy link
Contributor

Friendly reminder: Looks like this PR hasn’t been backported yet.
To create backports run node scripts/backport --pr 62469 or prevent reminders by adding the backport:skip label.

@kibanamachine
Copy link
Contributor

Friendly reminder: Looks like this PR hasn’t been backported yet.
To create backports run node scripts/backport --pr 62469 or prevent reminders by adding the backport:skip label.

@Bamieh Bamieh added the backport:skip This commit does not require backporting label Apr 16, 2020
@kibanamachine kibanamachine removed the backport missing Added to PRs automatically when the are determined to be missing a backport. label Apr 16, 2020
@Bamieh
Copy link
Member Author

Bamieh commented Apr 16, 2020

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

@lukeelmers lukeelmers added the Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc label Oct 1, 2021
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-core (Team:Core)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:skip This commit does not require backporting Feature:Telemetry release_note:skip Skip the PR/issue when compiling release notes Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc v7.8.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Telemetry] upgrade node-crypto and request-crypto to v1.1.0
9 participants