12 KiB
RBAC Deployment Guide
Pre-Deployment Checklist
1. Verify Implementation
- ✅ All backend files pass syntax check
- ✅ Frontend components have no errors
- ✅ Translations complete (EN/RO)
- ✅ Documentation finalized
2. Database Backup
# Create backup before deploying RBAC changes
docker exec streamflow-backend npm run backup:create
3. Review Configuration
# Ensure JWT_SECRET is set
echo $JWT_SECRET
# Verify NODE_ENV for production
echo $NODE_ENV
Deployment Steps
Step 1: Stop Application
cd /home/iulian/projects/tv
docker-compose down
Step 2: Pull Latest Changes
git pull origin main
# or copy updated files manually
Step 3: Rebuild Docker Containers
docker-compose build --no-cache
Step 4: Start Application
docker-compose up -d
Step 5: Verify Database Initialization
# Check logs for RBAC initialization
docker-compose logs backend | grep -i "role"
# Expected output:
# ✓ Role seeded: admin
# ✓ Role seeded: moderator
# ✓ Role seeded: user
# ✓ Role seeded: viewer
# ✓ Permission audit log table created
Step 6: Verify Routes
# Test RBAC API endpoints
curl -H "Authorization: Bearer YOUR_ADMIN_TOKEN" \
http://localhost:5000/api/rbac/roles
# Should return array of 4 default roles
Post-Deployment Testing
1. Login as Admin
- Navigate to
/login - Login with admin credentials
- Access
/security/rbac - Verify RBAC Dashboard loads
2. Test Role Management
Create Custom Role:
- Click "Create Role" button
- Fill in:
- Role Key:
content_editor - Name:
Content Editor - Description:
Manages playlists and channels
- Role Key:
- Select permissions:
playlists.view,playlists.create,playlists.editchannels.view,channels.edit
- Click "Create"
- Verify role appears in list
Edit Role:
- Click edit icon on custom role
- Add permission:
playlists.delete - Click "Save"
- Verify permissions updated
Delete Role:
- Click delete icon on custom role
- Confirm deletion
- Verify role removed
3. Test User Role Assignment
- Go to "User Roles" tab
- Select a test user
- Click "Change Role"
- Assign "moderator" role
- Verify role assigned
- Check audit log for
role_assignedentry
4. Test Permission Checking
As Moderator:
- Login as moderator user
- Access
/security/rbac - Should see error: "Insufficient permissions"
- Access
/stats- Should work ✅ - Access
/settings- Should only view, not edit
As Regular User:
- Login as regular user
- Access
/security- Should not show admin features - Access own playlists - Should work ✅
- Try to access user management - Should fail ❌
As Viewer:
- Login as viewer user
- Try to create playlist - Should fail ❌
- View channels - Should work ✅
- View favorites - Should work ✅
5. Test Audit Logging
- Go to RBAC Dashboard > Audit Log tab
- Verify all test actions logged:
role_createdrole_updatedrole_deletedrole_assigned
- Check details show correct user, IP, timestamp
6. Test Permission Cache
- Assign user to "moderator" role
- Wait 5 minutes (cache TTL)
- Verify user has new permissions
- Or force refresh by reassigning role
Verification Tests
Backend API Tests
# Set your admin token
TOKEN="your_admin_jwt_token"
# Test 1: List all permissions
curl -H "Authorization: Bearer $TOKEN" \
http://localhost:5000/api/rbac/permissions | jq
# Test 2: List all roles
curl -H "Authorization: Bearer $TOKEN" \
http://localhost:5000/api/rbac/roles | jq
# Test 3: Get my permissions
curl -H "Authorization: Bearer $TOKEN" \
http://localhost:5000/api/rbac/my-permissions | jq
# Test 4: Create custom role
curl -X POST -H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"role_key": "test_role",
"name": "Test Role",
"description": "Testing role creation",
"permissions": ["playlists.view", "channels.view"]
}' \
http://localhost:5000/api/rbac/roles | jq
# Test 5: Get audit log
curl -H "Authorization: Bearer $TOKEN" \
http://localhost:5000/api/rbac/audit-log | jq
# Test 6: Get statistics
curl -H "Authorization: Bearer $TOKEN" \
http://localhost:5000/api/rbac/stats | jq
Frontend Component Tests
-
RBAC Dashboard Access
- Admin: ✅ Full access
- Moderator: ❌ Access denied
- User: ❌ Access denied
- Viewer: ❌ Access denied
-
My Permissions Tab
- All roles: ✅ Can view own permissions
- Permissions grouped by category
- Correct permission counts
-
Role Management
- Create role: ✅ Works for admin
- Edit role: ✅ Works for admin (custom roles only)
- Delete role: ✅ Works for admin (custom roles only)
- System role protection: ✅ Cannot modify
-
User Role Assignment
- Assign role: ✅ Works for admin
- Self-assignment prevention: ✅ Cannot change own role
- Non-existent role: ❌ Error shown
Monitoring & Maintenance
Daily Checks
-
Audit Log Review
- Check for suspicious role changes
- Monitor permission grant patterns
- Review failed permission checks
-
Role Distribution
- Review
/api/rbac/stats - Ensure appropriate role distribution
- Identify over-privileged accounts
- Review
-
Permission Cache Performance
- Monitor cache hit rate (should be >90%)
- Check for cache invalidation patterns
Weekly Maintenance
-
Clean Old Audit Logs (Optional)
DELETE FROM permission_audit_log WHERE created_at < datetime('now', '-90 days'); -
Review Custom Roles
- Are all custom roles still needed?
- Can any be consolidated?
- Remove unused roles
-
Permission Audit
- Review user permissions
- Apply principle of least privilege
- Demote over-privileged users
Monthly Review
-
Security Assessment
- Review role definitions
- Check for permission creep
- Validate default role permissions
-
Performance Tuning
- Analyze cache efficiency
- Check database query performance
- Review audit log size
-
Documentation Update
- Update custom role documentation
- Record permission changes
- Update deployment notes
Troubleshooting
Issue: RBAC routes not working
Symptoms: 404 errors on /api/rbac/* endpoints
Solution:
- Verify route registration in
server.js:app.use('/api/rbac', require('./routes/rbac')); - Restart server
- Check logs for route loading errors
Issue: Roles not seeded
Symptoms: Empty roles table, dashboard shows no roles
Solution:
- Check database initialization logs:
docker-compose logs backend | grep -i "role seeded" - Manually initialize:
docker exec -it streamflow-backend node -e " const { initializeRoles } = require('./middleware/rbac'); initializeRoles(); " - Restart application
Issue: Permission checks failing
Symptoms: "Insufficient permissions" errors for valid users
Solution:
- Clear permission cache:
// In backend console const { clearAllPermissionCache } = require('./middleware/rbac'); clearAllPermissionCache(); - Verify user role:
SELECT id, username, role FROM users WHERE id = ?; - Verify role permissions:
SELECT * FROM roles WHERE role_key = 'user'; - Check permission spelling (case-sensitive)
Issue: Cannot delete custom role
Symptoms: "Cannot delete role that is assigned to users"
Solution:
- Find users with that role:
SELECT id, username FROM users WHERE role = 'custom_role_key'; - Reassign users to different role
- Then delete custom role
Issue: Audit log not recording
Symptoms: Empty audit log despite changes
Solution:
- Verify table exists:
SELECT name FROM sqlite_master WHERE type='table' AND name='permission_audit_log'; - Check for INSERT errors in logs
- Verify
logPermissionAction()calls in code
Issue: Frontend dashboard not loading
Symptoms: Blank page or React errors
Solution:
- Check browser console for errors
- Verify translations loaded:
console.log(i18n.t('rbac.dashboard')); - Check API responses in Network tab
- Verify authentication token valid
Rollback Plan
If RBAC causes issues:
Quick Rollback (Keep RBAC, Disable Checks)
- Comment out RBAC middleware in routes:
// Temporarily comment out // const { requirePermission } = require('../middleware/rbac'); // Keep using requireAdmin router.get('/users', authenticate, requireAdmin, ...); - Restart server
- RBAC data preserved, enforcement disabled
Full Rollback (Remove RBAC)
- Restore database backup:
docker exec streamflow-backend npm run backup:restore -- backup-name.zip - Revert code changes:
git revert HEAD~5 # Revert last 5 commits - Rebuild and restart:
docker-compose down docker-compose build --no-cache docker-compose up -d
Partial Rollback (Keep Tables, Remove UI)
- Comment out RBAC Dashboard route in
App.jsx - Remove RBAC link from Security Dashboard
- Keep backend routes active (for future use)
Performance Benchmarks
Expected Performance
Permission Check (Cached):
- Response time: <5ms
- Database queries: 0
- Cache hit rate: >90%
Permission Check (Uncached):
- Response time: <50ms
- Database queries: 1
- Cache miss rate: <10%
Role Assignment:
- Response time: <100ms
- Database queries: 3
- Audit log writes: 1
Audit Log Query:
- Response time: <200ms (100 records)
- Database queries: 1
- Pagination supported
Monitoring Queries
-- Check role distribution
SELECT r.name, COUNT(u.id) as user_count
FROM roles r
LEFT JOIN users u ON r.role_key = u.role
GROUP BY r.role_key;
-- Recent permission changes
SELECT action, COUNT(*) as count
FROM permission_audit_log
WHERE created_at >= datetime('now', '-7 days')
GROUP BY action;
-- Most active permission managers
SELECT u.username, COUNT(pal.id) as change_count
FROM permission_audit_log pal
JOIN users u ON pal.user_id = u.id
WHERE pal.created_at >= datetime('now', '-30 days')
GROUP BY u.id
ORDER BY change_count DESC
LIMIT 10;
Security Checklist
Pre-Production
- All default roles reviewed and approved
- System role permissions validated
- Custom roles documented
- Audit logging tested
- Permission cache working correctly
- All tests passing
- Documentation complete
Production
- JWT_SECRET is strong and unique
- NODE_ENV=production
- HTTPS enabled
- Rate limiting active
- Audit logs monitored
- Backup strategy in place
- Rollback plan tested
Post-Deployment
- All roles seeded correctly
- Permission checks working
- Audit log recording events
- Dashboard accessible to admin
- Users can view own permissions
- No performance degradation
- Logs show no errors
Success Criteria
✅ Deployment Successful When:
- All 4 default roles visible in dashboard
- Admin can create/edit/delete custom roles
- Role assignment works correctly
- Permission checks enforce access control
- Audit log records all changes
- Statistics show accurate data
- Users can view own permissions
- No errors in application logs
- Performance within expected benchmarks
- All security tests pass
Support Resources
- Documentation:
/docs/RBAC_IMPLEMENTATION.md - API Reference: See documentation above
- Frontend Guide: Check RBACDashboard.jsx comments
- Security Guide:
/docs/SECURITY_IMPLEMENTATION_COMPLETE.md
For issues or questions:
- Check troubleshooting section
- Review audit logs for clues
- Enable debug logging
- Contact development team
Deployment Date: 2024-12-13
Version: 1.0
Status: ✅ Production Ready