Skip to content

fix: improve realm_exists method by using a lightweight endpoint - #33

Merged
remdub merged 2 commits into
mainfrom
realm_exists
Jul 13, 2026
Merged

fix: improve realm_exists method by using a lightweight endpoint#33
remdub merged 2 commits into
mainfrom
realm_exists

Conversation

@remdub

@remdub remdub commented Jul 13, 2026

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • Bug Fixes

    • Improved realm validation to more reliably confirm both realm availability and administrative access.
    • Reduced unnecessary overhead when checking whether a realm exists.
  • Documentation

    • Updated the upcoming release notes to reflect the improved realm existence check.

@coderabbitai

coderabbitai Bot commented Jul 13, 2026

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 116c8ae6-73c7-4372-baa5-a922800e928f

📥 Commits

Reviewing files that changed from the base of the PR and between bede8f1 and e928a3b.

📒 Files selected for processing (1)
  • src/pas/plugins/kimug/utils.py

📝 Walkthrough

Walkthrough

realm_exists now checks realm availability with a lightweight unauthenticated HEAD request, then verifies admin access through an authenticated user-count request. The unreleased changelog documents this endpoint change.

Changes

Realm validation

Layer / File(s) Summary
Realm existence and admin access check
src/pas/plugins/kimug/utils.py, CHANGES.md
realm_exists performs separate existence and admin-access checks, logs HTTP failures, returns False for non-200 responses, and documents the lightweight endpoint update.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: improving realm_exists by switching to a lighter endpoint.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch realm_exists

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/pas/plugins/kimug/utils.py`:
- Around line 640-642: Update the admin-access probe described near the users
migration flow to use an endpoint requiring the same create/update-user
privileges as the migration’s write path, rather than relying on /users/count;
alternatively, rename the method and adjust its contract to explicitly promise
only user-count access if that is the intended behavior.
- Around line 649-650: Update the requests.head call in the realm URL probe to
explicitly enable redirects with allow_redirects=True, while preserving the
existing timeout and non-200 status handling.
- Around line 649-650: Update realm_exists to call requests.head with redirects
enabled and handle RequestException for both the HEAD and GET requests,
returning the existing failure result instead of propagating network errors.
Replace reliance on /admin/realms/{realm}/users/count with a check that verifies
full realm-management access, avoiding false positives from
view-users/query-users permissions.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 35192c23-da6b-4be5-a98a-ae31b77e8c86

📥 Commits

Reviewing files that changed from the base of the PR and between 1bb7179 and bede8f1.

📒 Files selected for processing (2)
  • CHANGES.md
  • src/pas/plugins/kimug/utils.py

Comment on lines +640 to +642
(tiny cached endpoint, no RealmRepresentation rebuild) followed by a GET on
/admin/realms/{realm}/users/count to confirm effective admin access with a
minimal integer payload.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔒 Security & Privacy | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Map the file first, then inspect the relevant sections.
wc -l src/pas/plugins/kimug/utils.py
ast-grep outline src/pas/plugins/kimug/utils.py --view expanded

sed -n '600,700p' src/pas/plugins/kimug/utils.py
rg -n "users/count|view-users|manage-users|realm-management|can count users|count users" src/pas/plugins/kimug/utils.py

Repository: IMIO/pas.plugins.kimug

Length of output: 7131


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Inspect callers and nearby logic for realm_exists and admin operations.
rg -n "realm_exists\(" -n src/pas/plugins/kimug/utils.py src -g '*.py'
sed -n '430,520p' src/pas/plugins/kimug/utils.py
sed -n '700,740p' src/pas/plugins/kimug/utils.py

Repository: IMIO/pas.plugins.kimug

Length of output: 5541


🌐 Web query:

Keycloak users/count endpoint permissions view-users manage-users realm-management docs

💡 Result:

To access the Keycloak users/count endpoint via the Admin REST API, a user must possess sufficient permissions granted through the realm-management client [1]. Permission Requirements: The count operation on the Users resource requires permissions equivalent to querying or viewing users [2]. Specifically, the evaluator for this resource requires that the caller has at least one of the following administrative roles, which are defined within the realm-management client [2][1]: 1. view-users: Allows an administrator to view all users and groups in the realm [3][4]. 2. manage-users: Provides broader access, including all privileges granted by view-users, as well as the ability to manage user memberships, roles, and groups [3][4]. 3. query-users: A more specific role that provides permission to search and query user information [4]. Technical Implementation: The UsersResource API class handles these requests, and the underlying logic (UserPermissionEvaluator) verifies that the caller has at least one of these roles [2][5][6]. If the feature is enabled, fine-grained authorization (FGAP) may also grant access if specific permissions are configured for the user resource [3][2]. In standard configurations, possessing the view-users or manage-users role is sufficient to successfully call the count endpoint [2][5]. If a user lacks these roles, the API will return a 403 Forbidden status [7]. Ensure that the service account or user account being used to authenticate the API call is mapped to at least one of these roles under the realm-management client for the respective realm [4][1].

Citations:


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Inspect the Keycloak user access path that follows realm_exists.
sed -n '220,360p' src/pas/plugins/kimug/utils.py
sed -n '360,470p' src/pas/plugins/kimug/utils.py
sed -n '470,620p' src/pas/plugins/kimug/utils.py

Repository: IMIO/pas.plugins.kimug

Length of output: 17141


Use a check that matches the migration’s write path. /users/count can return 200 with only view/query-user privileges, but this flow later creates and updates users. Probe an endpoint that requires the same admin rights as the migration, or rename this method so it only promises user-count access.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/pas/plugins/kimug/utils.py` around lines 640 - 642, Update the
admin-access probe described near the users migration flow to use an endpoint
requiring the same create/update-user privileges as the migration’s write path,
rather than relying on /users/count; alternatively, rename the method and adjust
its contract to explicitly promise only user-count access if that is the
intended behavior.

Comment thread src/pas/plugins/kimug/utils.py Outdated
@remdub
remdub merged commit 16a0137 into main Jul 13, 2026
7 of 8 checks passed
@remdub
remdub deleted the realm_exists branch July 13, 2026 12:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant