datasette-secrets 0.1a2__tar.gz → 0.1a4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: datasette-secrets
3
- Version: 0.1a2
3
+ Version: 0.1a4
4
4
  Summary: Manage secrets such as API keys for use with other Datasette plugins
5
5
  Author: Datasette
6
6
  License: Apache-2.0
@@ -3,7 +3,7 @@ from cryptography.fernet import Fernet
3
3
  import dataclasses
4
4
  from datasette import hookimpl, Forbidden, Permission, Response
5
5
  from datasette.plugins import pm
6
- from datasette.utils import await_me_maybe
6
+ from datasette.utils import await_me_maybe, sqlite3
7
7
  import os
8
8
  from typing import Optional
9
9
  from . import hookspecs
@@ -23,16 +23,22 @@ async def get_secret(datasette, secret_name, actor_id=None):
23
23
  return os.environ[env_var]
24
24
  # Now look it up in the database
25
25
  config = get_config(datasette)
26
+ if config is None:
27
+ return None
28
+ encryption_key = config["encryption_key"]
26
29
  db = get_database(datasette)
27
- db_secret = (
28
- await db.execute(
29
- "select id, encrypted from datasette_secrets where name = ? order by version desc limit 1",
30
- (secret_name,),
31
- )
32
- ).first()
30
+ try:
31
+ db_secret = (
32
+ await db.execute(
33
+ "select id, encrypted from datasette_secrets where name = ? order by version desc limit 1",
34
+ (secret_name,),
35
+ )
36
+ ).first()
37
+ except sqlite3.OperationalError:
38
+ return None
33
39
  if not db_secret:
34
40
  return None
35
- key = Fernet(config["encryption_key"].encode("utf-8"))
41
+ key = Fernet(encryption_key.encode("utf-8"))
36
42
  decrypted = key.decrypt(db_secret["encrypted"])
37
43
  # Update the last used timestamp and actor_id
38
44
  params = (actor_id, db_secret["id"])
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: datasette-secrets
3
- Version: 0.1a2
3
+ Version: 0.1a4
4
4
  Summary: Manage secrets such as API keys for use with other Datasette plugins
5
5
  Author: Datasette
6
6
  License: Apache-2.0
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "datasette-secrets"
3
- version = "0.1a2"
3
+ version = "0.1a4"
4
4
  description = "Manage secrets such as API keys for use with other Datasette plugins"
5
5
  readme = "README.md"
6
6
  authors = [{name = "Datasette"}]
@@ -4,7 +4,7 @@ from datasette import hookimpl
4
4
  from datasette.app import Datasette
5
5
  from datasette.cli import cli
6
6
  from datasette.plugins import pm
7
- from datasette_secrets import get_secret, Secret
7
+ from datasette_secrets import get_secret, Secret, startup, get_config
8
8
  import pytest
9
9
  from unittest.mock import ANY
10
10
 
@@ -269,6 +269,19 @@ async def test_get_secret(ds, monkeypatch):
269
269
  """
270
270
  assert remove_whitespace(expected_html) in remove_whitespace(response.text)
271
271
 
272
+ # Finally it should still work even if the datasette_secrets table is missing
273
+ await db.execute_write("drop table datasette_secrets")
274
+ monkeypatch.delenv("DATASETTE_SECRETS_EXAMPLE_SECRET")
275
+ assert await get_secret(ds, "EXAMPLE_SECRET") is None
276
+
277
+
278
+ @pytest.mark.asyncio
279
+ async def test_if_not_configured(register_multiple_secrets):
280
+ ds = Datasette()
281
+ config = get_config(ds)
282
+ assert config is None
283
+ assert await get_secret(ds, "OPENAI_API_KEY") is None
284
+
272
285
 
273
286
  @pytest.mark.asyncio
274
287
  async def test_secret_index_page(ds, register_multiple_secrets):