From e42d029c2539a6717fde8324f38afafe67dce5cb Mon Sep 17 00:00:00 2001 From: Florent Daigniere Date: Thu, 8 Dec 2022 17:41:33 +0100 Subject: [PATCH] normalize booleans --- core/base/libs/socrate/socrate/system.py | 13 +++++++++++++ optional/fetchmail/fetchmail.py | 8 ++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/core/base/libs/socrate/socrate/system.py b/core/base/libs/socrate/socrate/system.py index 319da0a9..b6ee7761 100644 --- a/core/base/libs/socrate/socrate/system.py +++ b/core/base/libs/socrate/socrate/system.py @@ -15,6 +15,14 @@ def resolve_hostname(hostname): except Exception as e: log.warn("Unable to lookup '%s': %s",hostname,e) raise e + +def _coerce_value(value): + if isinstance(value, str) and value.lower() in ('true','yes'): + return True + elif isinstance(value, str) and value.lower() in ('false', 'no'): + return False + return value + def set_env(required_secrets=[]): """ This will set all the environment variables and retains only the secrets we need """ secret_key = os.environ.get('SECRET_KEY') @@ -29,6 +37,11 @@ def set_env(required_secrets=[]): for secret in required_secrets: os.environ[f'{secret}_KEY'] = hmac.new(bytearray(secret_key, 'utf-8'), bytearray(secret, 'utf-8'), 'sha256').hexdigest() + return { + key: _coerce_value(os.environ.get(key, value)) + for key, value in os.environ.items() + } + def clean_env(): """ remove all secret keys """ [os.environ.pop(key, None) for key in os.environ.keys() if key.endswith("_KEY")] diff --git a/optional/fetchmail/fetchmail.py b/optional/fetchmail/fetchmail.py index 20af1e7f..97622feb 100755 --- a/optional/fetchmail/fetchmail.py +++ b/optional/fetchmail/fetchmail.py @@ -99,15 +99,15 @@ if __name__ == "__main__": os.chmod("/data/fetchids", 0o700) os.setgid(id_fetchmail.pw_gid) os.setuid(id_fetchmail.pw_uid) - system.set_env() + config = system.set_env() while True: - delay = int(os.environ.get("FETCHMAIL_DELAY", 60)) + delay = int(os.environ.get('FETCHMAIL_DELAY', 60)) print("Sleeping for {} seconds".format(delay)) time.sleep(delay) - if not os.environ.get("FETCHMAIL_ENABLED", 'True') in ('True', 'true'): + if not config.get('FETCHMAIL_ENABLED', True): print("Fetchmail disabled, skipping...") continue - run(os.environ.get("DEBUG", None) == "True") + run(config.get('DEBUG', False)) sys.stdout.flush()