-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
route.ts
144 lines (126 loc) · 4.59 KB
/
route.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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import * as t from 'io-ts';
import { createObservabilityOnboardingServerRoute } from '../create_observability_onboarding_server_route';
import { getFallbackESUrl } from '../../lib/get_fallback_urls';
import { getKibanaUrl } from '../../lib/get_fallback_urls';
import { getAgentVersion } from '../../lib/get_agent_version';
import { saveObservabilityOnboardingFlow } from '../../lib/state';
import { createShipperApiKey } from '../../lib/api_key/create_shipper_api_key';
import { ObservabilityOnboardingFlow } from '../../saved_objects/observability_onboarding_status';
import { hasLogMonitoringPrivileges } from '../../lib/api_key/has_log_monitoring_privileges';
const logMonitoringPrivilegesRoute = createObservabilityOnboardingServerRoute({
endpoint: 'GET /internal/observability_onboarding/logs/setup/privileges',
options: { tags: [] },
handler: async (resources): Promise<{ hasPrivileges: boolean }> => {
const { context } = resources;
const {
elasticsearch: { client },
} = await context.core;
const hasPrivileges = await hasLogMonitoringPrivileges(client.asCurrentUser);
return { hasPrivileges };
},
});
const installShipperSetupRoute = createObservabilityOnboardingServerRoute({
endpoint: 'GET /internal/observability_onboarding/logs/setup/environment',
options: { tags: [] },
async handler(resources): Promise<{
apiEndpoint: string;
scriptDownloadUrl: string;
elasticAgentVersion: string;
elasticsearchUrl: string[];
}> {
const {
core,
plugins,
kibanaVersion,
services: { esLegacyConfigService },
} = resources;
const fleetPluginStart = await plugins.fleet.start();
const elasticAgentVersion = await getAgentVersion(fleetPluginStart, kibanaVersion);
const kibanaUrl = getKibanaUrl(core.setup, plugins.cloud?.setup);
const scriptDownloadUrl = new URL(
core.setup.http.staticAssets.getPluginAssetHref('standalone_agent_setup.sh'),
kibanaUrl
).toString();
const apiEndpoint = new URL(`${kibanaUrl}/internal/observability_onboarding`).toString();
const elasticsearchUrl = plugins.cloud?.setup?.elasticsearchUrl
? [plugins.cloud?.setup?.elasticsearchUrl]
: await getFallbackESUrl(esLegacyConfigService);
return {
apiEndpoint,
elasticsearchUrl,
scriptDownloadUrl,
elasticAgentVersion,
};
},
});
const createAPIKeyRoute = createObservabilityOnboardingServerRoute({
endpoint: 'POST /internal/observability_onboarding/otel/api_key',
options: { tags: [] },
params: t.type({}),
async handler(resources): Promise<{ apiKeyEncoded: string }> {
const { context } = resources;
const {
elasticsearch: { client },
} = await context.core;
const { encoded: apiKeyEncoded } = await createShipperApiKey(client.asCurrentUser, 'otel logs');
return { apiKeyEncoded };
},
});
const createFlowRoute = createObservabilityOnboardingServerRoute({
endpoint: 'POST /internal/observability_onboarding/logs/flow',
options: { tags: [] },
params: t.type({
body: t.intersection([
t.type({
name: t.string,
}),
t.type({
type: t.union([t.literal('logFiles'), t.literal('systemLogs')]),
}),
t.partial({
state: t.record(t.string, t.unknown),
}),
]),
}),
async handler(resources): Promise<{ apiKeyEncoded: string; onboardingId: string }> {
const {
context,
params: {
body: { name, type, state },
},
core,
request,
} = resources;
const coreStart = await core.start();
const {
elasticsearch: { client },
} = await context.core;
const { encoded: apiKeyEncoded } = await createShipperApiKey(
client.asCurrentUser,
`standalone_agent_logs_onboarding_${name}`
);
const generatedState = type === 'systemLogs' ? { namespace: 'default' } : state;
const savedObjectsClient = coreStart.savedObjects.getScopedClient(request);
const { id } = await saveObservabilityOnboardingFlow({
savedObjectsClient,
observabilityOnboardingState: {
type,
state: generatedState as ObservabilityOnboardingFlow['state'],
progress: {},
},
});
return { apiKeyEncoded, onboardingId: id };
},
});
export const logsOnboardingRouteRepository = {
...logMonitoringPrivilegesRoute,
...installShipperSetupRoute,
...createFlowRoute,
...createAPIKeyRoute,
};