Skip to content

Conversation

@migmartri
Copy link
Member

@migmartri migmartri commented Nov 8, 2025

Summary

Refactored the authorization system to improve performance and simplify architecture by moving from database-stored Casbin policies to an in-memory enforcer for role-based access control while storing API token policies directly in the database.

Benefits

  • Improved Performance: Eliminates database queries for role policy lookups during authorization checks
  • Simplified Architecture: Clear separation between user RBAC (in-memory) and token ACL (database-stored)
  • Reduced Dependencies: Removes PostgreSQL adapter requirement for Casbin
  • Flexible Token Management: Token permissions stored alongside token metadata for easier management
  • Better Maintainability: Explicit dual enforcement model that's easier to reason about

Technical Changes

  • Casbin now uses in-memory adapter for static role policies
  • API token ACL policies stored in new policies JSONB field in api_tokens table
  • Added EnforceWithPolicies method for token-based authorization checks
  • Database migration populates existing tokens with default policies
  • Updated authorization middleware to route users and tokens to appropriate enforcement methods
  • Removed database adapter dependencies and related sync logic

Testing

  • All existing tests passing
  • Updated test infrastructure to use mockery v3
  • Added test coverage for new EnforceWithPolicies method

refs #2486

@migmartri migmartri requested review from Piskoo, javirln and jiparis and removed request for javirln and jiparis November 8, 2025 00:11
@migmartri migmartri marked this pull request as draft November 8, 2025 00:11
@migmartri migmartri marked this pull request as ready for review November 8, 2025 00:49
@migmartri migmartri requested a review from javirln November 10, 2025 12:30
Signed-off-by: Miguel Martinez <miguel@chainloop.dev>
…oken policies

Refactored the authorization system to improve performance and simplify architecture by moving from database-stored Casbin policies to an in-memory enforcer for role-based access control while storing API token policies directly in the database.

Benefits:
- Improved performance by eliminating database queries for role policy lookups
- Simplified architecture with clear separation between user RBAC and token ACL
- Reduced infrastructure dependencies by removing PostgreSQL adapter requirement
- More flexible token permissions management stored alongside token metadata
- Easier to reason about authorization flow with explicit dual enforcement model

Technical changes:
- Casbin now uses in-memory adapter for static role policies
- API token ACL policies stored in new policies JSONB field
- Added EnforceWithPolicies method for token-based authorization
- Migration populates existing tokens with default policies
- Updated middleware to route users and tokens to appropriate enforcement methods
- Removed database adapter dependencies and related sync logic

Signed-off-by: Miguel Martinez <miguel@chainloop.dev>
Removed obsolete test for database sync behavior and consolidated to mockery v3 mocks package.

Changes:
- Removed TestSyncMultipleEnforcers (tested database sync, no longer relevant with in-memory enforcer)
- Migrated casbackend_test.go to use mocks package instead of mocks_test.go
- Deleted pkg/biz/mocks_test.go (replaced by pkg/biz/mocks/ package)
- Added mockery v3 and API token policies notes to CLAUDE.md

Signed-off-by: Miguel Martinez <miguel@chainloop.dev>
Signed-off-by: Miguel Martinez <miguel@chainloop.dev>
Signed-off-by: Miguel Martinez <miguel@chainloop.dev>
Signed-off-by: Miguel Martinez <miguel@chainloop.dev>
Signed-off-by: Miguel Martinez <miguel@chainloop.dev>
Signed-off-by: Miguel Martinez <miguel@chainloop.dev>
@migmartri migmartri merged commit 837d613 into chainloop-dev:main Nov 10, 2025
13 checks passed
@migmartri migmartri deleted the 2486 branch November 10, 2025 21:11
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.

2 participants