-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'case_action' of github.com:elastic/kibana into case_action
- Loading branch information
Showing
20 changed files
with
752 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
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
42 changes: 42 additions & 0 deletions
42
x-pack/plugins/cases/server/connectors/cases/cases_connector.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,42 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type { ServiceParams } from '@kbn/actions-plugin/server'; | ||
import { SubActionConnector } from '@kbn/actions-plugin/server'; | ||
import { CASES_CONNECTOR_SUB_ACTION } from './constants'; | ||
import type { CasesConnectorConfig, CasesConnectorSecrets } from './types'; | ||
import { CasesConnectorParamsSchema } from './schema'; | ||
|
||
export class CasesConnector extends SubActionConnector< | ||
CasesConnectorConfig, | ||
CasesConnectorSecrets | ||
> { | ||
constructor(params: ServiceParams<CasesConnectorConfig, CasesConnectorSecrets>) { | ||
super(params); | ||
|
||
this.registerSubActions(); | ||
} | ||
|
||
private registerSubActions() { | ||
this.registerSubAction({ | ||
name: CASES_CONNECTOR_SUB_ACTION.RUN, | ||
method: 'run', | ||
schema: CasesConnectorParamsSchema, | ||
}); | ||
} | ||
|
||
/** | ||
* Method is not needed for the Case Connector. | ||
* The function throws an error as a reminder to | ||
* implement it if we need it in the future. | ||
*/ | ||
protected getResponseErrorMessage(): string { | ||
throw new Error('Method not implemented.'); | ||
} | ||
|
||
public async run() {} | ||
} |
283 changes: 283 additions & 0 deletions
283
x-pack/plugins/cases/server/connectors/cases/cases_oracle_service.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,283 @@ | ||
/* | ||
* 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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { createHash } from 'node:crypto'; | ||
import stringify from 'json-stable-stringify'; | ||
import { savedObjectsClientMock } from '@kbn/core-saved-objects-api-server-mocks'; | ||
import { loggerMock } from '@kbn/logging-mocks'; | ||
|
||
import { CasesOracleService } from './cases_oracle_service'; | ||
import { CASE_ORACLE_SAVED_OBJECT } from '../../../common/constants'; | ||
import { isEmpty, set } from 'lodash'; | ||
|
||
describe('CasesOracleService', () => { | ||
const unsecuredSavedObjectsClient = savedObjectsClientMock.create(); | ||
const mockLogger = loggerMock.create(); | ||
|
||
let service: CasesOracleService; | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
service = new CasesOracleService({ unsecuredSavedObjectsClient, log: mockLogger }); | ||
}); | ||
|
||
describe('getRecordId', () => { | ||
it('return the record ID correctly', async () => { | ||
const ruleId = 'test-rule-id'; | ||
const spaceId = 'default'; | ||
const owner = 'cases'; | ||
const grouping = { 'host.ip': '0.0.0.1' }; | ||
|
||
const payload = `${ruleId}:${spaceId}:${owner}:${stringify(grouping)}`; | ||
const hash = createHash('sha256'); | ||
|
||
hash.update(payload); | ||
|
||
const hex = hash.digest('hex'); | ||
|
||
expect(service.getRecordId({ ruleId, spaceId, owner, grouping })).toEqual(hex); | ||
}); | ||
|
||
it('sorts the grouping definition correctly', async () => { | ||
const ruleId = 'test-rule-id'; | ||
const spaceId = 'default'; | ||
const owner = 'cases'; | ||
const grouping = { 'host.ip': '0.0.0.1', 'agent.id': '8a4f500d' }; | ||
const sortedGrouping = { 'agent.id': '8a4f500d', 'host.ip': '0.0.0.1' }; | ||
|
||
const payload = `${ruleId}:${spaceId}:${owner}:${stringify(sortedGrouping)}`; | ||
const hash = createHash('sha256'); | ||
|
||
hash.update(payload); | ||
|
||
const hex = hash.digest('hex'); | ||
|
||
expect(service.getRecordId({ ruleId, spaceId, owner, grouping })).toEqual(hex); | ||
}); | ||
|
||
it('return the record ID correctly without grouping', async () => { | ||
const ruleId = 'test-rule-id'; | ||
const spaceId = 'default'; | ||
const owner = 'cases'; | ||
|
||
const payload = `${ruleId}:${spaceId}:${owner}`; | ||
const hash = createHash('sha256'); | ||
|
||
hash.update(payload); | ||
|
||
const hex = hash.digest('hex'); | ||
|
||
expect(service.getRecordId({ ruleId, spaceId, owner })).toEqual(hex); | ||
}); | ||
|
||
it('return the record ID correctly with empty grouping', async () => { | ||
const ruleId = 'test-rule-id'; | ||
const spaceId = 'default'; | ||
const owner = 'cases'; | ||
const grouping = {}; | ||
|
||
const payload = `${ruleId}:${spaceId}:${owner}:${stringify(grouping)}`; | ||
const hash = createHash('sha256'); | ||
|
||
hash.update(payload); | ||
|
||
const hex = hash.digest('hex'); | ||
|
||
expect(service.getRecordId({ ruleId, spaceId, owner, grouping })).toEqual(hex); | ||
}); | ||
|
||
it('return the record ID correctly without rule', async () => { | ||
const spaceId = 'default'; | ||
const owner = 'cases'; | ||
const grouping = { 'host.ip': '0.0.0.1' }; | ||
|
||
const payload = `${spaceId}:${owner}:${stringify(grouping)}`; | ||
const hash = createHash('sha256'); | ||
|
||
hash.update(payload); | ||
|
||
const hex = hash.digest('hex'); | ||
|
||
expect(service.getRecordId({ spaceId, owner, grouping })).toEqual(hex); | ||
}); | ||
|
||
it('throws an error when the ruleId and the grouping is missing', async () => { | ||
const spaceId = 'default'; | ||
const owner = 'cases'; | ||
|
||
// @ts-expect-error: ruleId and grouping are omitted for testing | ||
expect(() => service.getRecordId({ spaceId, owner })).toThrowErrorMatchingInlineSnapshot( | ||
`"ruleID or grouping is required"` | ||
); | ||
}); | ||
|
||
it.each(['ruleId', 'spaceId', 'owner'])( | ||
'return the record ID correctly with empty string for %s', | ||
async (key) => { | ||
const getPayloadValue = (value: string) => (isEmpty(value) ? '' : `${value}:`); | ||
|
||
const params = { | ||
ruleId: 'test-rule-id', | ||
spaceId: 'default', | ||
owner: 'cases', | ||
}; | ||
|
||
const grouping = { 'host.ip': '0.0.0.1' }; | ||
|
||
set(params, key, ''); | ||
|
||
const payload = `${getPayloadValue(params.ruleId)}${getPayloadValue( | ||
params.spaceId | ||
)}${getPayloadValue(params.owner)}${stringify(grouping)}`; | ||
|
||
const hash = createHash('sha256'); | ||
|
||
hash.update(payload); | ||
|
||
const hex = hash.digest('hex'); | ||
|
||
expect(service.getRecordId({ ...params, grouping })).toEqual(hex); | ||
} | ||
); | ||
}); | ||
|
||
describe('getRecord', () => { | ||
const cases = [{ id: 'test-case-id' }]; | ||
const rules = [{ id: 'test-rule-id' }]; | ||
const grouping = { 'host.ip': '0.0.0.1', 'agent.id': '8a4f500d' }; | ||
|
||
const oracleSO = { | ||
id: 'so-id', | ||
version: 'so-version', | ||
attributes: { | ||
counter: 1, | ||
cases, | ||
rules, | ||
grouping, | ||
createdAt: '2023-10-10T10:23:42.769Z', | ||
updatedAt: '2023-10-10T10:23:42.769Z', | ||
}, | ||
type: CASE_ORACLE_SAVED_OBJECT, | ||
references: [], | ||
}; | ||
|
||
beforeEach(() => { | ||
unsecuredSavedObjectsClient.get.mockResolvedValue(oracleSO); | ||
}); | ||
|
||
it('gets a record correctly', async () => { | ||
const record = await service.getRecord('so-id'); | ||
|
||
expect(record).toEqual({ ...oracleSO.attributes, id: 'so-id', version: 'so-version' }); | ||
}); | ||
}); | ||
|
||
describe('createRecord', () => { | ||
const cases = [{ id: 'test-case-id' }]; | ||
const rules = [{ id: 'test-rule-id' }]; | ||
const grouping = { 'host.ip': '0.0.0.1', 'agent.id': '8a4f500d' }; | ||
|
||
const oracleSO = { | ||
id: 'so-id', | ||
version: 'so-version', | ||
attributes: { | ||
counter: 1, | ||
cases, | ||
rules, | ||
grouping, | ||
createdAt: '2023-10-10T10:23:42.769Z', | ||
updatedAt: '2023-10-10T10:23:42.769Z', | ||
}, | ||
type: CASE_ORACLE_SAVED_OBJECT, | ||
references: [], | ||
}; | ||
|
||
beforeEach(() => { | ||
unsecuredSavedObjectsClient.create.mockResolvedValue(oracleSO); | ||
}); | ||
|
||
it('creates a record correctly', async () => { | ||
const record = await service.createRecord('so-id', { cases, rules, grouping }); | ||
|
||
expect(record).toEqual({ ...oracleSO.attributes, id: 'so-id', version: 'so-version' }); | ||
}); | ||
|
||
it('calls the unsecuredSavedObjectsClient.create method correctly', async () => { | ||
const id = 'so-id'; | ||
|
||
await service.createRecord(id, { cases, rules, grouping }); | ||
|
||
expect(unsecuredSavedObjectsClient.create).toHaveBeenCalledWith( | ||
'cases-oracle', | ||
{ | ||
cases, | ||
counter: 1, | ||
createdAt: expect.anything(), | ||
rules, | ||
grouping, | ||
updatedAt: null, | ||
}, | ||
{ id } | ||
); | ||
}); | ||
}); | ||
|
||
describe('increaseCounter', () => { | ||
const cases = [{ id: 'test-case-id' }]; | ||
const rules = [{ id: 'test-rule-id' }]; | ||
const grouping = { 'host.ip': '0.0.0.1', 'agent.id': '8a4f500d' }; | ||
|
||
const oracleSO = { | ||
id: 'so-id', | ||
version: 'so-version', | ||
attributes: { | ||
counter: 1, | ||
cases, | ||
rules, | ||
grouping, | ||
createdAt: '2023-10-10T10:23:42.769Z', | ||
updatedAt: '2023-10-10T10:23:42.769Z', | ||
}, | ||
type: CASE_ORACLE_SAVED_OBJECT, | ||
references: [], | ||
}; | ||
|
||
const oracleSOWithIncreasedCounter = { | ||
...oracleSO, | ||
attributes: { ...oracleSO.attributes, counter: 2 }, | ||
}; | ||
|
||
beforeEach(() => { | ||
unsecuredSavedObjectsClient.get.mockResolvedValue(oracleSO); | ||
unsecuredSavedObjectsClient.update.mockResolvedValue(oracleSOWithIncreasedCounter); | ||
}); | ||
|
||
it('increases the counter correctly', async () => { | ||
const record = await service.increaseCounter('so-id'); | ||
|
||
expect(record).toEqual({ | ||
...oracleSO.attributes, | ||
id: 'so-id', | ||
version: 'so-version', | ||
counter: 2, | ||
}); | ||
}); | ||
|
||
it('calls the unsecuredSavedObjectsClient.update method correctly', async () => { | ||
await service.increaseCounter('so-id'); | ||
|
||
expect(unsecuredSavedObjectsClient.update).toHaveBeenCalledWith( | ||
'cases-oracle', | ||
'so-id', | ||
{ | ||
counter: 2, | ||
}, | ||
{ version: 'so-version' } | ||
); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.