Skip to content

armgit / ente

Public Imported from GitHub
0

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

Create user records atomically

Neeraj Guptafdc3fdee9bf6
diff --git a/server/pkg/controller/user/user.go b/server/pkg/controller/user/user.go
index 42f28ecb80..7c62def779 100644
--- a/server/pkg/controller/user/user.go
+++ b/server/pkg/controller/user/user.go
@@ -380,17 +380,7 @@ func (c *UserController) NotifyAccountDeletion(userID int64, userEmail string, i
}
}
-func (c *UserController) attachFreeSubscription(userID int64) (ente.Subscription, error) {
- subscription := billing.GetFreeSubscription(userID)
- generatedID, err := c.BillingRepo.AddSubscription(subscription)
- if err != nil {
- return subscription, stacktrace.Propagate(err, "")
- }
- subscription.ID = generatedID
- return subscription, nil
-}
-
-func (c *UserController) createUser(email string, source *string) (int64, ente.Subscription, error) {
+func (c *UserController) createUser(ctx context.Context, email string, source *string) (int64, ente.Subscription, error) {
encryptedEmail, err := crypto.Encrypt(email, c.SecretEncryptionKey)
if err != nil {
return -1, ente.Subscription{}, stacktrace.Propagate(err, "")
@@ -399,18 +389,26 @@ func (c *UserController) createUser(email string, source *string) (int64, ente.S
if err != nil {
return -1, ente.Subscription{}, stacktrace.Propagate(err, "")
}
- userID, err := c.UserRepo.Create(encryptedEmail, emailHash, source)
+ transaction, err := c.UserRepo.DB.BeginTx(ctx, nil)
+ if err != nil {
+ return -1, ente.Subscription{}, stacktrace.Propagate(err, "failed to start user creation transaction")
+ }
+ defer transaction.Rollback()
+ userID, err := c.UserRepo.CreateTx(ctx, transaction, encryptedEmail, emailHash, source)
if err != nil {
return -1, ente.Subscription{}, stacktrace.Propagate(err, "")
}
- err = c.UsageRepo.Create(userID)
- if err != nil {
+ if err := c.UsageRepo.CreateTx(ctx, transaction, userID); err != nil {
return -1, ente.Subscription{}, stacktrace.Propagate(err, "failed to add entry in usage")
}
- subscription, err := c.attachFreeSubscription(userID)
+ subscription := billing.GetFreeSubscription(userID)
+ subscription.ID, err = c.BillingRepo.AddSubscriptionTx(ctx, transaction, subscription)
if err != nil {
return -1, ente.Subscription{}, stacktrace.Propagate(err, "")
}
+ if err := transaction.Commit(); err != nil {
+ return -1, ente.Subscription{}, stacktrace.Propagate(err, "failed to commit user creation")
+ }
// Mailing list failures must not block user creation.
go func() {
if err := c.MailingListsController.Subscribe(email); err != nil {
diff --git a/server/pkg/controller/user/user_creation_test.go b/server/pkg/controller/user/user_creation_test.go
new file mode 100644
index 0000000000..a857c9ca64
--- /dev/null
+++ b/server/pkg/controller/user/user_creation_test.go
@@ -0,0 +1,52 @@
+package user
+
+import (
+ "testing"
+
+ "github.com/ente/museum/internal/testutil"
+ "github.com/ente/museum/pkg/repo"
+)
+
+func TestCreateUserRollsBackWhenSubscriptionCreationFails(t *testing.T) {
+ testutil.WithServerRoot(t)
+ db := testutil.RequireTestDB(t)
+ testutil.ResetTables(t, db)
+ t.Cleanup(func() { testutil.ResetTables(t, db) })
+
+ controller := &UserController{
+ UserRepo: &repo.UserRepository{DB: db},
+ UsageRepo: &repo.UsageRepository{DB: db},
+ BillingRepo: &repo.BillingRepository{DB: db},
+ SecretEncryptionKey: testutil.SecretEncryptionKey(),
+ HashingKey: testutil.HashingKey(),
+ }
+
+ _, err := db.Exec(`ALTER TABLE subscriptions
+ ADD CONSTRAINT reject_free_subscription_for_atomicity_test
+ CHECK (false)`)
+ if err != nil {
+ t.Fatalf("failed to install subscription failure constraint: %v", err)
+ }
+ t.Cleanup(func() {
+ if _, err := db.Exec(`ALTER TABLE subscriptions
+ DROP CONSTRAINT reject_free_subscription_for_atomicity_test`); err != nil {
+ t.Errorf("failed to remove subscription failure constraint: %v", err)
+ }
+ })
+
+ if _, _, err := controller.createUser(t.Context(), "[email protected]", nil); err == nil {
+ t.Fatal("createUser() succeeded despite subscription failure")
+ }
+
+ var users, usages, subscriptions int64
+ err = db.QueryRow(`SELECT
+ (SELECT count(*) FROM users),
+ (SELECT count(*) FROM usage),
+ (SELECT count(*) FROM subscriptions)`).Scan(&users, &usages, &subscriptions)
+ if err != nil {
+ t.Fatalf("failed to count account rows: %v", err)
+ }
+ if users != 0 || usages != 0 || subscriptions != 0 {
+ t.Fatalf("partial account persisted: users=%d usage=%d subscriptions=%d", users, usages, subscriptions)
+ }
+}
diff --git a/server/pkg/controller/user/userauth.go b/server/pkg/controller/user/userauth.go
index 88ffa55102..ccecee7f20 100644
--- a/server/pkg/controller/user/userauth.go
+++ b/server/pkg/controller/user/userauth.go
@@ -631,7 +631,7 @@ func (c *UserController) onVerificationSuccess(context *gin.Context, email strin
if viper.GetBool("internal.disable-registration") {
return ente.EmailAuthorizationResponse{}, stacktrace.Propagate(ente.ErrPermissionDenied, "")
} else {
- userID, _, err = c.createUser(email, source)
+ userID, _, err = c.createUser(context, email, source)
if err != nil {
return ente.EmailAuthorizationResponse{}, stacktrace.Propagate(err, "")
}
diff --git a/server/pkg/repo/billing.go b/server/pkg/repo/billing.go
index 4792f6afaf..078a83915d 100644
--- a/server/pkg/repo/billing.go
+++ b/server/pkg/repo/billing.go
@@ -1,6 +1,7 @@
package repo
import (
+ "context"
"database/sql"
"encoding/json"
"fmt"
@@ -17,9 +18,9 @@ type BillingRepository struct {
DB *sql.DB
}
-func (repo *BillingRepository) AddSubscription(s ente.Subscription) (int64, error) {
+func (repo *BillingRepository) AddSubscriptionTx(ctx context.Context, tx *sql.Tx, s ente.Subscription) (int64, error) {
var subscriptionID int64
- err := repo.DB.QueryRow(`INSERT INTO subscriptions(user_id, storage, original_transaction_id, expiry_time, product_id, payment_provider, attributes)
+ err := tx.QueryRowContext(ctx, `INSERT INTO subscriptions(user_id, storage, original_transaction_id, expiry_time, product_id, payment_provider, attributes)
VALUES($1, $2, $3, $4, $5, $6, $7)
RETURNING subscription_id`, s.UserID, s.Storage,
s.OriginalTransactionID, s.ExpiryTime, s.ProductID, s.PaymentProvider,
diff --git a/server/pkg/repo/usage.go b/server/pkg/repo/usage.go
index 377e7afd15..6329374af9 100644
--- a/server/pkg/repo/usage.go
+++ b/server/pkg/repo/usage.go
@@ -42,12 +42,9 @@ func (repo *UsageRepository) GetUsage(userID int64) (int64, error) {
return usage, stacktrace.Propagate(err, "")
}
-func (repo *UsageRepository) Create(userID int64) error {
- _, err := repo.DB.Exec(`INSERT INTO usage(user_id, storage_consumed) VALUES ($1,$2) ON CONFLICT DO NOTHING;`,
- userID,
- 0,
- )
- return stacktrace.Propagate(err, "failed to insert/update")
+func (repo *UsageRepository) CreateTx(ctx context.Context, tx *sql.Tx, userID int64) error {
+ _, err := tx.ExecContext(ctx, `INSERT INTO usage(user_id, storage_consumed) VALUES ($1, 0)`, userID)
+ return stacktrace.Propagate(err, "failed to insert usage")
}
func (repo *UsageRepository) GetCombinedUsage(ctx context.Context, userIDs []int64) (int64, error) {
diff --git a/server/pkg/repo/user.go b/server/pkg/repo/user.go
index 1a0656777b..9598e4e56f 100644
--- a/server/pkg/repo/user.go
+++ b/server/pkg/repo/user.go
@@ -302,9 +302,9 @@ func (repo *UserRepository) GetUserUsageWithSubData(ctx context.Context, userIds
return result, nil
}
-func (repo *UserRepository) Create(encryptedEmail ente.EncryptionResult, emailHash string, source *string) (int64, error) {
+func (repo *UserRepository) CreateTx(ctx context.Context, tx *sql.Tx, encryptedEmail ente.EncryptionResult, emailHash string, source *string) (int64, error) {
var userID int64
- err := repo.DB.QueryRow(`INSERT INTO users(encrypted_email, email_decryption_nonce, email_hash, creation_time, source) VALUES($1, $2, $3, $4, $5) RETURNING user_id`,
+ err := tx.QueryRowContext(ctx, `INSERT INTO users(encrypted_email, email_decryption_nonce, email_hash, creation_time, source) VALUES($1, $2, $3, $4, $5) RETURNING user_id`,
encryptedEmail.Cipher, encryptedEmail.Nonce, emailHash, time.Microseconds(), source).Scan(&userID)
if err != nil {
return -1, stacktrace.Propagate(err, "")