Gracefully opening encryption key and secrets with exit on failure
This commit is contained in:
parent
7fb139b362
commit
ed0dcfeadb
@ -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:
|
||||||
encryption_key = file.read()
|
with open("/etc/secrets/encryption_key","rb") as file:
|
||||||
|
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:
|
||||||
with open (path) as file:
|
try:
|
||||||
return file.read().strip()
|
with open (path) as file:
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user