Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
register_new_matrix_user: read server url from config (#13616)
Browse files Browse the repository at this point in the history
Fixes #3672:
`https://localhost:8448` is virtually never right.
  • Loading branch information
richvdh authored Aug 25, 2022
1 parent a282446 commit a2ce614
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
1 change: 1 addition & 0 deletions changelog.d/13616.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a longstanding bug in `register_new_matrix_user` which meant it was always necessary to explicitly give a server URL.
57 changes: 51 additions & 6 deletions synapse/_scripts/register_new_matrix_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
import hmac
import logging
import sys
from typing import Callable, Optional
from typing import Any, Callable, Dict, Optional

import requests
import yaml

_DEFAULT_SERVER_URL = "http://localhost:8008"


def request_registration(
user: str,
Expand Down Expand Up @@ -203,31 +205,74 @@ def main() -> None:

parser.add_argument(
"server_url",
default="https://localhost:8448",
nargs="?",
help="URL to use to talk to the homeserver. Defaults to "
" 'https://localhost:8448'.",
help="URL to use to talk to the homeserver. By default, tries to find a "
"suitable URL from the configuration file. Otherwise, defaults to "
f"'{_DEFAULT_SERVER_URL}'.",
)

args = parser.parse_args()

if "config" in args and args.config:
config = yaml.safe_load(args.config)

if args.shared_secret:
secret = args.shared_secret
else:
# argparse should check that we have either config or shared secret
assert config

secret = config.get("registration_shared_secret", None)
if not secret:
print("No 'registration_shared_secret' defined in config.")
sys.exit(1)

if args.server_url:
server_url = args.server_url
elif config:

This comment has been minimized.

Copy link
@andrewshadura

andrewshadura Dec 7, 2022

Contributor

This will fail if --config has not been passed, as nothing defines config in that case.

server_url = _find_client_listener(config)
if not server_url:
server_url = _DEFAULT_SERVER_URL
print(
"Unable to find a suitable HTTP listener in the configuration file. "
f"Trying {server_url} as a last resort.",
file=sys.stderr,
)
else:
secret = args.shared_secret
server_url = _DEFAULT_SERVER_URL
print(
f"No server url or configuration file given. Defaulting to {server_url}.",
file=sys.stderr,
)

admin = None
if args.admin or args.no_admin:
admin = args.admin

register_new_user(
args.user, args.password, args.server_url, secret, admin, args.user_type
args.user, args.password, server_url, secret, admin, args.user_type
)


def _find_client_listener(config: Dict[str, Any]) -> Optional[str]:
# try to find a listener in the config. Returns a host:port pair
for listener in config.get("listeners", []):
if listener.get("type") != "http" or listener.get("tls", False):
continue

if not any(
name == "client"
for resource in listener.get("resources", [])
for name in resource.get("names", [])
):
continue

# TODO: consider bind_addresses
return f"http://localhost:{listener['port']}"

# no suitable listeners?
return None


if __name__ == "__main__":
main()

0 comments on commit a2ce614

Please sign in to comment.