-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Security Solution][Detections][Tech Debt] - Move to using common io-ts types #75009
Merged
Merged
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8548c1d
wip using shared types, NewRule
yctercero 4e77ecf
cleaned up types
yctercero 9a7b05c
updated some rule hooks and tests
yctercero 79ab71d
Merge branch 'master' of github.com:yctercero/kibana into ui-types
yctercero dcfb598
Merge branch 'master' of github.com:yctercero/kibana into ui-types
yctercero f713a28
removed api io-ts validation, can be discussed separatly
yctercero 6a9f432
cleanup
yctercero f983e83
Merge branch 'master' of github.com:yctercero/kibana into ui-types
yctercero 0b3773d
update missing mock in unit test
yctercero 5e1fd07
update unit test
yctercero 6393862
update test description
yctercero 3656316
Merge branch 'master' into ui-types
elasticmachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
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
18 changes: 18 additions & 0 deletions
18
x-pack/plugins/security_solution/common/detection_engine/schemas/request/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,18 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
export * from './add_prepackaged_rules_schema'; | ||
export * from './create_rules_bulk_schema'; | ||
export * from './create_rules_schema'; | ||
export * from './export_rules_schema'; | ||
export * from './find_rules_schema'; | ||
export * from './import_rules_schema'; | ||
export * from './patch_rules_bulk_schema'; | ||
export * from './patch_rules_schema'; | ||
export * from './query_rules_schema'; | ||
export * from './query_signals_index_schema'; | ||
export * from './set_signal_status_schema'; | ||
export * from './update_rules_bulk_schema'; | ||
export * from './update_rules_schema'; |
13 changes: 13 additions & 0 deletions
13
x-pack/plugins/security_solution/common/detection_engine/schemas/response/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,13 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
export * from './error_schema'; | ||
export * from './find_rules_schema'; | ||
export * from './import_rules_schema'; | ||
export * from './prepackaged_rules_schema'; | ||
export * from './prepackaged_rules_status_schema'; | ||
export * from './rules_bulk_schema'; | ||
export * from './rules_schema'; | ||
export * from './type_timeline_only_schema'; |
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 |
---|---|---|
|
@@ -43,4 +43,6 @@ export { | |
ExceptionListType, | ||
Type, | ||
ENDPOINT_LIST_ID, | ||
toPromise, | ||
toError, | ||
} from '../../lists/common'; |
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 |
---|---|---|
|
@@ -6,7 +6,9 @@ | |
|
||
import { KibanaServices } from '../../../../common/lib/kibana'; | ||
import { | ||
addRule, | ||
createRule, | ||
updateRule, | ||
patchRule, | ||
fetchRules, | ||
fetchRuleById, | ||
enableRules, | ||
|
@@ -19,9 +21,17 @@ import { | |
fetchTags, | ||
getPrePackagedRulesStatus, | ||
} from './api'; | ||
import { ruleMock, rulesMock } from './mock'; | ||
import { getRulesSchemaMock } from '../../../../../common/detection_engine/schemas/response/rules_schema.mocks'; | ||
import { getCreateRulesSchemaMock } from '../../../../../common/detection_engine/schemas/request/create_rules_schema.mock'; | ||
import { getUpdateRulesSchemaMock } from '../../../../../common/detection_engine/schemas/request/update_rules_schema.mock'; | ||
import { getPatchRulesSchemaMock } from '../../../../../common/detection_engine/schemas/request/patch_rules_schema.mock'; | ||
import { rulesMock } from './mock'; | ||
import { buildEsQuery } from 'src/plugins/data/common'; | ||
|
||
import { | ||
UpdateRulesSchema, | ||
CreateRulesSchema, | ||
PatchRulesSchema, | ||
} from '../../../../../common/detection_engine/schemas/request'; | ||
const abortCtrl = new AbortController(); | ||
const mockKibanaServices = KibanaServices.get as jest.Mock; | ||
jest.mock('../../../../common/lib/kibana'); | ||
|
@@ -30,25 +40,145 @@ const fetchMock = jest.fn(); | |
mockKibanaServices.mockReturnValue({ http: { fetch: fetchMock } }); | ||
|
||
describe('Detections Rules API', () => { | ||
describe('addRule', () => { | ||
describe('createRule', () => { | ||
beforeEach(() => { | ||
fetchMock.mockClear(); | ||
fetchMock.mockResolvedValue(ruleMock); | ||
fetchMock.mockResolvedValue(getRulesSchemaMock()); | ||
}); | ||
|
||
test('check parameter url, body', async () => { | ||
await addRule({ rule: ruleMock, signal: abortCtrl.signal }); | ||
test('POSTs rule', async () => { | ||
const payload = getCreateRulesSchemaMock(); | ||
await createRule({ rule: payload, signal: abortCtrl.signal }); | ||
expect(fetchMock).toHaveBeenCalledWith('/api/detection_engine/rules', { | ||
body: | ||
'{"description":"some desc","enabled":true,"false_positives":[],"filters":[],"from":"now-360s","index":["apm-*-transaction*","auditbeat-*","endgame-*","filebeat-*","packetbeat-*","winlogbeat-*"],"interval":"5m","rule_id":"bbd3106e-b4b5-4d7c-a1a2-47531d6a2baf","language":"kuery","risk_score":75,"name":"Test rule","query":"user.email: \'root@elastic.co\'","references":[],"severity":"high","tags":["APM"],"to":"now","type":"query","threat":[],"throttle":null}', | ||
'{"description":"Detecting root and admin users","name":"Query with a rule id","severity":"high","type":"query","risk_score":55,"query":"user.name: root or user.name: admin","language":"kuery","rule_id":"rule-1","actions":[],"author":[],"enabled":true,"false_positives":[],"from":"now-6m","interval":"5m","max_signals":100,"risk_score_mapping":[],"severity_mapping":[],"tags":[],"to":"now","threat":[],"throttle":null,"references":[],"version":1,"exceptions_list":[]}', | ||
method: 'POST', | ||
signal: abortCtrl.signal, | ||
}); | ||
}); | ||
|
||
test('happy path', async () => { | ||
const ruleResp = await addRule({ rule: ruleMock, signal: abortCtrl.signal }); | ||
expect(ruleResp).toEqual(ruleMock); | ||
it('rejects with an error if request payload is invalid (and does not make API call)', async () => { | ||
const payload: Partial<CreateRulesSchema> = { | ||
rule_id: 'rule-1', | ||
description: 'some description', | ||
from: 'now-5m', | ||
to: 'now', | ||
name: 'some-name', | ||
severity: 'low', | ||
type: 'query', | ||
interval: '5m', | ||
index: ['index-1'], | ||
}; | ||
|
||
await expect( | ||
createRule({ | ||
rule: (payload as unknown) as CreateRulesSchema, | ||
signal: abortCtrl.signal, | ||
}) | ||
).rejects.toEqual(new Error('Invalid value "undefined" supplied to "risk_score"')); | ||
expect(fetchMock).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('rejects with an error if response payload is invalid', async () => { | ||
const payload: CreateRulesSchema = getCreateRulesSchemaMock(); | ||
const badResponse = { ...getRulesSchemaMock(), id: undefined }; | ||
fetchMock.mockResolvedValue(badResponse); | ||
|
||
await expect( | ||
createRule({ | ||
rule: payload, | ||
signal: abortCtrl.signal, | ||
}) | ||
).rejects.toEqual(new Error('Invalid value "undefined" supplied to "id"')); | ||
}); | ||
}); | ||
|
||
describe('updateRule', () => { | ||
beforeEach(() => { | ||
fetchMock.mockClear(); | ||
fetchMock.mockResolvedValue(getRulesSchemaMock()); | ||
}); | ||
|
||
test('POSTs rule', async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test name here says |
||
const payload = getUpdateRulesSchemaMock(); | ||
await updateRule({ rule: payload, signal: abortCtrl.signal }); | ||
expect(fetchMock).toHaveBeenCalledWith('/api/detection_engine/rules', { | ||
body: | ||
'{"description":"some description","name":"Query with a rule id","severity":"high","type":"query","risk_score":55,"query":"user.name: root or user.name: admin","language":"kuery","rule_id":"rule-1","actions":[],"author":[],"enabled":true,"false_positives":[],"from":"now-6m","interval":"5m","max_signals":100,"risk_score_mapping":[],"severity_mapping":[],"tags":[],"to":"now","threat":[],"throttle":null,"references":[],"exceptions_list":[]}', | ||
method: 'PUT', | ||
signal: abortCtrl.signal, | ||
}); | ||
}); | ||
|
||
it('rejects with an error if request payload is invalid (and does not make API call)', async () => { | ||
const payload: Omit<UpdateRulesSchema, 'note'> & { | ||
note: number; | ||
} = { ...getUpdateRulesSchemaMock(), note: 23 }; | ||
|
||
await expect( | ||
updateRule({ | ||
rule: (payload as unknown) as UpdateRulesSchema, | ||
signal: abortCtrl.signal, | ||
}) | ||
).rejects.toEqual(new Error('Invalid value "23" supplied to "note"')); | ||
expect(fetchMock).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('rejects with an error if response payload is invalid', async () => { | ||
const payload: UpdateRulesSchema = getUpdateRulesSchemaMock(); | ||
const badResponse = { ...getRulesSchemaMock(), id: undefined }; | ||
fetchMock.mockResolvedValue(badResponse); | ||
|
||
await expect( | ||
updateRule({ | ||
rule: payload, | ||
signal: abortCtrl.signal, | ||
}) | ||
).rejects.toEqual(new Error('Invalid value "undefined" supplied to "id"')); | ||
}); | ||
}); | ||
|
||
describe('patchRule', () => { | ||
beforeEach(() => { | ||
fetchMock.mockClear(); | ||
fetchMock.mockResolvedValue(getRulesSchemaMock()); | ||
}); | ||
|
||
test('PATCHs rule', async () => { | ||
const payload = getPatchRulesSchemaMock(); | ||
await patchRule({ ruleProperties: payload, signal: abortCtrl.signal }); | ||
expect(fetchMock).toHaveBeenCalledWith('/api/detection_engine/rules', { | ||
body: JSON.stringify(payload), | ||
method: 'PATCH', | ||
signal: abortCtrl.signal, | ||
}); | ||
}); | ||
|
||
it('rejects with an error if request payload is invalid (and does not make API call)', async () => { | ||
const payload: Omit<PatchRulesSchema, 'note'> & { | ||
note: number; | ||
} = { ...getPatchRulesSchemaMock(), note: 3 }; | ||
|
||
await expect( | ||
patchRule({ | ||
ruleProperties: (payload as unknown) as PatchRulesSchema, | ||
signal: abortCtrl.signal, | ||
}) | ||
).rejects.toEqual(new Error('Invalid value "3" supplied to "note"')); | ||
expect(fetchMock).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('rejects with an error if response payload is invalid', async () => { | ||
const payload: PatchRulesSchema = getPatchRulesSchemaMock(); | ||
const badResponse = { ...getRulesSchemaMock(), id: undefined }; | ||
fetchMock.mockResolvedValue(badResponse); | ||
|
||
await expect( | ||
patchRule({ | ||
ruleProperties: payload, | ||
signal: abortCtrl.signal, | ||
}) | ||
).rejects.toEqual(new Error('Invalid value "undefined" supplied to "id"')); | ||
}); | ||
}); | ||
|
||
|
@@ -280,7 +410,7 @@ describe('Detections Rules API', () => { | |
describe('fetchRuleById', () => { | ||
beforeEach(() => { | ||
fetchMock.mockClear(); | ||
fetchMock.mockResolvedValue(ruleMock); | ||
fetchMock.mockResolvedValue(getRulesSchemaMock()); | ||
}); | ||
|
||
test('check parameter url, query', async () => { | ||
|
@@ -296,7 +426,7 @@ describe('Detections Rules API', () => { | |
|
||
test('happy path', async () => { | ||
const ruleResp = await fetchRuleById({ id: 'mySuperRuleId', signal: abortCtrl.signal }); | ||
expect(ruleResp).toEqual(ruleMock); | ||
expect(ruleResp).toEqual(getRulesSchemaMock()); | ||
}); | ||
}); | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this was just a typo? Seems like all other typescript ones are uppercase.