Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
0913844
Extracting saved object references before saving action_task_params s…
ymao1 Aug 17, 2021
7d74359
Injecting saved object ids from references when reading action_task_p…
ymao1 Aug 17, 2021
c46c87f
Adding migration
ymao1 Aug 17, 2021
5f90789
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 Aug 17, 2021
4088232
Adding unit test for migrations
ymao1 Aug 18, 2021
e21c15b
Not differentiating between preconfigured or not
ymao1 Aug 18, 2021
2c8ec86
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 Aug 18, 2021
0ff73d0
Adding functional test for migration
ymao1 Aug 18, 2021
d0783f8
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 Aug 18, 2021
291e109
Skip extracting action id if action is preconfigured
ymao1 Aug 18, 2021
e18e5d3
Only migrating action task params for non preconfigured connectors
ymao1 Aug 19, 2021
73ccd07
Simplifying related saved objects
ymao1 Aug 19, 2021
6d4c6ab
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 Aug 19, 2021
5bd8ed4
Fixing functional test
ymao1 Aug 19, 2021
930cdc6
Fixing migration
ymao1 Aug 19, 2021
91d5577
Javascript is sometimes magical
ymao1 Aug 19, 2021
7dee905
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 Aug 19, 2021
5f90910
Updating functional test
ymao1 Aug 19, 2021
9df1864
Merge branch 'master' into alerting/action-task-params-references
kibanamachine Aug 23, 2021
105e57e
PR feedback
ymao1 Aug 23, 2021
8d277c6
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 Aug 23, 2021
328bfef
Merge branch 'alerting/action-task-params-references' of https://gith…
ymao1 Aug 23, 2021
140d7ac
Merge branch 'master' into alerting/action-task-params-references
kibanamachine Aug 25, 2021
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
123 changes: 120 additions & 3 deletions x-pack/plugins/actions/server/create_execute_function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,15 @@ describe('execute()', () => {
params: { baz: false },
apiKey: Buffer.from('123:abc').toString('base64'),
},
{}
{
references: [
{
id: '123',
name: 'actionRef',
type: 'action',
},
],
}
);
expect(actionTypeRegistry.isActionExecutable).toHaveBeenCalledWith('123', 'mock-action', {
notifyUsage: true,
Expand Down Expand Up @@ -128,14 +136,27 @@ describe('execute()', () => {
apiKey: Buffer.from('123:abc').toString('base64'),
relatedSavedObjects: [
{
id: 'some-id',
id: 'related_some-type_0',
namespace: 'some-namespace',
type: 'some-type',
typeId: 'some-typeId',
},
],
},
{}
{
references: [
{
id: '123',
name: 'actionRef',
type: 'action',
},
{
id: 'some-id',
name: 'related_some-type_0',
type: 'some-type',
},
],
}
);
});

Expand Down Expand Up @@ -214,6 +235,102 @@ describe('execute()', () => {
);
});

test('schedules the action with all given parameters with a preconfigured action and relatedSavedObjects', async () => {
const executeFn = createExecutionEnqueuerFunction({
taskManager: mockTaskManager,
actionTypeRegistry: actionTypeRegistryMock.create(),
isESOCanEncrypt: true,
preconfiguredActions: [
{
id: '123',
actionTypeId: 'mock-action-preconfigured',
config: {},
isPreconfigured: true,
name: 'x',
secrets: {},
},
],
});
const source = { type: 'alert', id: uuid.v4() };

savedObjectsClient.get.mockResolvedValueOnce({
id: '123',
type: 'action',
attributes: {
actionTypeId: 'mock-action',
},
references: [],
});
savedObjectsClient.create.mockResolvedValueOnce({
id: '234',
type: 'action_task_params',
attributes: {},
references: [],
});
await executeFn(savedObjectsClient, {
id: '123',
params: { baz: false },
spaceId: 'default',
apiKey: Buffer.from('123:abc').toString('base64'),
source: asSavedObjectExecutionSource(source),
relatedSavedObjects: [
{
id: 'some-id',
namespace: 'some-namespace',
type: 'some-type',
typeId: 'some-typeId',
},
],
});
expect(mockTaskManager.schedule).toHaveBeenCalledTimes(1);
expect(mockTaskManager.schedule.mock.calls[0]).toMatchInlineSnapshot(`
Array [
Object {
"params": Object {
"actionTaskParamsId": "234",
"spaceId": "default",
},
"scope": Array [
"actions",
],
"state": Object {},
"taskType": "actions:mock-action-preconfigured",
},
]
`);
expect(savedObjectsClient.get).not.toHaveBeenCalled();
expect(savedObjectsClient.create).toHaveBeenCalledWith(
'action_task_params',
{
actionId: '123',
params: { baz: false },
apiKey: Buffer.from('123:abc').toString('base64'),
relatedSavedObjects: [
{
id: 'related_some-type_0',
namespace: 'some-namespace',
type: 'some-type',
typeId: 'some-typeId',
},
],
},
{
references: [
{
id: source.id,
name: 'source',
type: source.type,
},
{
id: 'some-id',
name: 'related_some-type_0',
type: 'some-type',
},
],
}
);
});

test('throws when passing isESOCanEncrypt with false as a value', async () => {
const executeFn = createExecutionEnqueuerFunction({
taskManager: mockTaskManager,
Expand Down
38 changes: 30 additions & 8 deletions x-pack/plugins/actions/server/create_execute_function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from './types';
import { ACTION_TASK_PARAMS_SAVED_OBJECT_TYPE } from './constants/saved_objects';
import { ExecuteOptions as ActionExecutorOptions } from './lib/action_executor';
import { isSavedObjectExecutionSource } from './lib';
import { extractSavedObjectReferences, isSavedObjectExecutionSource } from './lib';
import { RelatedSavedObjects } from './lib/related_saved_objects';

interface CreateExecuteFunctionOptions {
Expand Down Expand Up @@ -53,23 +53,45 @@ export function createExecutionEnqueuerFunction({
);
}

const action = await getAction(unsecuredSavedObjectsClient, preconfiguredActions, id);
const { action, isPreconfigured } = await getAction(
unsecuredSavedObjectsClient,
preconfiguredActions,
id
);
validateCanActionBeUsed(action);

const { actionTypeId } = action;
if (!actionTypeRegistry.isActionExecutable(id, actionTypeId, { notifyUsage: true })) {
actionTypeRegistry.ensureActionTypeEnabled(actionTypeId);
}

// Get saved object references from action ID and relatedSavedObjects
const { references, relatedSavedObjectWithRefs } = extractSavedObjectReferences(
id,
isPreconfigured,
relatedSavedObjects
);
const executionSourceReference = executionSourceAsSavedObjectReferences(source);

const taskReferences = [];
if (executionSourceReference.references) {
taskReferences.push(...executionSourceReference.references);
}
if (references) {
taskReferences.push(...references);
}

const actionTaskParamsRecord = await unsecuredSavedObjectsClient.create(
ACTION_TASK_PARAMS_SAVED_OBJECT_TYPE,
{
actionId: id,
params,
apiKey,
relatedSavedObjects,
relatedSavedObjects: relatedSavedObjectWithRefs,
},
executionSourceAsSavedObjectReferences(source)
{
references: taskReferences,
}
);

await taskManager.schedule({
Expand All @@ -93,7 +115,7 @@ export function createEphemeralExecutionEnqueuerFunction({
unsecuredSavedObjectsClient: SavedObjectsClientContract,
{ id, params, spaceId, source, apiKey }: ExecuteOptions
): Promise<RunNowResult> {
const action = await getAction(unsecuredSavedObjectsClient, preconfiguredActions, id);
const { action } = await getAction(unsecuredSavedObjectsClient, preconfiguredActions, id);
validateCanActionBeUsed(action);

const { actionTypeId } = action;
Expand Down Expand Up @@ -148,12 +170,12 @@ async function getAction(
unsecuredSavedObjectsClient: SavedObjectsClientContract,
preconfiguredActions: PreConfiguredAction[],
actionId: string
): Promise<PreConfiguredAction | RawAction> {
): Promise<{ action: PreConfiguredAction | RawAction; isPreconfigured: boolean }> {
const pcAction = preconfiguredActions.find((action) => action.id === actionId);
if (pcAction) {
return pcAction;
return { action: pcAction, isPreconfigured: true };
}

const { attributes } = await unsecuredSavedObjectsClient.get<RawAction>('action', actionId);
return attributes;
return { action: attributes, isPreconfigured: false };
}
Loading