-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathhandlers.ts
107 lines (99 loc) · 3.65 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
/*
* 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 { RequestHandler } from 'src/core/server';
import { TypeOf } from '@kbn/config-schema';
import { outputService, appContextService } from '../../services';
import { GetFleetStatusResponse } from '../../../common';
import { setupIngestManager, setupFleet } from '../../services/setup';
import { PostFleetSetupRequestSchema } from '../../types';
import { IngestManagerError, getHTTPResponseCode } from '../../errors';
export const getFleetStatusHandler: RequestHandler = async (context, request, response) => {
const soClient = context.core.savedObjects.client;
try {
const isAdminUserSetup = (await outputService.getAdminUser(soClient)) !== null;
const isApiKeysEnabled = await appContextService.getSecurity().authc.areAPIKeysEnabled();
const isTLSEnabled = appContextService.getHttpSetup().getServerInfo().protocol === 'https';
const isProductionMode = appContextService.getIsProductionMode();
const isCloud = appContextService.getCloud()?.isCloudEnabled ?? false;
const isTLSCheckDisabled = appContextService.getConfig()?.fleet?.tlsCheckDisabled ?? false;
const missingRequirements: GetFleetStatusResponse['missing_requirements'] = [];
if (!isAdminUserSetup) {
missingRequirements.push('fleet_admin_user');
}
if (!isApiKeysEnabled) {
missingRequirements.push('api_keys');
}
if (!isTLSCheckDisabled && !isCloud && isProductionMode && !isTLSEnabled) {
missingRequirements.push('tls_required');
}
const body: GetFleetStatusResponse = {
isReady: missingRequirements.length === 0,
missing_requirements: missingRequirements,
};
return response.ok({
body,
});
} catch (e) {
return response.customError({
statusCode: 500,
body: { message: e.message },
});
}
};
export const createFleetSetupHandler: RequestHandler<
undefined,
undefined,
TypeOf<typeof PostFleetSetupRequestSchema.body>
> = async (context, request, response) => {
try {
const soClient = context.core.savedObjects.client;
const callCluster = context.core.elasticsearch.legacy.client.callAsCurrentUser;
await setupIngestManager(soClient, callCluster);
await setupFleet(soClient, callCluster, {
forceRecreate: request.body?.forceRecreate ?? false,
});
return response.ok({
body: { isInitialized: true },
});
} catch (e) {
return response.customError({
statusCode: 500,
body: { message: e.message },
});
}
};
export const ingestManagerSetupHandler: RequestHandler = async (context, request, response) => {
const soClient = context.core.savedObjects.client;
const callCluster = context.core.elasticsearch.legacy.client.callAsCurrentUser;
const logger = appContextService.getLogger();
try {
await setupIngestManager(soClient, callCluster);
return response.ok({
body: { isInitialized: true },
});
} catch (e) {
if (e instanceof IngestManagerError) {
logger.error(e.message);
return response.customError({
statusCode: getHTTPResponseCode(e),
body: { message: e.message },
});
}
if (e.isBoom) {
logger.error(e.output.payload.message);
return response.customError({
statusCode: e.output.statusCode,
body: { message: e.output.payload.message },
});
}
logger.error(e.message);
logger.error(e.stack);
return response.customError({
statusCode: 500,
body: { message: e.message },
});
}
};