feat: Phase 1 Week 3 - Role-Based Access Control (RBAC) #169
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.
Date: 16 Jan 2025
Developer Name: [Your Name]
Description
This PR implements Phase 1, Week 3: Role-Based Access Control (RBAC) for the Feature Flag Backend. It adds comprehensive authorization based on user roles (ADMIN, DEVELOPER, VIEWER) and protects all endpoints with appropriate permission checks.
Key Features Implemented:
RBAC Permission System
RBAC Helper Functions
RequirePermission(): Checks if user has specific permissionRequireAnyPermission(): Checks if user has any of the provided permissionsCanAccessUserResource(): Validates resource ownership (users can access own resources, ADMIN can access all)Feature Flag Endpoints Protection
GET /feature-flags- RequiresREAD_FEATURE_FLAGpermissionGET /feature-flags/{flagId}- RequiresREAD_FEATURE_FLAGpermissionPOST /feature-flags- RequiresCREATE_FEATURE_FLAGpermission (ADMIN, DEVELOPER)PATCH /feature-flags/{flagId}- RequiresUPDATE_FEATURE_FLAGpermission (ADMIN, DEVELOPER)User Feature Flag Mapping Endpoints Protection
GET /users/{userId}/feature-flags- RequiresREAD_USER_MAPPING+ resource ownership checkGET /users/{userId}/feature-flags/{flagId}- RequiresREAD_USER_MAPPING+ resource ownership checkPOST /users/{userId}/feature-flags/{flagId}- RequiresCREATE_USER_MAPPING+ resource ownership checkPATCH /users/{userId}/feature-flags/{flagId}- RequiresUPDATE_USER_MAPPING+ resource ownership checkUser Management Endpoints Protection
GET /users/{userId}- RequiresREAD_USER+ resource ownership checkPUT /users/{userId}- RequiresUPDATE_USER+ resource ownership checkSecurity Enhancements
JWTMiddlewareWithUserVerification()for enhanced securityPermission Matrix:
Documentation Updated?
Under Feature Flag
Database Changes
Database Changes:
usertable with role fieldBreaking Changes
Breaking Changes:
Request Body Changes:
CreateFeatureFlagRequest.userId- Now optional (removed from request, uses authenticated user)UpdateFeatureFlagRequest.userId- Now optional (removed from request, uses authenticated user)CreateFeatureFlagUserMappingRequest.userId- Now optional (removed from request, uses authenticated user)UpdateFeatureFlagUserMappingRequest.userId- Now optional (removed from request, uses authenticated user)Authorization Changes:
Development Tested?
Testing:
Screenshots
Screenshot 1
Test Coverage
Test Coverage Details
Additional Notes
API Changes
Before:
POST /feature-flags/
{
"name": "new-feature",
"description": "Description",
"userId": "user-123" // Required
}After:
POST /feature-flags/
{
"name": "new-feature",
"description": "Description"
// userId automatically extracted from authenticated token
// Requires CREATE_FEATURE_FLAG permission (ADMIN or DEVELOPER)
}### Usage Example
Permission Check:
// Check if user has permission
permResponse, err := utils.RequirePermission(userContext, utils.PermissionCreateFeatureFlag)
if err != nil || permResponse.StatusCode != http.StatusOK {
return permResponse, err
}
// Check resource ownership
if !utils.CanAccessUserResource(userContext, resourceUserId) {
return events.APIGatewayProxyResponse{
StatusCode: http.StatusForbidden,
Body: "You can only access your own resources",
}, nil
}### Role Behavior Examples
VIEWER trying to create feature flag:
403 Forbidden: Insufficient permissionsDEVELOPER trying to update another user's profile:
403 Forbidden: You can only update your own profileVIEWER trying to access another user's feature flags:
403 Forbidden: You can only access your own feature flag mappingsNon-ADMIN trying to change user role:
403 Forbidden: Only ADMIN can update user rolesMigration Notes
Next Steps (Week 4)
Security Considerations
Files Changed
layer/utils/RBAC.go(new - RBAC permission system)getAllFeatureFlags/main.go(updated - added RBAC)getFeatureFlagById/main.go(updated - added RBAC)createFeatureFlag/main.go(updated - added RBAC)updateFeatureFlag/main.go(updated - added RBAC, migrated to new middleware)getUserFeatureFlags/main.go(updated - added RBAC + ownership check)getUserFeatureFlag/main.go(updated - added RBAC + ownership check)createUserFeatureFlag/main.go(updated - added RBAC + ownership check)updateUserFeatureFlag/main.go(updated - added RBAC + ownership check)getUserById/main.go(updated - added RBAC + ownership check)updateUser/main.go(updated - added RBAC + ownership check + role restrictions)layer/utils/RequestResponse.go(updated - made userId optional in requests)Migration Path: