forked from janus-idp/backstage-showcase
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dynamic backend plugins support (janus-idp#522)
* Wire dynamic backend plugins in the backend app Signed-off-by: David Festal <dfestal@redhat.com> * Placeholder for built-in-container dynamic plugins Signed-off-by: David Festal <dfestal@redhat.com> * Add dynamic plugin installation script and steps Signed-off-by: David Festal <dfestal@redhat.com> * Add the requirements.txt file Signed-off-by: David Festal <dfestal@redhat.com> * Add changeset Signed-off-by: David Festal <dfestal@redhat.com> * Dynamic plugins preliminary documentation Signed-off-by: David Festal <dfestal@redhat.com> * Fix small error after review comment Signed-off-by: David Festal <dfestal@redhat.com> * PR review fixes Signed-off-by: David Festal <dfestal@redhat.com> --------- Signed-off-by: David Festal <dfestal@redhat.com>
- Loading branch information
1 parent
f61842d
commit 5e45008
Showing
16 changed files
with
632 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'backend': minor | ||
--- | ||
|
||
Add support for dynamic backend plugins. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import os | ||
import sys | ||
import yaml | ||
import tarfile | ||
import shutil | ||
import subprocess | ||
|
||
class InstallException(Exception): | ||
"""Exception class from which every exception in this library will derive.""" | ||
pass | ||
|
||
def merge(source, destination): | ||
for key, value in source.items(): | ||
if isinstance(value, dict): | ||
# get node or create one | ||
node = destination.setdefault(key, {}) | ||
merge(value, node) | ||
else: | ||
# if key exists in destination trigger an error | ||
if key in destination: | ||
raise InstallException('Config key ' + key + ' defined for 2 dynamic plugins') | ||
|
||
destination[key] = value | ||
|
||
return destination | ||
|
||
def main(): | ||
dynamicPluginsRoot = sys.argv[1] | ||
maxEntrySize = int(os.environ.get('MAX_ENTRY_SIZE', 10000000)) | ||
|
||
dynamicPluginsFile = os.path.join(dynamicPluginsRoot, 'dynamic-plugins.yaml') | ||
dynamicPluginsGlobalConfigFile = os.path.join(dynamicPluginsRoot, 'app-config.dynamic-plugins.yaml') | ||
|
||
# test if file dynamic-plugins.yaml exists | ||
if not os.path.isfile(dynamicPluginsFile): | ||
print(f'No {dynamicPluginsFile} file found. Skipping dynamic plugins installation.') | ||
with open(dynamicPluginsGlobalConfigFile, 'w') as file: | ||
file.write('') | ||
file.close() | ||
exit(0) | ||
|
||
with open(dynamicPluginsFile, 'r') as file: | ||
plugins = yaml.safe_load(file) | ||
|
||
if plugins == '' or plugins is None: | ||
print(f'{dynamicPluginsFile} file is empty. Skipping dynamic plugins installation.') | ||
with open(dynamicPluginsGlobalConfigFile, 'w') as file: | ||
file.write('') | ||
file.close() | ||
exit(0) | ||
|
||
# test that plugins is a list | ||
if not isinstance(plugins, list): | ||
raise InstallException(f'{dynamicPluginsFile} content must be a list') | ||
|
||
globalConfig = { | ||
'dynamicPlugins': { | ||
'rootDirectory': 'dynamic-plugins-root' | ||
} | ||
} | ||
|
||
# iterate through the list of plugins | ||
for plugin in plugins: | ||
package = plugin['package'] | ||
if package.startswith('./'): | ||
package = os.path.join(os.getcwd(), package[2:]) | ||
|
||
print('\n======= Installing dynamic plugin', package, flush=True) | ||
|
||
print('\t==> Grabbing package archive through `npm pack`', flush=True) | ||
completed = subprocess.run(['npm', 'pack', package], capture_output=True, cwd=dynamicPluginsRoot) | ||
if completed.returncode != 0: | ||
raise InstallException(f'Error while installing plugin { package } with \'npm pack\' : ' + completed.stderr.decode('utf-8')) | ||
|
||
archive = os.path.join(dynamicPluginsRoot, completed.stdout.decode('utf-8').strip()) | ||
directory = archive.replace('.tgz', '') | ||
|
||
print('\t==> Removing previous plugin directory', directory, flush=True) | ||
shutil.rmtree(directory, ignore_errors=True, onerror=None) | ||
os.mkdir(directory) | ||
|
||
print('\t==> Extracting package archive', archive, flush=True) | ||
file = tarfile.open(archive, 'r:gz') # NOSONAR | ||
# extract the archive content but take care of zip bombs | ||
for member in file.getmembers(): | ||
if member.isreg(): | ||
if not member.name.startswith('package/'): | ||
raise InstallException("NPM package archive archive does not start with 'package/' as it should: " + member.name) | ||
|
||
if member.size > maxEntrySize: | ||
raise InstallException('Zip bomb detected in ' + member.name) | ||
|
||
# Remove the `package/` prefix from the file name | ||
member.name = member.name[8:] | ||
file.extract(member, path=directory) | ||
elif member.isdir(): | ||
print('\t\tSkipping directory entry', member.name, flush=True) | ||
else: | ||
raise InstallException('NPM package archive contains a non regular file: ' + member.name) | ||
|
||
file.close() | ||
|
||
print('\t==> Removing package archive', archive, flush=True) | ||
os.remove(archive) | ||
|
||
if 'pluginConfig' not in plugin: | ||
print('\t==> Successfully installed dynamic plugin', package, flush=True) | ||
continue | ||
|
||
# if some plugin configuration is defined, merge it with the global configuration | ||
|
||
print('\t==> Merging plugin-specific configuration', flush=True) | ||
config = plugin['pluginConfig'] | ||
if config is not None and isinstance(config, dict): | ||
merge(config, globalConfig) | ||
|
||
print('\t==> Successfully installed dynamic plugin', package, flush=True) | ||
|
||
yaml.safe_dump(globalConfig, open(dynamicPluginsGlobalConfigFile, 'w')) | ||
|
||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
PyYAML==6.0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
**/node_modules/** | ||
**/dist/** | ||
**/dist-dynamic/** | ||
**/templates/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"], | ||
"parser": "@typescript-eslint/parser", | ||
"plugins": ["@typescript-eslint"], | ||
"root": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"private": true, | ||
"scripts": { | ||
"export-dynamic": "for D in $(ls -d */ 2>/dev/null); do yarn --cwd \"${D}\" install --frozen-lockfile && yarn --cwd \"${D}\" export-dynamic && rm -Rf ${D}/node_modules ${D}/.yarn ${D}/dist; done" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.