mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
"$env": "X" mechanism now works with nested lists, closes #837
This commit is contained in:
parent
f39f111331
commit
fba8ff6e76
6 changed files with 48 additions and 12 deletions
|
|
@ -45,6 +45,7 @@ from .utils import (
|
|||
format_bytes,
|
||||
module_from_path,
|
||||
parse_metadata,
|
||||
resolve_env_secrets,
|
||||
sqlite3,
|
||||
to_css_class,
|
||||
)
|
||||
|
|
@ -367,18 +368,7 @@ class Datasette:
|
|||
return None
|
||||
plugin_config = plugins.get(plugin_name)
|
||||
# Resolve any $file and $env keys
|
||||
if isinstance(plugin_config, dict):
|
||||
# Create a copy so we don't mutate the version visible at /-/metadata.json
|
||||
plugin_config_copy = dict(plugin_config)
|
||||
for key, value in plugin_config_copy.items():
|
||||
if isinstance(value, dict):
|
||||
if list(value.keys()) == ["$env"]:
|
||||
plugin_config_copy[key] = os.environ.get(
|
||||
list(value.values())[0]
|
||||
)
|
||||
elif list(value.keys()) == ["$file"]:
|
||||
plugin_config_copy[key] = open(list(value.values())[0]).read()
|
||||
return plugin_config_copy
|
||||
plugin_config = resolve_env_secrets(plugin_config, os.environ)
|
||||
return plugin_config
|
||||
|
||||
def app_css_hash(self):
|
||||
|
|
|
|||
|
|
@ -904,3 +904,19 @@ async def check_visibility(datasette, actor, action, resource, default=True):
|
|||
None, action, resource=resource, default=default,
|
||||
)
|
||||
return visible, private
|
||||
|
||||
|
||||
def resolve_env_secrets(config, environ):
|
||||
'Create copy that recursively replaces {"$env": "NAME"} with values from environ'
|
||||
if isinstance(config, dict):
|
||||
if list(config.keys()) == ["$env"]:
|
||||
return environ.get(list(config.values())[0])
|
||||
else:
|
||||
return {
|
||||
key: resolve_env_secrets(value, environ)
|
||||
for key, value in config.items()
|
||||
}
|
||||
elif isinstance(config, list):
|
||||
return [resolve_env_secrets(value, environ) for value in config]
|
||||
else:
|
||||
return config
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue