normalize booleans

main
Florent Daigniere 2 years ago
parent 4e3874b0c1
commit e42d029c25

@ -15,6 +15,14 @@ def resolve_hostname(hostname):
except Exception as e: except Exception as e:
log.warn("Unable to lookup '%s': %s",hostname,e) log.warn("Unable to lookup '%s': %s",hostname,e)
raise 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=[]): def set_env(required_secrets=[]):
""" This will set all the environment variables and retains only the secrets we need """ """ This will set all the environment variables and retains only the secrets we need """
secret_key = os.environ.get('SECRET_KEY') secret_key = os.environ.get('SECRET_KEY')
@ -29,6 +37,11 @@ def set_env(required_secrets=[]):
for secret in 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() 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(): def clean_env():
""" remove all secret keys """ """ remove all secret keys """
[os.environ.pop(key, None) for key in os.environ.keys() if key.endswith("_KEY")] [os.environ.pop(key, None) for key in os.environ.keys() if key.endswith("_KEY")]

@ -99,15 +99,15 @@ if __name__ == "__main__":
os.chmod("/data/fetchids", 0o700) os.chmod("/data/fetchids", 0o700)
os.setgid(id_fetchmail.pw_gid) os.setgid(id_fetchmail.pw_gid)
os.setuid(id_fetchmail.pw_uid) os.setuid(id_fetchmail.pw_uid)
system.set_env() config = system.set_env()
while True: while True:
delay = int(os.environ.get("FETCHMAIL_DELAY", 60)) delay = int(os.environ.get('FETCHMAIL_DELAY', 60))
print("Sleeping for {} seconds".format(delay)) print("Sleeping for {} seconds".format(delay))
time.sleep(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...") print("Fetchmail disabled, skipping...")
continue continue
run(os.environ.get("DEBUG", None) == "True") run(config.get('DEBUG', False))
sys.stdout.flush() sys.stdout.flush()

Loading…
Cancel
Save