Skip to content

Commit

Permalink
revert change in ESO and prohibit partial updates in alert and action…
Browse files Browse the repository at this point in the history
… clients
  • Loading branch information
gmmorris committed Aug 14, 2020
1 parent 1560549 commit 640c0b2
Show file tree
Hide file tree
Showing 13 changed files with 330 additions and 456 deletions.
22 changes: 12 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,18 +972,19 @@ 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 [],
},
]
Expand Down Expand Up @@ -1046,7 +1047,7 @@ describe('update()', () => {
},
references: [],
});
unsecuredSavedObjectsClient.update.mockResolvedValueOnce({
unsecuredSavedObjectsClient.create.mockResolvedValueOnce({
id: 'my-action',
type: 'action',
attributes: {
Expand Down Expand Up @@ -1084,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 @@ -1100,6 +1100,8 @@ describe('update()', () => {
"secrets": Object {},
},
Object {
"id": "my-action",
"overwrite": true,
"references": Array [],
},
]
Expand All @@ -1124,7 +1126,7 @@ describe('update()', () => {
},
references: [],
});
unsecuredSavedObjectsClient.update.mockResolvedValueOnce({
unsecuredSavedObjectsClient.create.mockResolvedValueOnce({
id: 'my-action',
type: 'action',
attributes: {
Expand Down
11 changes: 6 additions & 5 deletions x-pack/plugins/actions/server/actions_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import Boom from 'boom';
import {
ILegacyScopedClusterClient,
SavedObjectsClientContract,
SavedObjectAttributes,
SavedObject,
KibanaRequest,
Expand All @@ -31,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 @@ -55,7 +55,7 @@ interface ConstructorOptions {
defaultKibanaIndex: string;
scopedClusterClient: ILegacyScopedClusterClient;
actionTypeRegistry: ActionTypeRegistry;
unsecuredSavedObjectsClient: SavedObjectsClientContract;
unsecuredSavedObjectsClient: SavedObjectsClientWithoutUpdates;
preconfiguredActions: PreConfiguredAction[];
actionExecutor: ActionExecutorContract;
executionEnqueuer: ExecutionEnqueuer;
Expand All @@ -71,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 @@ -162,9 +162,8 @@ export class ActionsClient {

this.actionTypeRegistry.ensureActionTypeEnabled(actionTypeId);

const result = await this.unsecuredSavedObjectsClient.update<RawAction>(
const result = await this.unsecuredSavedObjectsClient.create<RawAction>(
'action',
id,
{
...attributes,
actionTypeId,
Expand All @@ -174,6 +173,8 @@ export class ActionsClient {
},
omitBy(
{
id,
overwrite: true,
references,
version,
},
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>>;
};
Loading

0 comments on commit 640c0b2

Please sign in to comment.