-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Security Solutions] (Phase 1) Initial checkin with kbn package secur…
…ity-utils added (#99151) ## Summary Good place for one off utils to use and reduce/remove the circular deps we have between security_solutions, lists, and anywhere else we have them for different utils. Phase 1: Adds kbn package of `kbn-securitysolution-utils` through lift and shift Phase 2: Deprecated the functions Phase 3: Removes the functions in favor of using the `kbn-securitysolution-utils` Phase 4+: Adds more and removes dependencies/copies across plugins within the security solution. Maybe we break things out by domain later as this grows in size. - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios
- Loading branch information
1 parent
29e48b8
commit 08d7e1c
Showing
12 changed files
with
273 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
load("@npm//@bazel/typescript:index.bzl", "ts_config", "ts_project") | ||
load("@build_bazel_rules_nodejs//:index.bzl", "js_library", "pkg_npm") | ||
|
||
PKG_BASE_NAME = "kbn-securitysolution-utils" | ||
|
||
PKG_REQUIRE_NAME = "@kbn/securitysolution-utils" | ||
|
||
SOURCE_FILES = glob( | ||
[ | ||
"src/**/*.ts", | ||
], | ||
exclude = [ | ||
"**/*.test.*", | ||
"**/*.mock.*", | ||
], | ||
) | ||
|
||
SRCS = SOURCE_FILES | ||
|
||
filegroup( | ||
name = "srcs", | ||
srcs = SRCS, | ||
) | ||
|
||
NPM_MODULE_EXTRA_FILES = [ | ||
"package.json", | ||
"README.md", | ||
] | ||
|
||
SRC_DEPS = [ | ||
"@npm//tslib", | ||
"@npm//uuid", | ||
] | ||
|
||
TYPES_DEPS = [ | ||
"@npm//@types/jest", | ||
"@npm//@types/node", | ||
"@npm//@types/uuid" | ||
] | ||
|
||
DEPS = SRC_DEPS + TYPES_DEPS | ||
|
||
ts_config( | ||
name = "tsconfig", | ||
src = "tsconfig.json", | ||
deps = [ | ||
"//:tsconfig.base.json", | ||
], | ||
) | ||
|
||
ts_project( | ||
name = "tsc", | ||
srcs = SRCS, | ||
args = ["--pretty"], | ||
declaration = True, | ||
declaration_map = True, | ||
incremental = True, | ||
out_dir = "target", | ||
root_dir = "src", | ||
source_map = True, | ||
tsconfig = ":tsconfig", | ||
deps = DEPS, | ||
) | ||
|
||
js_library( | ||
name = PKG_BASE_NAME, | ||
package_name = PKG_REQUIRE_NAME, | ||
srcs = NPM_MODULE_EXTRA_FILES, | ||
visibility = ["//visibility:public"], | ||
deps = [":tsc"] + DEPS, | ||
) | ||
|
||
pkg_npm( | ||
name = "npm_module", | ||
deps = [ | ||
":%s" % PKG_BASE_NAME, | ||
], | ||
) | ||
|
||
filegroup( | ||
name = "build", | ||
srcs = [ | ||
":npm_module", | ||
], | ||
visibility = ["//visibility:public"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# kbn-securitysolution-utils | ||
|
||
This is where shared utils for security solution should go that are going to be shared among plugins. | ||
This was originally created to remove the dependencies between security_solution and other projects such as lists. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
*/ | ||
|
||
module.exports = { | ||
preset: '@kbn/test', | ||
rootDir: '../..', | ||
roots: ['<rootDir>/packages/kbn-securitysolution-utils'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"name": "@kbn/securitysolution-utils", | ||
"version": "1.0.0", | ||
"description": "security solution utilities to use across plugins such lists, security_solution, cases, etc...", | ||
"license": "SSPL-1.0 OR Elastic License 2.0", | ||
"main": "./target/index.js", | ||
"types": "./target/index.d.ts", | ||
"private": true | ||
} |
78 changes: 78 additions & 0 deletions
78
packages/kbn-securitysolution-utils/src/add_remove_id_to_item/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* 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 { addIdToItem, removeIdFromItem } from '.'; | ||
|
||
jest.mock('uuid', () => ({ | ||
v4: jest.fn().mockReturnValue('123'), | ||
})); | ||
|
||
describe('add_remove_id_to_item', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('addIdToItem', () => { | ||
test('it adds an id to an empty item', () => { | ||
expect(addIdToItem({})).toEqual({ id: '123' }); | ||
}); | ||
|
||
test('it adds a complex object', () => { | ||
expect( | ||
addIdToItem({ | ||
field: '', | ||
type: 'mapping', | ||
value: '', | ||
}) | ||
).toEqual({ | ||
id: '123', | ||
field: '', | ||
type: 'mapping', | ||
value: '', | ||
}); | ||
}); | ||
|
||
test('it adds an id to an existing item', () => { | ||
expect(addIdToItem({ test: '456' })).toEqual({ id: '123', test: '456' }); | ||
}); | ||
|
||
test('it does not change the id if it already exists', () => { | ||
expect(addIdToItem({ id: '456' })).toEqual({ id: '456' }); | ||
}); | ||
|
||
test('it returns the same reference if it has an id already', () => { | ||
const obj = { id: '456' }; | ||
expect(addIdToItem(obj)).toBe(obj); | ||
}); | ||
|
||
test('it returns a new reference if it adds an id to an item', () => { | ||
const obj = { test: '456' }; | ||
expect(addIdToItem(obj)).not.toBe(obj); | ||
}); | ||
}); | ||
|
||
describe('removeIdFromItem', () => { | ||
test('it removes an id from an item', () => { | ||
expect(removeIdFromItem({ id: '456' })).toEqual({}); | ||
}); | ||
|
||
test('it returns a new reference if it removes an id from an item', () => { | ||
const obj = { id: '123', test: '456' }; | ||
expect(removeIdFromItem(obj)).not.toBe(obj); | ||
}); | ||
|
||
test('it does not effect an item without an id', () => { | ||
expect(removeIdFromItem({ test: '456' })).toEqual({ test: '456' }); | ||
}); | ||
|
||
test('it returns the same reference if it does not have an id already', () => { | ||
const obj = { test: '456' }; | ||
expect(removeIdFromItem(obj)).toBe(obj); | ||
}); | ||
}); | ||
}); |
51 changes: 51 additions & 0 deletions
51
packages/kbn-securitysolution-utils/src/add_remove_id_to_item/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* 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 uuid from 'uuid'; | ||
|
||
/** | ||
* This is useful for when you have arrays without an ID and need to add one for | ||
* ReactJS keys. I break the types slightly by introducing an id to an arbitrary item | ||
* but then cast it back to the regular type T. | ||
* Usage of this could be considered tech debt as I am adding an ID when the backend | ||
* could be doing the same thing but it depends on how you want to model your data and | ||
* if you view modeling your data with id's to please ReactJS a good or bad thing. | ||
* @param item The item to add an id to. | ||
*/ | ||
type NotArray<T> = T extends unknown[] ? never : T; | ||
export const addIdToItem = <T>(item: NotArray<T>): T => { | ||
const maybeId: typeof item & { id?: string } = item; | ||
if (maybeId.id != null) { | ||
return item; | ||
} else { | ||
return { ...item, id: uuid.v4() }; | ||
} | ||
}; | ||
|
||
/** | ||
* This is to reverse the id you added to your arrays for ReactJS keys. | ||
* @param item The item to remove the id from. | ||
*/ | ||
export const removeIdFromItem = <T>( | ||
item: NotArray<T> | ||
): | ||
| T | ||
| Pick< | ||
T & { | ||
id?: string | undefined; | ||
}, | ||
Exclude<keyof T, 'id'> | ||
> => { | ||
const maybeId: typeof item & { id?: string } = item; | ||
if (maybeId.id != null) { | ||
const { id, ...noId } = maybeId; | ||
return noId; | ||
} else { | ||
return item; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export * from './add_remove_id_to_item'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"declaration": true, | ||
"declarationMap": true, | ||
"incremental": true, | ||
"outDir": "target", | ||
"rootDir": "src", | ||
"sourceMap": true, | ||
"sourceRoot": "../../../../packages/kbn-securitysolution-utils/src", | ||
"types": [ | ||
"jest", | ||
"node" | ||
] | ||
}, | ||
"include": [ | ||
"src/**/*" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters