Skip to content

Commit 9c3e5fa

Browse files
committed
refactor: migrate Casbin to in-memory enforcer with database-stored token 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>
1 parent 2864ed0 commit 9c3e5fa

File tree

3 files changed

+218
-0
lines changed

3 files changed

+218
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
all: false
2+
dir: '{{.InterfaceDir}}'
3+
filename: mocks_test.go
4+
force-file-write: true
5+
formatter: goimports
6+
include-auto-generated: false
7+
log-level: info
8+
structname: '{{.Mock}}{{.InterfaceName}}'
9+
pkgname: '{{.SrcPackageName}}'
10+
recursive: false
11+
require-template-schema-exists: true
12+
template: testify
13+
template-schema: '{{.Template}}.schema.json'
14+
packages:
15+
github.com/chainloop-dev/chainloop/app/controlplane/pkg/authz/middleware:
16+
interfaces:
17+
Enforcer:

app/controlplane/pkg/authz/middleware/mocks_test.go

Lines changed: 175 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
-- Modify "api_tokens" table
2+
ALTER TABLE "api_tokens" ADD COLUMN "policies" jsonb NULL;
3+
-- Populate existing tokens with default policies
4+
UPDATE "api_tokens" SET "policies" = '[
5+
{"Resource": "workflow_run", "Action": "list"},
6+
{"Resource": "workflow_run", "Action": "read"},
7+
{"Resource": "workflow", "Action": "read"},
8+
{"Resource": "workflow", "Action": "list"},
9+
{"Resource": "workflow", "Action": "create"},
10+
{"Resource": "workflow_contract", "Action": "list"},
11+
{"Resource": "workflow_contract", "Action": "read"},
12+
{"Resource": "workflow_contract", "Action": "update"},
13+
{"Resource": "workflow_contract", "Action": "create"},
14+
{"Resource": "cas_artifact", "Action": "read"},
15+
{"Resource": "referrer", "Action": "read"},
16+
{"Resource": "organization", "Action": "read"},
17+
{"Resource": "robot_account", "Action": "create"},
18+
{"Resource": "integration_available", "Action": "read"},
19+
{"Resource": "integration_available", "Action": "list"},
20+
{"Resource": "integration_registered", "Action": "list"},
21+
{"Resource": "integration_registered", "Action": "read"},
22+
{"Resource": "integration_registered", "Action": "create"},
23+
{"Resource": "integration_attached", "Action": "list"},
24+
{"Resource": "integration_attached", "Action": "create"},
25+
{"Resource": "cas_artifact", "Action": "create"}
26+
]'::jsonb WHERE "policies" IS NULL;

0 commit comments

Comments
 (0)