Skip to content

Commit

Permalink
get_secret() returns None if table is missing, closes #13
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Apr 24, 2024
1 parent e930f22 commit 1b25b9f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
17 changes: 10 additions & 7 deletions datasette_secrets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import dataclasses
from datasette import hookimpl, Forbidden, Permission, Response
from datasette.plugins import pm
from datasette.utils import await_me_maybe
from datasette.utils import await_me_maybe, sqlite3
import os
from typing import Optional
from . import hookspecs
Expand All @@ -24,12 +24,15 @@ async def get_secret(datasette, secret_name, actor_id=None):
# Now look it up in the database
config = get_config(datasette)
db = get_database(datasette)
db_secret = (
await db.execute(
"select id, encrypted from datasette_secrets where name = ? order by version desc limit 1",
(secret_name,),
)
).first()
try:
db_secret = (
await db.execute(
"select id, encrypted from datasette_secrets where name = ? order by version desc limit 1",
(secret_name,),
)
).first()
except sqlite3.OperationalError:
return None
if not db_secret:
return None
key = Fernet(config["encryption_key"].encode("utf-8"))
Expand Down
7 changes: 6 additions & 1 deletion tests/test_secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from datasette.app import Datasette
from datasette.cli import cli
from datasette.plugins import pm
from datasette_secrets import get_secret, Secret
from datasette_secrets import get_secret, Secret, startup
import pytest
from unittest.mock import ANY

Expand Down Expand Up @@ -269,6 +269,11 @@ async def test_get_secret(ds, monkeypatch):
"""
assert remove_whitespace(expected_html) in remove_whitespace(response.text)

# Finally it should still work even if the datasette_secrets table is missing
await db.execute_write("drop table datasette_secrets")
monkeypatch.delenv("DATASETTE_SECRETS_EXAMPLE_SECRET")
assert await get_secret(ds, "EXAMPLE_SECRET") is None


@pytest.mark.asyncio
async def test_secret_index_page(ds, register_multiple_secrets):
Expand Down

0 comments on commit 1b25b9f

Please sign in to comment.