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

[Alerting & Actions] Overwrite SOs when updating instead of partially updating #73688

Merged
merged 60 commits into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from 47 commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
d0c0249
use create to overwrite actions on update in action
gmmorris Jul 29, 2020
8124051
Merge branch 'master' into actions/webhook-remove-header
elasticmachine Jul 29, 2020
b0e5938
Revert "use create to overwrite actions on update in action"
gmmorris Jul 30, 2020
72546d0
changed ESO to overwrite on update
gmmorris Jul 30, 2020
031e0d8
Merge branch 'actions/webhook-remove-header' of github.com:gmmorris/k…
gmmorris Jul 30, 2020
e4280f2
added e2e test for new ESO update
gmmorris Jul 30, 2020
28b1676
Merge branch 'master' into actions/webhook-remove-header
gmmorris Jul 31, 2020
797781f
added `version` to SavedObjects create api
gmmorris Aug 5, 2020
1b5d8b2
use version in ESO updates and updated bulkUpdate to use bulkCreate
gmmorris Aug 5, 2020
2b09920
Merge branch 'master' into actions/webhook-remove-header
gmmorris Aug 5, 2020
a5b4efb
updated core docs
gmmorris Aug 5, 2020
d5c0038
corrected alert updates in client
gmmorris Aug 6, 2020
06d8c91
fixed references
gmmorris Aug 11, 2020
4fc6ce0
handle mixed bulkUpdates in ESO
gmmorris Aug 11, 2020
283d3ac
merge returned docs in bulkUpdate in a slightly cleaner manner
gmmorris Aug 11, 2020
1dbf2f2
Merge branch 'master' into actions/webhook-remove-header
gmmorris Aug 11, 2020
e8748e9
fixed unit tesst and usage of ESO in actions client
gmmorris Aug 12, 2020
d500411
Merge branch 'master' into actions/webhook-remove-header
gmmorris Aug 12, 2020
c4829bf
fixed snapshot
gmmorris Aug 12, 2020
489b2d1
prevent partial updates at typing level in alerts client
gmmorris Aug 13, 2020
3557146
Merge branch 'master' into actions/webhook-remove-header
gmmorris Aug 13, 2020
79256e0
fixed typing issue in update
gmmorris Aug 13, 2020
a88eba6
fixed typing broken by making apiKey an optional field
gmmorris Aug 13, 2020
bd12cb4
Merge branch 'master' into actions/webhook-remove-header
gmmorris Aug 14, 2020
6a2f3ca
skip endpoint test to ensur eeverything else passes
gmmorris Aug 14, 2020
1560549
Revert "skip endpoint test to ensur eeverything else passes"
gmmorris Aug 14, 2020
640c0b2
revert change in ESO and prohibit partial updates in alert and action…
gmmorris Aug 14, 2020
122cf26
Merge branch 'master' into actions/webhook-remove-header
gmmorris Aug 14, 2020
3dfce39
fixed import
gmmorris Aug 14, 2020
692894e
fixed alerts clients unit tests
gmmorris Aug 17, 2020
c913bee
Merge branch 'master' into actions/webhook-remove-header
gmmorris Aug 17, 2020
13c4ec4
added version to saved object creation
gmmorris Aug 17, 2020
d4820e1
added version to saved object bulk creation
gmmorris Aug 17, 2020
814b98d
added missing core docs
gmmorris Aug 17, 2020
3f49f67
Merge branch 'master' into saved-objects/version-on-create
elasticmachine Aug 18, 2020
7bd360c
Revert "added missing core docs"
gmmorris Aug 18, 2020
caa1834
Revert "added version to saved object bulk creation"
gmmorris Aug 18, 2020
8f3dfc8
Revert "Revert "added version to saved object bulk creation""
gmmorris Aug 18, 2020
c4e8c59
Revert "Revert "added missing core docs""
gmmorris Aug 18, 2020
c391446
omit version from SO import/export
gmmorris Aug 18, 2020
4dfcf10
Merge branch 'master' into saved-objects/version-on-create
gmmorris Aug 18, 2020
a0fc384
remove version when loading sample data
gmmorris Aug 18, 2020
78cb2cf
Merge branch 'saved-objects/version-on-create' into actions/webhook-r…
gmmorris Aug 19, 2020
fd42b0c
Merge branch 'master' into actions/webhook-remove-header
gmmorris Aug 19, 2020
c58689a
only allow id in eso when theres a version
gmmorris Aug 21, 2020
96689fa
added comment about allowing id
gmmorris Aug 21, 2020
452f964
Merge branch 'master' into actions/webhook-remove-header
gmmorris Aug 21, 2020
c9deb85
Merge branch 'master' into actions/webhook-remove-header
gmmorris Sep 2, 2020
1f641c3
added comment about type
gmmorris Sep 2, 2020
17a325d
Merge branch 'master' into actions/webhook-remove-header
gmmorris Sep 15, 2020
c527d9e
revert removal of partial updates from so client in alerts client
gmmorris Sep 15, 2020
1434bbe
removed potential clashes with OCC changes
gmmorris Sep 15, 2020
2472325
fixed unit tests
gmmorris Sep 15, 2020
7bfbad5
Merge branch 'master' into actions/webhook-remove-header
gmmorris Sep 16, 2020
7780d0f
fixed emrge conflicts
gmmorris Sep 16, 2020
0e797f5
removed unused type
gmmorris Sep 16, 2020
21cb7a7
Merge branch 'master' into actions/webhook-remove-header
gmmorris Sep 16, 2020
fec5cdd
removed decryption from operations that dont need it
gmmorris Sep 16, 2020
e32e091
reduced unneeded changes
gmmorris Sep 16, 2020
93807e6
removed unneeded undefined
gmmorris Sep 16, 2020
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
28 changes: 18 additions & 10 deletions x-pack/plugins/actions/server/actions_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@ describe('update()', () => {
},
references: [],
});
unsecuredSavedObjectsClient.update.mockResolvedValueOnce({
unsecuredSavedObjectsClient.create.mockResolvedValueOnce({
id: 'my-action',
type: 'action',
attributes: {
Expand Down Expand Up @@ -946,7 +946,7 @@ describe('update()', () => {
},
references: [],
});
unsecuredSavedObjectsClient.update.mockResolvedValueOnce({
unsecuredSavedObjectsClient.create.mockResolvedValueOnce({
id: 'my-action',
type: 'action',
attributes: {
Expand All @@ -972,17 +972,21 @@ describe('update()', () => {
name: 'my name',
config: {},
});
expect(unsecuredSavedObjectsClient.update).toHaveBeenCalledTimes(1);
expect(unsecuredSavedObjectsClient.update.mock.calls[0]).toMatchInlineSnapshot(`
expect(unsecuredSavedObjectsClient.create).toHaveBeenCalledTimes(1);
expect(unsecuredSavedObjectsClient.create.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"action",
"my-action",
Object {
"actionTypeId": "my-action-type",
"config": Object {},
"name": "my name",
"secrets": Object {},
},
Object {
"id": "my-action",
"overwrite": true,
"references": Array [],
},
]
`);
expect(unsecuredSavedObjectsClient.get).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -1043,7 +1047,7 @@ describe('update()', () => {
},
references: [],
});
unsecuredSavedObjectsClient.update.mockResolvedValueOnce({
unsecuredSavedObjectsClient.create.mockResolvedValueOnce({
id: 'my-action',
type: 'action',
attributes: {
Expand Down Expand Up @@ -1081,11 +1085,10 @@ describe('update()', () => {
c: true,
},
});
expect(unsecuredSavedObjectsClient.update).toHaveBeenCalledTimes(1);
expect(unsecuredSavedObjectsClient.update.mock.calls[0]).toMatchInlineSnapshot(`
expect(unsecuredSavedObjectsClient.create).toHaveBeenCalledTimes(1);
expect(unsecuredSavedObjectsClient.create.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"action",
"my-action",
Object {
"actionTypeId": "my-action-type",
"config": Object {
Expand All @@ -1096,6 +1099,11 @@ describe('update()', () => {
"name": "my name",
"secrets": Object {},
},
Object {
"id": "my-action",
"overwrite": true,
"references": Array [],
},
]
`);
});
Expand All @@ -1118,7 +1126,7 @@ describe('update()', () => {
},
references: [],
});
unsecuredSavedObjectsClient.update.mockResolvedValueOnce({
unsecuredSavedObjectsClient.create.mockResolvedValueOnce({
id: 'my-action',
type: 'action',
attributes: {
Expand Down
38 changes: 27 additions & 11 deletions x-pack/plugins/actions/server/actions_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
import Boom from 'boom';
import {
ILegacyScopedClusterClient,
SavedObjectsClientContract,
SavedObjectAttributes,
SavedObject,
KibanaRequest,
} from 'src/core/server';

import { i18n } from '@kbn/i18n';
import { omitBy, isUndefined } from 'lodash';
import { ActionTypeRegistry } from './action_type_registry';
import { validateConfig, validateSecrets, ActionExecutorContract } from './lib';
import {
Expand All @@ -30,6 +30,7 @@ import {
} from './create_execute_function';
import { ActionsAuthorization } from './authorization/actions_authorization';
import { ActionType } from '../common';
import { SavedObjectsClientWithoutUpdates } from './saved_objects';

// We are assuming there won't be many actions. This is why we will load
// all the actions in advance and assume the total count to not go over 10000.
Expand All @@ -54,7 +55,7 @@ interface ConstructorOptions {
defaultKibanaIndex: string;
scopedClusterClient: ILegacyScopedClusterClient;
actionTypeRegistry: ActionTypeRegistry;
unsecuredSavedObjectsClient: SavedObjectsClientContract;
unsecuredSavedObjectsClient: SavedObjectsClientWithoutUpdates;
preconfiguredActions: PreConfiguredAction[];
actionExecutor: ActionExecutorContract;
executionEnqueuer: ExecutionEnqueuer;
Expand All @@ -70,7 +71,7 @@ interface UpdateOptions {
export class ActionsClient {
private readonly defaultKibanaIndex: string;
private readonly scopedClusterClient: ILegacyScopedClusterClient;
private readonly unsecuredSavedObjectsClient: SavedObjectsClientContract;
private readonly unsecuredSavedObjectsClient: SavedObjectsClientWithoutUpdates;
private readonly actionTypeRegistry: ActionTypeRegistry;
private readonly preconfiguredActions: PreConfiguredAction[];
private readonly actionExecutor: ActionExecutorContract;
Expand Down Expand Up @@ -150,21 +151,36 @@ export class ActionsClient {
'update'
);
}
const existingObject = await this.unsecuredSavedObjectsClient.get<RawAction>('action', id);
const { actionTypeId } = existingObject.attributes;
const { attributes, references, version } = await this.unsecuredSavedObjectsClient.get<
RawAction
>('action', id);
const { actionTypeId } = attributes;
const { name, config, secrets } = action;
const actionType = this.actionTypeRegistry.get(actionTypeId);
const validatedActionTypeConfig = validateConfig(actionType, config);
const validatedActionTypeSecrets = validateSecrets(actionType, secrets);

this.actionTypeRegistry.ensureActionTypeEnabled(actionTypeId);

const result = await this.unsecuredSavedObjectsClient.update<RawAction>('action', id, {
actionTypeId,
name,
config: validatedActionTypeConfig as SavedObjectAttributes,
secrets: validatedActionTypeSecrets as SavedObjectAttributes,
});
const result = await this.unsecuredSavedObjectsClient.create<RawAction>(
'action',
{
...attributes,
actionTypeId,
name,
config: validatedActionTypeConfig as SavedObjectAttributes,
secrets: validatedActionTypeSecrets as SavedObjectAttributes,
},
omitBy(
{
id,
overwrite: true,
references,
version,
},
isUndefined
)
);

return {
id,
Expand Down
10 changes: 6 additions & 4 deletions x-pack/plugins/actions/server/create_execute_function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { SavedObjectsClientContract } from '../../../../src/core/server';
import { TaskManagerStartContract } from '../../task_manager/server';
import { RawAction, ActionTypeRegistryContract, PreConfiguredAction } from './types';
import { ACTION_TASK_PARAMS_SAVED_OBJECT_TYPE } from './saved_objects';
import {
ACTION_TASK_PARAMS_SAVED_OBJECT_TYPE,
SavedObjectsClientWithoutUpdates,
} from './saved_objects';

interface CreateExecuteFunctionOptions {
taskManager: TaskManagerStartContract;
Expand All @@ -24,7 +26,7 @@ export interface ExecuteOptions {
}

export type ExecutionEnqueuer = (
savedObjectsClient: SavedObjectsClientContract,
savedObjectsClient: SavedObjectsClientWithoutUpdates,
options: ExecuteOptions
) => Promise<void>;

Expand All @@ -35,7 +37,7 @@ export function createExecutionEnqueuerFunction({
preconfiguredActions,
}: CreateExecuteFunctionOptions) {
return async function execute(
savedObjectsClient: SavedObjectsClientContract,
savedObjectsClient: SavedObjectsClientWithoutUpdates,
{ id, params, spaceId, apiKey }: ExecuteOptions
) {
if (isESOUsingEphemeralEncryptionKey === true) {
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/actions/server/saved_objects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { SavedObjectsServiceSetup } from 'kibana/server';
import mappings from './mappings.json';
import { EncryptedSavedObjectsPluginSetup } from '../../../encrypted_saved_objects/server';

export { SavedObjectsClientWithoutUpdates } from './saved_objects_client_without_updates';

export const ACTION_SAVED_OBJECT_TYPE = 'action';
export const ACTION_TASK_PARAMS_SAVED_OBJECT_TYPE = 'action_task_params';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { SavedObjectsClientContract, SavedObjectsCreateOptions, SavedObject } from 'kibana/server';

type AlertSavedObjectsCreateOptions = Omit<SavedObjectsCreateOptions, 'id' | 'overwrite'>;
type AlertSavedObjectsUpdateOptions = Omit<SavedObjectsCreateOptions, 'id' | 'overwrite'> &
Pick<Required<SavedObjectsCreateOptions>, 'id' | 'overwrite'>;

export type SavedObjectsClientWithoutUpdates = Omit<
SavedObjectsClientContract,
'create' | 'update' | 'bulkUpdate'
> & {
create<T = unknown>(
type: string,
attributes: T,
options?: AlertSavedObjectsCreateOptions | AlertSavedObjectsUpdateOptions
): Promise<SavedObject<T>>;
};
gmmorris marked this conversation as resolved.
Show resolved Hide resolved
Loading