register-new-matrix-user: add a flag to ignore already existing users (#17304)

Co-authored-by: Andrew Morgan <andrew@amorgan.xyz>
This commit is contained in:
Jörg Thalheim 2024-06-19 13:03:08 +02:00 committed by GitHub
parent 9104a9f0d0
commit c99203d98c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 34 additions and 3 deletions

View file

@ -0,0 +1,2 @@
`register_new_matrix_user` now supports a --exists-ok flag to allow registration of users that already exist in the database.
This is useful for scripts that bootstrap user accounts with initial passwords.

2
debian/changelog vendored
View file

@ -1,6 +1,6 @@
matrix-synapse-py3 (1.109.0+nmu1) UNRELEASED; urgency=medium matrix-synapse-py3 (1.109.0+nmu1) UNRELEASED; urgency=medium
* `register_new_matrix_user` now supports a --password-file flag. * `register_new_matrix_user` now supports a --password-file and a --exists-ok flag.
-- Synapse Packaging team <packages@matrix.org> Tue, 18 Jun 2024 13:29:36 +0100 -- Synapse Packaging team <packages@matrix.org> Tue, 18 Jun 2024 13:29:36 +0100

View file

@ -48,6 +48,9 @@ A sample YAML file accepted by `register_new_matrix_user` is described below:
Shared secret as defined in server config file. This is an optional Shared secret as defined in server config file. This is an optional
parameter as it can be also supplied via the YAML file. parameter as it can be also supplied via the YAML file.
* `--exists-ok`:
Do not fail if the user already exists. The user account will be not updated in this case.
* `server_url`: * `server_url`:
URL of the home server. Defaults to 'https://localhost:8448'. URL of the home server. Defaults to 'https://localhost:8448'.

View file

@ -52,6 +52,7 @@ def request_registration(
user_type: Optional[str] = None, user_type: Optional[str] = None,
_print: Callable[[str], None] = print, _print: Callable[[str], None] = print,
exit: Callable[[int], None] = sys.exit, exit: Callable[[int], None] = sys.exit,
exists_ok: bool = False,
) -> None: ) -> None:
url = "%s/_synapse/admin/v1/register" % (server_location.rstrip("/"),) url = "%s/_synapse/admin/v1/register" % (server_location.rstrip("/"),)
@ -97,6 +98,10 @@ def request_registration(
r = requests.post(url, json=data) r = requests.post(url, json=data)
if r.status_code != 200: if r.status_code != 200:
response = r.json()
if exists_ok and response["errcode"] == "M_USER_IN_USE":
_print("User already exists. Skipping.")
return
_print("ERROR! Received %d %s" % (r.status_code, r.reason)) _print("ERROR! Received %d %s" % (r.status_code, r.reason))
if 400 <= r.status_code < 500: if 400 <= r.status_code < 500:
try: try:
@ -115,6 +120,7 @@ def register_new_user(
shared_secret: str, shared_secret: str,
admin: Optional[bool], admin: Optional[bool],
user_type: Optional[str], user_type: Optional[str],
exists_ok: bool = False,
) -> None: ) -> None:
if not user: if not user:
try: try:
@ -154,7 +160,13 @@ def register_new_user(
admin = False admin = False
request_registration( request_registration(
user, password, server_location, shared_secret, bool(admin), user_type user,
password,
server_location,
shared_secret,
bool(admin),
user_type,
exists_ok=exists_ok,
) )
@ -173,6 +185,11 @@ def main() -> None:
default=None, default=None,
help="Local part of the new user. Will prompt if omitted.", help="Local part of the new user. Will prompt if omitted.",
) )
parser.add_argument(
"--exists-ok",
action="store_true",
help="Do not fail if user already exists.",
)
password_group = parser.add_mutually_exclusive_group() password_group = parser.add_mutually_exclusive_group()
password_group.add_argument( password_group.add_argument(
"-p", "-p",
@ -192,6 +209,7 @@ def main() -> None:
default=None, default=None,
help="User type as specified in synapse.api.constants.UserTypes", help="User type as specified in synapse.api.constants.UserTypes",
) )
admin_group = parser.add_mutually_exclusive_group() admin_group = parser.add_mutually_exclusive_group()
admin_group.add_argument( admin_group.add_argument(
"-a", "-a",
@ -281,7 +299,15 @@ def main() -> None:
if args.admin or args.no_admin: if args.admin or args.no_admin:
admin = args.admin admin = args.admin
register_new_user(args.user, password, server_url, secret, admin, args.user_type) register_new_user(
args.user,
password,
server_url,
secret,
admin,
args.user_type,
exists_ok=args.exists_ok,
)
def _read_file(file_path: Any, config_path: str) -> str: def _read_file(file_path: Any, config_path: str) -> str: