|
1 | 1 | package handlers |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "net/http" |
| 6 | + "time" |
5 | 7 |
|
6 | 8 | "github.com/gin-gonic/gin" |
7 | 9 | "go.uber.org/zap" |
8 | 10 |
|
9 | 11 | "github.com/e2b-dev/infra/packages/dashboard-api/internal/api" |
| 12 | + "github.com/e2b-dev/infra/packages/db/pkg/dberrors" |
10 | 13 | "github.com/e2b-dev/infra/packages/shared/pkg/logger" |
11 | 14 | ) |
12 | 15 |
|
| 16 | +const identityDeleteMaxRetries = 3 |
| 17 | + |
13 | 18 | func (s *APIStore) DeleteAdminUsersUserId(c *gin.Context, userId api.UserId) { |
14 | 19 | ctx := c.Request.Context() |
15 | 20 |
|
16 | 21 | // Resolve the external identity references while user_identities still exists. |
17 | 22 | handle, err := s.userProfiles.PrepareDeleteUser(ctx, userId) |
18 | 23 | if err != nil { |
19 | 24 | logger.L().Error(ctx, "failed to prepare user deletion", zap.String("user_id", userId.String()), zap.Error(err)) |
20 | | - s.sendAPIStoreError(c, http.StatusInternalServerError, "Failed to prepare user deletion") |
| 25 | + s.sendAPIStoreError(c, http.StatusNotFound, fmt.Sprintf("User %s not found or has no identity provider record", userId)) |
21 | 26 |
|
22 | 27 | return |
23 | 28 | } |
24 | 29 |
|
25 | 30 | // Delete from public.users (cascades to user_identities via FK). |
26 | 31 | // Done before the IdP removal so a DB failure does not orphan the identity. |
27 | 32 | if err := s.authDB.Write.DeletePublicUser(ctx, userId); err != nil { |
| 33 | + if dberrors.IsNotFoundError(err) { |
| 34 | + s.sendAPIStoreError(c, http.StatusNotFound, fmt.Sprintf("User %s not found", userId)) |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + if dberrors.IsForeignKeyViolation(err) { |
| 39 | + logger.L().Warn(ctx, "cannot delete user due to existing references", zap.String("user_id", userId.String()), zap.Error(err)) |
| 40 | + s.sendAPIStoreError(c, http.StatusConflict, "Cannot delete user: existing references (e.g. addons) must be removed first") |
| 41 | + |
| 42 | + return |
| 43 | + } |
| 44 | + |
28 | 45 | logger.L().Error(ctx, "failed to delete public user", zap.String("user_id", userId.String()), zap.Error(err)) |
29 | 46 | s.sendAPIStoreError(c, http.StatusInternalServerError, "Failed to delete public user record") |
30 | 47 |
|
31 | 48 | return |
32 | 49 | } |
33 | 50 |
|
34 | 51 | // Remove the external identity (e.g. Ory) using pre-fetched references. |
35 | | - if err := handle.Execute(ctx); err != nil { |
36 | | - logger.L().Error(ctx, "failed to delete user identity provider record", zap.String("user_id", userId.String()), zap.Error(err)) |
37 | | - s.sendAPIStoreError(c, http.StatusInternalServerError, "Failed to delete user identity provider record") |
| 52 | + // Retry since the DB rows are already gone and we must not leave the IdP identity active. |
| 53 | + var identityErr error |
| 54 | + for attempt := range identityDeleteMaxRetries { |
| 55 | + identityErr = handle.Execute(ctx) |
| 56 | + if identityErr == nil { |
| 57 | + break |
| 58 | + } |
| 59 | + |
| 60 | + logger.L().Warn(ctx, "retrying identity deletion", |
| 61 | + zap.String("user_id", userId.String()), |
| 62 | + zap.Int("attempt", attempt+1), |
| 63 | + zap.Error(identityErr), |
| 64 | + ) |
| 65 | + |
| 66 | + time.Sleep(time.Duration(attempt+1) * 200 * time.Millisecond) |
| 67 | + } |
| 68 | + |
| 69 | + if identityErr != nil { |
| 70 | + logger.L().Error(ctx, "failed to delete user identity provider record after retries", zap.String("user_id", userId.String()), zap.Error(identityErr)) |
| 71 | + s.sendAPIStoreError(c, http.StatusInternalServerError, |
| 72 | + "User DB records deleted but identity provider removal failed — the IdP identity may need manual cleanup") |
38 | 73 |
|
39 | 74 | return |
40 | 75 | } |
|
0 commit comments