-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
handlers.ts
281 lines (264 loc) · 8.69 KB
/
handlers.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
/*
* 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 { TypeOf } from '@kbn/config-schema';
import { RequestHandler, ResponseHeaders } from 'src/core/server';
import bluebird from 'bluebird';
import { fullAgentPolicyToYaml } from '../../../common/services';
import { appContextService, agentPolicyService, packagePolicyService } from '../../services';
import { listAgents } from '../../services/agents';
import { AGENT_SAVED_OBJECT_TYPE } from '../../constants';
import {
GetAgentPoliciesRequestSchema,
GetOneAgentPolicyRequestSchema,
CreateAgentPolicyRequestSchema,
UpdateAgentPolicyRequestSchema,
CopyAgentPolicyRequestSchema,
DeleteAgentPolicyRequestSchema,
GetFullAgentPolicyRequestSchema,
AgentPolicy,
NewPackagePolicy,
} from '../../types';
import {
GetAgentPoliciesResponse,
GetAgentPoliciesResponseItem,
GetOneAgentPolicyResponse,
CreateAgentPolicyResponse,
UpdateAgentPolicyResponse,
CopyAgentPolicyResponse,
DeleteAgentPolicyResponse,
GetFullAgentPolicyResponse,
defaultPackages,
} from '../../../common';
import { defaultIngestErrorHandler } from '../../errors';
export const getAgentPoliciesHandler: RequestHandler<
undefined,
TypeOf<typeof GetAgentPoliciesRequestSchema.query>
> = async (context, request, response) => {
const soClient = context.core.savedObjects.client;
const { full: withPackagePolicies = false, ...restOfQuery } = request.query;
try {
const { items, total, page, perPage } = await agentPolicyService.list(soClient, {
withPackagePolicies,
...restOfQuery,
});
const body: GetAgentPoliciesResponse = {
items,
total,
page,
perPage,
};
await bluebird.map(
items,
(agentPolicy: GetAgentPoliciesResponseItem) =>
listAgents(soClient, {
showInactive: false,
perPage: 0,
page: 1,
kuery: `${AGENT_SAVED_OBJECT_TYPE}.policy_id:${agentPolicy.id}`,
}).then(({ total: agentTotal }) => (agentPolicy.agents = agentTotal)),
{ concurrency: 10 }
);
return response.ok({ body });
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};
export const getOneAgentPolicyHandler: RequestHandler<
TypeOf<typeof GetOneAgentPolicyRequestSchema.params>
> = async (context, request, response) => {
const soClient = context.core.savedObjects.client;
try {
const agentPolicy = await agentPolicyService.get(soClient, request.params.agentPolicyId);
if (agentPolicy) {
const body: GetOneAgentPolicyResponse = {
item: agentPolicy,
};
return response.ok({
body,
});
} else {
return response.customError({
statusCode: 404,
body: { message: 'Agent policy not found' },
});
}
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};
export const createAgentPolicyHandler: RequestHandler<
undefined,
TypeOf<typeof CreateAgentPolicyRequestSchema.query>,
TypeOf<typeof CreateAgentPolicyRequestSchema.body>
> = async (context, request, response) => {
const soClient = context.core.savedObjects.client;
const callCluster = context.core.elasticsearch.legacy.client.callAsCurrentUser;
const user = (await appContextService.getSecurity()?.authc.getCurrentUser(request)) || undefined;
const withSysMonitoring = request.query.sys_monitoring ?? false;
try {
// eslint-disable-next-line prefer-const
let [agentPolicy, newSysPackagePolicy] = await Promise.all<
AgentPolicy,
NewPackagePolicy | undefined
>([
agentPolicyService.create(soClient, request.body, {
user,
}),
// If needed, retrieve System package information and build a new package policy for the system package
// NOTE: we ignore failures in attempting to create package policy, since agent policy might have been created
// successfully
withSysMonitoring
? packagePolicyService
.buildPackagePolicyFromPackage(soClient, defaultPackages.System)
.catch(() => undefined)
: undefined,
]);
// Create the system monitoring package policy and add it to agent policy.
if (withSysMonitoring && newSysPackagePolicy !== undefined && agentPolicy !== undefined) {
newSysPackagePolicy.policy_id = agentPolicy.id;
newSysPackagePolicy.namespace = agentPolicy.namespace;
await packagePolicyService.create(soClient, callCluster, newSysPackagePolicy, {
user,
bumpRevision: false,
});
}
await agentPolicyService.createFleetPolicyChangeAction(soClient, agentPolicy.id);
const body: CreateAgentPolicyResponse = {
item: agentPolicy,
};
return response.ok({
body,
});
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};
export const updateAgentPolicyHandler: RequestHandler<
TypeOf<typeof UpdateAgentPolicyRequestSchema.params>,
unknown,
TypeOf<typeof UpdateAgentPolicyRequestSchema.body>
> = async (context, request, response) => {
const soClient = context.core.savedObjects.client;
const user = await appContextService.getSecurity()?.authc.getCurrentUser(request);
try {
const agentPolicy = await agentPolicyService.update(
soClient,
request.params.agentPolicyId,
request.body,
{
user: user || undefined,
}
);
const body: UpdateAgentPolicyResponse = { item: agentPolicy };
return response.ok({
body,
});
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};
export const copyAgentPolicyHandler: RequestHandler<
TypeOf<typeof CopyAgentPolicyRequestSchema.params>,
unknown,
TypeOf<typeof CopyAgentPolicyRequestSchema.body>
> = async (context, request, response) => {
const soClient = context.core.savedObjects.client;
const user = await appContextService.getSecurity()?.authc.getCurrentUser(request);
try {
const agentPolicy = await agentPolicyService.copy(
soClient,
request.params.agentPolicyId,
request.body,
{
user: user || undefined,
}
);
const body: CopyAgentPolicyResponse = { item: agentPolicy };
return response.ok({
body,
});
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};
export const deleteAgentPoliciesHandler: RequestHandler<
unknown,
unknown,
TypeOf<typeof DeleteAgentPolicyRequestSchema.body>
> = async (context, request, response) => {
const soClient = context.core.savedObjects.client;
try {
const body: DeleteAgentPolicyResponse = await agentPolicyService.delete(
soClient,
request.body.agentPolicyId
);
return response.ok({
body,
});
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};
export const getFullAgentPolicy: RequestHandler<
TypeOf<typeof GetFullAgentPolicyRequestSchema.params>,
TypeOf<typeof GetFullAgentPolicyRequestSchema.query>
> = async (context, request, response) => {
const soClient = context.core.savedObjects.client;
try {
const fullAgentPolicy = await agentPolicyService.getFullAgentPolicy(
soClient,
request.params.agentPolicyId,
{ standalone: request.query.standalone === true }
);
if (fullAgentPolicy) {
const body: GetFullAgentPolicyResponse = {
item: fullAgentPolicy,
};
return response.ok({
body,
});
} else {
return response.customError({
statusCode: 404,
body: { message: 'Agent policy not found' },
});
}
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};
export const downloadFullAgentPolicy: RequestHandler<
TypeOf<typeof GetFullAgentPolicyRequestSchema.params>,
TypeOf<typeof GetFullAgentPolicyRequestSchema.query>
> = async (context, request, response) => {
const soClient = context.core.savedObjects.client;
const {
params: { agentPolicyId },
} = request;
try {
const fullAgentPolicy = await agentPolicyService.getFullAgentPolicy(soClient, agentPolicyId, {
standalone: request.query.standalone === true,
});
if (fullAgentPolicy) {
const body = fullAgentPolicyToYaml(fullAgentPolicy);
const headers: ResponseHeaders = {
'content-type': 'text/x-yaml',
'content-disposition': `attachment; filename="elastic-agent.yml"`,
};
return response.ok({
body,
headers,
});
} else {
return response.customError({
statusCode: 404,
body: { message: 'Agent policy not found' },
});
}
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};