[Cases] Fix push to external service error when connector's mapping does not exists (#102894)

Co-authored-by: Jonathan Buttner <jonathan.buttner@elastic.co>
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Christos Nasikas 2021-06-24 16:31:18 +03:00 committed by GitHub
parent b1b182bdec
commit b70b34f884
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 94 additions and 6 deletions

View file

@ -110,20 +110,24 @@ export const push = async (
alertsInfo,
});
const connectorMappings = await casesClientInternal.configuration.getMappings({
const getMappingsResponse = await casesClientInternal.configuration.getMappings({
connector: theCase.connector,
});
if (connectorMappings.length === 0) {
throw new Error('Connector mapping has not been created');
}
const mappings =
getMappingsResponse.length === 0
? await casesClientInternal.configuration.createMappings({
connector: theCase.connector,
owner: theCase.owner,
})
: getMappingsResponse[0].attributes.mappings;
const externalServiceIncident = await createIncident({
actionsClient,
theCase,
userActions,
connector: connector as ActionConnector,
mappings: connectorMappings[0].attributes.mappings,
mappings,
alerts,
casesConnectors,
});

View file

@ -46,6 +46,7 @@ import {
CasesConfigurationsResponse,
CaseUserActionsResponse,
AlertResponse,
ConnectorMappings,
CasesByAlertId,
} from '../../../../plugins/cases/common/api';
import { getPostCaseRequest, postCollectionReq, postCommentGenAlertReq } from './mock';
@ -578,6 +579,32 @@ export const ensureSavedObjectIsAuthorized = (
entities.forEach((entity) => expect(owners.includes(entity.owner)).to.be(true));
};
interface ConnectorMappingsSavedObject {
'cases-connector-mappings': ConnectorMappings;
}
/**
* Returns connector mappings saved objects from Elasticsearch directly.
*/
export const getConnectorMappingsFromES = async ({ es }: { es: KibanaClient }) => {
const mappings: ApiResponse<
estypes.SearchResponse<ConnectorMappingsSavedObject>
> = await es.search({
index: '.kibana',
body: {
query: {
term: {
type: {
value: 'cases-connector-mappings',
},
},
},
},
});
return mappings;
};
export const createCaseWithConnector = async ({
supertest,
configureReq = {},

View file

@ -28,12 +28,19 @@ import {
deleteAllCaseItems,
superUserSpace1Auth,
createCaseWithConnector,
createConnector,
getServiceNowConnector,
getConnectorMappingsFromES,
} from '../../../../common/lib/utils';
import {
ExternalServiceSimulator,
getExternalServiceSimulatorPath,
} from '../../../../../alerting_api_integration/common/fixtures/plugins/actions_simulators/server/plugin';
import { CaseStatuses, CaseUserActionResponse } from '../../../../../../plugins/cases/common/api';
import {
CaseConnector,
CaseStatuses,
CaseUserActionResponse,
} from '../../../../../../plugins/cases/common/api';
import {
globalRead,
noKibanaPrivileges,
@ -95,6 +102,56 @@ export default ({ getService }: FtrProviderContext): void => {
).to.equal(true);
});
it('should create the mappings when pushing a case', async () => {
// create a connector but not a configuration so that the mapping will not be present
const connector = await createConnector({
supertest,
req: {
...getServiceNowConnector(),
config: { apiUrl: servicenowSimulatorURL },
},
});
actionsRemover.add('default', connector.id, 'action', 'actions');
const postedCase = await createCase(
supertest,
{
...getPostCaseRequest(),
connector: {
id: connector.id,
name: connector.name,
type: connector.connector_type_id,
fields: {
urgency: '2',
impact: '2',
severity: '2',
category: 'software',
subcategory: 'os',
},
} as CaseConnector,
},
200
);
// there should be no mappings initially
let mappings = await getConnectorMappingsFromES({ es });
expect(mappings.body.hits.hits.length).to.eql(0);
await pushCase({
supertest,
caseId: postedCase.id,
connectorId: connector.id,
});
// the mappings should now be created after the push
mappings = await getConnectorMappingsFromES({ es });
expect(mappings.body.hits.hits.length).to.be(1);
expect(
mappings.body.hits.hits[0]._source?.['cases-connector-mappings'].mappings.length
).to.be.above(0);
});
it('pushes a comment appropriately', async () => {
const { postedCase, connector } = await createCaseWithConnector({
supertest,