Add Get Password From File

This commit is contained in:
Michael 2025-12-02 15:45:00 -06:00
parent 5f0920baad
commit ddf3129a38
No known key found for this signature in database
GPG Key ID: 63FBE98417C278D2
1 changed files with 28 additions and 2 deletions

View File

@ -4,6 +4,7 @@ Functionality:
- encapsulate persistence of application properties
"""
import os.path
from os import environ
try:
@ -15,6 +16,31 @@ except ModuleNotFoundError:
pass
def get_password_from_file(env_var_name) -> str:
"""get password from file"""
env_var_file: str = env_var_name + "_FILE"
env_var_name_val = environ.get(env_var_name)
env_var_path_val = environ.get(env_var_file)
if env_var_name_val is not None:
return str(env_var_name_val)
elif env_var_path_val is not None:
if os.path.isfile(str(env_var_path_val)):
with open(
file=str(env_var_path_val), mode="r", encoding="utf-8"
) as file:
return file.read()
else:
print(f"{env_var_file} file: {env_var_path_val} does not exist")
else:
print(f"either {env_var_name} or {env_var_file} must be set")
return ""
class EnvironmentSettings:
"""
Handle settings for the application that are driven from the environment.
@ -29,7 +55,7 @@ class EnvironmentSettings:
TA_PORT: int = int(environ.get("TA_PORT", False))
TA_BACKEND_PORT: int = int(environ.get("TA_BACKEND_PORT", False))
TA_USERNAME: str = str(environ.get("TA_USERNAME"))
TA_PASSWORD: str = str(environ.get("TA_PASSWORD"))
TA_PASSWORD: str = get_password_from_file("TA_PASSWORD")
# Application Paths
MEDIA_DIR: str = str(environ.get("TA_MEDIA_DIR", "/youtube"))
@ -42,7 +68,7 @@ class EnvironmentSettings:
# ElasticSearch
ES_URL: str = str(environ.get("ES_URL"))
ES_PASS: str = str(environ.get("ELASTIC_PASSWORD"))
ES_PASS: str = get_password_from_file("ELASTIC_PASSWORD")
ES_USER: str = str(environ.get("ELASTIC_USER", "elastic"))
ES_SNAPSHOT_DIR: str = str(
environ.get(