Persistent secrets

This commit is contained in:
Piero Toffanin 2023-03-11 12:06:36 -05:00
parent 683d155b1e
commit 3fe629f8cf
2 changed files with 15 additions and 2 deletions

View file

@ -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())

View file

@ -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")