From 53e8d3ea45cf40def79691d0a889fab875453b46 Mon Sep 17 00:00:00 2001 From: Paul Bienkowski Date: Wed, 17 Nov 2021 19:56:40 +0100 Subject: [PATCH] Improve keycloak user export further --- api/obs/api/routes/login.py | 9 +++++++- api/obs/bin/openbikesensor_api.py | 7 ++++++ api/tools/import_from_mongodb.py | 38 ++++++++++++++++++------------- 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/api/obs/api/routes/login.py b/api/obs/api/routes/login.py index dbb5b97..1132e37 100644 --- a/api/obs/api/routes/login.py +++ b/api/obs/api/routes/login.py @@ -93,7 +93,14 @@ async def login_redirect(req): ).scalar() if user: + log.info( + "Re-matched existing user %s (sub: %s) based on email and username (%s)", + user.id, + user.sub, + preferred_username, + ) user.match_by_username_email = False + user.sub = sub if user is None: log.info( @@ -104,7 +111,7 @@ async def login_redirect(req): user = User(sub=sub, username=preferred_username, email=email) req.ctx.db.add(user) else: - log.info("Logged in known user (id: %s).", user.id) + log.info("Logged in known user (id: %s, sub: %s).", user.id, user.sub) if email != user.email: log.debug("Updating user (id: %s) email from auth system.", user.id) diff --git a/api/obs/bin/openbikesensor_api.py b/api/obs/bin/openbikesensor_api.py index 4079901..c213303 100755 --- a/api/obs/bin/openbikesensor_api.py +++ b/api/obs/bin/openbikesensor_api.py @@ -4,6 +4,7 @@ import sys import os import argparse import asyncio +import logging from obs.api.app import app from obs.api.db import connect_db @@ -11,6 +12,12 @@ from obs.api.db import connect_db def main(): debug = app.config.DEBUG + + logging.basicConfig( + level=logging.DEBUG if debug else logging.INFO, + format="%(levelname)s: %(message)s", + ) + app.run( host=app.config.HOST, port=app.config.PORT, diff --git a/api/tools/import_from_mongodb.py b/api/tools/import_from_mongodb.py index 8977019..926bca6 100644 --- a/api/tools/import_from_mongodb.py +++ b/api/tools/import_from_mongodb.py @@ -61,7 +61,6 @@ async def import_users(mongo, session, keycloak_users_file): new_user = User( sub=str(uuid4()), - match_by_username_email=True, email=user["email"], username=user["username"], bio=user.get("bio"), @@ -70,22 +69,24 @@ async def import_users(mongo, session, keycloak_users_file): api_key=str(user["_id"]), created_at=user.get("createdAt") or datetime.utcnow(), updated_at=user.get("updatedAt") or datetime.utcnow(), + match_by_username_email=True, ) - needs_email_verification = user.get("needsEmailValidation", True) - required_actions = ["UPDATE_PASSWORD"] - if needs_email_verification: - required_actions.append("VERIFY_EMAIL") + if keycloak_users_file: + needs_email_verification = user.get("needsEmailValidation", True) + required_actions = ["UPDATE_PASSWORD"] + if needs_email_verification: + required_actions.append("VERIFY_EMAIL") - keycloak_users.append( - { - "username": user["username"], - "email": user["email"], - "enabled": True, - "requiredActions": required_actions, - "emailVerified": not needs_email_verification, - } - ) + keycloak_users.append( + { + "username": new_user.username, + "email": new_user.email, + "enabled": True, + "requiredActions": required_actions, + "emailVerified": not needs_email_verification, + } + ) session.add(new_user) log.info("Creating user %s", new_user.username) @@ -98,7 +99,7 @@ async def import_users(mongo, session, keycloak_users_file): id_map[old_id_by_email[user.email]] = user.id if keycloak_users_file: - json.dump(keycloak_users, keycloak_users_file, indent=4) + json.dump({"users": keycloak_users}, keycloak_users_file, indent=4) log.info("Wrote keycloak users file to %s.", keycloak_users_file.name) return id_map @@ -111,6 +112,8 @@ def parse_datetime(s): async def import_tracks(mongo, session, user_id_map): + track_count = 0 + async for track in mongo.tracks.find({}): stats = track.get("statistics") or {} new_track = Track( @@ -139,7 +142,6 @@ async def import_tracks(mongo, session, user_id_map): ) session.add(new_track) - log.info("Creating track %s", new_track.slug) comment_ids = track.get("comments") or [] if comment_ids: @@ -155,6 +157,10 @@ async def import_tracks(mongo, session, user_id_map): new_track.comments.append(new_comment) session.add(new_comment) + track_count += 1 + + log.info("Created %s tracks", track_count) + await session.commit()