Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment variable parsing support for Config files #108

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion limacharlie/Configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,6 +18,8 @@
import json
import glob

_enable_env_parsing()

class LcConfigException( Exception ):
pass

Expand Down
18 changes: 17 additions & 1 deletion limacharlie/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.'''
Expand Down Expand Up @@ -231,4 +233,18 @@ def __exit__(self, exception, value, tb):
self.busy = False
time.sleep(self.delay)
if exception is not None:
return False
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)