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

[data views] implement fallback sha1 hash for fields request #175181

Merged
merged 5 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,7 @@
"require-in-the-middle": "^7.2.0",
"reselect": "^4.1.6",
"rison-node": "1.0.2",
"rusha": "^0.8.14",
"rxjs": "^7.5.5",
"safe-squel": "^5.12.5",
"seedrandom": "^3.0.5",
Expand Down Expand Up @@ -1459,6 +1460,7 @@
"@types/redux-actions": "^2.6.1",
"@types/redux-logger": "^3.0.8",
"@types/resolve": "^1.20.1",
"@types/rusha": "^0.8.3",
Copy link
Member

@kertal kertal Jan 22, 2024

Choose a reason for hiding this comment

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

Thx for taking care of this so quickly 👍 QQ: why not using kbn-crypto-browser, it's not sha1 but sha256, which should do the job, and no new dependency would be necessary (and it just this could be used, to simplify the code)
https://github.com/elastic/kibana/blob/4824d9da8c59f03522779d1b41d531f52b255dab/packages/kbn-crypto-browser/README.md#L4-L3
Maybe it would also reduce the size of the async chunk which has a plus of +12.0KB with this PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, that does make more sense!

Copy link
Member

Choose a reason for hiding this comment

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

good idea here :)

"@types/seedrandom": ">=2.0.0 <4.0.0",
"@types/selenium-webdriver": "^4.1.21",
"@types/semver": "^7",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ const API_BASE_URL: string = `/api/index_patterns/`;
const version = '1';

async function sha1(str: string) {
const enc = new TextEncoder();
const hash = await crypto.subtle.digest('SHA-1', enc.encode(str));
return Array.from(new Uint8Array(hash))
.map((v) => v.toString(16).padStart(2, '0'))
.join('');
try {
const enc = new TextEncoder();
const hash = await crypto.subtle.digest('SHA-1', enc.encode(str));
return Array.from(new Uint8Array(hash))
.map((v) => v.toString(16).padStart(2, '0'))
.join('');
} catch (error) {
const Rusha = await import('./sha1_rusha');
return Rusha.sha1(str);
}
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/plugins/data_views/public/data_views/sha1_rusha.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { sha1 } from './sha1_rusha';
import { createHash } from 'crypto';

describe('Rusha package', () => {
test('sha1 equals built in sha1', async function () {
const content = 'hello world';

expect(await sha1(content)).toEqual(createHash('sha1').update(content).digest('hex'));
});
});
13 changes: 13 additions & 0 deletions src/plugins/data_views/public/data_views/sha1_rusha.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import Rusha from 'rusha';

export const sha1 = async (str: string) => {
return Rusha.createHash().update(str).digest('hex');
};
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10219,6 +10219,13 @@
resolved "https://registry.yarnpkg.com/@types/retry/-/retry-0.12.0.tgz#2b35eccfcee7d38cd72ad99232fbd58bffb3c84d"
integrity sha512-wWKOClTTiizcZhXnPY4wikVAwmdYHp8q6DmC+EJUzAMsycb7HB32Kh9RN4+0gExjmPmZSAQjgURXIGATPegAvA==

"@types/rusha@^0.8.3":
version "0.8.3"
resolved "https://registry.yarnpkg.com/@types/rusha/-/rusha-0.8.3.tgz#d27cb1c8ce7d56dfd5148e641599d4f0755a7ca7"
integrity sha512-vLRHP7yUlgqh3bJsqydGk0ebcSrFjTHCQuj/eW2fKRSFJrAvGCIKCVaHOtstpsCvDzCXil5lzBbx7OWFWmBPeQ==
dependencies:
"@types/node" "*"

"@types/scheduler@*":
version "0.16.2"
resolved "https://registry.yarnpkg.com/@types/scheduler/-/scheduler-0.16.2.tgz#1a62f89525723dde24ba1b01b092bf5df8ad4d39"
Expand Down Expand Up @@ -27153,6 +27160,11 @@ run-queue@^1.0.0, run-queue@^1.0.3:
dependencies:
aproba "^1.1.1"

rusha@^0.8.14:
version "0.8.14"
resolved "https://registry.yarnpkg.com/rusha/-/rusha-0.8.14.tgz#a977d0de9428406138b7bb90d3de5dcd024e2f68"
integrity sha512-cLgakCUf6PedEu15t8kbsjnwIFFR2D4RfL+W3iWFJ4iac7z4B0ZI8fxy4R3J956kAI68HclCFGL8MPoUVC3qVA==

rw@1, rw@^1.3.3:
version "1.3.3"
resolved "https://registry.yarnpkg.com/rw/-/rw-1.3.3.tgz#3f862dfa91ab766b14885ef4d01124bfda074fb4"
Expand Down