Gracefully opening encryption key and secrets with exit on failure

This commit is contained in:
Florian 2025-10-09 08:05:40 +02:00
parent 7fb139b362
commit ed0dcfeadb

View File

@ -1,7 +1,15 @@
from cryptography.fernet import Fernet from cryptography.fernet import Fernet
import sys
with open("/etc/secrets/encryption_key","rb") as file: try:
with open("/etc/secrets/encryption_key","rb") as file:
encryption_key = file.read() encryption_key = file.read()
except FileNotFoundError:
print("[FATAL] Encryption key not found")
sys.exit(1)
except Exception as e:
print(f"[FATAL]Failed to read encryption key: {e}")
sys.exit(1)
fernet = Fernet(encryption_key) fernet = Fernet(encryption_key)
@ -12,5 +20,14 @@ def decrypt_token(token:str)->str:
return fernet.decrypt(token.encode()).decode() return fernet.decrypt(token.encode()).decode()
def return_credentials(path: str)->str: def return_credentials(path: str)->str:
try:
with open (path) as file: with open (path) as file:
return file.read().strip() return file.read().strip()
except FileNotFoundError:
print(f"[FATAL] Secret file not found: {path}")
sys.exit(1)
except Exception as e:
print(f"[FATAL] Failed to read secret file {path}: {e}")
sys.exit(1)