Updated so it now can either read from a path or environment variable
This commit is contained in:
1 parent
c2aa63ec54
commit
dd5bd18bcb
3 files changed
+33
-9
No files matched your search
+1
-2
@@ -1,11 +1,10 @@
|
|||||||
[project]
|
[project]
|
||||||
name = "lib-secret-manager"
|
name = "lib-secret-manager"
|
||||||
version = "0.2.0"
|
version = "0.3.0"
|
||||||
description = "Shared secret manager that consumes secrets"
|
description = "Shared secret manager that consumes secrets"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
requires-python = ">=3.10"
|
requires-python = ">=3.10"
|
||||||
authors = [{ name = "Florian Gänsejunge" }]
|
authors = [{ name = "Florian Gänsejunge" }]
|
||||||
dependencies = ["simple-logger-handler"]
|
|
||||||
|
|
||||||
[project.optional-dependencies]
|
[project.optional-dependencies]
|
||||||
encryption = ["cryptography==46.0.2"]
|
encryption = ["cryptography==46.0.2"]
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
from .credentials_loader import return_credentials, cleanup_secret_files
|
import logging
|
||||||
|
from .credentials_loader import get_secret, cleanup_secret_files, MissingCredentialError
|
||||||
|
|
||||||
__all__ = [
|
logging.getLogger(__name__).addHandler(logging.NullHandler())
|
||||||
"return_credentials",
|
__all__ = ["get_secret","cleanup_secret_files","MissingCredentialError"]
|
||||||
"cleanup_secret_files",
|
|
||||||
]
|
|
||||||
@@ -1,9 +1,35 @@
|
|||||||
from simple_logger_handler import setup_logger
|
import logging
|
||||||
import os
|
import os
|
||||||
from typing import Set
|
from typing import Set
|
||||||
|
|
||||||
logger = setup_logger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
class MissingCredentialError(RuntimeError):
|
||||||
|
"""Neither the environment variable nor the secret file provided a value."""
|
||||||
|
|
||||||
|
def get_secret(env_var: str, path: str | None = None) -> str:
|
||||||
|
"""Resolve a secret from an environment variable, falling back to a file.
|
||||||
|
|
||||||
|
Resolution order:
|
||||||
|
1. The environment variable ``env_var`` (if set and non-empty).
|
||||||
|
2. The file at ``path`` (if given and it exists).
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
MissingCredentialError: If neither source provides a value.
|
||||||
|
"""
|
||||||
|
value = os.getenv(env_var, "").strip()
|
||||||
|
if value:
|
||||||
|
logger.debug(f"Loaded secret from environment variable {env_var}")
|
||||||
|
return value
|
||||||
|
|
||||||
|
if path:
|
||||||
|
try:
|
||||||
|
return return_credentials(path)
|
||||||
|
except FileNotFoundError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
sources = f"environment variable {env_var}" + (f" or file {path}" if path else "")
|
||||||
|
raise MissingCredentialError(f"No credential found: set {sources}")
|
||||||
|
|
||||||
def return_credentials(path: str) -> str:
|
def return_credentials(path: str) -> str:
|
||||||
"""Read and return credentials from a file."""
|
"""Read and return credentials from a file."""
|
||||||
|
|||||||
Reference in new issue
Block a user