diff --git a/limacharlie/Configs.py b/limacharlie/Configs.py index c091321..d4fdaf2 100644 --- a/limacharlie/Configs.py +++ b/limacharlie/Configs.py @@ -2,7 +2,7 @@ from .Replicants import Integrity from .Replicants import Logging from .Replicants import Exfil -from .utils import _isStringCompat +from .utils import _isStringCompat, _enable_env_parsing from .Extensions import Extension # Detect if this is Python 2 or 3 @@ -18,6 +18,8 @@ import json import glob +_enable_env_parsing() + class LcConfigException( Exception ): pass diff --git a/limacharlie/utils.py b/limacharlie/utils.py index 01dc4db..5432e10 100644 --- a/limacharlie/utils.py +++ b/limacharlie/utils.py @@ -8,6 +8,8 @@ import threading import time +import yaml +import os class LcApiException ( Exception ): '''Exception type used for various errors in the LimaCharlie SDK.''' @@ -231,4 +233,18 @@ def __exit__(self, exception, value, tb): self.busy = False time.sleep(self.delay) if exception is not None: - return False \ No newline at end of file + return False + +# Custom YAML node constructor to handle !ENV +# tags and replace them with an ENV value +def _env_constructor(loader, node): + value = loader.construct_scalar(node) + env_value = os.getenv(value) + # Raise an error if the variable is not set + if env_value is None: + raise ValueError(f"Environment variable {value} is not set.") + else: + return env_value + +def _enable_env_parsing(): + yaml.SafeLoader.add_constructor('!ENV', _env_constructor)