-
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
TheHive Connector for Cases #180931
Merged
Merged
TheHive Connector for Cases #180931
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
6ddf8c5
Add support of case connector for thehive
brijesh-elastic 48938ec
Merge branch 'main' into thehive_cases_plugin
elasticmachine a98d141
Merge branch 'main' into thehive_cases_plugin
elasticmachine 19b81b2
Resolve comments
brijesh-elastic 1ea1945
Merge branch 'main' into thehive_cases_plugin
elasticmachine c5ba79a
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine c58a578
Update docs
brijesh-elastic db3d840
Remove the hideInUi flag
brijesh-elastic 6d0dea7
Merge branch 'main' into thehive_cases_plugin
brijesh-elastic 1aa9649
Adding back hideInUi flag
brijesh-elastic c8e1aea
Merge branch 'main' into thehive_cases_plugin
cnasikas ac4f754
Merge branch 'main' into thehive_cases_plugin
elasticmachine bf5598d
Resolve comments
brijesh-elastic 63b8bd6
Merge branch 'main' into thehive_cases_plugin
brijesh-elastic 0b5da8a
Update TheHiveTLP enum to be safer in the future
brijesh-elastic f7c3aa6
Merge branch 'main' into thehive_cases_plugin
brijesh-elastic 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
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
50 changes: 50 additions & 0 deletions
50
x-pack/plugins/cases/public/components/connectors/thehive/case_fields.test.tsx
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,50 @@ | ||
/* | ||
* 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 React from 'react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { screen } from '@testing-library/react'; | ||
import Fields from './case_fields'; | ||
import { theHiveConnector as connector } from '../mock'; | ||
import { MockFormWrapperComponent } from '../test_utils'; | ||
import type { AppMockRenderer } from '../../../common/mock'; | ||
import { createAppMockRenderer } from '../../../common/mock'; | ||
import { TheHiveTLP } from './types'; | ||
|
||
describe('TheHive Cases Fields', () => { | ||
const fields = { | ||
TLP: 1, | ||
}; | ||
|
||
let appMockRenderer: AppMockRenderer; | ||
|
||
beforeEach(() => { | ||
appMockRenderer = createAppMockRenderer(); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('all params fields are rendered', () => { | ||
appMockRenderer.render( | ||
<MockFormWrapperComponent fields={fields}> | ||
<Fields connector={connector} /> | ||
</MockFormWrapperComponent> | ||
); | ||
|
||
expect(screen.getByText('TLP')).toBeInTheDocument(); | ||
}); | ||
|
||
it('sets TLP correctly', async () => { | ||
appMockRenderer.render( | ||
<MockFormWrapperComponent fields={fields}> | ||
<Fields connector={connector} /> | ||
</MockFormWrapperComponent> | ||
); | ||
|
||
userEvent.selectOptions(screen.getByTestId('tlp-field'), '4'); | ||
expect(await screen.findByTestId('tlp-field')).toHaveValue(TheHiveTLP.RED.toString()); | ||
}); | ||
}); |
60 changes: 60 additions & 0 deletions
60
x-pack/plugins/cases/public/components/connectors/thehive/case_fields.tsx
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,60 @@ | ||
/* | ||
* 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 React from 'react'; | ||
import { SelectField } from '@kbn/es-ui-shared-plugin/static/forms/components'; | ||
import { UseField, useFormContext } from '@kbn/es-ui-shared-plugin/static/forms/hook_form_lib'; | ||
import { fieldValidators } from '@kbn/es-ui-shared-plugin/static/forms/helpers'; | ||
import type { ConnectorFieldsProps } from '../types'; | ||
import * as i18n from './translations'; | ||
import { TheHiveTLP } from './types'; | ||
|
||
const { emptyField } = fieldValidators; | ||
|
||
const tlpOptions = Object.entries(TheHiveTLP).reduce<Array<{ text: string; value: number }>>( | ||
(acc, [key, value]) => (typeof value === 'number' ? [...acc, { text: key, value }] : acc), | ||
[] | ||
); | ||
|
||
const TheHiveFieldsComponent: React.FunctionComponent<ConnectorFieldsProps> = () => { | ||
const form = useFormContext(); | ||
|
||
const onTLPChange: (value: string) => void = (value: string) => { | ||
form.setFieldValue('fields.tlp', parseInt(value, 10)); | ||
}; | ||
|
||
return ( | ||
<div data-test-subj={'connector-fields-Thehive'}> | ||
<UseField | ||
path="fields.tlp" | ||
component={SelectField} | ||
config={{ | ||
label: i18n.TLP_LABEL, | ||
validations: [ | ||
{ | ||
validator: emptyField(i18n.TLP_REQUIRED), | ||
}, | ||
], | ||
defaultValue: TheHiveTLP.AMBER, | ||
}} | ||
onChange={onTLPChange} | ||
componentProps={{ | ||
euiFieldProps: { | ||
'data-test-subj': 'tlp-field', | ||
options: tlpOptions, | ||
fullWidth: true, | ||
}, | ||
}} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
TheHiveFieldsComponent.displayName = 'ThehiveFields'; | ||
|
||
// eslint-disable-next-line import/no-default-export | ||
export { TheHiveFieldsComponent as default }; |
34 changes: 34 additions & 0 deletions
34
x-pack/plugins/cases/public/components/connectors/thehive/case_fields_preview.test.tsx
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,34 @@ | ||
/* | ||
* 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 React from 'react'; | ||
import { screen } from '@testing-library/react'; | ||
import FieldsPreview from './case_fields_preview'; | ||
import type { AppMockRenderer } from '../../../common/mock'; | ||
import { theHiveConnector } from '../mock'; | ||
import { createAppMockRenderer } from '../../../common/mock'; | ||
import { createQueryWithMarkup } from '../../../common/test_utils'; | ||
|
||
describe('TheHive Fields: Preview', () => { | ||
const fields = { | ||
tlp: 1, | ||
}; | ||
|
||
let appMockRenderer: AppMockRenderer; | ||
|
||
beforeEach(() => { | ||
appMockRenderer = createAppMockRenderer(); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('renders all fields correctly', () => { | ||
appMockRenderer.render(<FieldsPreview connector={theHiveConnector} fields={fields} />); | ||
|
||
const getByText = createQueryWithMarkup(screen.getByText); | ||
expect(getByText('TLP: GREEN')).toBeInTheDocument(); | ||
}); | ||
}); |
54 changes: 54 additions & 0 deletions
54
x-pack/plugins/cases/public/components/connectors/thehive/case_fields_preview.tsx
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,54 @@ | ||
/* | ||
* 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 React, { useMemo } from 'react'; | ||
|
||
import type { TheHiveFieldsType } from '../../../../common/types/domain'; | ||
import { ConnectorTypes } from '../../../../common/types/domain'; | ||
import type { ConnectorFieldsPreviewProps } from '../types'; | ||
import { ConnectorCard } from '../card'; | ||
import * as i18n from './translations'; | ||
import { TheHiveTLP } from './types'; | ||
|
||
const mapTLP = (tlpValue: number): string => { | ||
const entry = Object.entries(TheHiveTLP).find(([_, value]) => value === tlpValue); | ||
return entry?.[0] ?? 'AMBER'; | ||
}; | ||
|
||
const TheHiveFieldsPreviewComponent: React.FunctionComponent< | ||
ConnectorFieldsPreviewProps<TheHiveFieldsType> | ||
> = ({ fields, connector }) => { | ||
const { tlp } = fields ?? {}; | ||
|
||
const listItems = useMemo( | ||
() => [ | ||
...(tlp !== null | ||
? [ | ||
{ | ||
title: i18n.TLP_LABEL, | ||
description: mapTLP(tlp), | ||
}, | ||
] | ||
: []), | ||
], | ||
[tlp] | ||
); | ||
|
||
return ( | ||
<ConnectorCard | ||
connectorType={ConnectorTypes.theHive} | ||
isLoading={false} | ||
listItems={listItems} | ||
title={connector.name} | ||
/> | ||
); | ||
}; | ||
|
||
TheHiveFieldsPreviewComponent.displayName = 'TheHiveFieldsPreview'; | ||
|
||
// eslint-disable-next-line import/no-default-export | ||
export { TheHiveFieldsPreviewComponent as default }; |
20 changes: 20 additions & 0 deletions
20
x-pack/plugins/cases/public/components/connectors/thehive/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,20 @@ | ||
/* | ||
* 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 { lazy } from 'react'; | ||
|
||
import type { CaseConnector } from '../types'; | ||
import type { TheHiveFieldsType } from '../../../../common/types/domain'; | ||
import { ConnectorTypes } from '../../../../common/types/domain'; | ||
|
||
export * from './types'; | ||
|
||
export const getCaseConnector = (): CaseConnector<TheHiveFieldsType> => ({ | ||
id: ConnectorTypes.theHive, | ||
fieldsComponent: lazy(() => import('./case_fields')), | ||
previewComponent: lazy(() => import('./case_fields_preview')), | ||
}); |
16 changes: 16 additions & 0 deletions
16
x-pack/plugins/cases/public/components/connectors/thehive/translations.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,16 @@ | ||
/* | ||
* 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 { i18n } from '@kbn/i18n'; | ||
|
||
export const TLP_LABEL = i18n.translate('xpack.cases.connectors.thehive.tlpLable', { | ||
defaultMessage: 'TLP', | ||
}); | ||
|
||
export const TLP_REQUIRED = i18n.translate('xpack.cases.connectors.thehive.tlpLableRequired', { | ||
defaultMessage: 'TLP is required', | ||
}); |
14 changes: 14 additions & 0 deletions
14
x-pack/plugins/cases/public/components/connectors/thehive/types.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,14 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export enum TheHiveTLP { | ||
CLEAR = 0, | ||
GREEN = 1, | ||
AMBER = 2, | ||
'AMBER+STRICT' = 3, | ||
RED = 4, | ||
} |
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 is better if we do
The reason is that by doing
we rely on the order of the enum (
index
) which may change or be different in the future. If we explicitly map the key to an integer we avoid this problem. The key can be used as the value to be shown in the UI and there is no need to rely on the order of the enum. What do you think?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.
Yes, That make more sense. Let me change it.