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

[Entity Analytics] Move scripted metric painless scripts to static file & remove category based weighting #182038

Merged
merged 27 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
df28ab8
Try making risk engine painless scripts static. also remove category …
oatkiller Apr 29, 2024
62b2ba6
Merge branch 'main' into risk-score-painless-refactor
oatkiller Apr 29, 2024
b1b6f0b
move to async loading
hop-dev May 10, 2024
a37811e
update test description
hop-dev May 10, 2024
4d6bae6
Merge branch 'main' into risk-score-painless-refactor
kibanamachine May 10, 2024
b1b2d6f
remove risk weights file and tests
hop-dev May 10, 2024
7f5a753
Merge branch 'risk-score-painless-refactor' of https://github.com/ela…
hop-dev May 10, 2024
b34b5a0
Merge branch 'main' into risk-score-painless-refactor
oatkiller May 10, 2024
691bda5
remove commented out code
hop-dev May 13, 2024
9ff6505
fix risk scoring
hop-dev May 13, 2024
66f317c
minimise painless
hop-dev May 13, 2024
cd67a37
Merge branch 'risk-score-painless-refactor' of https://github.com/ela…
hop-dev May 13, 2024
c99a6c3
re-add sorting
hop-dev May 13, 2024
5690955
condense painless further
hop-dev May 14, 2024
c4ef442
make score a double
hop-dev May 14, 2024
18b9171
remove category score test
hop-dev May 14, 2024
69f6b1c
remove category weight types
hop-dev May 14, 2024
bfc7be6
Merge branch 'main' into risk-score-painless-refactor
hop-dev May 14, 2024
9d2a3be
update jest snapshot
hop-dev May 14, 2024
8065dff
remove category weight tests
hop-dev May 15, 2024
6398ac0
do not use category weights in preview tests
hop-dev May 15, 2024
a00eb04
Merge branch 'main' into risk-score-painless-refactor
hop-dev May 15, 2024
c8ef19f
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine May 15, 2024
0f15a20
remove sort and add comment explaining why
hop-dev May 15, 2024
26f6e25
Merge branch 'main' into risk-score-painless-refactor
hop-dev May 16, 2024
471c1c8
remove category weights again
hop-dev May 16, 2024
4bd54e4
comment out even more painless
hop-dev May 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ export const RiskScoreWeightGlobalShared = z.object({
type: z.literal('global_identifier'),
});

export type RiskScoreWeightGlobal = z.infer<typeof RiskScoreWeightGlobal>;
export const RiskScoreWeightGlobal = z.union([
export type RiskScoreWeight = z.infer<typeof RiskScoreWeight>;
export const RiskScoreWeight = z.union([
RiskScoreWeightGlobalShared.merge(
z.object({
host: RiskScoreEntityIdentifierWeights,
Expand All @@ -171,34 +171,6 @@ export const RiskScoreWeightGlobal = z.union([
),
]);

export type RiskScoreWeightCategoryShared = z.infer<typeof RiskScoreWeightCategoryShared>;
export const RiskScoreWeightCategoryShared = z.object({
type: z.literal('risk_category'),
value: RiskScoreCategories,
});

export type RiskScoreWeightCategory = z.infer<typeof RiskScoreWeightCategory>;
export const RiskScoreWeightCategory = z.union([
RiskScoreWeightCategoryShared.merge(
z.object({
host: RiskScoreEntityIdentifierWeights,
user: RiskScoreEntityIdentifierWeights.optional(),
})
),
RiskScoreWeightCategoryShared.merge(
z.object({
host: RiskScoreEntityIdentifierWeights.optional(),
user: RiskScoreEntityIdentifierWeights,
})
),
]);

/**
* Configuration used to tune risk scoring. Weights can be used to change the score contribution of risk inputs for hosts and users at both a global level and also for Risk Input categories (e.g. 'category_1').
*/
export type RiskScoreWeight = z.infer<typeof RiskScoreWeight>;
export const RiskScoreWeight = z.union([RiskScoreWeightGlobal, RiskScoreWeightCategory]);

/**
* A list of weights to be applied to the scoring calculation.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ components:
enum:
- global_identifier

RiskScoreWeightGlobal:
RiskScoreWeight:
oneOf:
- allOf:
- $ref: '#/components/schemas/RiskScoreWeightGlobalShared'
Expand All @@ -225,65 +225,12 @@ components:
user:
$ref: '#/components/schemas/RiskScoreEntityIdentifierWeights'

RiskScoreWeightCategoryShared:
x-inline: true
type: object
required:
- type
- value
properties:
type:
type: string
enum:
- risk_category
value:
$ref: '#/components/schemas/RiskScoreCategories'

RiskScoreWeightCategory:
oneOf:
- allOf:
- $ref: '#/components/schemas/RiskScoreWeightCategoryShared'
- type: object
required:
- host
properties:
host:
$ref: '#/components/schemas/RiskScoreEntityIdentifierWeights'
user:
$ref: '#/components/schemas/RiskScoreEntityIdentifierWeights'

- allOf:
- $ref: '#/components/schemas/RiskScoreWeightCategoryShared'
- type: object
required:
- user
properties:
host:
$ref: '#/components/schemas/RiskScoreEntityIdentifierWeights'
user:
$ref: '#/components/schemas/RiskScoreEntityIdentifierWeights'

RiskScoreWeight:
description: "Configuration used to tune risk scoring. Weights can be used to change the score contribution of risk inputs for hosts and users at both a global level and also for Risk Input categories (e.g. 'category_1')."
oneOf:
- $ref: '#/components/schemas/RiskScoreWeightGlobal'
- $ref: '#/components/schemas/RiskScoreWeightCategory'
example:
type: 'risk_category'
value: 'category_1'
host: 0.8
user: 0.4

RiskScoreWeights:
description: 'A list of weights to be applied to the scoring calculation.'
type: array
items:
$ref: '#/components/schemas/RiskScoreWeight'
example:
- type: 'risk_category'
value: 'category_1'
host: 0.8
user: 0.4
- type: 'global_identifier'
host: 0.5
user: 0.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ describe('risk weight schema', () => {
const decoded = RiskScoreWeight.safeParse(payload) as SafeParseError<object>;

expect(decoded.success).toBeFalsy();
expect(stringifyZodError(decoded.error)).toEqual(
'host: Required, user: Required, type: Invalid literal value, expected "risk_category", value: Invalid literal value, expected "category_1", host: Required, and 3 more'
);
expect(stringifyZodError(decoded.error)).toContain('host: Required, user: Required');
});

it('allows a single host weight', () => {
Expand Down Expand Up @@ -123,44 +121,10 @@ describe('risk weight schema', () => {

expect(decoded.success).toBeFalsy();
expect(stringifyZodError(decoded.error)).toEqual(
'type: Invalid literal value, expected "global_identifier", host: Required, type: Invalid literal value, expected "global_identifier", value: Invalid literal value, expected "category_1", host: Required, and 1 more'
'type: Invalid literal value, expected "global_identifier", host: Required, type: Invalid literal value, expected "global_identifier"'
);
});

it('rejects if neither host nor user weight are specified', () => {
const payload = { type, value: RiskCategories.category_1 };
const decoded = RiskScoreWeight.safeParse(payload) as SafeParseError<object>;

expect(decoded.success).toBeFalsy();
expect(stringifyZodError(decoded.error)).toEqual(
'type: Invalid literal value, expected "global_identifier", host: Required, type: Invalid literal value, expected "global_identifier", user: Required, host: Required, and 1 more'
);
});

it('allows a single host weight', () => {
const payload = { type, value: RiskCategories.category_1, host: 0.1 };
const decoded = RiskScoreWeight.safeParse(payload) as SafeParseSuccess<object>;

expect(decoded.success).toBeTruthy();
expect(decoded.data).toEqual(payload);
});

it('allows a single user weight', () => {
const payload = { type, value: RiskCategories.category_1, user: 0.1 };
const decoded = RiskScoreWeight.safeParse(payload) as SafeParseSuccess<object>;

expect(decoded.success).toBeTruthy();
expect(decoded.data).toEqual(payload);
});

it('allows both a host and user weight', () => {
const payload = { type, value: RiskCategories.category_1, user: 0.1, host: 0.5 };
const decoded = RiskScoreWeight.safeParse(payload) as SafeParseSuccess<object>;

expect(decoded.success).toBeTruthy();
expect(decoded.data).toEqual(payload);
});

it('rejects a weight outside of 0-1', () => {
const payload = { type, value: RiskCategories.category_1, host: -5 };
const decoded = RiskScoreWeight.safeParse(payload) as SafeParseError<object>;
Expand All @@ -170,47 +134,6 @@ describe('risk weight schema', () => {
`host: Number must be greater than or equal to 0`
);
});

it('removes extra keys if specified', () => {
const payload = {
type,
value: RiskCategories.category_1,
host: 0.1,
extra: 'even more',
};
const decoded = RiskScoreWeight.safeParse(payload) as SafeParseSuccess<object>;

expect(decoded.success).toBeTruthy();
expect(decoded.data).toEqual({ type, value: RiskCategories.category_1, host: 0.1 });
});

describe('allowed category values', () => {
it('allows the alerts type for a category', () => {
const payload = {
type,
value: RiskCategories.category_1,
host: 0.1,
};
const decoded = RiskScoreWeight.safeParse(payload) as SafeParseSuccess<object>;

expect(decoded.success).toBeTruthy();
expect(decoded.data).toEqual(payload);
});

it('rejects an unknown category value', () => {
const payload = {
type,
value: 'unknown',
host: 0.1,
};
const decoded = RiskScoreWeight.safeParse(payload) as SafeParseError<object>;

expect(decoded.success).toBeFalsy();
expect(stringifyZodError(decoded.error)).toContain(
'type: Invalid literal value, expected "global_identifier", type: Invalid literal value, expected "global_identifier", user: Required, value: Invalid literal value, expected "category_1", value: Invalid literal value, expected "category_1", and 1 more'
);
});
});
});
});
});