-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathplugin.tsx
340 lines (300 loc) · 11.1 KB
/
plugin.tsx
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
* 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 React, { FC } from 'react';
import type {
CoreSetup,
CoreStart,
Plugin,
PluginInitializerContext,
HttpStart,
IBasePath,
AnalyticsServiceSetup,
} from '@kbn/core/public';
import { i18n } from '@kbn/i18n';
import useObservable from 'react-use/lib/useObservable';
import { BehaviorSubject, catchError, from, map, of } from 'rxjs';
import type { SecurityPluginSetup, SecurityPluginStart } from '@kbn/security-plugin/public';
import { HomePublicPluginSetup } from '@kbn/home-plugin/public';
import { Sha256 } from '@kbn/crypto-browser';
import { registerCloudDeploymentIdAnalyticsContext } from '../common/register_cloud_deployment_id_analytics_context';
import { getIsCloudEnabled } from '../common/is_cloud_enabled';
import {
ELASTIC_SUPPORT_LINK,
CLOUD_SNAPSHOTS_PATH,
GET_CHAT_USER_DATA_ROUTE_PATH,
} from '../common/constants';
import type { GetChatUserDataResponseBody } from '../common/types';
import { createUserMenuLinks } from './user_menu_links';
import { getFullCloudUrl } from './utils';
import { ChatConfig, ServicesProvider } from './services';
export interface CloudConfigType {
id?: string;
cname?: string;
base_url?: string;
profile_url?: string;
deployment_url?: string;
organization_url?: string;
full_story: {
enabled: boolean;
org_id?: string;
eventTypesAllowlist?: string[];
};
/** Configuration to enable live chat in Cloud-enabled instances of Kibana. */
chat: {
/** Determines if chat is enabled. */
enabled: boolean;
/** The URL to the remotely-hosted chat application. */
chatURL: string;
};
}
interface CloudSetupDependencies {
home?: HomePublicPluginSetup;
security?: Pick<SecurityPluginSetup, 'authc'>;
}
interface CloudStartDependencies {
security?: SecurityPluginStart;
}
export interface CloudStart {
/**
* A React component that provides a pre-wired `React.Context` which connects components to Cloud services.
*/
CloudContextProvider: FC<{}>;
}
export interface CloudSetup {
cloudId?: string;
cname?: string;
baseUrl?: string;
deploymentUrl?: string;
profileUrl?: string;
organizationUrl?: string;
snapshotsUrl?: string;
isCloudEnabled: boolean;
}
interface SetupFullStoryDeps {
analytics: AnalyticsServiceSetup;
basePath: IBasePath;
}
interface SetupChatDeps extends Pick<CloudSetupDependencies, 'security'> {
http: CoreSetup['http'];
}
export class CloudPlugin implements Plugin<CloudSetup> {
private readonly config: CloudConfigType;
private isCloudEnabled: boolean;
private chatConfig$ = new BehaviorSubject<ChatConfig>({ enabled: false });
constructor(private readonly initializerContext: PluginInitializerContext) {
this.config = this.initializerContext.config.get<CloudConfigType>();
this.isCloudEnabled = false;
}
public setup(core: CoreSetup, { home, security }: CloudSetupDependencies) {
this.setupTelemetryContext(core.analytics, security, this.config.id);
this.setupFullStory({ analytics: core.analytics, basePath: core.http.basePath }).catch((e) =>
// eslint-disable-next-line no-console
console.debug(`Error setting up FullStory: ${e.toString()}`)
);
const {
id,
cname,
profile_url: profileUrl,
organization_url: organizationUrl,
deployment_url: deploymentUrl,
base_url: baseUrl,
} = this.config;
this.isCloudEnabled = getIsCloudEnabled(id);
this.setupChat({ http: core.http, security }).catch((e) =>
// eslint-disable-next-line no-console
console.debug(`Error setting up Chat: ${e.toString()}`)
);
if (home) {
home.environment.update({ cloud: this.isCloudEnabled });
if (this.isCloudEnabled) {
home.tutorials.setVariable('cloud', { id, baseUrl, profileUrl, deploymentUrl });
}
}
const fullCloudDeploymentUrl = getFullCloudUrl(baseUrl, deploymentUrl);
const fullCloudProfileUrl = getFullCloudUrl(baseUrl, profileUrl);
const fullCloudOrganizationUrl = getFullCloudUrl(baseUrl, organizationUrl);
const fullCloudSnapshotsUrl = `${fullCloudDeploymentUrl}/${CLOUD_SNAPSHOTS_PATH}`;
return {
cloudId: id,
cname,
baseUrl,
deploymentUrl: fullCloudDeploymentUrl,
profileUrl: fullCloudProfileUrl,
organizationUrl: fullCloudOrganizationUrl,
snapshotsUrl: fullCloudSnapshotsUrl,
isCloudEnabled: this.isCloudEnabled,
};
}
public start(coreStart: CoreStart, { security }: CloudStartDependencies): CloudStart {
const { deployment_url: deploymentUrl, base_url: baseUrl } = this.config;
coreStart.chrome.setHelpSupportUrl(ELASTIC_SUPPORT_LINK);
const setLinks = (authorized: boolean) => {
if (!authorized) return;
if (baseUrl && deploymentUrl) {
coreStart.chrome.setCustomNavLink({
title: i18n.translate('xpack.cloud.deploymentLinkLabel', {
defaultMessage: 'Manage this deployment',
}),
euiIconType: 'logoCloud',
href: getFullCloudUrl(baseUrl, deploymentUrl),
});
}
if (security && this.isCloudEnabled) {
const userMenuLinks = createUserMenuLinks(this.config);
security.navControlService.addUserMenuLinks(userMenuLinks);
}
};
this.checkIfAuthorizedForLinks({ http: coreStart.http, security })
.then(setLinks)
// In the event of an unexpected error, fail *open*.
// Cloud admin console will always perform the actual authorization checks.
.catch(() => setLinks(true));
// There's a risk that the request for chat config will take too much time to complete, and the provider
// will maintain a stale value. To avoid this, we'll use an Observable.
const CloudContextProvider: FC = ({ children }) => {
const chatConfig = useObservable(this.chatConfig$, { enabled: false });
return <ServicesProvider chat={chatConfig}>{children}</ServicesProvider>;
};
return {
CloudContextProvider,
};
}
public stop() {}
/**
* Determines if the current user should see links back to Cloud.
* This isn't a true authorization check, but rather a heuristic to
* see if the current user is *likely* a cloud deployment administrator.
*
* At this point, we do not have enough information to reliably make this determination,
* but we do know that all cloud deployment admins are superusers by default.
*/
private async checkIfAuthorizedForLinks({
http,
security,
}: {
http: HttpStart;
security?: SecurityPluginStart;
}) {
if (http.anonymousPaths.isAnonymous(window.location.pathname)) {
return false;
}
// Security plugin is disabled
if (!security) return true;
// Otherwise check roles. If user is not defined due to an unexpected error, then fail *open*.
// Cloud admin console will always perform the actual authorization checks.
const user = await security.authc.getCurrentUser().catch(() => null);
return user?.roles.includes('superuser') ?? true;
}
/**
* If the right config is provided, register the FullStory shipper to the analytics client.
* @param analytics Core's Analytics service's setup contract.
* @param basePath Core's http.basePath helper.
* @private
*/
private async setupFullStory({ analytics, basePath }: SetupFullStoryDeps) {
const { enabled, org_id: fullStoryOrgId, eventTypesAllowlist } = this.config.full_story;
if (!enabled || !fullStoryOrgId) {
return; // do not load any FullStory code in the browser if not enabled
}
// Keep this import async so that we do not load any FullStory code into the browser when it is disabled.
const { FullStoryShipper } = await import('@kbn/analytics-shippers-fullstory');
analytics.registerShipper(FullStoryShipper, {
eventTypesAllowlist,
fullStoryOrgId,
// Load an Elastic-internally audited script. Ideally, it should be hosted on a CDN.
scriptUrl: basePath.prepend(
`/internal/cloud/${this.initializerContext.env.packageInfo.buildNum}/fullstory.js`
),
namespace: 'FSKibana',
});
}
/**
* Set up the Analytics context providers.
* @param analytics Core's Analytics service. The Setup contract.
* @param security The security plugin.
* @param cloudId The Cloud Org ID.
* @private
*/
private setupTelemetryContext(
analytics: AnalyticsServiceSetup,
security?: Pick<SecurityPluginSetup, 'authc'>,
cloudId?: string
) {
registerCloudDeploymentIdAnalyticsContext(analytics, cloudId);
if (security) {
analytics.registerContextProvider({
name: 'cloud_user_id',
context$: from(security.authc.getCurrentUser()).pipe(
map((user) => {
if (user.elastic_cloud_user) {
// If the user is managed by ESS, use the plain username as the user ID:
// The username is expected to be unique for these users,
// and it matches how users are identified in the Cloud UI, so it allows us to correlate them.
return { userId: user.username, isElasticCloudUser: true };
}
return {
// For the rest of the authentication providers, we want to add the cloud deployment ID to make it unique.
// Especially in the case of Elasticsearch-backed authentication, where users are commonly repeated
// across multiple deployments (i.e.: `elastic` superuser).
userId: cloudId ? `${cloudId}:${user.username}` : user.username,
isElasticCloudUser: false,
};
}),
// The hashing here is to keep it at clear as possible in our source code that we do not send literal user IDs
map(({ userId, isElasticCloudUser }) => ({ userId: sha256(userId), isElasticCloudUser })),
catchError(() => of({ userId: undefined, isElasticCloudUser: false }))
),
schema: {
userId: {
type: 'keyword',
_meta: { description: 'The user id scoped as seen by Cloud (hashed)' },
},
isElasticCloudUser: {
type: 'boolean',
_meta: {
description: '`true` if the user is managed by ESS.',
},
},
},
});
}
}
private async setupChat({ http, security }: SetupChatDeps) {
if (!this.isCloudEnabled) {
return;
}
const { enabled, chatURL } = this.config.chat;
if (!security || !enabled || !chatURL) {
return;
}
try {
const {
email,
id,
token: jwt,
} = await http.get<GetChatUserDataResponseBody>(GET_CHAT_USER_DATA_ROUTE_PATH);
if (!email || !id || !jwt) {
return;
}
this.chatConfig$.next({
enabled,
chatURL,
user: {
email,
id,
jwt,
},
});
} catch (e) {
// eslint-disable-next-line no-console
console.debug(`[cloud.chat] Could not retrieve chat config: ${e.res.status} ${e.message}`, e);
}
}
}
function sha256(str: string) {
return new Sha256().update(str, 'utf8').digest('hex');
}