Read API authentication token from an environment variable

This commit is contained in:
Adam Goldsmith 2023-01-05 21:47:43 -05:00
parent b5bc574433
commit a9088c981e

View File

@ -5,6 +5,7 @@ Update Mailman 2 lists via a json API of the form {"LIST": ["ADDRESS", ...]}
""" """
import argparse import argparse
import os
from pathlib import Path from pathlib import Path
import subprocess import subprocess
@ -37,8 +38,8 @@ def sync_members(
print(output.stdout) print(output.stdout)
def main(mailman_bin: Path, api: str, token: str, list_suffix: str, dry_run: bool): def main(mailman_bin: Path, api: str, api_auth: str, list_suffix: str, dry_run: bool):
r = requests.get(api, headers={"Authorization": "Token " + token}) r = requests.get(api, headers={"Authorization": api_auth})
if not r.ok: if not r.ok:
print(f"Failed to get mailing list data from api: {r.status_code} {r.text}") print(f"Failed to get mailing list data from api: {r.status_code} {r.text}")
return return
@ -67,7 +68,6 @@ if __name__ == "__main__":
help="Path to Mailman site admin scripts (default %(default)s)", 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("--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("--list-suffix", help="Suffix for mailing lists")
argp.add_argument( argp.add_argument(
"-n", "-n",
@ -77,8 +77,14 @@ if __name__ == "__main__":
) )
args = argp.parse_args() args = argp.parse_args()
if "API_AUTH" in os.environ:
api_auth = os.environ
else:
print("Missing API_AUTH environment variable")
exit(-1)
try: 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: except subprocess.CalledProcessError as e:
print(e.stderr) print(e.stderr)
raise raise