Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cases] Removing spreads from connector mappings #156622

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions x-pack/plugins/cases/common/api/connectors/mappings.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { decodeOrThrow } from '../runtime_types';
import { ConnectorMappingsPartialRt } from './mappings';

describe('mappings', () => {
describe('ConnectorMappingsPartialRt', () => {
it('strips excess fields from the object', () => {
const res = decodeOrThrow(ConnectorMappingsPartialRt)({ bananas: 'yes', owner: 'hi' });
expect(res).toMatchObject({
owner: 'hi',
});
});

it('does not throw when the object is empty', () => {
expect(() => decodeOrThrow(ConnectorMappingsPartialRt)({})).not.toThrow();
});
});
});
32 changes: 17 additions & 15 deletions x-pack/plugins/cases/common/api/connectors/mappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,50 @@

import * as rt from 'io-ts';

const ActionTypeRT = rt.union([
const ActionTypeRt = rt.union([
rt.literal('append'),
rt.literal('nothing'),
rt.literal('overwrite'),
]);
const CaseFieldRT = rt.union([
const CaseFieldRt = rt.union([
rt.literal('title'),
rt.literal('description'),
rt.literal('comments'),
rt.literal('tags'),
]);

const ThirdPartyFieldRT = rt.union([rt.string, rt.literal('not_mapped')]);
export type ActionType = rt.TypeOf<typeof ActionTypeRT>;
export type CaseField = rt.TypeOf<typeof CaseFieldRT>;
export type ThirdPartyField = rt.TypeOf<typeof ThirdPartyFieldRT>;
const ThirdPartyFieldRt = rt.union([rt.string, rt.literal('not_mapped')]);
export type ActionType = rt.TypeOf<typeof ActionTypeRt>;
export type CaseField = rt.TypeOf<typeof CaseFieldRt>;
export type ThirdPartyField = rt.TypeOf<typeof ThirdPartyFieldRt>;

export const ConnectorMappingsAttributesRT = rt.type({
action_type: ActionTypeRT,
source: CaseFieldRT,
target: ThirdPartyFieldRT,
const ConnectorMappingsAttributesRt = rt.type({
action_type: ActionTypeRt,
source: CaseFieldRt,
target: ThirdPartyFieldRt,
});

export const ConnectorMappingsRt = rt.type({
mappings: rt.array(ConnectorMappingsAttributesRT),
mappings: rt.array(ConnectorMappingsAttributesRt),
owner: rt.string,
});

export type ConnectorMappingsAttributes = rt.TypeOf<typeof ConnectorMappingsAttributesRT>;
export const ConnectorMappingsPartialRt = rt.exact(rt.partial(ConnectorMappingsRt.props));

export type ConnectorMappingsAttributes = rt.TypeOf<typeof ConnectorMappingsAttributesRt>;
export type ConnectorMappings = rt.TypeOf<typeof ConnectorMappingsRt>;

const FieldTypeRT = rt.union([rt.literal('text'), rt.literal('textarea')]);
const FieldTypeRt = rt.union([rt.literal('text'), rt.literal('textarea')]);

const ConnectorFieldRt = rt.type({
id: rt.string,
name: rt.string,
required: rt.boolean,
type: FieldTypeRT,
type: FieldTypeRt,
});

export type ConnectorField = rt.TypeOf<typeof ConnectorFieldRt>;

const GetDefaultMappingsResponseRt = rt.array(ConnectorMappingsAttributesRT);
const GetDefaultMappingsResponseRt = rt.array(ConnectorMappingsAttributesRt);

export type GetDefaultMappingsResponse = rt.TypeOf<typeof GetDefaultMappingsResponseRt>;
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* 2.0.
*/

import type { SavedObject } from '@kbn/core/server';
import type { ConnectorMappings } from '../../../common/api';

export interface ConnectorMappingsPersistedAttributes {
mappings: Array<{
action_type: string;
Expand All @@ -13,3 +16,6 @@ export interface ConnectorMappingsPersistedAttributes {
}>;
owner: string;
}

export type ConnectorMappingsTransformed = ConnectorMappings;
export type ConnectorMappingsSavedObjectTransformed = SavedObject<ConnectorMappingsTransformed>;
42 changes: 33 additions & 9 deletions x-pack/plugins/cases/server/services/connector_mappings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

import type {
Logger,
SavedObject,
SavedObjectsFindResponse,
SavedObjectsFindResult,
SavedObjectsUpdateResponse,
} from '@kbn/core/server';

Expand All @@ -18,16 +18,24 @@ import type {
PostConnectorMappingsArgs,
UpdateConnectorMappingsArgs,
} from './types';
import type { ConnectorMappingsPersistedAttributes } from '../../common/types/connector_mappings';
import type { ConnectorMappings } from '../../../common/api';
import type {
ConnectorMappingsPersistedAttributes,
ConnectorMappingsSavedObjectTransformed,
ConnectorMappingsTransformed,
} from '../../common/types/connector_mappings';
import {
ConnectorMappingsRt,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Maybe we can do export type ConnectorMappingsTransformedRt = ConnectorMappingsRt; in x-pack/plugins/cases/server/common/types/connector_mappings.ts and use it here instead of ConnectorMappingsRt.

decodeOrThrow,
ConnectorMappingsPartialRt,
} from '../../../common/api';

export class ConnectorMappingsService {
constructor(private readonly log: Logger) {}

public async find({
unsecuredSavedObjectsClient,
options,
}: FindConnectorMappingsArgs): Promise<SavedObjectsFindResponse<ConnectorMappings>> {
}: FindConnectorMappingsArgs): Promise<SavedObjectsFindResponse<ConnectorMappingsTransformed>> {
try {
this.log.debug(`Attempting to find all connector mappings`);
const connectorMappings =
Expand All @@ -36,7 +44,15 @@ export class ConnectorMappingsService {
type: CASE_CONNECTOR_MAPPINGS_SAVED_OBJECT,
});

return connectorMappings as SavedObjectsFindResponse<ConnectorMappings>;
const validatedMappings: Array<SavedObjectsFindResult<ConnectorMappingsTransformed>> = [];

for (const mapping of connectorMappings.saved_objects) {
const validatedMapping = decodeOrThrow(ConnectorMappingsRt)(mapping.attributes);

validatedMappings.push(Object.assign(mapping, { attributes: validatedMapping }));
}

return Object.assign(connectorMappings, { saved_objects: validatedMappings });
} catch (error) {
this.log.error(`Attempting to find all connector mappings: ${error}`);
throw error;
Expand All @@ -48,7 +64,7 @@ export class ConnectorMappingsService {
attributes,
references,
refresh,
}: PostConnectorMappingsArgs): Promise<SavedObject<ConnectorMappings>> {
}: PostConnectorMappingsArgs): Promise<ConnectorMappingsSavedObjectTransformed> {
try {
this.log.debug(`Attempting to POST a new connector mappings`);
const connectorMappings =
Expand All @@ -61,7 +77,9 @@ export class ConnectorMappingsService {
}
);

return connectorMappings as SavedObject<ConnectorMappings>;
const validatedAttributes = decodeOrThrow(ConnectorMappingsRt)(connectorMappings.attributes);

return Object.assign(connectorMappings, { attributes: validatedAttributes });
} catch (error) {
this.log.error(`Error on POST a new connector mappings: ${error}`);
throw error;
Expand All @@ -74,7 +92,9 @@ export class ConnectorMappingsService {
attributes,
references,
refresh,
}: UpdateConnectorMappingsArgs): Promise<SavedObjectsUpdateResponse<ConnectorMappings>> {
}: UpdateConnectorMappingsArgs): Promise<
SavedObjectsUpdateResponse<ConnectorMappingsTransformed>
> {
try {
this.log.debug(`Attempting to UPDATE connector mappings ${mappingId}`);
const updatedMappings =
Expand All @@ -88,7 +108,11 @@ export class ConnectorMappingsService {
}
);

return updatedMappings as SavedObjectsUpdateResponse<ConnectorMappings>;
const validatedAttributes = decodeOrThrow(ConnectorMappingsPartialRt)(
updatedMappings.attributes
);

return Object.assign(updatedMappings, { attributes: validatedAttributes });
} catch (error) {
this.log.error(`Error on UPDATE connector mappings ${mappingId}: ${error}`);
throw error;
Expand Down