Fix RLS permissions and JWT validation for admin club creation #5

Merged
MasterMito merged 18 commits from fix/rls-permission-test-failure into epic/admin_rework_second_try 2026-03-20 11:42:05 +01:00
Owner

Summary
This PR fixes multiple issues that prevented admin users from creating clubs:

Changes

1. Fixed RLS Permission Issue in Tests

  • Added BYPASSRLS privilege to app_admin role
  • Granted full schema and table access to app_admin
  • Allow rls_test_user to assume app_admin role

2. Fixed JWT Validation

  • Added custom IssuerSigningKeyResolver to fetch JWKS from internal Keycloak URL
  • Configured MetadataAddress for Docker internal communication
  • Fixed middleware order: Authentication before TenantValidation
  • Added CORS configuration for frontend requests

3. Fixed Admin Authorization

  • Properly parse realm_access claim as JSON (not string contains)
  • Fixed RequireGlobalAdmin policy, TenantValidationMiddleware, and ClubRoleClaimsTransformation

4. Frontend Changes

  • Added .env.local with correct API URL
  • Updated displays to show member names instead of UUIDs

Testing

  • All 63 backend tests passing
  • Admin can now create clubs via UI
  • JWT tokens validated correctly
Summary This PR fixes multiple issues that prevented admin users from creating clubs: ## Changes ### 1. Fixed RLS Permission Issue in Tests - Added BYPASSRLS privilege to app_admin role - Granted full schema and table access to app_admin - Allow rls_test_user to assume app_admin role ### 2. Fixed JWT Validation - Added custom IssuerSigningKeyResolver to fetch JWKS from internal Keycloak URL - Configured MetadataAddress for Docker internal communication - Fixed middleware order: Authentication before TenantValidation - Added CORS configuration for frontend requests ### 3. Fixed Admin Authorization - Properly parse realm_access claim as JSON (not string contains) - Fixed RequireGlobalAdmin policy, TenantValidationMiddleware, and ClubRoleClaimsTransformation ### 4. Frontend Changes - Added .env.local with correct API URL - Updated displays to show member names instead of UUIDs ## Testing - All 63 backend tests passing - Admin can now create clubs via UI - JWT tokens validated correctly
MasterMito added 17 commits 2026-03-20 11:27:31 +01:00
Merge pull request 'feat: restrict admin access to club operations and rollout test environment' (#4) from epic/admin_rework_second_try into main
CI Pipeline / Backend Build & Test (push) Successful in 48s
CI Pipeline / Frontend Lint, Test & Build (push) Successful in 32s
CI Pipeline / Infrastructure Validation (push) Successful in 3s
3cf7c3a221
Reviewed-on: #4
- Add BYPASSRLS privilege to app_admin role
- Grant full schema and table access to app_admin
- Allow rls_test_user to assume app_admin role
- Fixes: permission denied for table clubs (42501)
The realm_access claim in JWT is a JSON object, not a simple string.
Previous string contains check was looking for escaped quotes in wrong format.

- Parse realm_access as JSON to extract roles array
- Check if 'admin' exists in roles array
- Fallback to string contains check if JSON parsing fails
- Applied fix in RequireGlobalAdmin policy, TenantValidationMiddleware,
  and ClubRoleClaimsTransformation

Fixes: Admin users getting 401 when trying to create clubs
- Add CORS policy to allow frontend requests from localhost:3000
- Exempt /api/debug endpoints from tenant validation
- Fix JSON parsing in realm_access claim checks
- Added JWT authentication event logging to diagnose validation failures
- Fixed docker-compose networking for API to reach Keycloak via hostname
- Debug endpoint now accessible without auth for troubleshooting
- Still investigating why claims are not populated despite token being present
The JWT middleware needs to fetch signing keys from Keycloak before
tenant validation runs. The previous order caused signature validation
to fail because the middleware was blocking the JWKS endpoint requests.

- Moved Authentication before TenantValidationMiddleware
- Removed realm endpoint from exemption list (not needed with correct order)
- This allows JWT middleware to fetch signing keys and validate tokens
- Add MetadataAddress configuration to JWT middleware for internal Docker URLs
- Add KC_HOSTNAME_ADMIN and KC_SPI_HOSTNAME_DEFAULT_ADMIN to Keycloak env
- This ensures API can fetch JWKS from Keycloak via internal Docker network
- Tests passing: 63/63
- Added IssuerSigningKeyResolver to fetch JWKS directly from internal Keycloak URL
- This bypasses the localhost:8080 URLs in Keycloak's discovery document
- Ensures JWT tokens are validated against correct signing keys
MasterMito added 1 commit 2026-03-20 11:37:05 +01:00
The test was expecting Forbidden when no tenant context is provided,
but the middleware actually returns BadRequest when X-Tenant-Id header
is missing. Updated the test and added GetClubsCurrent_InvalidTenant_ReturnsForbidden
to properly test the Forbidden case.
MasterMito merged commit 984ab77137 into epic/admin_rework_second_try 2026-03-20 11:42:05 +01:00
MasterMito deleted branch fix/rls-permission-test-failure 2026-03-20 11:42:06 +01:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: MasterMito/work-club-manager#5