/
find_legacy_plugin_specs.ts
198 lines (175 loc) · 5.69 KB
/
find_legacy_plugin_specs.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
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { Observable, merge, forkJoin } from 'rxjs';
import { toArray, tap, distinct, map } from 'rxjs/operators';
import {
findPluginSpecs,
defaultConfig,
// @ts-ignore
} from '../../../../legacy/plugin_discovery/find_plugin_specs.js';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { collectUiExports as collectLegacyUiExports } from '../../../../legacy/ui/ui_exports/collect_ui_exports';
import { LoggerFactory } from '../../logging';
import { PackageInfo } from '../../config';
import {
LegacyUiExports,
LegacyNavLink,
LegacyPluginSpec,
LegacyPluginPack,
LegacyConfig,
} from '../types';
const REMOVE_FROM_ARRAY: LegacyNavLink[] = [];
function getUiAppsNavLinks({ uiAppSpecs = [] }: LegacyUiExports, pluginSpecs: LegacyPluginSpec[]) {
return uiAppSpecs.flatMap(spec => {
if (!spec) {
return REMOVE_FROM_ARRAY;
}
const id = spec.pluginId || spec.id;
if (!id) {
throw new Error('Every app must specify an id');
}
if (spec.pluginId && !pluginSpecs.some(plugin => plugin.getId() === spec.pluginId)) {
throw new Error(`Unknown plugin id "${spec.pluginId}"`);
}
const listed = typeof spec.listed === 'boolean' ? spec.listed : true;
if (spec.hidden || !listed) {
return REMOVE_FROM_ARRAY;
}
return {
id,
title: spec.title,
order: typeof spec.order === 'number' ? spec.order : 0,
icon: spec.icon,
euiIconType: spec.euiIconType,
url: spec.url || `/app/${id}`,
linkToLastSubUrl: spec.linkToLastSubUrl,
};
});
}
function getNavLinks(uiExports: LegacyUiExports, pluginSpecs: LegacyPluginSpec[]) {
return (uiExports.navLinkSpecs || [])
.map<LegacyNavLink>(spec => ({
id: spec.id,
title: spec.title,
order: typeof spec.order === 'number' ? spec.order : 0,
url: spec.url,
subUrlBase: spec.subUrlBase || spec.url,
icon: spec.icon,
euiIconType: spec.euiIconType,
linkToLastSub: 'linkToLastSubUrl' in spec ? spec.linkToLastSubUrl : false,
hidden: 'hidden' in spec ? spec.hidden : false,
disabled: 'disabled' in spec ? spec.disabled : false,
tooltip: spec.tooltip || '',
}))
.concat(getUiAppsNavLinks(uiExports, pluginSpecs))
.sort((a, b) => a.order - b.order);
}
export async function findLegacyPluginSpecs(
settings: unknown,
loggerFactory: LoggerFactory,
packageInfo: PackageInfo
) {
const configToMutate: LegacyConfig = defaultConfig(settings);
const {
pack$,
invalidDirectoryError$,
invalidPackError$,
otherError$,
deprecation$,
invalidVersionSpec$,
spec$,
disabledSpec$,
}: {
pack$: Observable<LegacyPluginPack>;
invalidDirectoryError$: Observable<{ path: string }>;
invalidPackError$: Observable<{ path: string }>;
otherError$: Observable<unknown>;
deprecation$: Observable<{ spec: LegacyPluginSpec; message: string }>;
invalidVersionSpec$: Observable<LegacyPluginSpec>;
spec$: Observable<LegacyPluginSpec>;
disabledSpec$: Observable<LegacyPluginSpec>;
} = findPluginSpecs(settings, configToMutate) as any;
const logger = loggerFactory.get('legacy-plugins');
const log$ = merge(
pack$.pipe(
tap(definition => {
const path = definition.getPath();
logger.debug(`Found plugin at ${path}`, { path });
})
),
invalidDirectoryError$.pipe(
tap(error => {
logger.warn(`Unable to scan directory for plugins "${error.path}"`, {
err: error,
dir: error.path,
});
})
),
invalidPackError$.pipe(
tap(error => {
logger.warn(`Skipping non-plugin directory at ${error.path}`, {
path: error.path,
});
})
),
otherError$.pipe(
tap(error => {
// rethrow unhandled errors, which will fail the server
throw error;
})
),
invalidVersionSpec$.pipe(
map(spec => {
const name = spec.getId();
const pluginVersion = spec.getExpectedKibanaVersion();
const kibanaVersion = packageInfo.version;
return `Plugin "${name}" was disabled because it expected Kibana version "${pluginVersion}", and found "${kibanaVersion}".`;
}),
distinct(),
tap(message => {
logger.warn(message);
})
),
deprecation$.pipe(
tap(({ spec, message }) => {
const deprecationLogger = loggerFactory.get(
'plugins',
spec.getConfigPrefix(),
'config',
'deprecation'
);
deprecationLogger.warn(message);
})
)
);
const [disabledPluginSpecs, pluginSpecs] = await forkJoin(
disabledSpec$.pipe(toArray()),
spec$.pipe(toArray()),
log$.pipe(toArray())
).toPromise();
const uiExports = collectLegacyUiExports(pluginSpecs);
const navLinks = getNavLinks(uiExports, pluginSpecs);
return {
disabledPluginSpecs,
pluginSpecs,
pluginExtendedConfig: configToMutate,
uiExports,
navLinks,
};
}