Skip to content

Commit 7205268

Browse files
committed
fix: use federated-identity endpoint in all_github_identities
Previously this called a_get_user() for every user in Keycloak to read federatedIdentities, but the /users list endpoint doesn't include that field so we were fetching the full UserRepresentation (including heavy userProfileMetadata) for every user. This runs every 5 minutes and was a likely contributor to OOM kills. Switch to the dedicated /users/{id}/federated-identity endpoint which returns only the identity records needed.
1 parent b05d0d1 commit 7205268

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

arthur/apis/directory/keycloak.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
from functools import cache
44

5-
from keycloak import KeycloakAdmin
5+
from keycloak import KeycloakAdmin, urls_patterns
6+
from keycloak.exceptions import KeycloakGetError, raise_error_from_response
67

78
from arthur.config import CONFIG
89

@@ -79,15 +80,20 @@ async def get_user_github_id(username: str) -> str | None:
7980
async def all_github_identities() -> dict[str, dict[str, str]]:
8081
"""Fetch Keycloak usernames and their linked GitHub identity information."""
8182
client = create_client()
82-
8383
users = await client.a_get_users()
8484
github_identities = {}
8585

8686
for user in users:
87-
user_details = await client.a_get_user(user["id"])
88-
for ident in user_details["federatedIdentities"]:
87+
url = urls_patterns.URL_ADMIN_USER_FEDERATED_IDENTITIES.format(
88+
**{"realm-name": client.connection.realm_name, "id": user["id"]}
89+
)
90+
identities = raise_error_from_response(
91+
await client.connection.a_raw_get(url),
92+
KeycloakGetError,
93+
)
94+
for ident in identities:
8995
if ident["identityProvider"] == "github":
90-
github_identities[user_details["username"]] = {
96+
github_identities[user["username"]] = {
9197
"user_id": ident.get("userId", ""),
9298
"user_name": ident.get("userName", ""),
9399
}

0 commit comments

Comments
 (0)