From d9a4a2c66538bd86f81ac126e0553435e90093a3 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Thu, 5 Jan 2023 21:47:43 -0500 Subject: [PATCH] Read API authentication token from an environment variable --- mailman_sync.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/mailman_sync.py b/mailman_sync.py index 550e2b5..7da52e5 100755 --- a/mailman_sync.py +++ b/mailman_sync.py @@ -5,6 +5,7 @@ Update Mailman 2 lists via a json API of the form {"LIST": ["ADDRESS", ...]} """ import argparse +import os from pathlib import Path import subprocess @@ -37,8 +38,8 @@ def sync_members( print(output.stdout) -def main(mailman_bin: Path, api: str, token: str, list_suffix: str, dry_run: bool): - r = requests.get(api, headers={"Authorization": "Token " + token}) +def main(mailman_bin: Path, api: str, api_auth: str, list_suffix: str, dry_run: bool): + r = requests.get(api, headers={"Authorization": api_auth}) if not r.ok: print(f"Failed to get mailing list data from api: {r.status_code} {r.text}") return @@ -67,7 +68,6 @@ if __name__ == "__main__": help="Path to Mailman site admin scripts (default %(default)s)", ) argp.add_argument("--api", required=True, help="API endpoint to retrieve JSON from") - argp.add_argument("--token", required=True, help="Authorization Token for API") argp.add_argument("--list-suffix", help="Suffix for mailing lists") argp.add_argument( "-n", @@ -77,8 +77,14 @@ if __name__ == "__main__": ) args = argp.parse_args() + if "API_AUTH" in os.environ: + api_auth = os.environ.get('API_AUTH') + else: + print("Missing API_AUTH environment variable") + exit(-1) + try: - main(args.bin, args.api, args.token, args.list_suffix, args.dry_run) + main(args.bin, args.api, api_auth, args.list_suffix, args.dry_run) except subprocess.CalledProcessError as e: print(e.stderr) raise