Skip to content

Commit

Permalink
Secret plugin configuration options (#539)
Browse files Browse the repository at this point in the history
Closes #538
  • Loading branch information
simonw committed Nov 11, 2019
1 parent dea9f94 commit ec75852
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
11 changes: 10 additions & 1 deletion datasette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,16 @@ def plugin_config(self, plugin_name, database=None, table=None, fallback=True):
)
if plugins is None:
return None
return plugins.get(plugin_name)
plugin_config = plugins.get(plugin_name)
# Resolve any $file and $env keys
if isinstance(plugin_config, dict):
for key, value in plugin_config.items():
if isinstance(value, dict):
if list(value.keys()) == ["$env"]:
plugin_config[key] = os.environ.get(list(value.values())[0])
elif list(value.keys()) == ["$file"]:
plugin_config[key] = open(list(value.values())[0]).read()
return plugin_config

def app_css_hash(self):
if not hasattr(self, "_app_css_hash"):
Expand Down
33 changes: 33 additions & 0 deletions docs/plugins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,39 @@ Here is an example of some plugin configuration for a specific table::

This tells the ``datasette-cluster-map`` column which latitude and longitude columns should be used for a table called ``Street_Tree_List`` inside a database file called ``sf-trees.db``.

Secret configuration values
~~~~~~~~~~~~~~~~~~~~~~~~~~~

Any values embedded in ``metadata.json`` will be visible to anyone who views the ``/-/metadata`` page of your Datasette instance. Some plugins may need configuration that should stay secret - API keys for example. There are two ways in which you can store secret configuration values.

**As environment variables**. If your secret lives in an environment variable that is available to the Datasette process, you can indicate that the configuration value should be read from that environment variable like so::

{
"plugins": {
"datasette-auth-github": {
"client_secret": {
"$env": "GITHUB_CLIENT_SECRET"
}
}
}
}


**As values in separate files**. Your secrets can also live in files on disk. To specify a secret should be read from a file, provide the full file path like this::

{
"plugins": {
"datasette-auth-github": {
"client_secret": {
"$file": "/secrets/client-secret"
}
}
}
}

Writing plugins that accept configuration
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

When you are writing plugins, you can access plugin configuration like this using the ``datasette.plugin_config()`` method. If you know you need plugin configuration for a specific table, you can access it like this::

plugin_config = datasette.plugin_config(
Expand Down
10 changes: 9 additions & 1 deletion tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
from urllib.parse import unquote


# This temp file is used by one of the plugin config tests
TEMP_PLUGIN_SECRET_FILE = os.path.join(tempfile.gettempdir(), "plugin-secret")


class TestResponse:
def __init__(self, status, headers, body):
self.status = status
Expand Down Expand Up @@ -246,7 +250,11 @@ def generate_sortable_rows(num):
"source_url": "https://github.com/simonw/datasette/blob/master/tests/fixtures.py",
"about": "About Datasette",
"about_url": "https://github.com/simonw/datasette",
"plugins": {"name-of-plugin": {"depth": "root"}},
"plugins": {
"name-of-plugin": {"depth": "root"},
"env-plugin": {"foo": {"$env": "FOO_ENV"}},
"file-plugin": {"foo": {"$file": TEMP_PLUGIN_SECRET_FILE}},
},
"databases": {
"fixtures": {
"description": "Test tables description",
Expand Down
15 changes: 14 additions & 1 deletion tests/test_plugins.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from bs4 import BeautifulSoup as Soup
from .fixtures import app_client # noqa
from .fixtures import app_client, make_app_client, TEMP_PLUGIN_SECRET_FILE # noqa
import base64
import json
import os
import re
import pytest
import urllib
Expand Down Expand Up @@ -125,6 +126,18 @@ def test_plugin_config(app_client):
assert None is app_client.ds.plugin_config("unknown-plugin")


def test_plugin_config_env(app_client):
os.environ["FOO_ENV"] = "FROM_ENVIRONMENT"
assert {"foo": "FROM_ENVIRONMENT"} == app_client.ds.plugin_config("env-plugin")
del os.environ["FOO_ENV"]


def test_plugin_config_file(app_client):
open(TEMP_PLUGIN_SECRET_FILE, "w").write("FROM_FILE")
assert {"foo": "FROM_FILE"} == app_client.ds.plugin_config("file-plugin")
os.remove(TEMP_PLUGIN_SECRET_FILE)


@pytest.mark.parametrize(
"path,expected_extra_body_script",
[
Expand Down

0 comments on commit ec75852

Please sign in to comment.