fix: bootstrap initial admin for RBAC role system - #592
Open
Imran2210 wants to merge 6 commits into
Open
Conversation
|
@Imran2210 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
This PR fixes the RBAC bootstrap deadlock: after a fresh deploy, no user had the
adminrole, soPOST /v1/rolescould never be called successfully. The fix seeds an initial admin fromINITIAL_ADMIN_USER_ID, adds a first-admin bootstrap path when the role map is empty, and wires RBAC middleware into protected routes so the role system is no longer dead code.Related Issue
Changes
🚀 Initial Admin Bootstrapping
[ADD]
src/config.tsinitialAdminUserIdparsed fromINITIAL_ADMIN_USER_ID.[ADD]
src/lib/roles.tsseedInitialAdmin(userId)which callsassignRole(userId, "admin")only when no admin exists.hasRole(userId, role)so middleware and routes share one role-map lookup.[MODIFY]
src/index.tsseedInitialAdmin(config.initialAdminUserId)during app startup so a fresh deploy has an admin user.[MODIFY]
src/routes/roles.tsPOST /v1/rolesto succeed whenlistRoles().length === 0, even without an existing admin.requireRole("admin").🔐 RBAC Middleware Enforcement
[MODIFY]
src/middleware/rbac.tsidentifyUserpopulates the current user from the auth token.requireAuthreturns401for missing or invalid credentials.requireRole("admin")returns403when the identified user lacks the required role.[MODIFY]
src/index.ts/src/routes/roles.ts📄 Configuration Docs
.env.exampleINITIAL_ADMIN_USER_IDwith a placeholder value for fresh deployments.Verification Results
INITIAL_ADMIN_USER_IDis seeded into the role map at startupPOST /v1/rolescan be called successfully after fresh deploy201when role map is empty403once roles already existrequireAuth/requireRole/identifyUserare wired to protected endpointsCloses #524