Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
fix: run legacy users.roles backfill once (#3964)
Kyle Mendella4ae1a0f2046
diff --git a/backend/internal/bootstrap/bootstrap.go b/backend/internal/bootstrap/bootstrap.goindex fa0ada1c7..4873b786f 100644--- a/backend/internal/bootstrap/bootstrap.go+++ b/backend/internal/bootstrap/bootstrap.go@@ -290,8 +290,8 @@ func initializeStartupState(p initializeStartupStateParams) {func(ctx context.Context) error {// Backfill legacy users.roles first so CreateDefaultAdmin's// zero-global-admin recovery gate sees upgraded assignments.- // No-op on modern schemas; runRoleStartupTasks repeats it- // idempotently for agent-mode and error-retry coverage.+ // Runs once (kv completion marker); runRoleStartupTasks repeats+ // the call for agent-mode and error-retry coverage.if err := p.Role.BackfillLegacyRoleAssignments(ctx); err != nil {slog.WarnContext(ctx, "Failed to backfill legacy role assignments before admin bootstrap", "error", err)}@@ -354,7 +354,8 @@ func runRoleStartupTasks(ctx context.Context, roleService *role.RoleService, api}// Backfill must run AFTER EnsureBuiltInRoles (it references the role IDs// seeded there) and BEFORE BackfillApiKeyPermissions / AssertGlobalAdminExists- // (both consult the assignments table this populates).+ // (both consult the assignments table this populates). It is a no-op once+ // its kv completion marker exists.if err := roleService.BackfillLegacyRoleAssignments(ctx); err != nil {slog.ErrorContext(ctx, "Failed to backfill legacy users.roles into user_role_assignments", "error", err)}diff --git a/backend/internal/role/service.go b/backend/internal/role/service.goindex 6cd54f888..fcbb750aa 100644--- a/backend/internal/role/service.go+++ b/backend/internal/role/service.go@@ -16,6 +16,7 @@ import ("github.com/getarcaneapp/arcane/backend/v2/internal/common""github.com/getarcaneapp/arcane/backend/v2/internal/database"+ "github.com/getarcaneapp/arcane/backend/v2/internal/kv""github.com/getarcaneapp/arcane/backend/v2/pkg/authz""github.com/getarcaneapp/arcane/backend/v2/pkg/pagination""github.com/getarcaneapp/arcane/backend/v2/pkg/utils/dbutil"@@ -88,21 +89,9 @@ func (s *RoleService) EnsureBuiltInRoles(ctx context.Context) error {})}-// BackfillLegacyRoleAssignments migrates the pre-RBAC users.roles JSON column-// into rows in user_role_assignments. Safe to call on every boot: a no-op once-// the column is gone.-//-// Users with "admin" in their legacy roles get a global Admin assignment;-// every other user gets a global Viewer assignment. The NULL environment_id-// lands the perms in PermissionSet.Global, which is what ps.Allows(perm, "")-// consults for org-level checks (list environments, read settings, list users,-// etc.) AND for env-scoped checks at the union step. Inserting per-environment-// viewer rows instead would lock non-admins out of the settings area entirely.-//-// Lives here (not as a SQL migration) so the column-existence check is trivial-// in Go and the same code path covers both postgres and sqlite. Idempotent via-// ON CONFLICT DO NOTHING on the (user_id, role_id, env) unique index, so a-// half-finished prior run can be safely retried.+const legacyRoleBackfillCompletedKey = "migration.legacy_user_roles.v1.completed"++// BackfillLegacyRoleAssignments converts pre-RBAC users.roles into global assignments once, gated by a kv marker committed with the rows.func (s *RoleService) BackfillLegacyRoleAssignments(ctx context.Context) error {migrator := s.db.WithContext(ctx).Migrator()if !migrator.HasColumn("users", "roles") {@@ -114,9 +103,19 @@ func (s *RoleService) BackfillLegacyRoleAssignments(ctx context.Context) error {Roles string `gorm:"column:roles"`}- return dbutil.WithTx(ctx, s.db.DB, func(tx *gorm.DB) error {+ var inserted int64+ err := dbutil.WithTx(ctx, s.db.DB, func(tx *gorm.DB) error {+ claimed, err := kv.NewKVService(&database.DB{DB: tx}).CreateIfAbsent(ctx, legacyRoleBackfillCompletedKey, time.Now().UTC().Format(time.RFC3339))+ if err != nil {+ return errors.WrapIf(err, "failed to claim legacy role backfill marker")+ }+ if !claimed {+ return nil+ }var rows []legacyUser- if err := tx.Table("users").Select("id, roles").Scan(&rows).Error; err != nil {+ if err := tx.Table("users").Select("id, roles").+ Where("NOT EXISTS (SELECT 1 FROM user_role_assignments ura WHERE ura.user_id = users.id)").+ Scan(&rows).Error; err != nil {return errors.WrapIf(err, "failed to read legacy users.roles for backfill")}for _, u := range rows {@@ -129,23 +128,25 @@ func (s *RoleService) BackfillLegacyRoleAssignments(ctx context.Context) error {RoleID: roleID,Source: RoleAssignmentSourceManual,}- if err := tx.Clauses(clause.OnConflict{DoNothing: true}).Create(&assignment).Error; err != nil {- return errors.WrapIff(err, "failed to backfill assignment for user %s", u.ID)+ result := tx.Clauses(clause.OnConflict{DoNothing: true}).Create(&assignment)+ if result.Error != nil {+ return errors.WrapIff(result.Error, "failed to backfill assignment for user %s", u.ID)}+ inserted += result.RowsAffected}- slog.InfoContext(ctx, "Backfilled legacy users.roles into user_role_assignments", "userCount", len(rows))return nil})+ if err != nil {+ return err+ }+ if inserted > 0 {+ slog.InfoContext(ctx, "Backfilled legacy users.roles into user_role_assignments", "assignmentCount", inserted)+ }+ return nil}-// legacyRolesContainsAdminInternal reports whether a pre-RBAC users.roles JSON-// value contains the literal "admin" (case-insensitive). Empty / null / malformed-// JSON yields false — treat as non-admin and assign Viewer.+// legacyRolesContainsAdminInternal reports whether a legacy users.roles JSON value names "admin"; anything else was a regular user.func legacyRolesContainsAdminInternal(raw string) bool {- raw = strings.TrimSpace(raw)- if raw == "" || raw == "[]" || raw == "null" {- return false- }var roles []stringif err := json.Unmarshal([]byte(raw), &roles); err != nil {return falsediff --git a/backend/internal/role/service_test.go b/backend/internal/role/service_test.goindex baff62982..b07e05b9b 100644--- a/backend/internal/role/service_test.go+++ b/backend/internal/role/service_test.go@@ -6,6 +6,7 @@ import ("github.com/getarcaneapp/arcane/backend/v2/internal/settings""context"+ "path/filepath""slices""testing""time"@@ -59,15 +60,78 @@ func TestValidatePermissionsAgainstCallerRejectsUnknownPermissionBeforeEscalatiorequire.False(t, errors.Is(err, common.ErrRolePermissionEscalation))}-func TestBackfillLegacyRoleAssignmentsIsNoOpWhenColumnAbsent(t *testing.T) {+func TestBackfillLegacyRoleAssignments(t *testing.T) {ctx := context.Background()- _, roleSvc := setupUserAndRoleServices(t)- // setupUserAndRoleServices runs migrations through to current, so- // users.roles never exists in the fresh test schema.- require.False(t, roleSvc.db.Migrator().HasColumn("users", "roles"))- require.NoError(t, roleSvc.BackfillLegacyRoleAssignments(ctx))- // Idempotent — second call is also fine.- require.NoError(t, roleSvc.BackfillLegacyRoleAssignments(ctx))++ t.Run("no-op without legacy column", func(t *testing.T) {+ _, roleSvc := setupUserAndRoleServices(t)+ require.False(t, roleSvc.db.Migrator().HasColumn("users", "roles"))+ require.NoError(t, roleSvc.BackfillLegacyRoleAssignments(ctx))+ require.NoError(t, roleSvc.BackfillLegacyRoleAssignments(ctx))+ })++ t.Run("converts legacy roles once", func(t *testing.T) {+ db, err := database.Initialize(ctx, "file:"+filepath.Join(t.TempDir(), "arcane.db"), database.MigrationOptions{})+ require.NoError(t, err)+ t.Cleanup(func() { require.NoError(t, db.Close()) })+ require.True(t, db.Migrator().HasColumn("users", "roles"))+ roleSvc := NewRoleService(db)+ require.NoError(t, roleSvc.EnsureBuiltInRoles(ctx))+ require.NoError(t, db.Exec("INSERT INTO environments (id, name, api_url) VALUES (?, ?, ?)", "env-1", "env-1", "http://env-1").Error)++ for id, roles := range map[string]string{+ "seed-admin": `[]`,+ "legacy-admin": `["user", " ADMIN "]`,+ "legacy-user": `["user"]`,+ "legacy-empty": `[]`,+ "legacy-null": `null`,+ "legacy-blank": `[" "]`,+ "legacy-broken": `not json`,+ "scoped-manual": `["admin"]`,+ "global-oidc": `["admin"]`,+ } {+ require.NoError(t, db.Exec("INSERT INTO users (id, username, password_hash, roles) VALUES (?, ?, ?, ?)", id, id, "unused", roles).Error)+ }+ require.NoError(t, db.Create(&UserRoleAssignment{UserID: "seed-admin", RoleID: authz.BuiltInRoleAdmin, Source: RoleAssignmentSourceManual}).Error)+ envID := "env-1"+ require.NoError(t, roleSvc.SetUserAssignments(ctx, "scoped-manual", []UserRoleAssignment{{RoleID: authz.BuiltInRoleViewer, EnvironmentID: &envID}}))+ require.NoError(t, roleSvc.ReplaceOidcAssignments(ctx, "global-oidc", []UserRoleAssignment{{RoleID: authz.BuiltInRoleViewer}}))++ require.NoError(t, roleSvc.BackfillLegacyRoleAssignments(ctx))+ require.Equal(t, []string{authz.BuiltInRoleAdmin}, assignedRoleIDs(t, roleSvc, "legacy-admin"))+ for _, id := range []string{"legacy-user", "legacy-empty", "legacy-null", "legacy-blank", "legacy-broken"} {+ require.Equal(t, []string{authz.BuiltInRoleViewer}, assignedRoleIDs(t, roleSvc, id), id)+ }+ scoped, err := roleSvc.ListUserAssignments(ctx, "scoped-manual")+ require.NoError(t, err)+ require.Len(t, scoped, 1)+ require.Equal(t, &envID, scoped[0].EnvironmentID)+ oidc, err := roleSvc.ListUserAssignments(ctx, "global-oidc")+ require.NoError(t, err)+ require.Len(t, oidc, 1)+ require.Equal(t, RoleAssignmentSourceOidc, oidc[0].Source)+ ps, err := roleSvc.ResolveUserPermissionsInDB(ctx, db.DB, "scoped-manual")+ require.NoError(t, err)+ require.True(t, ps.Allows(authz.PermContainersList, "env-1"))+ require.False(t, ps.Allows(authz.PermContainersList, ""))++ require.NoError(t, roleSvc.SetUserAssignments(ctx, "legacy-user", nil))+ require.NoError(t, db.Exec("INSERT INTO users (id, username, password_hash, roles) VALUES (?, ?, ?, ?)", "later-admin", "later-admin", "unused", `["admin"]`).Error)+ require.NoError(t, NewRoleService(db).BackfillLegacyRoleAssignments(ctx))+ require.Empty(t, assignedRoleIDs(t, roleSvc, "legacy-user"))+ require.Empty(t, assignedRoleIDs(t, roleSvc, "later-admin"))+ })+}++func assignedRoleIDs(t *testing.T, roleSvc *RoleService, userID string) []string {+ t.Helper()+ assignments, err := roleSvc.ListUserAssignments(context.Background(), userID)+ require.NoError(t, err)+ ids := make([]string, 0, len(assignments))+ for _, a := range assignments {+ ids = append(ids, a.RoleID)+ }+ return ids}func TestEnsureBuiltInRolesMigratesVariablePermissionsWithoutBackfillingCustomGrants(t *testing.T) {