diff --git a/x-pack/legacy/plugins/siem/index.ts b/x-pack/legacy/plugins/siem/index.ts index db398821aecfd..3773283555b32 100644 --- a/x-pack/legacy/plugins/siem/index.ts +++ b/x-pack/legacy/plugins/siem/index.ts @@ -40,7 +40,7 @@ export const siem = (kibana: any) => { id: APP_ID, configPrefix: 'xpack.siem', publicDir: resolve(__dirname, 'public'), - require: ['kibana', 'elasticsearch', 'alerting', 'actions'], + require: ['kibana', 'elasticsearch', 'alerting', 'actions', 'triggers_actions_ui'], uiExports: { app: { description: i18n.translate('xpack.siem.securityDescription', { diff --git a/x-pack/legacy/plugins/siem/public/containers/case/configure/api.ts b/x-pack/legacy/plugins/siem/public/containers/case/configure/api.ts new file mode 100644 index 0000000000000..a6db36d8f64e7 --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/containers/case/configure/api.ts @@ -0,0 +1,98 @@ +/* + * 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. + */ + +import { isEmpty } from 'lodash/fp'; +import { + CasesConnectorsFindResult, + CasesConfigurePatch, + CasesConfigureResponse, + CasesConfigureRequest, +} from '../../../../../../../plugins/case/common/api'; +import { KibanaServices } from '../../../lib/kibana'; + +import { CASES_CONFIGURE_URL } from '../constants'; +import { ApiProps } from '../types'; +import { convertToCamelCase, decodeCaseConfigureResponse } from '../utils'; +import { CaseConfigure, PatchConnectorProps } from './types'; + +export const fetchConnectors = async ({ signal }: ApiProps): Promise => { + const response = await KibanaServices.get().http.fetch( + `${CASES_CONFIGURE_URL}/connectors/_find`, + { + method: 'GET', + signal, + } + ); + + return response; +}; + +export const getCaseConfigure = async ({ signal }: ApiProps): Promise => { + const response = await KibanaServices.get().http.fetch( + CASES_CONFIGURE_URL, + { + method: 'GET', + signal, + } + ); + + return !isEmpty(response) + ? convertToCamelCase( + decodeCaseConfigureResponse(response) + ) + : null; +}; + +export const postCaseConfigure = async ( + caseConfiguration: CasesConfigureRequest, + signal: AbortSignal +): Promise => { + const response = await KibanaServices.get().http.fetch( + CASES_CONFIGURE_URL, + { + method: 'POST', + body: JSON.stringify(caseConfiguration), + signal, + } + ); + return convertToCamelCase( + decodeCaseConfigureResponse(response) + ); +}; + +export const patchCaseConfigure = async ( + caseConfiguration: CasesConfigurePatch, + signal: AbortSignal +): Promise => { + const response = await KibanaServices.get().http.fetch( + CASES_CONFIGURE_URL, + { + method: 'PATCH', + body: JSON.stringify(caseConfiguration), + signal, + } + ); + return convertToCamelCase( + decodeCaseConfigureResponse(response) + ); +}; + +export const patchConfigConnector = async ({ + connectorId, + config, + signal, +}: PatchConnectorProps): Promise => { + const response = await KibanaServices.get().http.fetch( + `${CASES_CONFIGURE_URL}/connectors/${connectorId}`, + { + method: 'PATCH', + body: JSON.stringify(config), + signal, + } + ); + + return response; +}; diff --git a/x-pack/legacy/plugins/siem/public/containers/case/configure/types.ts b/x-pack/legacy/plugins/siem/public/containers/case/configure/types.ts new file mode 100644 index 0000000000000..840828307163c --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/containers/case/configure/types.ts @@ -0,0 +1,43 @@ +/* + * 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. + */ + +import { ElasticUser, ApiProps } from '../types'; +import { + ActionType, + CasesConnectorConfiguration, + CasesConfigurationMaps, + CaseField, + ClosureType, + Connector, + ThirdPartyField, +} from '../../../../../../../plugins/case/common/api'; + +export { ActionType, CasesConfigurationMaps, CaseField, ClosureType, Connector, ThirdPartyField }; + +export interface CasesConfigurationMapping { + source: CaseField; + target: ThirdPartyField; + actionType: ActionType; +} + +export interface CaseConfigure { + createdAt: string; + createdBy: ElasticUser; + connectorId: string; + closureType: ClosureType; + updatedAt: string; + updatedBy: ElasticUser; + version: string; +} + +export interface PatchConnectorProps extends ApiProps { + connectorId: string; + config: CasesConnectorConfiguration; +} + +export interface CCMapsCombinedActionAttributes extends CasesConfigurationMaps { + actionType?: ActionType; +} diff --git a/x-pack/legacy/plugins/siem/public/containers/case/configure/use_configure.tsx b/x-pack/legacy/plugins/siem/public/containers/case/configure/use_configure.tsx new file mode 100644 index 0000000000000..22ac54093d1dc --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/containers/case/configure/use_configure.tsx @@ -0,0 +1,129 @@ +/* + * 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. + */ + +import { useState, useEffect, useCallback } from 'react'; +import { getCaseConfigure, patchCaseConfigure, postCaseConfigure } from './api'; + +import { useStateToaster, errorToToaster } from '../../../components/toasters'; +import * as i18n from '../translations'; +import { ClosureType } from './types'; + +interface PersistCaseConfigure { + connectorId: string; + closureType: ClosureType; +} + +export interface ReturnUseCaseConfigure { + loading: boolean; + refetchCaseConfigure: () => void; + persistCaseConfigure: ({ connectorId, closureType }: PersistCaseConfigure) => unknown; + persistLoading: boolean; +} + +interface UseCaseConfigure { + setConnectorId: (newConnectorId: string) => void; + setClosureType: (newClosureType: ClosureType) => void; +} + +export const useCaseConfigure = ({ + setConnectorId, + setClosureType, +}: UseCaseConfigure): ReturnUseCaseConfigure => { + const [, dispatchToaster] = useStateToaster(); + const [loading, setLoading] = useState(true); + const [persistLoading, setPersistLoading] = useState(false); + const [version, setVersion] = useState(''); + + const refetchCaseConfigure = useCallback(() => { + let didCancel = false; + const abortCtrl = new AbortController(); + + const fetchCaseConfiguration = async () => { + try { + setLoading(true); + const res = await getCaseConfigure({ signal: abortCtrl.signal }); + if (!didCancel) { + setLoading(false); + if (res != null) { + setConnectorId(res.connectorId); + setClosureType(res.closureType); + setVersion(res.version); + } + } + } catch (error) { + if (!didCancel) { + setLoading(false); + errorToToaster({ + title: i18n.ERROR_TITLE, + error: error.body && error.body.message ? new Error(error.body.message) : error, + dispatchToaster, + }); + } + } + }; + + fetchCaseConfiguration(); + + return () => { + didCancel = true; + abortCtrl.abort(); + }; + }, []); + + const persistCaseConfigure = useCallback( + async ({ connectorId, closureType }: PersistCaseConfigure) => { + let didCancel = false; + const abortCtrl = new AbortController(); + const saveCaseConfiguration = async () => { + try { + setPersistLoading(true); + const res = + version.length === 0 + ? await postCaseConfigure( + { connector_id: connectorId, closure_type: closureType }, + abortCtrl.signal + ) + : await patchCaseConfigure( + { connector_id: connectorId, closure_type: closureType, version }, + abortCtrl.signal + ); + if (!didCancel) { + setPersistLoading(false); + setConnectorId(res.connectorId); + setClosureType(res.closureType); + setVersion(res.version); + } + } catch (error) { + if (!didCancel) { + setPersistLoading(false); + errorToToaster({ + title: i18n.ERROR_TITLE, + error: error.body && error.body.message ? new Error(error.body.message) : error, + dispatchToaster, + }); + } + } + }; + saveCaseConfiguration(); + return () => { + didCancel = true; + abortCtrl.abort(); + }; + }, + [version] + ); + + useEffect(() => { + refetchCaseConfigure(); + }, []); + + return { + loading, + refetchCaseConfigure, + persistCaseConfigure, + persistLoading, + }; +}; diff --git a/x-pack/legacy/plugins/siem/public/containers/case/configure/use_connectors.tsx b/x-pack/legacy/plugins/siem/public/containers/case/configure/use_connectors.tsx new file mode 100644 index 0000000000000..f905ebe756d7d --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/containers/case/configure/use_connectors.tsx @@ -0,0 +1,115 @@ +/* + * 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. + */ + +import { useState, useEffect, useCallback } from 'react'; + +import { useStateToaster, errorToToaster } from '../../../components/toasters'; +import * as i18n from '../translations'; +import { fetchConnectors, patchConfigConnector } from './api'; +import { CasesConfigurationMapping, Connector } from './types'; + +export interface ReturnConnectors { + loading: boolean; + connectors: Connector[]; + refetchConnectors: () => void; + updateConnector: (connectorId: string, mappings: CasesConfigurationMapping[]) => unknown; +} + +export const useConnectors = (): ReturnConnectors => { + const [, dispatchToaster] = useStateToaster(); + const [loading, setLoading] = useState(true); + const [connectors, setConnectors] = useState([]); + + const refetchConnectors = useCallback(() => { + let didCancel = false; + const abortCtrl = new AbortController(); + const getConnectors = async () => { + try { + setLoading(true); + const res = await fetchConnectors({ signal: abortCtrl.signal }); + if (!didCancel) { + setLoading(false); + setConnectors(res.data); + } + } catch (error) { + if (!didCancel) { + setLoading(false); + setConnectors([]); + errorToToaster({ + title: i18n.ERROR_TITLE, + error: error.body && error.body.message ? new Error(error.body.message) : error, + dispatchToaster, + }); + } + } + }; + getConnectors(); + return () => { + didCancel = true; + abortCtrl.abort(); + }; + }, []); + + const updateConnector = useCallback( + (connectorId: string, mappings: CasesConfigurationMapping[]) => { + if (connectorId === 'none') { + return; + } + + let didCancel = false; + const abortCtrl = new AbortController(); + const update = async () => { + try { + setLoading(true); + await patchConfigConnector({ + connectorId, + config: { + cases_configuration: { + mapping: mappings.map(m => ({ + source: m.source, + target: m.target, + action_type: m.actionType, + })), + }, + }, + signal: abortCtrl.signal, + }); + if (!didCancel) { + setLoading(false); + refetchConnectors(); + } + } catch (error) { + if (!didCancel) { + setLoading(false); + refetchConnectors(); + errorToToaster({ + title: i18n.ERROR_TITLE, + error: error.body && error.body.message ? new Error(error.body.message) : error, + dispatchToaster, + }); + } + } + }; + update(); + return () => { + didCancel = true; + abortCtrl.abort(); + }; + }, + [] + ); + + useEffect(() => { + refetchConnectors(); + }, []); + + return { + loading, + connectors, + refetchConnectors, + updateConnector, + }; +}; diff --git a/x-pack/legacy/plugins/siem/public/containers/case/constants.ts b/x-pack/legacy/plugins/siem/public/containers/case/constants.ts index a0e57faa7661f..ab8dc98db4f64 100644 --- a/x-pack/legacy/plugins/siem/public/containers/case/constants.ts +++ b/x-pack/legacy/plugins/siem/public/containers/case/constants.ts @@ -5,5 +5,6 @@ */ export const CASES_URL = `/api/cases`; +export const CASES_CONFIGURE_URL = `/api/cases/configure`; export const DEFAULT_TABLE_ACTIVE_PAGE = 1; export const DEFAULT_TABLE_LIMIT = 5; diff --git a/x-pack/legacy/plugins/siem/public/containers/case/types.ts b/x-pack/legacy/plugins/siem/public/containers/case/types.ts index 74e9515a154de..65d94865bf00c 100644 --- a/x-pack/legacy/plugins/siem/public/containers/case/types.ts +++ b/x-pack/legacy/plugins/siem/public/containers/case/types.ts @@ -11,7 +11,8 @@ export interface Comment { createdAt: string; createdBy: ElasticUser; comment: string; - updatedAt: string; + updatedAt: string | null; + updatedBy: ElasticUser | null; version: string; } @@ -25,7 +26,8 @@ export interface Case { status: string; tags: string[]; title: string; - updatedAt: string; + updatedAt: string | null; + updatedBy: ElasticUser | null; version: string; } @@ -69,3 +71,7 @@ export interface FetchCasesProps { queryParams?: QueryParams; filterOptions?: FilterOptions; } + +export interface ApiProps { + signal: AbortSignal; +} diff --git a/x-pack/legacy/plugins/siem/public/containers/case/use_get_case.tsx b/x-pack/legacy/plugins/siem/public/containers/case/use_get_case.tsx index 3436aa8908117..a179b6f546b9b 100644 --- a/x-pack/legacy/plugins/siem/public/containers/case/use_get_case.tsx +++ b/x-pack/legacy/plugins/siem/public/containers/case/use_get_case.tsx @@ -59,7 +59,8 @@ const initialData: Case = { status: '', tags: [], title: '', - updatedAt: '', + updatedAt: null, + updatedBy: null, version: '', }; diff --git a/x-pack/legacy/plugins/siem/public/containers/case/utils.ts b/x-pack/legacy/plugins/siem/public/containers/case/utils.ts index ea297f6930fe3..8f24d5a435240 100644 --- a/x-pack/legacy/plugins/siem/public/containers/case/utils.ts +++ b/x-pack/legacy/plugins/siem/public/containers/case/utils.ts @@ -21,6 +21,8 @@ import { throwErrors, CommentResponse, CommentResponseRt, + CasesConfigureResponse, + CaseConfigureResponseRt, } from '../../../../../../plugins/case/common/api'; import { ToasterError } from '../../components/toasters'; import { AllCases, Case } from './types'; @@ -78,3 +80,9 @@ export const decodeCasesFindResponse = (respCases?: CasesFindResponse) => export const decodeCommentResponse = (respComment?: CommentResponse) => pipe(CommentResponseRt.decode(respComment), fold(throwErrors(createToasterPlainError), identity)); + +export const decodeCaseConfigureResponse = (respCase?: CasesConfigureResponse) => + pipe( + CaseConfigureResponseRt.decode(respCase), + fold(throwErrors(createToasterPlainError), identity) + ); diff --git a/x-pack/legacy/plugins/siem/public/legacy.ts b/x-pack/legacy/plugins/siem/public/legacy.ts index 49a03c93120d4..157ec54353a3e 100644 --- a/x-pack/legacy/plugins/siem/public/legacy.ts +++ b/x-pack/legacy/plugins/siem/public/legacy.ts @@ -5,11 +5,19 @@ */ import { npSetup, npStart } from 'ui/new_platform'; +import { PluginsSetup, PluginsStart } from 'ui/new_platform/new_platform'; import { PluginInitializerContext } from '../../../../../src/core/public'; import { plugin } from './'; +import { + TriggersAndActionsUIPublicPluginSetup, + TriggersAndActionsUIPublicPluginStart, +} from '../../../../plugins/triggers_actions_ui/public'; const pluginInstance = plugin({} as PluginInitializerContext); -pluginInstance.setup(npSetup.core, npSetup.plugins); -pluginInstance.start(npStart.core, npStart.plugins); +type myPluginsSetup = PluginsSetup & { triggers_actions_ui: TriggersAndActionsUIPublicPluginSetup }; +type myPluginsStart = PluginsStart & { triggers_actions_ui: TriggersAndActionsUIPublicPluginStart }; + +pluginInstance.setup(npSetup.core, npSetup.plugins as myPluginsSetup); +pluginInstance.start(npStart.core, npStart.plugins as myPluginsStart); diff --git a/x-pack/legacy/plugins/siem/public/lib/connectors/config.ts b/x-pack/legacy/plugins/siem/public/lib/connectors/config.ts new file mode 100644 index 0000000000000..baeb69b3f6943 --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/lib/connectors/config.ts @@ -0,0 +1,36 @@ +/* + * 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. + */ + +import { CasesConfigurationMapping } from '../../containers/case/configure/types'; +import serviceNowLogo from './logos/servicenow.svg'; +import { Connector } from './types'; + +const connectors: Record = { + '.servicenow': { + actionTypeId: '.servicenow', + logo: serviceNowLogo, + }, +}; + +const defaultMapping: CasesConfigurationMapping[] = [ + { + source: 'title', + target: 'short_description', + actionType: 'overwrite', + }, + { + source: 'description', + target: 'description', + actionType: 'overwrite', + }, + { + source: 'comments', + target: 'comments', + actionType: 'append', + }, +]; + +export { connectors, defaultMapping }; diff --git a/x-pack/legacy/plugins/siem/public/lib/connectors/index.ts b/x-pack/legacy/plugins/siem/public/lib/connectors/index.ts new file mode 100644 index 0000000000000..fdf337b5ef120 --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/lib/connectors/index.ts @@ -0,0 +1,7 @@ +/* + * 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 { getActionType as serviceNowActionType } from './servicenow'; diff --git a/x-pack/legacy/plugins/siem/public/lib/connectors/logos/servicenow.svg b/x-pack/legacy/plugins/siem/public/lib/connectors/logos/servicenow.svg new file mode 100755 index 0000000000000..dcd022a8dca18 --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/lib/connectors/logos/servicenow.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/x-pack/legacy/plugins/siem/public/lib/connectors/servicenow.tsx b/x-pack/legacy/plugins/siem/public/lib/connectors/servicenow.tsx new file mode 100644 index 0000000000000..877757df30fb3 --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/lib/connectors/servicenow.tsx @@ -0,0 +1,225 @@ +/* + * 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. + */ +import React, { useCallback, ChangeEvent } from 'react'; +import { + EuiFieldText, + EuiFlexGroup, + EuiFlexItem, + EuiFormRow, + EuiFieldPassword, + EuiSpacer, +} from '@elastic/eui'; + +import { isEmpty, get } from 'lodash/fp'; + +import { + ActionConnectorFieldsProps, + ActionTypeModel, + ValidationResult, + ActionParamsProps, + // eslint-disable-next-line @kbn/eslint/no-restricted-paths +} from '../../../../../../plugins/triggers_actions_ui/public/types'; + +import { FieldMapping } from '../../pages/case/components/configure_cases/field_mapping'; + +import * as i18n from './translations'; + +import { ServiceNowActionConnector } from './types'; +import { isUrlInvalid } from './validators'; + +import { connectors, defaultMapping } from './config'; +import { CasesConfigurationMapping } from '../../containers/case/configure/types'; + +const serviceNowDefinition = connectors['.servicenow']; + +interface ServiceNowActionParams { + message: string; +} + +interface Errors { + apiUrl: string[]; + username: string[]; + password: string[]; +} + +export function getActionType(): ActionTypeModel { + return { + id: serviceNowDefinition.actionTypeId, + iconClass: serviceNowDefinition.logo, + selectMessage: i18n.SERVICENOW_DESC, + actionTypeTitle: i18n.SERVICENOW_TITLE, + validateConnector: (action: ServiceNowActionConnector): ValidationResult => { + const errors: Errors = { + apiUrl: [], + username: [], + password: [], + }; + + if (!action.config.apiUrl) { + errors.apiUrl = [...errors.apiUrl, i18n.SERVICENOW_API_URL_REQUIRED]; + } + + if (isUrlInvalid(action.config.apiUrl)) { + errors.apiUrl = [...errors.apiUrl, i18n.SERVICENOW_API_URL_INVALID]; + } + + if (!action.secrets.username) { + errors.username = [...errors.username, i18n.SERVICENOW_USERNAME_REQUIRED]; + } + + if (!action.secrets.password) { + errors.password = [...errors.password, i18n.SERVICENOW_PASSWORD_REQUIRED]; + } + + return { errors }; + }, + validateParams: (actionParams: ServiceNowActionParams): ValidationResult => { + return { errors: {} }; + }, + actionConnectorFields: ServiceNowConnectorFields, + actionParamsFields: ServiceNowParamsFields, + }; +} + +const ServiceNowConnectorFields: React.FunctionComponent> = ({ action, editActionConfig, editActionSecrets, errors }) => { + const { apiUrl, casesConfiguration: { mapping = [] } = {} } = action.config; + const { username, password } = action.secrets; + + const isApiUrlInvalid: boolean = errors.apiUrl.length > 0 && apiUrl != null; + const isUsernameInvalid: boolean = errors.username.length > 0 && username != null; + const isPasswordInvalid: boolean = errors.password.length > 0 && password != null; + + if (isEmpty(mapping)) { + editActionConfig('casesConfiguration', { + ...action.config.casesConfiguration, + mapping: defaultMapping, + }); + } + + const handleOnChangeActionConfig = useCallback( + (key: string, evt: ChangeEvent) => editActionConfig(key, evt.target.value), + [] + ); + + const handleOnBlurActionConfig = useCallback( + (key: string) => { + if (key === 'apiUrl' && action.config[key] == null) { + editActionConfig(key, ''); + } + }, + [action.config] + ); + + const handleOnChangeSecretConfig = useCallback( + (key: string, evt: ChangeEvent) => editActionSecrets(key, evt.target.value), + [] + ); + + const handleOnBlurSecretConfig = useCallback( + (key: string) => { + if (['username', 'password'].includes(key) && get(key, action.secrets) == null) { + editActionSecrets(key, ''); + } + }, + [action.secrets] + ); + + const handleOnChangeMappingConfig = useCallback( + (newMapping: CasesConfigurationMapping[]) => + editActionConfig('casesConfiguration', { + ...action.config.casesConfiguration, + mapping: newMapping, + }), + [action.config] + ); + + return ( + <> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ); +}; + +const ServiceNowParamsFields: React.FunctionComponent> = ({ actionParams, editAction, index, errors }) => { + return null; +}; diff --git a/x-pack/legacy/plugins/siem/public/lib/connectors/translations.ts b/x-pack/legacy/plugins/siem/public/lib/connectors/translations.ts new file mode 100644 index 0000000000000..ae2084120255c --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/lib/connectors/translations.ts @@ -0,0 +1,70 @@ +/* + * 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. + */ + +import { i18n } from '@kbn/i18n'; + +export const SERVICENOW_DESC = i18n.translate( + 'xpack.siem.case.connectors.servicenow.selectMessageText', + { + defaultMessage: 'Push or update SIEM case data to a new incident in ServiceNow', + } +); + +export const SERVICENOW_TITLE = i18n.translate( + 'xpack.siem.case.connectors.servicenow.actionTypeTitle', + { + defaultMessage: 'ServiceNow', + } +); + +export const SERVICENOW_API_URL_LABEL = i18n.translate( + 'xpack.siem.case.connectors.servicenow.apiUrlTextFieldLabel', + { + defaultMessage: 'URL', + } +); + +export const SERVICENOW_API_URL_REQUIRED = i18n.translate( + 'xpack.siem.case.connectors.servicenow.requiredApiUrlTextField', + { + defaultMessage: 'URL is required', + } +); + +export const SERVICENOW_API_URL_INVALID = i18n.translate( + 'xpack.siem.case.connectors.servicenow.invalidApiUrlTextField', + { + defaultMessage: 'URL is invalid', + } +); + +export const SERVICENOW_USERNAME_LABEL = i18n.translate( + 'xpack.siem.case.connectors.servicenow.usernameTextFieldLabel', + { + defaultMessage: 'Username', + } +); + +export const SERVICENOW_USERNAME_REQUIRED = i18n.translate( + 'xpack.siem.case.connectors.servicenow.requiredUsernameTextField', + { + defaultMessage: 'Username is required', + } +); + +export const SERVICENOW_PASSWORD_LABEL = i18n.translate( + 'xpack.siem.case.connectors.servicenow.passwordTextFieldLabel', + { + defaultMessage: 'Password', + } +); + +export const SERVICENOW_PASSWORD_REQUIRED = i18n.translate( + 'xpack.siem.case.connectors.servicenow.requiredPasswordTextField', + { + defaultMessage: 'Password is required', + } +); diff --git a/x-pack/legacy/plugins/siem/public/lib/connectors/types.ts b/x-pack/legacy/plugins/siem/public/lib/connectors/types.ts new file mode 100644 index 0000000000000..66326a6590deb --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/lib/connectors/types.ts @@ -0,0 +1,23 @@ +/* + * 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. + */ + +/* eslint-disable no-restricted-imports */ +/* eslint-disable @kbn/eslint/no-restricted-paths */ + +import { + ConfigType, + SecretsType, +} from '../../../../../../plugins/actions/server/builtin_action_types/servicenow/types'; + +export interface ServiceNowActionConnector { + config: ConfigType; + secrets: SecretsType; +} + +export interface Connector { + actionTypeId: string; + logo: string; +} diff --git a/x-pack/legacy/plugins/siem/public/lib/connectors/validators.ts b/x-pack/legacy/plugins/siem/public/lib/connectors/validators.ts new file mode 100644 index 0000000000000..2989cf4d98f85 --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/lib/connectors/validators.ts @@ -0,0 +1,7 @@ +/* + * 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 { isUrlInvalid } from '../../utils/validators'; diff --git a/x-pack/legacy/plugins/siem/public/pages/case/components/all_cases/__mock__/index.tsx b/x-pack/legacy/plugins/siem/public/pages/case/components/all_cases/__mock__/index.tsx index 433e1cb17da02..0fe8daafcb30a 100644 --- a/x-pack/legacy/plugins/siem/public/pages/case/components/all_cases/__mock__/index.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/case/components/all_cases/__mock__/index.tsx @@ -22,7 +22,8 @@ export const useGetCasesMockState: UseGetCasesState = { status: 'open', tags: ['defacement'], title: 'Another horrible breach', - updatedAt: '2020-02-13T19:44:23.627Z', + updatedAt: null, + updatedBy: null, version: 'WzQ3LDFd', }, { @@ -35,7 +36,8 @@ export const useGetCasesMockState: UseGetCasesState = { status: 'open', tags: ['phishing'], title: 'Bad email', - updatedAt: '2020-02-13T19:44:13.328Z', + updatedAt: null, + updatedBy: null, version: 'WzQ3LDFd', }, { @@ -48,7 +50,8 @@ export const useGetCasesMockState: UseGetCasesState = { status: 'open', tags: ['phishing'], title: 'Bad email', - updatedAt: '2020-02-13T19:44:11.328Z', + updatedAt: null, + updatedBy: null, version: 'WzQ3LDFd', }, { @@ -61,7 +64,8 @@ export const useGetCasesMockState: UseGetCasesState = { status: 'closed', tags: ['phishing'], title: 'Uh oh', - updatedAt: '2020-02-18T21:32:24.056Z', + updatedAt: null, + updatedBy: null, version: 'WzQ3LDFd', }, { @@ -74,7 +78,8 @@ export const useGetCasesMockState: UseGetCasesState = { status: 'open', tags: ['phishing'], title: 'Uh oh', - updatedAt: '2020-02-13T19:44:01.901Z', + updatedAt: null, + updatedBy: null, version: 'WzQ3LDFd', }, ], diff --git a/x-pack/legacy/plugins/siem/public/pages/case/components/case_view/__mock__/index.tsx b/x-pack/legacy/plugins/siem/public/pages/case/components/case_view/__mock__/index.tsx index 6e5ef46af41c4..53cc1f80b5c10 100644 --- a/x-pack/legacy/plugins/siem/public/pages/case/components/case_view/__mock__/index.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/case/components/case_view/__mock__/index.tsx @@ -22,6 +22,9 @@ export const caseProps: CaseProps = { username: 'smilovic', }, updatedAt: '2020-02-20T23:06:33.798Z', + updatedBy: { + username: 'elastic', + }, version: 'WzQ3LDFd', }, ], @@ -32,6 +35,9 @@ export const caseProps: CaseProps = { tags: ['defacement'], title: 'Another horrible breach!!', updatedAt: '2020-02-19T15:02:57.995Z', + updatedBy: { + username: 'elastic', + }, version: 'WzQ3LDFd', }, }; @@ -49,6 +55,9 @@ export const data: Case = { username: 'smilovic', }, updatedAt: '2020-02-20T23:06:33.798Z', + updatedBy: { + username: 'elastic', + }, version: 'WzQ3LDFd', }, ], @@ -59,5 +68,8 @@ export const data: Case = { tags: ['defacement'], title: 'Another horrible breach!!', updatedAt: '2020-02-19T15:02:57.995Z', + updatedBy: { + username: 'elastic', + }, version: 'WzQ3LDFd', }; diff --git a/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/closure_options.tsx b/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/closure_options.tsx index 3a2ef3bc21721..9879b9149059a 100644 --- a/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/closure_options.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/closure_options.tsx @@ -7,10 +7,21 @@ import React from 'react'; import { EuiDescribedFormGroup, EuiFormRow } from '@elastic/eui'; -import * as i18n from './translations'; +import { ClosureType } from '../../../../containers/case/configure/types'; import { ClosureOptionsRadio } from './closure_options_radio'; +import * as i18n from './translations'; + +interface ClosureOptionsProps { + closureTypeSelected: ClosureType; + disabled: boolean; + onChangeClosureType: (newClosureType: ClosureType) => void; +} -const ClosureOptionsComponent: React.FC = () => { +const ClosureOptionsComponent: React.FC = ({ + closureTypeSelected, + disabled, + onChangeClosureType, +}) => { return ( { description={i18n.CASE_CLOSURE_OPTIONS_DESC} > - + ); diff --git a/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/closure_options_radio.tsx b/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/closure_options_radio.tsx index 5d1476acee5b1..f32f867b2471d 100644 --- a/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/closure_options_radio.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/closure_options_radio.tsx @@ -4,37 +4,52 @@ * you may not use this file except in compliance with the Elastic License. */ -import React, { useState } from 'react'; +import React, { ReactNode, useCallback } from 'react'; import { EuiRadioGroup } from '@elastic/eui'; +import { ClosureType } from '../../../../containers/case/configure/types'; import * as i18n from './translations'; -const ID_PREFIX = 'closure_options'; -const DEFAULT_RADIO = `${ID_PREFIX}_manual`; +interface ClosureRadios { + id: ClosureType; + label: ReactNode; +} -const radios = [ +const radios: ClosureRadios[] = [ { - id: DEFAULT_RADIO, + id: 'close-by-user', label: i18n.CASE_CLOSURE_OPTIONS_MANUAL, }, { - id: `${ID_PREFIX}_new_incident`, + id: 'close-by-pushing', label: i18n.CASE_CLOSURE_OPTIONS_NEW_INCIDENT, }, - { - id: `${ID_PREFIX}_closed_incident`, - label: i18n.CASE_CLOSURE_OPTIONS_CLOSED_INCIDENT, - }, ]; -const ClosureOptionsRadioComponent: React.FC = () => { - const [selectedClosure, setSelectedClosure] = useState(DEFAULT_RADIO); +interface ClosureOptionsRadioComponentProps { + closureTypeSelected: ClosureType; + disabled: boolean; + onChangeClosureType: (newClosureType: ClosureType) => void; +} + +const ClosureOptionsRadioComponent: React.FC = ({ + closureTypeSelected, + disabled, + onChangeClosureType, +}) => { + const onChangeLocal = useCallback( + (id: string) => { + onChangeClosureType(id as ClosureType); + }, + [onChangeClosureType] + ); return ( ); diff --git a/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/connectors.tsx b/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/connectors.tsx index 561464e44c703..55b256b66b72b 100644 --- a/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/connectors.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/connectors.tsx @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import React from 'react'; +import React, { useState, useCallback } from 'react'; import { EuiDescribedFormGroup, EuiFormRow, @@ -18,6 +18,13 @@ import styled from 'styled-components'; import { ConnectorsDropdown } from './connectors_dropdown'; import * as i18n from './translations'; +import { + ActionsConnectorsContextProvider, + ConnectorAddFlyout, +} from '../../../../../../../../plugins/triggers_actions_ui/public'; +import { Connector } from '../../../../containers/case/configure/types'; +import { useKibana } from '../../../../lib/kibana'; + const EuiFormRowExtended = styled(EuiFormRow)` .euiFormRow__labelWrapper { .euiFormRow__label { @@ -26,26 +33,79 @@ const EuiFormRowExtended = styled(EuiFormRow)` } `; -const ConnectorsComponent: React.FC = () => { +interface Props { + connectors: Connector[]; + disabled: boolean; + isLoading: boolean; + onChangeConnector: (id: string) => void; + refetchConnectors: () => void; + selectedConnector: string; +} +const actionTypes = [ + { + id: '.servicenow', + name: 'ServiceNow', + enabled: true, + }, +]; + +const ConnectorsComponent: React.FC = ({ + connectors, + disabled, + isLoading, + onChangeConnector, + refetchConnectors, + selectedConnector, +}) => { + const { http, triggers_actions_ui, notifications, application } = useKibana().services; + const [addFlyoutVisible, setAddFlyoutVisibility] = useState(false); + + const handleShowFlyout = useCallback(() => setAddFlyoutVisibility(true), []); + const dropDownLabel = ( {i18n.INCIDENT_MANAGEMENT_SYSTEM_LABEL} - {i18n.ADD_NEW_CONNECTOR} + {i18n.ADD_NEW_CONNECTOR} ); + const reloadConnectors = useCallback(async () => refetchConnectors(), []); + return ( - {i18n.INCIDENT_MANAGEMENT_SYSTEM_TITLE}} - description={i18n.INCIDENT_MANAGEMENT_SYSTEM_DESC} - > - - - - + <> + {i18n.INCIDENT_MANAGEMENT_SYSTEM_TITLE}} + description={i18n.INCIDENT_MANAGEMENT_SYSTEM_DESC} + > + + + + + + + + ); }; diff --git a/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/connectors_dropdown.tsx b/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/connectors_dropdown.tsx index d43935deda395..a0a0ad6cd3e7f 100644 --- a/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/connectors_dropdown.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/connectors_dropdown.tsx @@ -4,50 +4,78 @@ * you may not use this file except in compliance with the Elastic License. */ -import React, { useState } from 'react'; -import { EuiSuperSelect, EuiIcon, EuiSuperSelectOption } from '@elastic/eui'; +import React, { useMemo } from 'react'; +import { EuiIcon, EuiSuperSelect } from '@elastic/eui'; import styled from 'styled-components'; +import { Connector } from '../../../../containers/case/configure/types'; +import { connectors as connectorsDefinition } from '../../../../lib/connectors/config'; import * as i18n from './translations'; +interface Props { + connectors: Connector[]; + disabled: boolean; + isLoading: boolean; + onChange: (id: string) => void; + selectedConnector: string; +} + const ICON_SIZE = 'm'; const EuiIconExtended = styled(EuiIcon)` margin-right: 13px; `; -const connectors: Array> = [ - { - value: 'no-connector', - inputDisplay: ( - <> - - {i18n.NO_CONNECTOR} - - ), - 'data-test-subj': 'no-connector', - }, - { - value: 'servicenow-connector', - inputDisplay: ( - <> - - {'My ServiceNow connector'} - - ), - 'data-test-subj': 'servicenow-connector', - }, -]; - -const ConnectorsDropdownComponent: React.FC = () => { - const [selectedConnector, setSelectedConnector] = useState(connectors[0].value); +const noConnectorOption = { + value: 'none', + inputDisplay: ( + <> + + {i18n.NO_CONNECTOR} + + ), + 'data-test-subj': 'no-connector', +}; + +const ConnectorsDropdownComponent: React.FC = ({ + connectors, + disabled, + isLoading, + onChange, + selectedConnector, +}) => { + const connectorsAsOptions = useMemo( + () => + connectors.reduce( + (acc, connector) => [ + ...acc, + { + value: connector.id, + inputDisplay: ( + <> + + {connector.name} + + ), + 'data-test-subj': connector.id, + }, + ], + [noConnectorOption] + ), + [connectors] + ); return ( ); }; diff --git a/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/field_mapping.tsx b/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/field_mapping.tsx index 814f1bfd75ae4..0c0dc14f1c218 100644 --- a/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/field_mapping.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/field_mapping.tsx @@ -4,63 +4,118 @@ * you may not use this file except in compliance with the Elastic License. */ -import React from 'react'; -import { EuiDescribedFormGroup, EuiFormRow, EuiFlexItem, EuiFlexGroup } from '@elastic/eui'; +import React, { useCallback } from 'react'; +import { EuiFormRow, EuiFlexItem, EuiFlexGroup, EuiSuperSelectOption } from '@elastic/eui'; import styled from 'styled-components'; -import * as i18n from './translations'; +import { + CasesConfigurationMapping, + ThirdPartyField, + CaseField, + ActionType, +} from '../../../../containers/case/configure/types'; import { FieldMappingRow } from './field_mapping_row'; +import * as i18n from './translations'; + +import { defaultMapping } from '../../../../lib/connectors/config'; const FieldRowWrapper = styled.div` margin-top: 8px; font-size: 14px; `; -const supportedThirdPartyFields = [ +const supportedThirdPartyFields: Array> = [ { - value: 'short_description', - inputDisplay: {'Short Description'}, + value: 'not_mapped', + inputDisplay: {i18n.FIELD_MAPPING_FIELD_NOT_MAPPED}, }, { - value: 'comment', - inputDisplay: {'Comment'}, + value: 'short_description', + inputDisplay: {i18n.FIELD_MAPPING_FIELD_SHORT_DESC}, }, { - value: 'tags', - inputDisplay: {'Tags'}, + value: 'comments', + inputDisplay: {i18n.FIELD_MAPPING_FIELD_COMMENTS}, }, { value: 'description', - inputDisplay: {'Description'}, + inputDisplay: {i18n.FIELD_MAPPING_FIELD_DESC}, }, ]; -const FieldMappingComponent: React.FC = () => ( - {i18n.FIELD_MAPPING_TITLE}} - description={i18n.FIELD_MAPPING_DESC} - > - - - - {i18n.FIELD_MAPPING_FIRST_COL} - - - {i18n.FIELD_MAPPING_SECOND_COL} - - - {i18n.FIELD_MAPPING_THIRD_COL} - - - - - - - - - - -); +interface FieldMappingProps { + disabled: boolean; + mapping: CasesConfigurationMapping[] | null; + onChangeMapping: (newMapping: CasesConfigurationMapping[]) => void; +} + +const FieldMappingComponent: React.FC = ({ + disabled, + mapping, + onChangeMapping, +}) => { + const onChangeActionType = useCallback( + (caseField: CaseField, newActionType: ActionType) => { + const myMapping = mapping ?? defaultMapping; + const findItemIndex = myMapping.findIndex(item => item.source === caseField); + if (findItemIndex >= 0) { + onChangeMapping([ + ...myMapping.slice(0, findItemIndex), + { ...myMapping[findItemIndex], actionType: newActionType }, + ...myMapping.slice(findItemIndex + 1), + ]); + } + }, + [mapping] + ); + + const onChangeThirdParty = useCallback( + (caseField: CaseField, newThirdPartyField: ThirdPartyField) => { + const myMapping = mapping ?? defaultMapping; + onChangeMapping( + myMapping.map(item => { + if (item.source !== caseField && item.target === newThirdPartyField) { + return { ...item, target: 'not_mapped' }; + } else if (item.source === caseField) { + return { ...item, target: newThirdPartyField }; + } + return item; + }) + ); + }, + [mapping] + ); + return ( + <> + + + + {i18n.FIELD_MAPPING_FIRST_COL} + + + {i18n.FIELD_MAPPING_SECOND_COL} + + + {i18n.FIELD_MAPPING_THIRD_COL} + + + + + {(mapping ?? defaultMapping).map(item => ( + + ))} + + + ); +}; export const FieldMapping = React.memo(FieldMappingComponent); diff --git a/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/field_mapping_row.tsx b/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/field_mapping_row.tsx index 0e446ad9bbe89..62e43c86af8d9 100644 --- a/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/field_mapping_row.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/field_mapping_row.tsx @@ -4,48 +4,67 @@ * you may not use this file except in compliance with the Elastic License. */ -import React, { useState } from 'react'; -import { EuiFlexItem, EuiFlexGroup, EuiSuperSelect, EuiIcon } from '@elastic/eui'; +import React, { useMemo } from 'react'; +import { + EuiFlexItem, + EuiFlexGroup, + EuiSuperSelect, + EuiIcon, + EuiSuperSelectOption, +} from '@elastic/eui'; +import { capitalize } from 'lodash/fp'; import * as i18n from './translations'; +import { + CaseField, + ActionType, + ThirdPartyField, +} from '../../../../containers/case/configure/types'; -interface ThirdPartyField { - value: string; - inputDisplay: JSX.Element; -} interface RowProps { - siemField: string; - thirdPartyOptions: ThirdPartyField[]; + disabled: boolean; + siemField: CaseField; + thirdPartyOptions: Array>; + onChangeActionType: (caseField: CaseField, newActionType: ActionType) => void; + onChangeThirdParty: (caseField: CaseField, newThirdPartyField: ThirdPartyField) => void; + selectedActionType: ActionType; + selectedThirdParty: ThirdPartyField; } -const editUpdateOptions = [ +const actionTypeOptions: Array> = [ { value: 'nothing', - inputDisplay: {i18n.FIELD_MAPPING_EDIT_NOTHING}, + inputDisplay: <>{i18n.FIELD_MAPPING_EDIT_NOTHING}, 'data-test-subj': 'edit-update-option-nothing', }, { value: 'overwrite', - inputDisplay: {i18n.FIELD_MAPPING_EDIT_OVERWRITE}, + inputDisplay: <>{i18n.FIELD_MAPPING_EDIT_OVERWRITE}, 'data-test-subj': 'edit-update-option-overwrite', }, { value: 'append', - inputDisplay: {i18n.FIELD_MAPPING_EDIT_APPEND}, + inputDisplay: <>{i18n.FIELD_MAPPING_EDIT_APPEND}, 'data-test-subj': 'edit-update-option-append', }, ]; -const FieldMappingRowComponent: React.FC = ({ siemField, thirdPartyOptions }) => { - const [selectedEditUpdate, setSelectedEditUpdate] = useState(editUpdateOptions[0].value); - const [selectedThirdParty, setSelectedThirdParty] = useState(thirdPartyOptions[0].value); - +const FieldMappingRowComponent: React.FC = ({ + disabled, + siemField, + thirdPartyOptions, + onChangeActionType, + onChangeThirdParty, + selectedActionType, + selectedThirdParty, +}) => { + const siemFieldCapitalized = useMemo(() => capitalize(siemField), [siemField]); return ( - {siemField} + {siemFieldCapitalized} @@ -54,16 +73,18 @@ const FieldMappingRowComponent: React.FC = ({ siemField, thirdPartyOpt diff --git a/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/index.tsx b/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/index.tsx new file mode 100644 index 0000000000000..da715fb66953f --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/index.tsx @@ -0,0 +1,201 @@ +/* + * 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. + */ + +import React, { useReducer, useCallback, useEffect, useState } from 'react'; +import styled, { css } from 'styled-components'; + +import { EuiFlexGroup, EuiFlexItem, EuiButton, EuiSpacer, EuiCallOut } from '@elastic/eui'; +import { noop, isEmpty } from 'lodash/fp'; +import { useConnectors } from '../../../../containers/case/configure/use_connectors'; +import { useCaseConfigure } from '../../../../containers/case/configure/use_configure'; +import { + ClosureType, + CasesConfigurationMapping, + CCMapsCombinedActionAttributes, +} from '../../../../containers/case/configure/types'; +import { Connectors } from '../configure_cases/connectors'; +import { ClosureOptions } from '../configure_cases/closure_options'; +import { Mapping } from '../configure_cases/mapping'; +import { SectionWrapper } from '../wrappers'; +import { configureCasesReducer, State } from './reducer'; +import * as i18n from './translations'; + +const FormWrapper = styled.div` + ${({ theme }) => css` + & > * { + margin-top 40px; + } + + padding-top: ${theme.eui.paddingSizes.l}; + padding-bottom: ${theme.eui.paddingSizes.l}; + `} +`; + +const initialState: State = { + connectorId: 'none', + closureType: 'close-by-user', + mapping: null, +}; + +const ConfigureCasesComponent: React.FC = () => { + const [connectorIsValid, setConnectorIsValid] = useState(true); + + const [{ connectorId, closureType, mapping }, dispatch] = useReducer( + configureCasesReducer(), + initialState + ); + + const setConnectorId = useCallback((newConnectorId: string) => { + dispatch({ + type: 'setConnectorId', + connectorId: newConnectorId, + }); + }, []); + + const setClosureType = useCallback((newClosureType: ClosureType) => { + dispatch({ + type: 'setClosureType', + closureType: newClosureType, + }); + }, []); + + const setMapping = useCallback((newMapping: CasesConfigurationMapping[]) => { + dispatch({ + type: 'setMapping', + mapping: newMapping, + }); + }, []); + + const { loading: loadingCaseConfigure, persistLoading, persistCaseConfigure } = useCaseConfigure({ + setConnectorId, + setClosureType, + }); + const { + loading: isLoadingConnectors, + connectors, + refetchConnectors, + updateConnector, + } = useConnectors(); + + const isLoadingAny = isLoadingConnectors || persistLoading || loadingCaseConfigure; + + const handleSubmit = useCallback( + // TO DO give a warning/error to user when field are not mapped so they have chance to do it + () => { + persistCaseConfigure({ connectorId, closureType }); + updateConnector(connectorId, mapping ?? []); + }, + [connectorId, closureType, mapping] + ); + + useEffect(() => { + if ( + !isEmpty(connectors) && + connectorId !== 'none' && + connectors.some(c => c.id === connectorId) + ) { + const myConnector = connectors.find(c => c.id === connectorId); + const myMapping = myConnector?.config?.casesConfiguration?.mapping ?? []; + setMapping( + myMapping.map((m: CCMapsCombinedActionAttributes) => ({ + source: m.source, + target: m.target, + actionType: m.action_type ?? m.actionType, + })) + ); + } + }, [connectors, connectorId]); + + useEffect(() => { + if ( + !isLoadingConnectors && + connectorId !== 'none' && + !connectors.some(c => c.id === connectorId) + ) { + setConnectorIsValid(false); + } else if ( + !isLoadingConnectors && + (connectorId === 'none' || connectors.some(c => c.id === connectorId)) + ) { + setConnectorIsValid(true); + } + }, [connectors, connectorId]); + + return ( + + {!connectorIsValid && ( + + + {i18n.WARNING_NO_CONNECTOR_MESSAGE} + + + )} + + + + + + + + + + + + + + + {i18n.CANCEL} + + + + + {i18n.SAVE_CHANGES} + + + + + + ); +}; + +export const ConfigureCases = React.memo(ConfigureCasesComponent); diff --git a/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/mapping.tsx b/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/mapping.tsx new file mode 100644 index 0000000000000..10c8f6b938023 --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/mapping.tsx @@ -0,0 +1,31 @@ +/* + * 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. + */ + +import React from 'react'; +import { EuiDescribedFormGroup } from '@elastic/eui'; + +import * as i18n from './translations'; + +import { FieldMapping } from './field_mapping'; +import { CasesConfigurationMapping } from '../../../../containers/case/configure/types'; + +interface MappingProps { + disabled: boolean; + mapping: CasesConfigurationMapping[] | null; + onChangeMapping: (newMapping: CasesConfigurationMapping[]) => void; +} + +const MappingComponent: React.FC = ({ disabled, mapping, onChangeMapping }) => ( + {i18n.FIELD_MAPPING_TITLE}} + description={i18n.FIELD_MAPPING_DESC} + > + + +); + +export const Mapping = React.memo(MappingComponent); diff --git a/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/reducer.ts b/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/reducer.ts new file mode 100644 index 0000000000000..f9e4a73b3c396 --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/reducer.ts @@ -0,0 +1,55 @@ +/* + * 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. + */ + +import { + ClosureType, + CasesConfigurationMapping, +} from '../../../../containers/case/configure/types'; + +export interface State { + mapping: CasesConfigurationMapping[] | null; + connectorId: string; + closureType: ClosureType; +} + +export type Action = + | { + type: 'setConnectorId'; + connectorId: string; + } + | { + type: 'setClosureType'; + closureType: ClosureType; + } + | { + type: 'setMapping'; + mapping: CasesConfigurationMapping[]; + }; + +export const configureCasesReducer = () => (state: State, action: Action) => { + switch (action.type) { + case 'setConnectorId': { + return { + ...state, + connectorId: action.connectorId, + }; + } + case 'setClosureType': { + return { + ...state, + closureType: action.closureType, + }; + } + case 'setMapping': { + return { + ...state, + mapping: action.mapping, + }; + } + default: + return state; + } +}; diff --git a/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/translations.ts b/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/translations.ts index ca2d878c58ee3..d24921a636082 100644 --- a/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/translations.ts +++ b/x-pack/legacy/plugins/siem/public/pages/case/components/configure_cases/translations.ts @@ -135,3 +135,54 @@ export const FIELD_MAPPING_EDIT_APPEND = i18n.translate( defaultMessage: 'Append', } ); + +export const CANCEL = i18n.translate('xpack.siem.case.configureCases.cancelButton', { + defaultMessage: 'Cancel', +}); + +export const SAVE_CHANGES = i18n.translate('xpack.siem.case.configureCases.saveChangesButton', { + defaultMessage: 'Save Changes', +}); + +export const WARNING_NO_CONNECTOR_TITLE = i18n.translate( + 'xpack.siem.case.configureCases.warningTitle', + { + defaultMessage: 'Warning', + } +); + +export const WARNING_NO_CONNECTOR_MESSAGE = i18n.translate( + 'xpack.siem.case.configureCases.warningMessage', + { + defaultMessage: + 'Configuration seems to be invalid. The selected connector is missing. Did you delete the connector?', + } +); + +export const FIELD_MAPPING_FIELD_NOT_MAPPED = i18n.translate( + 'xpack.siem.case.configureCases.fieldMappingFieldNotMapped', + { + defaultMessage: 'Not mapped', + } +); + +export const FIELD_MAPPING_FIELD_SHORT_DESC = i18n.translate( + 'xpack.siem.case.configureCases.fieldMappingFieldShortDescription', + { + defaultMessage: 'Short Description', + } +); + +export const FIELD_MAPPING_FIELD_DESC = i18n.translate( + 'xpack.siem.case.configureCases.fieldMappingFieldDescription', + { + defaultMessage: 'Description', + } +); + +export const FIELD_MAPPING_FIELD_COMMENTS = i18n.translate( + 'xpack.siem.case.configureCases.fieldMappingFieldComments', + { + defaultMessage: 'Comments', + } +); diff --git a/x-pack/legacy/plugins/siem/public/pages/case/configure_cases.tsx b/x-pack/legacy/plugins/siem/public/pages/case/configure_cases.tsx index 556d7779c664f..b546a88744439 100644 --- a/x-pack/legacy/plugins/siem/public/pages/case/configure_cases.tsx +++ b/x-pack/legacy/plugins/siem/public/pages/case/configure_cases.tsx @@ -5,17 +5,14 @@ */ import React from 'react'; -import styled, { css } from 'styled-components'; import { WrapperPage } from '../../components/wrapper_page'; import { CaseHeaderPage } from './components/case_header_page'; import { SpyRoute } from '../../utils/route/spy_routes'; import { getCaseUrl } from '../../components/link_to'; import { WhitePageWrapper, SectionWrapper } from './components/wrappers'; -import { Connectors } from './components/configure_cases/connectors'; import * as i18n from './translations'; -import { ClosureOptions } from './components/configure_cases/closure_options'; -import { FieldMapping } from './components/configure_cases/field_mapping'; +import { ConfigureCases } from './components/configure_cases'; const backOptions = { href: getCaseUrl(), @@ -28,17 +25,6 @@ const wrapperPageStyle: Record = { paddingBottom: '0', }; -const FormWrapper = styled.div` - ${({ theme }) => css` - & > * { - margin-top 40px; - } - - padding-top: ${theme.eui.paddingSizes.l}; - padding-bottom: ${theme.eui.paddingSizes.l}; - `} -`; - const ConfigureCasesPageComponent: React.FC = () => ( <> @@ -46,17 +32,7 @@ const ConfigureCasesPageComponent: React.FC = () => ( - - - - - - - - - - - + diff --git a/x-pack/legacy/plugins/siem/public/plugin.tsx b/x-pack/legacy/plugins/siem/public/plugin.tsx index 8be5510cda83a..f22add59a95d4 100644 --- a/x-pack/legacy/plugins/siem/public/plugin.tsx +++ b/x-pack/legacy/plugins/siem/public/plugin.tsx @@ -21,11 +21,19 @@ import { UsageCollectionSetup } from '../../../../../src/plugins/usage_collectio import { initTelemetry } from './lib/telemetry'; import { KibanaServices } from './lib/kibana'; +import { serviceNowActionType } from './lib/connectors'; + +import { + TriggersAndActionsUIPublicPluginSetup, + TriggersAndActionsUIPublicPluginStart, +} from '../../../../plugins/triggers_actions_ui/public'; + export { AppMountParameters, CoreSetup, CoreStart, PluginInitializerContext }; export interface SetupPlugins { home: HomePublicPluginSetup; usageCollection: UsageCollectionSetup; + triggers_actions_ui: TriggersAndActionsUIPublicPluginSetup; } export interface StartPlugins { data: DataPublicPluginStart; @@ -33,6 +41,7 @@ export interface StartPlugins { inspector: InspectorStart; newsfeed?: NewsfeedStart; uiActions: UiActionsStart; + triggers_actions_ui: TriggersAndActionsUIPublicPluginStart; } export type StartServices = CoreStart & StartPlugins; @@ -59,6 +68,8 @@ export class Plugin implements IPlugin { const [coreStart, startPlugins] = await core.getStartServices(); const { renderApp } = await import('./app'); + plugins.triggers_actions_ui.actionTypeRegistry.register(serviceNowActionType()); + return renderApp(coreStart, startPlugins as StartPlugins, params); }, }); diff --git a/x-pack/legacy/plugins/siem/public/utils/validators/index.ts b/x-pack/legacy/plugins/siem/public/utils/validators/index.ts new file mode 100644 index 0000000000000..99b01c8b22974 --- /dev/null +++ b/x-pack/legacy/plugins/siem/public/utils/validators/index.ts @@ -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; + * you may not use this file except in compliance with the Elastic License. + */ + +import { isEmpty } from 'lodash/fp'; + +const urlExpression = /(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})/gi; + +export const isUrlInvalid = (url: string | null | undefined) => { + if (!isEmpty(url) && url != null && url.match(urlExpression) == null) { + return true; + } + return false; +}; diff --git a/x-pack/plugins/case/common/api/cases/configure.ts b/x-pack/plugins/case/common/api/cases/configure.ts new file mode 100644 index 0000000000000..e0489ed7270fa --- /dev/null +++ b/x-pack/plugins/case/common/api/cases/configure.ts @@ -0,0 +1,107 @@ +/* + * 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. + */ + +import * as rt from 'io-ts'; + +import { ActionResult } from '../../../../actions/common'; +import { UserRT } from '../user'; + +/* + * This types below are related to the service now configuration + * mapping between our case and service-now + * + */ + +const ActionTypeRT = rt.union([ + rt.literal('append'), + rt.literal('nothing'), + rt.literal('overwrite'), +]); + +const CaseFieldRT = rt.union([ + rt.literal('title'), + rt.literal('description'), + rt.literal('comments'), +]); + +const ThirdPartyFieldRT = rt.union([ + rt.literal('comments'), + rt.literal('description'), + rt.literal('not_mapped'), + rt.literal('short_description'), +]); + +export const CasesConfigurationMapsRT = rt.type({ + source: CaseFieldRT, + target: ThirdPartyFieldRT, + action_type: ActionTypeRT, +}); + +export const CasesConfigurationRT = rt.type({ + mapping: rt.array(CasesConfigurationMapsRT), +}); + +export const CasesConnectorConfigurationRT = rt.type({ + cases_configuration: CasesConfigurationRT, + // version: rt.string, +}); + +export type ActionType = rt.TypeOf; +export type CaseField = rt.TypeOf; +export type ThirdPartyField = rt.TypeOf; + +export type CasesConfigurationMaps = rt.TypeOf; +export type CasesConfiguration = rt.TypeOf; +export type CasesConnectorConfiguration = rt.TypeOf; + +/** ********************************************************************** */ + +export type Connector = ActionResult; + +export interface CasesConnectorsFindResult { + page: number; + perPage: number; + total: number; + data: Connector[]; +} + +// TO DO we will need to add this type rt.literal('close-by-thrid-party') +const ClosureTypeRT = rt.union([rt.literal('close-by-user'), rt.literal('close-by-pushing')]); + +const CasesConfigureBasicRt = rt.type({ + connector_id: rt.string, + closure_type: ClosureTypeRT, +}); + +export const CasesConfigureRequestRt = CasesConfigureBasicRt; +export const CasesConfigurePatchRt = rt.intersection([ + rt.partial(CasesConfigureBasicRt.props), + rt.type({ version: rt.string }), +]); + +export const CaseConfigureAttributesRt = rt.intersection([ + CasesConfigureBasicRt, + rt.type({ + created_at: rt.string, + created_by: UserRT, + updated_at: rt.union([rt.string, rt.null]), + updated_by: rt.union([UserRT, rt.null]), + }), +]); + +export const CaseConfigureResponseRt = rt.intersection([ + CaseConfigureAttributesRt, + rt.type({ + version: rt.string, + }), +]); + +export type ClosureType = rt.TypeOf; + +export type CasesConfigureRequest = rt.TypeOf; +export type CasesConfigurePatch = rt.TypeOf; +export type CasesConfigureAttributes = rt.TypeOf; +export type CasesConfigureResponse = rt.TypeOf; diff --git a/x-pack/plugins/case/common/api/cases/index.ts b/x-pack/plugins/case/common/api/cases/index.ts index 5a355c631f396..5fbee98bc57ad 100644 --- a/x-pack/plugins/case/common/api/cases/index.ts +++ b/x-pack/plugins/case/common/api/cases/index.ts @@ -5,5 +5,6 @@ */ export * from './case'; +export * from './configure'; export * from './comment'; export * from './status'; diff --git a/x-pack/plugins/case/kibana.json b/x-pack/plugins/case/kibana.json index 4a0151546c8fb..f565dc1b6924e 100644 --- a/x-pack/plugins/case/kibana.json +++ b/x-pack/plugins/case/kibana.json @@ -2,7 +2,7 @@ "configPath": ["xpack", "case"], "id": "case", "kibanaVersion": "kibana", - "requiredPlugins": ["security"], + "requiredPlugins": ["security", "actions"], "optionalPlugins": [ "spaces", "security" diff --git a/x-pack/plugins/case/server/plugin.ts b/x-pack/plugins/case/server/plugin.ts index 7ce3a61f03779..1d6495c2d81f3 100644 --- a/x-pack/plugins/case/server/plugin.ts +++ b/x-pack/plugins/case/server/plugin.ts @@ -12,8 +12,12 @@ import { SecurityPluginSetup } from '../../security/server'; import { ConfigType } from './config'; import { initCaseApi } from './routes/api'; -import { caseSavedObjectType, caseCommentSavedObjectType } from './saved_object_types'; -import { CaseService } from './services'; +import { + caseSavedObjectType, + caseConfigureSavedObjectType, + caseCommentSavedObjectType, +} from './saved_object_types'; +import { CaseConfigureService, CaseService } from './services'; function createConfig$(context: PluginInitializerContext) { return context.config.create().pipe(map(config => config)); @@ -41,8 +45,10 @@ export class CasePlugin { core.savedObjects.registerType(caseSavedObjectType); core.savedObjects.registerType(caseCommentSavedObjectType); + core.savedObjects.registerType(caseConfigureSavedObjectType); - const service = new CaseService(this.log); + const caseServicePlugin = new CaseService(this.log); + const caseConfigureServicePlugin = new CaseConfigureService(this.log); this.log.debug( `Setting up Case Workflow with core contract [${Object.keys( @@ -50,12 +56,14 @@ export class CasePlugin { )}] and plugins [${Object.keys(plugins)}]` ); - const caseService = await service.setup({ + const caseService = await caseServicePlugin.setup({ authentication: plugins.security.authc, }); + const caseConfigureService = await caseConfigureServicePlugin.setup(); const router = core.http.createRouter(); initCaseApi({ + caseConfigureService, caseService, router, }); diff --git a/x-pack/plugins/case/server/routes/api/__fixtures__/mock_router.ts b/x-pack/plugins/case/server/routes/api/__fixtures__/mock_router.ts index 32348fecba1be..bc41ddbeff1f9 100644 --- a/x-pack/plugins/case/server/routes/api/__fixtures__/mock_router.ts +++ b/x-pack/plugins/case/server/routes/api/__fixtures__/mock_router.ts @@ -6,7 +6,7 @@ import { IRouter } from 'kibana/server'; import { loggingServiceMock, httpServiceMock } from '../../../../../../../src/core/server/mocks'; -import { CaseService } from '../../../services'; +import { CaseService, CaseConfigureService } from '../../../services'; import { authenticationMock } from '../__fixtures__'; import { RouteDeps } from '../types'; @@ -20,14 +20,18 @@ export const createRoute = async ( const log = loggingServiceMock.create().get('case'); - const service = new CaseService(log); - const caseService = await service.setup({ + const caseServicePlugin = new CaseService(log); + const caseConfigureServicePlugin = new CaseConfigureService(log); + + const caseService = await caseServicePlugin.setup({ authentication: badAuth ? authenticationMock.createInvalid() : authenticationMock.create(), }); + const caseConfigureService = await caseConfigureServicePlugin.setup(); api({ - router, + caseConfigureService, caseService, + router, }); return router[method].mock.calls[0][1]; diff --git a/x-pack/plugins/case/server/routes/api/cases/configure/get_configure.ts b/x-pack/plugins/case/server/routes/api/cases/configure/get_configure.ts new file mode 100644 index 0000000000000..2832edaa892d5 --- /dev/null +++ b/x-pack/plugins/case/server/routes/api/cases/configure/get_configure.ts @@ -0,0 +1,37 @@ +/* + * 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. + */ + +import { CaseConfigureResponseRt } from '../../../../../common/api'; +import { RouteDeps } from '../../types'; +import { wrapError } from '../../utils'; + +export function initGetCaseConfigure({ caseConfigureService, caseService, router }: RouteDeps) { + router.get( + { + path: '/api/cases/configure', + validate: false, + }, + async (context, request, response) => { + try { + const client = context.core.savedObjects.client; + + const myCaseConfigure = await caseConfigureService.find({ client }); + + return response.ok({ + body: + myCaseConfigure.saved_objects.length > 0 + ? CaseConfigureResponseRt.encode({ + ...myCaseConfigure.saved_objects[0].attributes, + version: myCaseConfigure.saved_objects[0].version ?? '', + }) + : {}, + }); + } catch (error) { + return response.customError(wrapError(error)); + } + } + ); +} diff --git a/x-pack/plugins/case/server/routes/api/cases/configure/get_connectors.ts b/x-pack/plugins/case/server/routes/api/cases/configure/get_connectors.ts new file mode 100644 index 0000000000000..b7d4977d16b17 --- /dev/null +++ b/x-pack/plugins/case/server/routes/api/cases/configure/get_connectors.ts @@ -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; + * you may not use this file except in compliance with the Elastic License. + */ + +import Boom from 'boom'; +import { RouteDeps } from '../../types'; +import { wrapError } from '../../utils'; + +/* + * Be aware that this api will only return 20 connectors + */ + +const CASE_SERVICE_NOW_ACTION = '.servicenow'; + +export function initCaseConfigureGetActionConnector({ caseService, router }: RouteDeps) { + router.get( + { + path: '/api/cases/configure/connectors/_find', + validate: false, + }, + async (context, request, response) => { + try { + const actionsClient = await context.actions?.getActionsClient(); + + if (actionsClient == null) { + throw Boom.notFound('Action client have not been found'); + } + + const results = await actionsClient.find({ + options: { + filter: `action.attributes.actionTypeId: ${CASE_SERVICE_NOW_ACTION}`, + }, + }); + return response.ok({ body: { ...results } }); + } catch (error) { + return response.customError(wrapError(error)); + } + } + ); +} diff --git a/x-pack/plugins/case/server/routes/api/cases/configure/patch_configure.ts b/x-pack/plugins/case/server/routes/api/cases/configure/patch_configure.ts new file mode 100644 index 0000000000000..1da1161ab01d1 --- /dev/null +++ b/x-pack/plugins/case/server/routes/api/cases/configure/patch_configure.ts @@ -0,0 +1,77 @@ +/* + * 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. + */ + +import Boom from 'boom'; +import { pipe } from 'fp-ts/lib/pipeable'; +import { fold } from 'fp-ts/lib/Either'; +import { identity } from 'fp-ts/lib/function'; + +import { + CasesConfigurePatchRt, + CaseConfigureResponseRt, + throwErrors, +} from '../../../../../common/api'; +import { RouteDeps } from '../../types'; +import { wrapError, escapeHatch } from '../../utils'; + +export function initPatchCaseConfigure({ caseConfigureService, caseService, router }: RouteDeps) { + router.patch( + { + path: '/api/cases/configure', + validate: { + body: escapeHatch, + }, + }, + async (context, request, response) => { + try { + const client = context.core.savedObjects.client; + const query = pipe( + CasesConfigurePatchRt.decode(request.body), + fold(throwErrors(Boom.badRequest), identity) + ); + + const myCaseConfigure = await caseConfigureService.find({ client }); + const { version, ...queryWithoutVersion } = query; + + if (myCaseConfigure.saved_objects.length === 0) { + throw Boom.conflict( + 'You can not patch this configuration since you did not created first with a post' + ); + } + + if (version !== myCaseConfigure.saved_objects[0].version) { + throw Boom.conflict( + 'This configuration has been updated. Please refresh before saving additional updates.' + ); + } + + const updatedBy = await caseService.getUser({ request, response }); + const { full_name, username } = updatedBy; + + const updateDate = new Date().toISOString(); + const patch = await caseConfigureService.patch({ + client, + caseConfigureId: myCaseConfigure.saved_objects[0].id, + updatedAttributes: { + ...queryWithoutVersion, + updated_at: updateDate, + updated_by: { full_name, username }, + }, + }); + + return response.ok({ + body: CaseConfigureResponseRt.encode({ + ...myCaseConfigure.saved_objects[0].attributes, + ...patch.attributes, + version: patch.version ?? '', + }), + }); + } catch (error) { + return response.customError(wrapError(error)); + } + } + ); +} diff --git a/x-pack/plugins/case/server/routes/api/cases/configure/patch_connector.ts b/x-pack/plugins/case/server/routes/api/cases/configure/patch_connector.ts new file mode 100644 index 0000000000000..a9fbe0ef4f721 --- /dev/null +++ b/x-pack/plugins/case/server/routes/api/cases/configure/patch_connector.ts @@ -0,0 +1,68 @@ +/* + * 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. + */ + +import { schema } from '@kbn/config-schema'; +import Boom from 'boom'; +import { pipe } from 'fp-ts/lib/pipeable'; +import { fold } from 'fp-ts/lib/Either'; +import { identity } from 'fp-ts/lib/function'; + +import { ActionResult } from '../../../../../../actions/common'; +import { CasesConnectorConfigurationRT, throwErrors } from '../../../../../common/api'; +import { RouteDeps } from '../../types'; +import { wrapError, escapeHatch } from '../../utils'; + +export function initCaseConfigurePatchActionConnector({ caseService, router }: RouteDeps) { + router.patch( + { + path: '/api/cases/configure/connectors/{connector_id}', + validate: { + params: schema.object({ + connector_id: schema.string(), + }), + body: escapeHatch, + }, + }, + async (context, request, response) => { + try { + const query = pipe( + CasesConnectorConfigurationRT.decode(request.body), + fold(throwErrors(Boom.badRequest), identity) + ); + + const client = context.core.savedObjects.client; + const { connector_id: connectorId } = request.params; + const { cases_configuration: casesConfiguration } = query; + + const normalizedMapping = casesConfiguration.mapping.map(m => ({ + source: m.source, + target: m.target, + actionType: m.action_type, + })); + + const action = await client.get('action', connectorId); + + const { config } = action.attributes; + const res = await client.update('action', connectorId, { + config: { + ...config, + casesConfiguration: { ...casesConfiguration, mapping: normalizedMapping }, + }, + }); + + return response.ok({ + body: CasesConnectorConfigurationRT.encode({ + cases_configuration: + res.attributes.config?.casesConfiguration ?? + action.attributes.config.casesConfiguration, + }), + }); + } catch (error) { + return response.customError(wrapError(error)); + } + } + ); +} diff --git a/x-pack/plugins/case/server/routes/api/cases/configure/post_configure.ts b/x-pack/plugins/case/server/routes/api/cases/configure/post_configure.ts new file mode 100644 index 0000000000000..a22dd8437e508 --- /dev/null +++ b/x-pack/plugins/case/server/routes/api/cases/configure/post_configure.ts @@ -0,0 +1,68 @@ +/* + * 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. + */ + +import Boom from 'boom'; +import { pipe } from 'fp-ts/lib/pipeable'; +import { fold } from 'fp-ts/lib/Either'; +import { identity } from 'fp-ts/lib/function'; + +import { + CasesConfigureRequestRt, + CaseConfigureResponseRt, + throwErrors, +} from '../../../../../common/api'; +import { RouteDeps } from '../../types'; +import { wrapError, escapeHatch } from '../../utils'; + +export function initPostCaseConfigure({ caseConfigureService, caseService, router }: RouteDeps) { + router.post( + { + path: '/api/cases/configure', + validate: { + body: escapeHatch, + }, + }, + async (context, request, response) => { + try { + const client = context.core.savedObjects.client; + const query = pipe( + CasesConfigureRequestRt.decode(request.body), + fold(throwErrors(Boom.badRequest), identity) + ); + + const myCaseConfigure = await caseConfigureService.find({ client }); + + if (myCaseConfigure.saved_objects.length > 0) { + await Promise.all( + myCaseConfigure.saved_objects.map(cc => + caseConfigureService.delete({ client, caseConfigureId: cc.id }) + ) + ); + } + const updatedBy = await caseService.getUser({ request, response }); + const { full_name, username } = updatedBy; + + const creationDate = new Date().toISOString(); + const post = await caseConfigureService.post({ + client, + attributes: { + ...query, + created_at: creationDate, + created_by: { full_name, username }, + updated_at: null, + updated_by: null, + }, + }); + + return response.ok({ + body: CaseConfigureResponseRt.encode({ ...post.attributes, version: post.version ?? '' }), + }); + } catch (error) { + return response.customError(wrapError(error)); + } + } + ); +} diff --git a/x-pack/plugins/case/server/routes/api/index.ts b/x-pack/plugins/case/server/routes/api/index.ts index cfaef1251bf8c..956f410c9c10a 100644 --- a/x-pack/plugins/case/server/routes/api/index.ts +++ b/x-pack/plugins/case/server/routes/api/index.ts @@ -25,6 +25,11 @@ import { initGetCasesStatusApi } from './cases/status/get_status'; import { initGetTagsApi } from './cases/tags/get_tags'; import { RouteDeps } from './types'; +import { initCaseConfigureGetActionConnector } from './cases/configure/get_connectors'; +import { initCaseConfigurePatchActionConnector } from './cases/configure/patch_connector'; +import { initGetCaseConfigure } from './cases/configure/get_configure'; +import { initPatchCaseConfigure } from './cases/configure/patch_configure'; +import { initPostCaseConfigure } from './cases/configure/post_configure'; export function initCaseApi(deps: RouteDeps) { // Cases @@ -41,6 +46,12 @@ export function initCaseApi(deps: RouteDeps) { initGetAllCommentsApi(deps); initPatchCommentApi(deps); initPostCommentApi(deps); + // Cases Configure + initCaseConfigureGetActionConnector(deps); + initCaseConfigurePatchActionConnector(deps); + initGetCaseConfigure(deps); + initPatchCaseConfigure(deps); + initPostCaseConfigure(deps); // Reporters initGetReportersApi(deps); // Status diff --git a/x-pack/plugins/case/server/routes/api/types.ts b/x-pack/plugins/case/server/routes/api/types.ts index e8668db5d232f..eac259cc69c5a 100644 --- a/x-pack/plugins/case/server/routes/api/types.ts +++ b/x-pack/plugins/case/server/routes/api/types.ts @@ -3,10 +3,12 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ + import { IRouter } from 'src/core/server'; -import { CaseServiceSetup } from '../../services'; +import { CaseConfigureServiceSetup, CaseServiceSetup } from '../../services'; export interface RouteDeps { + caseConfigureService: CaseConfigureServiceSetup; caseService: CaseServiceSetup; router: IRouter; } diff --git a/x-pack/plugins/case/server/routes/api/utils.ts b/x-pack/plugins/case/server/routes/api/utils.ts index 2d73c3aa7976d..04fe426bb2ecc 100644 --- a/x-pack/plugins/case/server/routes/api/utils.ts +++ b/x-pack/plugins/case/server/routes/api/utils.ts @@ -12,6 +12,7 @@ import { SavedObject, SavedObjectsFindResponse, } from 'kibana/server'; + import { CaseRequest, CaseResponse, @@ -21,7 +22,6 @@ import { CommentsResponse, CommentAttributes, } from '../../../common/api'; - import { SortFieldCase } from './types'; export const transformNewCase = ({ @@ -63,7 +63,8 @@ export const transformNewComment = ({ }); export function wrapError(error: any): CustomHttpResponseOptions { - const boom = isBoom(error) ? error : boomify(error); + const options = { statusCode: error.statusCode ?? 500 }; + const boom = isBoom(error) ? error : boomify(error, options); return { body: boom, headers: boom.output.headers, diff --git a/x-pack/plugins/case/server/saved_object_types/configure.ts b/x-pack/plugins/case/server/saved_object_types/configure.ts new file mode 100644 index 0000000000000..8ea6f6bba7d4f --- /dev/null +++ b/x-pack/plugins/case/server/saved_object_types/configure.ts @@ -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; + * you may not use this file except in compliance with the Elastic License. + */ + +import { SavedObjectsType } from 'src/core/server'; + +export const CASE_CONFIGURE_SAVED_OBJECT = 'cases-configure'; + +export const caseConfigureSavedObjectType: SavedObjectsType = { + name: CASE_CONFIGURE_SAVED_OBJECT, + hidden: false, + namespaceAgnostic: false, + mappings: { + properties: { + created_at: { + type: 'date', + }, + created_by: { + properties: { + username: { + type: 'keyword', + }, + full_name: { + type: 'keyword', + }, + }, + }, + connector_id: { + type: 'keyword', + }, + closure_type: { + type: 'keyword', + }, + updated_at: { + type: 'date', + }, + updated_by: { + properties: { + username: { + type: 'keyword', + }, + full_name: { + type: 'keyword', + }, + }, + }, + }, + }, +}; diff --git a/x-pack/plugins/case/server/saved_object_types/index.ts b/x-pack/plugins/case/server/saved_object_types/index.ts index 1e29b9dd98ead..978b3d35ee5c6 100644 --- a/x-pack/plugins/case/server/saved_object_types/index.ts +++ b/x-pack/plugins/case/server/saved_object_types/index.ts @@ -5,4 +5,5 @@ */ export { caseSavedObjectType, CASE_SAVED_OBJECT } from './cases'; +export { caseConfigureSavedObjectType, CASE_CONFIGURE_SAVED_OBJECT } from './configure'; export { caseCommentSavedObjectType, CASE_COMMENT_SAVED_OBJECT } from './comments'; diff --git a/x-pack/plugins/case/server/services/configure/index.ts b/x-pack/plugins/case/server/services/configure/index.ts new file mode 100644 index 0000000000000..42c0dc293a648 --- /dev/null +++ b/x-pack/plugins/case/server/services/configure/index.ts @@ -0,0 +1,99 @@ +/* + * 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. + */ + +import { + Logger, + SavedObject, + SavedObjectsClientContract, + SavedObjectsFindResponse, + SavedObjectsUpdateResponse, +} from 'kibana/server'; + +import { CasesConfigureAttributes, SavedObjectFindOptions } from '../../../common/api'; +import { CASE_CONFIGURE_SAVED_OBJECT } from '../../saved_object_types'; + +interface ClientArgs { + client: SavedObjectsClientContract; +} + +interface GetCaseConfigureArgs extends ClientArgs { + caseConfigureId: string; +} +interface FindCaseConfigureArgs extends ClientArgs { + options?: SavedObjectFindOptions; +} + +interface PostCaseConfigureArgs extends ClientArgs { + attributes: CasesConfigureAttributes; +} + +interface PatchCaseConfigureArgs extends ClientArgs { + caseConfigureId: string; + updatedAttributes: Partial; +} + +export interface CaseConfigureServiceSetup { + delete(args: GetCaseConfigureArgs): Promise<{}>; + get(args: GetCaseConfigureArgs): Promise>; + find(args: FindCaseConfigureArgs): Promise>; + patch( + args: PatchCaseConfigureArgs + ): Promise>; + post(args: PostCaseConfigureArgs): Promise>; +} + +export class CaseConfigureService { + constructor(private readonly log: Logger) {} + public setup = async (): Promise => ({ + delete: async ({ client, caseConfigureId }: GetCaseConfigureArgs) => { + try { + this.log.debug(`Attempting to DELETE case configure ${caseConfigureId}`); + return await client.delete(CASE_CONFIGURE_SAVED_OBJECT, caseConfigureId); + } catch (error) { + this.log.debug(`Error on DELETE case configure ${caseConfigureId}: ${error}`); + throw error; + } + }, + get: async ({ client, caseConfigureId }: GetCaseConfigureArgs) => { + try { + this.log.debug(`Attempting to GET case configuration ${caseConfigureId}`); + return await client.get(CASE_CONFIGURE_SAVED_OBJECT, caseConfigureId); + } catch (error) { + this.log.debug(`Error on GET case configuration ${caseConfigureId}: ${error}`); + throw error; + } + }, + find: async ({ client, options }: FindCaseConfigureArgs) => { + try { + this.log.debug(`Attempting to find all case configuration`); + return await client.find({ ...options, type: CASE_CONFIGURE_SAVED_OBJECT }); + } catch (error) { + this.log.debug(`Attempting to find all case configuration`); + throw error; + } + }, + post: async ({ client, attributes }: PostCaseConfigureArgs) => { + try { + this.log.debug(`Attempting to POST a new case configuration`); + return await client.create(CASE_CONFIGURE_SAVED_OBJECT, { ...attributes }); + } catch (error) { + this.log.debug(`Error on POST a new case configuration: ${error}`); + throw error; + } + }, + patch: async ({ client, caseConfigureId, updatedAttributes }: PatchCaseConfigureArgs) => { + try { + this.log.debug(`Attempting to UPDATE case configuration ${caseConfigureId}`); + return await client.update(CASE_CONFIGURE_SAVED_OBJECT, caseConfigureId, { + ...updatedAttributes, + }); + } catch (error) { + this.log.debug(`Error on UPDATE case configuration ${caseConfigureId}: ${error}`); + throw error; + } + }, + }); +} diff --git a/x-pack/plugins/case/server/services/index.ts b/x-pack/plugins/case/server/services/index.ts index ccb07280028b5..4bbffddf63251 100644 --- a/x-pack/plugins/case/server/services/index.ts +++ b/x-pack/plugins/case/server/services/index.ts @@ -23,6 +23,8 @@ import { CASE_SAVED_OBJECT, CASE_COMMENT_SAVED_OBJECT } from '../saved_object_ty import { readReporters } from './reporters/read_reporters'; import { readTags } from './tags/read_tags'; +export { CaseConfigureService, CaseConfigureServiceSetup } from './configure'; + interface ClientArgs { client: SavedObjectsClientContract; }