Skip to content

Commit

Permalink
[Security Solutions] (Phase 1) Initial checkin with kbn package secur…
Browse files Browse the repository at this point in the history
…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
FrankHassanabad authored May 4, 2021
1 parent 29e48b8 commit 08d7e1c
Show file tree
Hide file tree
Showing 12 changed files with 273 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/developer/getting-started/monorepo-packages.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ yarn kbn watch-bazel
- @kbn/expect
- @kbn/legacy-logging
- @kbn/logging
- @kbn/securitysolution-utils
- @kbn/securitysolution-io-ts-utils
- @kbn/std
- @kbn/tinymath
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
"@kbn/legacy-logging": "link:bazel-bin/packages/kbn-legacy-logging/npm_module",
"@kbn/logging": "link:bazel-bin/packages/kbn-logging/npm_module",
"@kbn/monaco": "link:packages/kbn-monaco",
"@kbn/securitysolution-utils": "link:bazel-bin/packages/kbn-securitysolution-utils/npm_module",
"@kbn/securitysolution-io-ts-utils": "link:bazel-bin/packages/kbn-securitysolution-io-ts-utils/npm_module",
"@kbn/server-http-tools": "link:packages/kbn-server-http-tools",
"@kbn/server-route-repository": "link:packages/kbn-server-route-repository",
Expand Down
1 change: 1 addition & 0 deletions packages/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ filegroup(
"//packages/kbn-legacy-logging:build",
"//packages/kbn-logging:build",
"//packages/kbn-securitysolution-io-ts-utils:build",
"//packages/kbn-securitysolution-utils:build",
"//packages/kbn-std:build",
"//packages/kbn-tinymath:build",
"//packages/kbn-utility-types:build",
Expand Down
86 changes: 86 additions & 0 deletions packages/kbn-securitysolution-utils/BUILD.bazel
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"],
)
4 changes: 4 additions & 0 deletions packages/kbn-securitysolution-utils/README.md
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.
13 changes: 13 additions & 0 deletions packages/kbn-securitysolution-utils/jest.config.js
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'],
};
9 changes: 9 additions & 0 deletions packages/kbn-securitysolution-utils/package.json
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
}
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);
});
});
});
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;
}
};
9 changes: 9 additions & 0 deletions packages/kbn-securitysolution-utils/src/index.ts
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';
19 changes: 19 additions & 0 deletions packages/kbn-securitysolution-utils/tsconfig.json
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/**/*"
]
}
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2691,6 +2691,7 @@
version "0.0.0"
uid ""

"@kbn/securitysolution-utils@link:bazel-bin/packages/kbn-securitysolution-utils/npm_module":
"@kbn/securitysolution-io-ts-utils@link:bazel-bin/packages/kbn-securitysolution-io-ts-utils/npm_module":
version "0.0.0"
uid ""
Expand Down

0 comments on commit 08d7e1c

Please sign in to comment.