-
Notifications
You must be signed in to change notification settings - Fork 827
/
plugins_discovery.ts
224 lines (209 loc) · 7.38 KB
/
plugins_discovery.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
217
218
219
220
221
222
223
224
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
/*
* 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.
*/
/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
import { readdir, stat } from 'fs';
import { resolve } from 'path';
import { bindNodeCallback, from, merge, Observable } from 'rxjs';
import { catchError, filter, map, mergeMap, shareReplay } from 'rxjs/operators';
import { CoreContext } from '../../core_context';
import { Logger } from '../../logging';
import { PluginWrapper } from '../plugin';
import { createPluginInitializerContext, InstanceInfo } from '../plugin_context';
import { PluginsConfig } from '../plugins_config';
import { PluginDiscoveryError } from './plugin_discovery_error';
import { parseManifest } from './plugin_manifest_parser';
const fsReadDir$ = bindNodeCallback<string, string[]>(readdir);
const fsStat$ = bindNodeCallback(stat);
const maxScanDepth = 5;
interface PluginSearchPathEntry {
dir: string;
depth: number;
}
/**
* Tries to discover all possible plugins based on the provided plugin config.
* Discovery result consists of two separate streams, the one (`plugin$`) is
* for the successfully discovered plugins and the other one (`error$`) is for
* all the errors that occurred during discovery process.
*
* @param config Plugin config instance.
* @param coreContext OpenSearch Dashboards core values.
* @internal
*/
export function discover(
config: PluginsConfig,
coreContext: CoreContext,
instanceInfo: InstanceInfo
) {
const log = coreContext.logger.get('plugins-discovery');
log.debug('Discovering plugins...');
if (config.additionalPluginPaths.length && coreContext.env.mode.dev) {
log.warn(
`Explicit plugin paths [${config.additionalPluginPaths}] should only be used in development. Relative imports may not work properly in production.`
);
}
const discoveryResults$ = merge(
from(config.additionalPluginPaths),
processPluginSearchPaths$(config.pluginSearchPaths, log)
).pipe(
mergeMap((pluginPathOrError) => {
return typeof pluginPathOrError === 'string'
? createPlugin$(pluginPathOrError, log, coreContext, instanceInfo)
: [pluginPathOrError];
}),
shareReplay()
);
return {
plugin$: discoveryResults$.pipe(
filter((entry): entry is PluginWrapper => entry instanceof PluginWrapper)
),
error$: discoveryResults$.pipe(
filter((entry): entry is PluginDiscoveryError => !(entry instanceof PluginWrapper))
),
};
}
/**
* Recursively iterates over every plugin search path and returns a merged stream of all
* sub-directories containing a manifest file. If directory cannot be read or it's impossible to get stat
* for any of the nested entries then error is added into the stream instead.
*
* @param pluginDirs List of the top-level directories to process.
* @param log Plugin discovery logger instance.
*/
function processPluginSearchPaths$(
pluginDirs: readonly string[],
log: Logger
): Observable<string | PluginDiscoveryError> {
function recursiveScanFolder(
ent: PluginSearchPathEntry
): Observable<string | PluginDiscoveryError> {
return from([ent]).pipe(
mergeMap((entry) => {
return findManifestInFolder(entry.dir, () => {
if (entry.depth > maxScanDepth) {
return [];
}
return mapSubdirectories(entry.dir, (subDir) =>
recursiveScanFolder({ dir: subDir, depth: entry.depth + 1 })
);
});
})
);
}
return from(pluginDirs.map((dir) => ({ dir, depth: 0 }))).pipe(
mergeMap((entry) => {
log.debug(`Scanning "${entry.dir}" for plugin sub-directories...`);
return fsReadDir$(entry.dir).pipe(
mergeMap(() => recursiveScanFolder(entry)),
catchError((err) => [PluginDiscoveryError.invalidSearchPath(entry.dir, err)])
);
})
);
}
/**
* Attempts to read manifest file in specified directory or calls `notFound` and returns results if not found. For any
* manifest files that cannot be read, a PluginDiscoveryError is added.
* @param dir
* @param notFound
*/
function findManifestInFolder(
dir: string,
notFound: () => never[] | Observable<string | PluginDiscoveryError>
): string[] | Observable<string | PluginDiscoveryError> {
return fsStat$(resolve(dir, 'opensearch_dashboards.json')).pipe(
mergeMap((stats) => {
// `opensearch_dashboards.json` exists in given directory, we got a plugin
if (stats.isFile()) {
return [dir];
}
return [];
}),
catchError((manifestStatError) => {
// did not find manifest. recursively process sub directories until we reach max depth.
if (manifestStatError.code !== 'ENOENT') {
return [PluginDiscoveryError.invalidPluginPath(dir, manifestStatError)];
}
return notFound();
})
);
}
/**
* Finds all subdirectories in `dir` and executed `mapFunc` for each one. For any directories that cannot be read,
* a PluginDiscoveryError is added.
* @param dir
* @param mapFunc
*/
function mapSubdirectories(
dir: string,
mapFunc: (subDir: string) => Observable<string | PluginDiscoveryError>
): Observable<string | PluginDiscoveryError> {
return fsReadDir$(dir).pipe(
mergeMap((subDirs: string[]) => subDirs.map((subDir) => resolve(dir, subDir))),
mergeMap((subDir) =>
fsStat$(subDir).pipe(
mergeMap((pathStat) => (pathStat.isDirectory() ? mapFunc(subDir) : [])),
catchError((subDirStatError) => [
PluginDiscoveryError.invalidPluginPath(subDir, subDirStatError),
])
)
)
);
}
/**
* Tries to load and parse the plugin manifest file located at the provided plugin
* directory path and produces an error result if it fails to do so or plugin manifest
* isn't valid.
* @param path Path to the plugin directory where manifest should be loaded from.
* @param log Plugin discovery logger instance.
* @param coreContext OpenSearch Dashboards core context.
*/
function createPlugin$(
path: string,
log: Logger,
coreContext: CoreContext,
instanceInfo: InstanceInfo
) {
return from(parseManifest(path, coreContext.env.packageInfo, log)).pipe(
map((manifest) => {
log.debug(`Successfully discovered plugin "${manifest.id}" at "${path}"`);
const opaqueId = Symbol(manifest.id);
return new PluginWrapper({
path,
manifest,
opaqueId,
initializerContext: createPluginInitializerContext(
coreContext,
opaqueId,
manifest,
instanceInfo
),
});
}),
catchError((err) => [err])
);
}