Skip to content

feat(auth): add RBAC role constants and hierarchy support - #647

Merged
pdettori merged 2 commits into
rossoctl:mainfrom
pdettori:api-auth/1-rbac-foundation
Feb 11, 2026
Merged

pdettori merged 2 commits into
rossoctl:mainfrom
pdettori:api-auth/1-rbac-foundation

Conversation

@pdettori

@pdettori pdettori commented Feb 10, 2026 •

Copy link
Copy Markdown
Member

Summary

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

This PR adds the foundation for Role-Based Access Control (RBAC) without protecting any endpoints yet - pure infrastructure.

Changes

  • Role Constants: Define ROLE_VIEWER, ROLE_OPERATOR, ROLE_ADMIN constants matching Keycloak role names
  • Role Hierarchy: Implement hierarchy where kagenti-admin inherits kagenti-operator inherits kagenti-viewer
  • get_effective_roles(): New function to expand roles via hierarchy
  • TokenData.has_role(): Updated to check hierarchy (admin can satisfy viewer requirements)
  • Mock User: Dev mode mock user now uses kagenti-admin role
  • Unit Tests: 17 comprehensive tests for role hierarchy logic

Testing

cd kagenti/backend
uv run pytest tests/test_auth.py -v

All 19 tests pass.

Test Plan

  • Unit tests for role constants
  • Unit tests for role hierarchy expansion
  • Unit tests for TokenData.has_role with hierarchy
  • Verify dev mode still works (mock user has admin role)

Stacked PR

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

  1. This PR - RBAC Foundation
  2. Protect Backend Endpoints
  3. Keycloak Realm Roles
  4. API OAuth Secret Job
  5. Documentation

Fixes #640

🤖 Generated with Claude Code

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

Adds foundational RBAC infrastructure for upcoming bearer-token authentication work by introducing explicit role constants, a role hierarchy expansion helper, and updating role checks to respect inheritance.

Changes:

  • Define ROLE_VIEWER / ROLE_OPERATOR / ROLE_ADMIN constants plus ROLE_HIERARCHY and get_effective_roles().
  • Update TokenData.has_role() to check roles using hierarchy expansion; dev-mode mock user now uses ROLE_ADMIN.
  • Add unit tests covering constants, hierarchy expansion, and TokenData.has_role() behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
kagenti/backend/app/core/auth.py Introduces RBAC constants/hierarchy + role expansion and updates role checking and dev-mode mock roles.
kagenti/backend/tests/test_auth.py Adds unit tests for role constants, hierarchy expansion, and hierarchy-aware role checks.

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

Comment thread kagenti/backend/app/core/auth.py Resolved
Comment thread kagenti/backend/tests/test_auth.py Resolved
@pdettori
pdettori force-pushed the api-auth/1-rbac-foundation branch from 764b597 to cc4f731 Compare February 10, 2026 21:49
Phase 1 of API bearer token authentication (epic rossoctl#639).

Add foundation for Role-Based Access Control:
- Define ROLE_VIEWER, ROLE_OPERATOR, ROLE_ADMIN constants
- Implement role hierarchy with transitive expansion
  (admin -> operator -> viewer, walks graph until no new roles)
- Add get_effective_roles() with cycle-safe graph traversal
- Update TokenData.has_role() to check hierarchy
- Update mock user in dev mode to use kagenti-admin role
- Add comprehensive unit tests for role hierarchy logic (19 tests)

No endpoints are protected yet - this is pure infrastructure.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
@pdettori
pdettori force-pushed the api-auth/1-rbac-foundation branch from cc4f731 to 3a60cab Compare February 10, 2026 21:51
Copilot AI review requested due to automatic review settings February 10, 2026 21:51

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

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.


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

Comment thread kagenti/backend/tests/test_auth.py Resolved
Comment thread kagenti/backend/app/core/auth.py Resolved
@pdettori pdettori moved this from Backlog to In review / Needs Testing in Rossoctl Issue Prioritization Feb 10, 2026
@pdettori
pdettori force-pushed the api-auth/1-rbac-foundation branch from 3a60cab to d2428fa Compare February 11, 2026 02:18
Compute effective roles (with hierarchy expansion) once at TokenData
initialization instead of on every has_role() call. This avoids
redundant recomputation when multiple role checks occur per request.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
@pdettori
pdettori force-pushed the api-auth/1-rbac-foundation branch from d2428fa to 6b3d5c8 Compare February 11, 2026 02:26

@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
pdettori merged commit bee59c1 into rossoctl:main Feb 11, 2026
15 checks passed
@github-project-automation github-project-automation Bot moved this from In review / Needs Testing to Done in Rossoctl Issue Prioritization Feb 11, 2026
@pdettori
pdettori deleted the api-auth/1-rbac-foundation branch February 11, 2026 20:04
pdettori added a commit to pdettori/rossoctl that referenced this pull request Apr 13, 2026
)

Non-admin users were denied access to tools, agents, and observability
pages because validate_token only mapped the 'admin' realm role to
kagenti-admin but gave no kagenti-* role to regular users. All
require_roles(ROLE_VIEWER) checks returned 403.

Add a default mapping that gives every authenticated user the
kagenti-viewer role, matching the current intent that all users can
view resources. This is part of the temporary mapping (see issue rossoctl#647)
that will be replaced by explicit Keycloak role provisioning.

Closes rossoctl#1130

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Implement Backend RBAC Foundation

3 participants