-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
execute.ts
282 lines (250 loc) · 9.17 KB
/
execute.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
* 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 expect from '@kbn/expect';
import { Spaces } from '../../scenarios';
import {
ESTestIndexTool,
ES_TEST_INDEX_NAME,
getUrlPrefix,
ObjectRemover,
getEventLog,
} from '../../../common/lib';
import { FtrProviderContext } from '../../../common/ftr_provider_context';
import { IValidatedEvent } from '../../../../../plugins/event_log/server';
const NANOS_IN_MILLIS = 1000 * 1000;
// eslint-disable-next-line import/no-default-export
export default function ({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const es = getService('legacyEs');
const retry = getService('retry');
const esTestIndexTool = new ESTestIndexTool(es, retry);
const authorizationIndex = '.kibana-test-authorization';
describe('execute', () => {
const objectRemover = new ObjectRemover(supertest);
before(async () => {
await esTestIndexTool.destroy();
await esTestIndexTool.setup();
await es.indices.create({ index: authorizationIndex });
});
after(async () => {
await esTestIndexTool.destroy();
await es.indices.delete({ index: authorizationIndex });
await objectRemover.removeAll();
});
it('should handle execute request appropriately', async () => {
const { body: createdAction } = await supertest
.post(`${getUrlPrefix(Spaces.space1.id)}/api/actions/action`)
.set('kbn-xsrf', 'foo')
.send({
name: 'My action',
actionTypeId: 'test.index-record',
config: {
unencrypted: `This value shouldn't get encrypted`,
},
secrets: {
encrypted: 'This value should be encrypted',
},
})
.expect(200);
objectRemover.add(Spaces.space1.id, createdAction.id, 'action', 'actions');
const reference = `actions-execute-1:${Spaces.space1.id}`;
const response = await supertest
.post(`${getUrlPrefix(Spaces.space1.id)}/api/actions/action/${createdAction.id}/_execute`)
.set('kbn-xsrf', 'foo')
.send({
params: {
reference,
index: ES_TEST_INDEX_NAME,
message: 'Testing 123',
},
});
expect(response.status).to.eql(200);
expect(response.body).to.be.an('object');
const searchResult = await esTestIndexTool.search('action:test.index-record', reference);
expect(searchResult.hits.total.value).to.eql(1);
const indexedRecord = searchResult.hits.hits[0];
expect(indexedRecord._source).to.eql({
params: {
reference,
index: ES_TEST_INDEX_NAME,
message: 'Testing 123',
},
config: {
unencrypted: `This value shouldn't get encrypted`,
},
secrets: {
encrypted: 'This value should be encrypted',
},
reference,
source: 'action:test.index-record',
});
await validateEventLog({
spaceId: Spaces.space1.id,
actionId: createdAction.id,
outcome: 'success',
message: `action executed: test.index-record:${createdAction.id}: My action`,
});
});
it('should handle failed executions', async () => {
const { body: createdAction } = await supertest
.post(`${getUrlPrefix(Spaces.space1.id)}/api/actions/action`)
.set('kbn-xsrf', 'foo')
.send({
name: 'failing action',
actionTypeId: 'test.failing',
})
.expect(200);
objectRemover.add(Spaces.space1.id, createdAction.id, 'action', 'actions');
const reference = `actions-failure-1:${Spaces.space1.id}`;
const response = await supertest
.post(`${getUrlPrefix(Spaces.space1.id)}/api/actions/action/${createdAction.id}/_execute`)
.set('kbn-xsrf', 'foo')
.send({
params: {
reference,
index: ES_TEST_INDEX_NAME,
},
});
expect(response.status).to.eql(200);
expect(response.body).to.eql({
actionId: createdAction.id,
status: 'error',
message: 'an error occurred while running the action executor',
serviceMessage: `expected failure for ${ES_TEST_INDEX_NAME} ${reference}`,
retry: false,
});
await validateEventLog({
spaceId: Spaces.space1.id,
actionId: createdAction.id,
outcome: 'failure',
message: `action execution failure: test.failing:${createdAction.id}: failing action`,
errorMessage: `an error occurred while running the action executor: expected failure for .kibana-alerting-test-data actions-failure-1:space1`,
});
});
it(`shouldn't execute an action from another space`, async () => {
const { body: createdAction } = await supertest
.post(`${getUrlPrefix(Spaces.space1.id)}/api/actions/action`)
.set('kbn-xsrf', 'foo')
.send({
name: 'My action',
actionTypeId: 'test.index-record',
config: {
unencrypted: `This value shouldn't get encrypted`,
},
secrets: {
encrypted: 'This value should be encrypted',
},
})
.expect(200);
objectRemover.add(Spaces.space1.id, createdAction.id, 'action', 'actions');
const reference = `actions-execute-2:${Spaces.space1.id}`;
await supertest
.post(`${getUrlPrefix(Spaces.other.id)}/api/actions/action/${createdAction.id}/_execute`)
.set('kbn-xsrf', 'foo')
.send({
params: {
reference,
index: ES_TEST_INDEX_NAME,
message: 'Testing 123',
},
})
.expect(404, {
statusCode: 404,
error: 'Not Found',
message: `Saved object [action/${createdAction.id}] not found`,
});
});
it('should handle execute request appropriately and have proper callCluster and savedObjectsClient authorization', async () => {
const reference = `actions-execute-3:${Spaces.space1.id}`;
const { body: createdAction } = await supertest
.post(`${getUrlPrefix(Spaces.space1.id)}/api/actions/action`)
.set('kbn-xsrf', 'foo')
.send({
name: 'My action',
actionTypeId: 'test.authorization',
})
.expect(200);
objectRemover.add(Spaces.space1.id, createdAction.id, 'action', 'actions');
const response = await supertest
.post(`${getUrlPrefix(Spaces.space1.id)}/api/actions/action/${createdAction.id}/_execute`)
.set('kbn-xsrf', 'foo')
.send({
params: {
callClusterAuthorizationIndex: authorizationIndex,
savedObjectsClientType: 'dashboard',
savedObjectsClientId: '1',
index: ES_TEST_INDEX_NAME,
reference,
},
});
expect(response.status).to.eql(200);
const searchResult = await esTestIndexTool.search('action:test.authorization', reference);
expect(searchResult.hits.total.value).to.eql(1);
const indexedRecord = searchResult.hits.hits[0];
expect(indexedRecord._source.state).to.eql({
callClusterSuccess: true,
callScopedClusterSuccess: true,
savedObjectsClientSuccess: false,
savedObjectsClientError: {
...indexedRecord._source.state.savedObjectsClientError,
output: {
...indexedRecord._source.state.savedObjectsClientError.output,
statusCode: 404,
},
},
});
});
});
interface ValidateEventLogParams {
spaceId: string;
actionId: string;
outcome: string;
message: string;
errorMessage?: string;
}
async function validateEventLog(params: ValidateEventLogParams): Promise<void> {
const { spaceId, actionId, outcome, message, errorMessage } = params;
const events: IValidatedEvent[] = await retry.try(async () => {
return await getEventLog({
getService,
spaceId,
type: 'action',
id: actionId,
provider: 'actions',
actions: ['execute'],
});
});
expect(events.length).to.equal(1);
const event = events[0];
const duration = event?.event?.duration;
const eventStart = Date.parse(event?.event?.start || 'undefined');
const eventEnd = Date.parse(event?.event?.end || 'undefined');
const dateNow = Date.now();
expect(typeof duration).to.be('number');
expect(eventStart).to.be.ok();
expect(eventEnd).to.be.ok();
const durationDiff = Math.abs(
Math.round(duration! / NANOS_IN_MILLIS) - (eventEnd - eventStart)
);
// account for rounding errors
expect(durationDiff < 1).to.equal(true);
expect(eventStart <= eventEnd).to.equal(true);
expect(eventEnd <= dateNow).to.equal(true);
expect(event?.event?.outcome).to.equal(outcome);
expect(event?.kibana?.saved_objects).to.eql([
{
rel: 'primary',
type: 'action',
id: actionId,
namespace: 'space1',
},
]);
expect(event?.message).to.eql(message);
if (errorMessage) {
expect(event?.error?.message).to.eql(errorMessage);
}
}
}