Skip to content

Commit

Permalink
Refactor plugin dependency validation into separate method
Browse files Browse the repository at this point in the history
  • Loading branch information
jportner committed Aug 4, 2021
1 parent 89e3cc8 commit b6a38a7
Showing 1 changed file with 43 additions and 33 deletions.
76 changes: 43 additions & 33 deletions src/core/server/plugins/plugins_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,39 +314,7 @@ export class PluginsService implements CoreService<PluginsServiceSetup, PluginsS
.toPromise();

for (const [pluginName, { plugin, isEnabled }] of pluginEnableStatuses) {
// validate that `requiredBundles` ids point to a discovered plugin which `includesUiPlugin`
for (const requiredBundleId of plugin.requiredBundles) {
if (!pluginEnableStatuses.has(requiredBundleId)) {
throw new Error(
`Plugin bundle with id "${requiredBundleId}" is required by plugin "${pluginName}" but it is missing.`
);
}

const requiredPlugin = pluginEnableStatuses.get(requiredBundleId)!.plugin;
if (!requiredPlugin.includesUiPlugin) {
throw new Error(
`Plugin bundle with id "${requiredBundleId}" is required by plugin "${pluginName}" but it doesn't have a UI bundle.`
);
}

if (requiredPlugin.manifest.type !== plugin.manifest.type) {
throw new Error(
`Plugin bundle with id "${requiredBundleId}" is required by plugin "${pluginName}" and expected to have "${plugin.manifest.type}" type, but its type is "${requiredPlugin.manifest.type}".`
);
}
}

// validate that OSS plugins do not have required dependencies on X-Pack plugins
if (plugin.source === 'oss') {
for (const id of [...plugin.requiredPlugins, ...plugin.requiredBundles]) {
const requiredPlugin = pluginEnableStatuses.get(id);
if (requiredPlugin && requiredPlugin.plugin.source === 'x-pack') {
throw new Error(
`X-Pack plugin or bundle with id "${id}" is required by OSS plugin "${pluginName}", which is prohibited. Consider making this an optional dependency instead.`
);
}
}
}
this.validatePluginDependencies(plugin, pluginEnableStatuses);

const pluginEnablement = this.shouldEnablePlugin(pluginName, pluginEnableStatuses);

Expand All @@ -370,6 +338,48 @@ export class PluginsService implements CoreService<PluginsServiceSetup, PluginsS
this.log.debug(`Discovered ${pluginEnableStatuses.size} plugins.`);
}

/** Throws an error if the plugin's dependencies are invalid. */
private validatePluginDependencies(
plugin: PluginWrapper,
pluginEnableStatuses: Map<PluginName, { plugin: PluginWrapper; isEnabled: boolean }>
) {
const { name, manifest, requiredBundles, requiredPlugins } = plugin;

// validate that `requiredBundles` ids point to a discovered plugin which `includesUiPlugin`
for (const requiredBundleId of requiredBundles) {
if (!pluginEnableStatuses.has(requiredBundleId)) {
throw new Error(
`Plugin bundle with id "${requiredBundleId}" is required by plugin "${name}" but it is missing.`
);
}

const requiredPlugin = pluginEnableStatuses.get(requiredBundleId)!.plugin;
if (!requiredPlugin.includesUiPlugin) {
throw new Error(
`Plugin bundle with id "${requiredBundleId}" is required by plugin "${name}" but it doesn't have a UI bundle.`
);
}

if (requiredPlugin.manifest.type !== plugin.manifest.type) {
throw new Error(
`Plugin bundle with id "${requiredBundleId}" is required by plugin "${name}" and expected to have "${manifest.type}" type, but its type is "${requiredPlugin.manifest.type}".`
);
}
}

// validate that OSS plugins do not have required dependencies on X-Pack plugins
if (plugin.source === 'oss') {
for (const id of [...requiredPlugins, ...requiredBundles]) {
const requiredPlugin = pluginEnableStatuses.get(id);
if (requiredPlugin && requiredPlugin.plugin.source === 'x-pack') {
throw new Error(
`X-Pack plugin or bundle with id "${id}" is required by OSS plugin "${name}", which is prohibited. Consider making this an optional dependency instead.`
);
}
}
}
}

private shouldEnablePlugin(
pluginName: PluginName,
pluginEnableStatuses: Map<PluginName, { plugin: PluginWrapper; isEnabled: boolean }>,
Expand Down

0 comments on commit b6a38a7

Please sign in to comment.