Repository: jelmer/redirect-webfinger (language: python)

Issue:
Does not appear to pick up environment variables running on Docker on WSL
It throws this error with the correct account specified:

`WARNING:root:Unknown resource: 'acct:user@domain.tld'`

When started with

`docker run --name webfinger -p 8080:8080 -e ACCT=user@domain.tld -e MASTODON_SERVER=mastodon.social -e MASTODON_USER=user ghcr.io/jelmer/redirect-webfinger:latest`

If started with 

`docker run --name webfinger -p 8080:8080 ghcr.io/jelmer/redirect-webfinger:latest --acct=user@domain.tld --mastodon-server=mastodon.social --mastodon-user=user`

It correctly returns the expected JSON response.

It looks like it is failing to pick up the environment variable as specified at https://github.com/jelmer/redirect-webfinger/blob/main/redirect_webfinger/__main__.py#L39

```
    parser.add_argument(
        '--acct', type=str, help='Example: jelmer@jelmer.uk', nargs='*',
        default=os.environ.get('ACCT'))
```

I confirmed that the environment variables were correctly set.

Docker Desktop 4.13.1 on Ubuntu 22.04.1 LTS (GNU/Linux 5.10.102.1-microsoft-standard-WSL2 x86_64) on Windows 11 Pro.

Suggested run for Docker on WSL as shown above in second example passing in arguments as command line, rather than setting as environment variables.




Discussion / hints:

Fix patch:
diff --git a/redirect_webfinger/__main__.py b/redirect_webfinger/__main__.py
index 5498d95..5aa3316 100644
--- a/redirect_webfinger/__main__.py
+++ b/redirect_webfinger/__main__.py
@@ -2,6 +2,7 @@
 
 import logging
 import os
+import sys
 
 from aiohttp import web
 
@@ -33,26 +34,31 @@ def create_app(accts: list[str], mastodon_server: str, mastodon_user: str) -> we
     return app
 
 
-def main(argv=None):
+def parse_args(argv=None, environ=os.environ):
     import argparse
     parser = argparse.ArgumentParser()
     parser.add_argument(
         '--acct', type=str, help='Example: jelmer@jelmer.uk', nargs='*',
-        default=os.environ.get('ACCT'))
+        default=environ['ACCT'].split(',') if 'ACCT' in environ else None)
     parser.add_argument(
         '--mastodon-server', type=str, help='Example: mastodon.cloud',
-        default=os.environ.get('MASTODON_SERVER'))
+        default=environ.get('MASTODON_SERVER'))
     parser.add_argument(
         '--mastodon-user', type=str, help='Example: jelmer',
-        default=os.environ.get('MASTODON_USER'))
-    args = parser.parse_args(argv)
+        default=environ.get('MASTODON_USER'))
 
+    args = parser.parse_args(argv)
     if not args.acct or not args.mastodon_server or not args.mastodon_user:
         parser.print_usage()
-        return 1
+        sys.exit(1)
+    return args
 
+
+def main(argv=None):
     logging.basicConfig()
 
+    args = parse_args(argv)
+
     app = create_app(args.acct, mastodon_server=args.mastodon_server,
                      mastodon_user=args.mastodon_user)
     web.run_app(app, host='0.0.0.0', port=8080)
@@ -60,5 +66,4 @@ def main(argv=None):
 
 
 if __name__ == '__main__':
-    import sys
     sys.exit(main(sys.argv[1:]))


Test patch:
diff --git a/test_redirect_webfinger.py b/test_redirect_webfinger.py
index 969a5d7..2982cb3 100644
--- a/test_redirect_webfinger.py
+++ b/test_redirect_webfinger.py
@@ -1,6 +1,7 @@
 #!/usr/bin/python3
 
-from redirect_webfinger.__main__ import create_app
+import pytest
+from redirect_webfinger.__main__ import create_app, parse_args
 
 
 def create_test_app():
@@ -38,3 +39,24 @@ async def test_valid(aiohttp_client):
                   {'href': 'https://mastodon.cloud/users/jelmer', 'rel': 'self', 'type': 'application/activity+json'},
                   {'rel': 'http://ostatus.org/schema/1.0/subscribe', 'template': 'https://mastodon.cloud/authorize_interaction?uri={uri}'}],
         'subject': 'acct:jelmer@jelmer.uk'}
+
+
+def test_parse_args():
+    args = parse_args([], environ={
+        'ACCT': 'jelmer@jelmer.uk', 
+        'MASTODON_SERVER': 'mastodon.cloud',
+        'MASTODON_USER': 'jelmer'})
+    assert args.acct == ['jelmer@jelmer.uk']
+    assert args.mastodon_server == 'mastodon.cloud'
+    assert args.mastodon_user == 'jelmer'
+
+    with pytest.raises(SystemExit):
+        parse_args([], environ={})
+
+    args = parse_args([
+        '--acct=jelmer@jelmer.uk',
+        '--mastodon-server=mastodon.cloud',
+        '--mastodon-user=jelmer'], {})
+    assert args.acct == ['jelmer@jelmer.uk']
+    assert args.mastodon_server == 'mastodon.cloud'
+    assert args.mastodon_user == 'jelmer'