Skip to content

Commit

Permalink
Mapping as singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Jun 8, 2021
1 parent 37b5f4a commit 9eaa654
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 51 deletions.
2 changes: 1 addition & 1 deletion x-pack/plugins/cases/server/client/cases/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { createCaseError, flattenCaseSavedObject, getAlertInfoFromComments } fro
import { ENABLE_CASE_CONNECTOR } from '../../../common/constants';
import { CasesClient, CasesClientArgs, CasesClientInternal } from '..';
import { Operations } from '../../authorization';
import { casesConnectors } from '../../connectors';

/**
* Returns true if the case should be closed based on the configuration settings and whether the case
Expand Down Expand Up @@ -77,7 +78,6 @@ export const push = async (
user,
logger,
authorization,
casesConnectors,
} = clientArgs;

try {
Expand Down
4 changes: 1 addition & 3 deletions x-pack/plugins/cases/server/client/cases/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
} from './utils';
import { flattenCaseSavedObject } from '../../common';
import { SECURITY_SOLUTION_OWNER } from '../../../common';
import { CasesConnectorsFactory } from '../../connectors';
import { casesConnectors } from '../../connectors';

const formatComment = {
commentId: commentObj.id,
Expand Down Expand Up @@ -436,8 +436,6 @@ describe('utils', () => {
isPreconfigured: false,
};

const casesConnectors = new CasesConnectorsFactory().getCasesConnectors();

it('creates an external incident', async () => {
const res = await createIncident({
actionsClient: actionsMock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,13 @@ import { ACTION_SAVED_OBJECT_TYPE } from '../../../../actions/server';
import { createCaseError } from '../../common/error';
import { CasesClientArgs } from '..';
import { CreateMappingsArgs } from './types';
import { casesConnectors } from '../../connectors';

export const createMappings = async (
{ connector, owner }: CreateMappingsArgs,
clientArgs: CasesClientArgs
): Promise<ConnectorMappingsAttributes[]> => {
const {
unsecuredSavedObjectsClient,
connectorMappingsService,
logger,
casesConnectors,
} = clientArgs;
const { unsecuredSavedObjectsClient, connectorMappingsService, logger } = clientArgs;

try {
const mappings = casesConnectors.get(connector.type)?.getMapping() ?? [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,13 @@ import { ACTION_SAVED_OBJECT_TYPE } from '../../../../actions/server';
import { createCaseError } from '../../common/error';
import { CasesClientArgs } from '..';
import { UpdateMappingsArgs } from './types';
import { casesConnectors } from '../../connectors';

export const updateMappings = async (
{ connector, mappingId }: UpdateMappingsArgs,
clientArgs: CasesClientArgs
): Promise<ConnectorMappingsAttributes[]> => {
const {
unsecuredSavedObjectsClient,
connectorMappingsService,
logger,
casesConnectors,
} = clientArgs;
const { unsecuredSavedObjectsClient, connectorMappingsService, logger } = clientArgs;

try {
const mappings = casesConnectors.get(connector.type)?.getMapping() ?? [];
Expand Down
3 changes: 0 additions & 3 deletions x-pack/plugins/cases/server/client/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import { PluginStartContract as FeaturesPluginStart } from '../../../features/se
import { PluginStartContract as ActionsPluginStart } from '../../../actions/server';
import { AuthorizationAuditLogger } from '../authorization';
import { CasesClient, createCasesClient } from '.';
import { CasesConnectorsFactory } from '../connectors';

interface CasesClientFactoryArgs {
securityPluginSetup?: SecurityPluginSetup;
Expand Down Expand Up @@ -91,7 +90,6 @@ export class CasesClientFactory {

const caseService = new CasesService(this.logger, this.options?.securityPluginStart?.authc);
const userInfo = caseService.getUser({ request });
const casesConnectorsFactory = new CasesConnectorsFactory();

return createCasesClient({
alertsService: new AlertService(),
Expand All @@ -112,7 +110,6 @@ export class CasesClientFactory {
logger: this.logger,
authorization: auth,
actionsClient: await this.options.actionsPluginStart.getActionsClientWithRequest(request),
casesConnectors: casesConnectorsFactory.getCasesConnectors(),
});
}
}
2 changes: 0 additions & 2 deletions x-pack/plugins/cases/server/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import type { PublicMethodsOf } from '@kbn/utility-types';
import { ElasticsearchClient, SavedObjectsClientContract, Logger } from 'kibana/server';
import { User } from '../../common/api';
import { Authorization } from '../authorization/authorization';
import { CasesConnectorsMap } from '../connectors/types';
import {
AlertServiceContract,
CaseConfigureService,
Expand All @@ -36,5 +35,4 @@ export interface CasesClientArgs {
readonly logger: Logger;
readonly authorization: PublicMethodsOf<Authorization>;
readonly actionsClient: PublicMethodsOf<ActionsClient>;
readonly casesConnectors: CasesConnectorsMap;
}
37 changes: 14 additions & 23 deletions x-pack/plugins/cases/server/connectors/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,20 @@ import { ConnectorTypes } from '../../common/api';
import { getCaseConnector as getJiraCaseConnector } from './jira';
import { getCaseConnector as getResilientCaseConnector } from './resilient';
import { getServiceNowITSMCaseConnector, getServiceNowSIRCaseConnector } from './servicenow';
import {
CasesConnectorsMap,
CasesConnectorTypes,
ICasesConnector,
ICasesConnectorFactory,
} from './types';
import { ICasesConnector, CasesConnectorsMap } from './types';

export class CasesConnectorsFactory implements ICasesConnectorFactory {
private readonly casesConnectorsMap: Map<CasesConnectorTypes, ICasesConnector<{}>>;
const mapping: Record<ConnectorTypes, ICasesConnector | null> = {
[ConnectorTypes.jira]: getJiraCaseConnector(),
[ConnectorTypes.serviceNowITSM]: getServiceNowITSMCaseConnector(),
[ConnectorTypes.serviceNowSIR]: getServiceNowSIRCaseConnector(),
[ConnectorTypes.resilient]: getResilientCaseConnector(),
[ConnectorTypes.none]: null,
};

constructor() {
this.casesConnectorsMap = new Map<CasesConnectorTypes, ICasesConnector>();
this.initMapping();
}
const isConnectorTypeSupported = (type: string): type is ConnectorTypes =>
Object.values(ConnectorTypes).includes(type as ConnectorTypes);

private initMapping() {
this.casesConnectorsMap.set(ConnectorTypes.jira, getJiraCaseConnector());
this.casesConnectorsMap.set(ConnectorTypes.serviceNowITSM, getServiceNowITSMCaseConnector());
this.casesConnectorsMap.set(ConnectorTypes.serviceNowSIR, getServiceNowSIRCaseConnector());
this.casesConnectorsMap.set(ConnectorTypes.resilient, getResilientCaseConnector());
}

public getCasesConnectors = (): CasesConnectorsMap => {
return this.casesConnectorsMap;
};
}
export const casesConnectors: CasesConnectorsMap = {
get: (type: string): ICasesConnector | undefined | null =>
isConnectorTypeSupported(type) ? mapping[type] : undefined,
};
2 changes: 1 addition & 1 deletion x-pack/plugins/cases/server/connectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { CommentRequest, CommentType } from '../../common/api';

export * from './types';
export { transformConnectorComment } from './case';
export { CasesConnectorsFactory } from './factory';
export { casesConnectors } from './factory';

/**
* Separator used for creating a json parsable array from the mustache syntax that the alerting framework
Expand Down
9 changes: 3 additions & 6 deletions x-pack/plugins/cases/server/connectors/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { Logger } from 'kibana/server';
import { CaseResponse, ConnectorMappingsAttributes, ConnectorTypes } from '../../common/api';
import { CaseResponse, ConnectorMappingsAttributes } from '../../common/api';
import { CasesClientGetAlertsResponse } from '../client/alerts/types';
import { CasesClientFactory } from '../client/factory';
import { RegisterActionType } from '../types';
Expand All @@ -31,9 +31,6 @@ export interface ICasesConnector<TExternalServiceParams = {}> {
getMapping: () => ConnectorMappingsAttributes[];
}

export type CasesConnectorTypes = Omit<ConnectorTypes, ConnectorTypes.none>;
export type CasesConnectorsMap = Map<CasesConnectorTypes, ICasesConnector>;

export interface ICasesConnectorFactory {
getCasesConnectors: () => CasesConnectorsMap;
export interface CasesConnectorsMap {
get: (type: string) => ICasesConnector | undefined | null;
}

0 comments on commit 9eaa654

Please sign in to comment.