From 3fe629f8cfb2e0203e683a4ebea943d2da88a529 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Sat, 11 Mar 2023 12:06:36 -0500 Subject: [PATCH] Persistent secrets --- libretranslate/secret.py | 8 ++++++-- libretranslate/storage.py | 9 +++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/libretranslate/secret.py b/libretranslate/secret.py index ce69910..a50fcc7 100644 --- a/libretranslate/secret.py +++ b/libretranslate/secret.py @@ -24,5 +24,9 @@ def get_current_secret(): def setup(args): if args.api_keys and args.require_api_key_secret: s = get_storage() - s.set_str("secret_0", generate_secret()) - s.set_str("secret_1", generate_secret()) + + if not s.exists("secret_0"): + s.set_str("secret_0", generate_secret()) + + if not s.exists("secret_1"): + s.set_str("secret_1", generate_secret()) diff --git a/libretranslate/storage.py b/libretranslate/storage.py index 7ffe849..22422f8 100644 --- a/libretranslate/storage.py +++ b/libretranslate/storage.py @@ -5,6 +5,9 @@ def get_storage(): return storage class Storage: + def exists(self, key): + raise Exception("not implemented") + def set_bool(self, key, value): raise Exception("not implemented") def get_bool(self, key): @@ -37,6 +40,9 @@ class Storage: class MemoryStorage(Storage): def __init__(self): self.store = {} + + def exists(self, key): + return key in self.store def set_bool(self, key, value): self.store[key] = bool(value) @@ -97,6 +103,9 @@ class RedisStorage(Storage): def __init__(self, redis_uri): self.conn = redis.from_url(redis_uri) self.conn.ping() + + def exists(self, key): + return bool(self.conn.exists(key)) def set_bool(self, key, value): self.conn.set(key, "1" if value else "0")