Skip to content

Commit

Permalink
updates from PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dhurley14 committed Jan 4, 2022
1 parent ced8410 commit 0262eb4
Showing 1 changed file with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { countBy } from 'lodash/fp';
import uuid from 'uuid';
import { Action } from '@kbn/securitysolution-io-ts-alerting-types';
import { SavedObjectsClientContract } from 'kibana/server';
import pMap from 'p-map';

import { RulesSchema } from '../../../../../common/detection_engine/schemas/response/rules_schema';
import { ImportRulesSchemaDecoded } from '../../../../../common/detection_engine/schemas/request/import_rules_schema';
Expand All @@ -30,6 +31,7 @@ import { SanitizedAlert } from '../../../../../../alerting/common';
import { LegacyRulesActionsSavedObject } from '../../rule_actions/legacy_get_rule_actions_saved_object';

type PromiseFromStreams = ImportRulesSchemaDecoded | Error;
const MAX_CONCURRENT_SEARCHES = 10;

export const getIdError = ({
id,
Expand Down Expand Up @@ -198,8 +200,8 @@ export const getTupleDuplicateErrorsAndUniqueRules = (
// functions copied from here
// https://github.com/elastic/kibana/blob/4584a8b570402aa07832cf3e5b520e5d2cfa7166/src/core/server/saved_objects/import/lib/check_origin_conflicts.ts#L55-L57
const createQueryTerm = (input: string) => input.replace(/\\/g, '\\\\').replace(/\"/g, '\\"');
const createQuery = (type: string, id: string, rawIdPrefix: string) =>
`"${createQueryTerm(`${rawIdPrefix}${type}:${id}`)}" | "${createQueryTerm(id)}"`;
const createQuery = (type: string, id: string) =>
`"${createQueryTerm(`${type}:${id}`)}" | "${createQueryTerm(id)}"`;

/**
* Query for a saved object with a given origin id and replace the
Expand All @@ -213,9 +215,7 @@ export const swapActionIds = async (
savedObjectsClient: SavedObjectsClientContract
): Promise<Action | Error> => {
try {
// TODO: might need to pass down namespace
// but for now it's blank to do local testing.
const search = createQuery('action', action.id, '');
const search = createQuery('action', action.id);
const foundAction = await savedObjectsClient.find<Action>({
type: 'action',
search,
Expand All @@ -226,7 +226,7 @@ export const swapActionIds = async (
return { ...action, id: foundAction.saved_objects[0].id };
} else if (foundAction.saved_objects.length > 1) {
return new Error(
`action connector with originId: ${action.id} had conflicts. Please resolve these conflicts either in the file you are attempting to upload or resolve the conflicting action connector in Kibana.`
`Found two action connectors with originId or _id: ${action.id} The upload cannot be completed unless the _id or the originId of the action connector is changed. See https://www.elastic.co/guide/en/kibana/current/sharing-saved-objects.html for more details`
);
}
} catch (exc) {
Expand Down Expand Up @@ -270,23 +270,28 @@ export const migrateLegacyActionsIds = async (
savedObjectsClient: SavedObjectsClientContract
): Promise<PromiseFromStreams[]> => {
const isImportRule = (r: unknown): r is ImportRulesSchemaDecoded => !(r instanceof Error);
return Promise.all(
rules.map(async (rule) => {

return pMap(
rules,
async (rule) => {
if (isImportRule(rule)) {
// can we swap the pre 8.0 action connector(s) id with the new,
// post-8.0 action id (swap the originId for the new _id?)
const newActions: Array<Action | Error> = await Promise.all(
rule.actions.map((action: Action) => swapActionIds(action, savedObjectsClient))
);

// any errors discovered while trying to migrate
// and swap the action connector ids?
// were there any errors discovered while trying to migrate and swap the action connector ids?
const actionMigrationErrors = newActions.filter(
(action): action is Error => action instanceof Error
);

const newlyMigratedActions: Action[] = newActions.filter(
(action): action is Action => !(action instanceof Error)
);

if (actionMigrationErrors == null || actionMigrationErrors.length === 0) {
return { ...rule, actions: newActions } as ImportRulesSchemaDecoded;
return { ...rule, actions: newlyMigratedActions };
}
// return an Error object with the rule_id and the error messages
// for the actions associated with that rule.
Expand All @@ -301,7 +306,8 @@ export const migrateLegacyActionsIds = async (
);
}
return rule;
})
},
{ concurrency: MAX_CONCURRENT_SEARCHES }
);
};

Expand Down

0 comments on commit 0262eb4

Please sign in to comment.