Skip to content

feat(auth): create Keycloak realm roles for RBAC - #671

Closed
pdettori wants to merge 2 commits into
rossoctl:mainfrom
pdettori:api-auth/3-keycloak-roles
Closed

pdettori wants to merge 2 commits into
rossoctl:mainfrom
pdettori:api-auth/3-keycloak-roles

Conversation

@pdettori

Copy link
Copy Markdown
Member

Summary

Phase 3 of API bearer token authentication (Epic #639).

⚠️ Stacked PR: This PR depends on #648 (Phase 2: Protect Backend Endpoints). Please merge Phase 2 first.

This PR ensures the three RBAC roles exist in Keycloak by extending the existing agent-oauth-secret job.

Changes

  • agent_oauth_secret.py:
    • Add ROLE_VIEWER, ROLE_OPERATOR, ROLE_ADMIN constants (matching backend)
    • Add create_realm_roles() method to create roles in Keycloak realm
    • Add assign_role_to_user() method to assign realm roles to users
    • Call create_realm_roles(RBAC_ROLES) after realm creation
    • Assign kagenti-admin role to test user for full API access

Testing

Deploy to dev cluster and verify:

  • Roles appear in Keycloak Admin UI under Realm Roles
  • Test user has kagenti-admin role assigned
  • Roles can be assigned to other users via Keycloak Admin

Stacked PR

This is PR 3/5 in the API authentication stack:

  1. #647 RBAC Foundation ✅
  2. #648 Protect Backend Endpoints ← Merge first
  3. This PR - Keycloak Realm Roles
  4. API OAuth Secret Job
  5. Documentation

🤖 Generated with Claude Code

Phase 3 of API bearer token authentication (Epic rossoctl#639).

- Add RBAC role constants: kagenti-viewer, kagenti-operator, kagenti-admin
- Add create_realm_roles() to create roles in Keycloak realm
- Add assign_role_to_user() to assign realm roles to users
- Create RBAC roles during agent-oauth-secret job execution
- Assign kagenti-admin role to test user for full API access

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Extends the existing agent-oauth-secret Keycloak bootstrap job to ensure Kagenti’s RBAC realm roles exist in Keycloak (and to grant the admin role to the configured test user), supporting Phase 3 of the bearer-token/RBAC rollout.

Changes:

  • Add RBAC role constants (kagenti-viewer/operator/admin) and an RBAC_ROLES list.
  • Add Keycloak helpers to create realm roles and assign a realm role to a user.
  • Invoke role creation after realm creation and assign kagenti-admin to the test user.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

- Move change_current_realm() inside try blocks for proper error handling
- Use skip_exists=False for create_realm_role to avoid misleading log
- Gate admin role assignment behind KEYCLOAK_TEST_USER_ADMIN env var

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
@pdettori

Copy link
Copy Markdown
Member Author

all All 4 copilot review comments addressed:


  ┌────────────────────────────────────────────────────────────────────┬──────────────────────────────────────────────────────────────────────────┐
  │                              Comment                               │                                   Fix                                    │
  ├────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────────┤
  │ create_realm_roles: change_current_realm() outside try block       │ Moved inside try with error handling                                     │
  ├────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────────┤
  │ create_realm_roles: skip_exists=True with misleading "Created" log │ Changed to skip_exists=False, relies on 409 handling                     │
  ├────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────────┤
  │ assign_role_to_user: change_current_realm() outside try block      │ Moved inside try block                                                   │
  ├────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────────┤
  │ Admin role assignment is risky                                     │ Gated behind KEYCLOAK_TEST_USER_ADMIN env var (defaults to true for dev) │
  └────────────────────────────────────────────────────────────────────┴──────────────────────────────────────────────────────────────────────────┘

@pdettori pdettori moved this from Backlog to In review / Needs Testing in Rossoctl Issue Prioritization Feb 12, 2026
@pdettori pdettori added Ready for Review! safe-to-test Maintainer reviewed - safe to run E2E tests labels Feb 12, 2026
@pdettori
pdettori changed the base branch from api-auth/2-protect-endpoints to main February 13, 2026 00:00

@maia-iyer maia-iyer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@pdettori

Copy link
Copy Markdown
Member Author

Closing in favor of #672, which now includes realm role creation directly in the API OAuth secret job. This is a better fit since the RBAC roles are specific to backend API authorization, not agent auth.

@pdettori pdettori closed this Feb 13, 2026
@github-project-automation github-project-automation Bot moved this from In review / Needs Testing to Done in Rossoctl Issue Prioritization Feb 13, 2026
@pdettori
pdettori deleted the api-auth/3-keycloak-roles branch April 23, 2026 19:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Ready for Review! safe-to-test Maintainer reviewed - safe to run E2E tests

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants