Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

server: delay storage deletion lockout until email sends

Neeraj Gupta93d8372f3308
diff --git a/server/pkg/controller/email/storage_warning.go b/server/pkg/controller/email/storage_warning.go
index 4b6c8b5290..e23803cd31 100644
--- a/server/pkg/controller/email/storage_warning.go
+++ b/server/pkg/controller/email/storage_warning.go
@@ -94,6 +94,16 @@ const (
storageWarningProcessResultSent storageWarningProcessResult = "sent"
)
+var sendStorageWarningTemplatedEmail = emailUtil.SendTemplatedEmailV2
+
+var persistStorageWarningHistory = func(notificationHistoryRepo *repo.NotificationHistoryRepository, snapshot storageWarningSnapshot, templateID string) error {
+ historyGroup := storageWarningHistoryGroup(snapshot)
+ if historyGroup == "" {
+ return notificationHistoryRepo.SetLastNotificationTimeToNow(snapshot.RecipientID, templateID)
+ }
+ return notificationHistoryRepo.SetLastNotificationTimeToNowWithGroup(snapshot.RecipientID, templateID, historyGroup)
+}
+
var storageWarningStageOrder = []string{
string(expiredWarningStage0),
string(expiredWarningStage30),
@@ -501,23 +511,14 @@ func (c *EmailNotificationController) processStorageWarningSnapshot(ctx context.
if !isInStorageWarningRollout(snapshot.RecipientID, snapshot.AccountEmail) {
return storageWarningProcessResultSkippedRollout, nil
}
- if storageWarningShouldResetUserAccess(snapshot) {
- if c.UserAccessResetter == nil {
- err := errors.New("storage warning user access resetter is not configured")
- logger.WithError(err).Error("Failed to restrict user access for storage scheduled deletion")
- return storageWarningProcessResultSkipped, err
- }
- // Intentionally do not enqueue data cleanup here yet. We will start
- // scheduling auto-deletion only after validating that this workflow is
- // consistently identifying the correct deletion candidates.
- if err := c.UserAccessResetter.ResetUserAccess(ctx, snapshot.RecipientID, logger); err != nil {
- logger.WithError(err).Error("Failed to restrict user access for storage scheduled deletion")
- return storageWarningProcessResultSkipped, err
- }
- logger.Info("Restricted user access for storage scheduled deletion")
+ shouldResetUserAccess := storageWarningShouldResetUserAccess(snapshot)
+ if shouldResetUserAccess && c.UserAccessResetter == nil {
+ err := errors.New("storage warning user access resetter is not configured")
+ logger.WithError(err).Error("Failed to restrict user access for storage scheduled deletion")
+ return storageWarningProcessResultSkipped, err
}
- err := emailUtil.SendTemplatedEmailV2(
+ err := sendStorageWarningTemplatedEmail(
[]string{snapshot.AccountEmail},
storageWarningFromName,
storageWarningFromEmail,
@@ -532,12 +533,18 @@ func (c *EmailNotificationController) processStorageWarningSnapshot(ctx context.
return storageWarningProcessResultSkipped, err
}
- historyGroup := storageWarningHistoryGroup(snapshot)
- if historyGroup == "" {
- err = c.NotificationHistoryRepo.SetLastNotificationTimeToNow(snapshot.RecipientID, templateID)
- } else {
- err = c.NotificationHistoryRepo.SetLastNotificationTimeToNowWithGroup(snapshot.RecipientID, templateID, historyGroup)
+ if shouldResetUserAccess {
+ // Intentionally do not enqueue data cleanup here yet. We will start
+ // scheduling auto-deletion only after validating that this workflow is
+ // consistently identifying the correct deletion candidates.
+ if err := c.UserAccessResetter.ResetUserAccess(ctx, snapshot.RecipientID, logger); err != nil {
+ logger.WithError(err).Error("Failed to restrict user access for storage scheduled deletion")
+ return storageWarningProcessResultSkipped, err
+ }
+ logger.Info("Restricted user access for storage scheduled deletion")
}
+
+ err = persistStorageWarningHistory(c.NotificationHistoryRepo, snapshot, templateID)
if err != nil {
logger.WithError(err).Error("Failed to persist storage warning history")
return storageWarningProcessResultSkipped, err
diff --git a/server/pkg/controller/email/storage_warning_test.go b/server/pkg/controller/email/storage_warning_test.go
index 485bd7b4af..24428f7f32 100644
--- a/server/pkg/controller/email/storage_warning_test.go
+++ b/server/pkg/controller/email/storage_warning_test.go
@@ -2,6 +2,7 @@ package email
import (
"context"
+ "errors"
"strings"
"testing"
@@ -15,6 +16,20 @@ import (
logtest "github.com/sirupsen/logrus/hooks/test"
)
+type recordingUserAccessResetter struct {
+ callCount int
+ callOrder *[]string
+ err error
+}
+
+func (r *recordingUserAccessResetter) ResetUserAccess(_ context.Context, _ int64, _ *log.Entry) error {
+ r.callCount++
+ if r.callOrder != nil {
+ *r.callOrder = append(*r.callOrder, "reset")
+ }
+ return r.err
+}
+
func TestBucketStorageWarningActiveOverage(t *testing.T) {
now := int64(100)
effectiveExpiry := now + 10
@@ -767,6 +782,97 @@ func TestProcessStorageWarningSnapshotSkipsDueToRolloutWithoutPerRecipientLog(t
}
}
+func TestProcessStorageWarningSnapshotScheduledDeletionSkipsResetWhenEmailFails(t *testing.T) {
+ originalSendStorageWarningTemplatedEmail := sendStorageWarningTemplatedEmail
+ originalPersistStorageWarningHistory := persistStorageWarningHistory
+ defer func() {
+ sendStorageWarningTemplatedEmail = originalSendStorageWarningTemplatedEmail
+ persistStorageWarningHistory = originalPersistStorageWarningHistory
+ }()
+
+ sendStorageWarningTemplatedEmail = func(_ []string, _ string, _ string, _ string, _ string, _ string, _ map[string]interface{}, _ []map[string]interface{}) error {
+ return errors.New("boom")
+ }
+ persistStorageWarningHistory = func(_ *repo.NotificationHistoryRepository, _ storageWarningSnapshot, _ string) error {
+ t.Fatal("did not expect history persistence when email send fails")
+ return nil
+ }
+
+ resetter := &recordingUserAccessResetter{}
+ snapshot := storageWarningSnapshot{
+ RecipientID: 12345,
+ AccountEmail: "[email protected]",
+ TotalUsage: storageWarningOverageThreshold + 10,
+ AllottedStorage: 0,
+ AvailableStorage: -10,
+ Bucket: storageWarningBucketExpired,
+ ExpiredStage: expiredWarningStageScheduledDeletion,
+ EffectiveExpiry: 1,
+ NotificationHistory: map[string]int64{
+ storageWarningExpired119TemplateID: 1,
+ },
+ }
+
+ result, err := (&EmailNotificationController{UserAccessResetter: resetter}).processStorageWarningSnapshot(context.Background(), snapshot)
+ if err == nil {
+ t.Fatal("expected email failure to be returned")
+ }
+ if result != storageWarningProcessResultSkipped {
+ t.Fatalf("unexpected result: got %q want %q", result, storageWarningProcessResultSkipped)
+ }
+ if resetter.callCount != 0 {
+ t.Fatalf("expected no access reset when email send fails, got %d resets", resetter.callCount)
+ }
+}
+
+func TestProcessStorageWarningSnapshotScheduledDeletionResetsAccessAfterEmail(t *testing.T) {
+ originalSendStorageWarningTemplatedEmail := sendStorageWarningTemplatedEmail
+ originalPersistStorageWarningHistory := persistStorageWarningHistory
+ defer func() {
+ sendStorageWarningTemplatedEmail = originalSendStorageWarningTemplatedEmail
+ persistStorageWarningHistory = originalPersistStorageWarningHistory
+ }()
+
+ callOrder := []string{}
+ sendStorageWarningTemplatedEmail = func(_ []string, _ string, _ string, _ string, _ string, _ string, _ map[string]interface{}, _ []map[string]interface{}) error {
+ callOrder = append(callOrder, "send")
+ return nil
+ }
+ persistStorageWarningHistory = func(_ *repo.NotificationHistoryRepository, _ storageWarningSnapshot, _ string) error {
+ callOrder = append(callOrder, "persist")
+ return nil
+ }
+
+ resetter := &recordingUserAccessResetter{callOrder: &callOrder}
+ snapshot := storageWarningSnapshot{
+ RecipientID: 12345,
+ AccountEmail: "[email protected]",
+ TotalUsage: storageWarningOverageThreshold + 10,
+ AllottedStorage: 0,
+ AvailableStorage: -10,
+ Bucket: storageWarningBucketExpired,
+ ExpiredStage: expiredWarningStageScheduledDeletion,
+ EffectiveExpiry: 1,
+ NotificationHistory: map[string]int64{
+ storageWarningExpired119TemplateID: 1,
+ },
+ }
+
+ result, err := (&EmailNotificationController{UserAccessResetter: resetter}).processStorageWarningSnapshot(context.Background(), snapshot)
+ if err != nil {
+ t.Fatalf("unexpected error: %v", err)
+ }
+ if result != storageWarningProcessResultSent {
+ t.Fatalf("unexpected result: got %q want %q", result, storageWarningProcessResultSent)
+ }
+ if resetter.callCount != 1 {
+ t.Fatalf("expected one access reset after successful email, got %d", resetter.callCount)
+ }
+ if got, want := strings.Join(callOrder, ","), "send,reset,persist"; got != want {
+ t.Fatalf("unexpected call order: got %q want %q", got, want)
+ }
+}
+
func TestBuildStorageWarningRunSummary(t *testing.T) {
stats := newStorageWarningRunStats()
stats.ProcessedUsers = 42