-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
plugin.ts
216 lines (181 loc) · 6.8 KB
/
plugin.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
/*
* 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 { combineLatest } from 'rxjs';
import { first } from 'rxjs/operators';
import {
ICustomClusterClient,
CoreSetup,
Logger,
PluginInitializerContext,
RecursiveReadonly,
} from '../../../../src/core/server';
import { deepFreeze } from '../../../../src/core/utils';
import { SpacesPluginSetup } from '../../spaces/server';
import { PluginSetupContract as FeaturesSetupContract } from '../../features/server';
import { LicensingPluginSetup } from '../../licensing/server';
import { Authentication, setupAuthentication } from './authentication';
import { Authorization, setupAuthorization } from './authorization';
import { createConfig$ } from './config';
import { defineRoutes } from './routes';
import { SecurityLicenseService, SecurityLicense } from '../common/licensing';
import { setupSavedObjects } from './saved_objects';
import { SecurityAuditLogger } from './audit';
import { elasticsearchClientPlugin } from './elasticsearch_client_plugin';
export type SpacesService = Pick<
SpacesPluginSetup['spacesService'],
'getSpaceId' | 'namespaceToSpaceId'
>;
export type FeaturesService = Pick<FeaturesSetupContract, 'getFeatures'>;
/**
* Describes a set of APIs that is available in the legacy platform only and required by this plugin
* to function properly.
*/
export interface LegacyAPI {
auditLogger: {
log: (eventType: string, message: string, data?: Record<string, unknown>) => void;
};
}
/**
* Describes public Security plugin contract returned at the `setup` stage.
*/
export interface SecurityPluginSetup {
authc: Authentication;
authz: Pick<Authorization, 'actions' | 'checkPrivilegesWithRequest' | 'mode'>;
/**
* If Spaces plugin is available it's supposed to register its SpacesService with Security plugin
* so that Security can get space ID from the URL or namespace. We can't declare optional dependency
* to Spaces since it'd result into circular dependency between these two plugins and circular
* dependencies aren't supported by the Core. In the future we have to get rid of this implicit
* dependency.
* @param service Spaces service exposed by the Spaces plugin.
*/
registerSpacesService: (service: SpacesService) => void;
__legacyCompat: {
registerLegacyAPI: (legacyAPI: LegacyAPI) => void;
registerPrivilegesWithCluster: () => void;
license: SecurityLicense;
config: RecursiveReadonly<{ secureCookies: boolean }>;
};
}
export interface PluginSetupDependencies {
features: FeaturesService;
licensing: LicensingPluginSetup;
}
/**
* Represents Security Plugin instance that will be managed by the Kibana plugin system.
*/
export class Plugin {
private readonly logger: Logger;
private clusterClient?: ICustomClusterClient;
private spacesService?: SpacesService | symbol = Symbol('not accessed');
private securityLicenseService?: SecurityLicenseService;
private legacyAPI?: LegacyAPI;
private readonly getLegacyAPI = () => {
if (!this.legacyAPI) {
throw new Error('Legacy API is not registered!');
}
return this.legacyAPI;
};
private readonly getSpacesService = () => {
// Changing property value from Symbol to undefined denotes the fact that property was accessed.
if (!this.wasSpacesServiceAccessed()) {
this.spacesService = undefined;
}
return this.spacesService as SpacesService | undefined;
};
constructor(private readonly initializerContext: PluginInitializerContext) {
this.logger = this.initializerContext.logger.get();
}
public async setup(core: CoreSetup, { features, licensing }: PluginSetupDependencies) {
const [config, legacyConfig] = await combineLatest([
createConfig$(this.initializerContext, core.http.isTlsEnabled),
this.initializerContext.config.legacy.globalConfig$,
])
.pipe(first())
.toPromise();
this.clusterClient = core.elasticsearch.createClient('security', {
plugins: [elasticsearchClientPlugin],
});
this.securityLicenseService = new SecurityLicenseService();
const { license } = this.securityLicenseService.setup({
license$: licensing.license$,
});
const authc = await setupAuthentication({
http: core.http,
clusterClient: this.clusterClient,
config,
license,
loggers: this.initializerContext.logger,
});
const authz = await setupAuthorization({
http: core.http,
clusterClient: this.clusterClient,
license,
loggers: this.initializerContext.logger,
kibanaIndexName: legacyConfig.kibana.index,
packageVersion: this.initializerContext.env.packageInfo.version,
getSpacesService: this.getSpacesService,
featuresService: features,
});
setupSavedObjects({
auditLogger: new SecurityAuditLogger(() => this.getLegacyAPI().auditLogger),
authz,
savedObjects: core.savedObjects,
});
core.capabilities.registerSwitcher(authz.disableUnauthorizedCapabilities);
defineRoutes({
router: core.http.createRouter(),
basePath: core.http.basePath,
logger: this.initializerContext.logger.get('routes'),
clusterClient: this.clusterClient,
config,
authc,
authz,
csp: core.http.csp,
license,
});
return deepFreeze<SecurityPluginSetup>({
authc,
authz: {
actions: authz.actions,
checkPrivilegesWithRequest: authz.checkPrivilegesWithRequest,
mode: authz.mode,
},
registerSpacesService: service => {
if (this.wasSpacesServiceAccessed()) {
throw new Error('Spaces service has been accessed before registration.');
}
this.spacesService = service;
},
__legacyCompat: {
registerLegacyAPI: (legacyAPI: LegacyAPI) => (this.legacyAPI = legacyAPI),
registerPrivilegesWithCluster: async () => await authz.registerPrivilegesWithCluster(),
license,
// We should stop exposing this config as soon as only new platform plugin consumes it.
// This is only currently required because we use legacy code to inject this as metadata
// for consumption by public code in the new platform.
config: { secureCookies: config.secureCookies },
},
});
}
public start() {
this.logger.debug('Starting plugin');
}
public stop() {
this.logger.debug('Stopping plugin');
if (this.clusterClient) {
this.clusterClient.close();
this.clusterClient = undefined;
}
if (this.securityLicenseService) {
this.securityLicenseService.stop();
this.securityLicenseService = undefined;
}
}
private wasSpacesServiceAccessed() {
return typeof this.spacesService !== 'symbol';
}
}