feat: add direct upload to 5 track
This commit is contained in:
@@ -36,14 +36,18 @@ func runUploadCleanup(args []string) error {
|
||||
for {
|
||||
pass, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
result, err := store.CleanupUploads(pass, *dryRun, objects.Delete)
|
||||
direct, directErr := store.CleanupDirectUploads(pass, *dryRun, objects.Delete)
|
||||
cancel()
|
||||
if encodeErr := json.NewEncoder(os.Stdout).Encode(result); encodeErr != nil {
|
||||
if encodeErr := json.NewEncoder(os.Stdout).Encode(map[string]any{"originals": result, "direct": direct}); encodeErr != nil {
|
||||
return encodeErr
|
||||
}
|
||||
if !*loop {
|
||||
if err == nil {
|
||||
err = directErr
|
||||
}
|
||||
return err
|
||||
}
|
||||
if err != nil {
|
||||
if err != nil || directErr != nil {
|
||||
log.Print("upload cleanup pass failed; journal retained for retry")
|
||||
}
|
||||
select {
|
||||
|
||||
@@ -4,8 +4,9 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"messenger/src/backend/services"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type createChatRequest struct {
|
||||
@@ -115,6 +116,10 @@ func chatSubresourceHandler(chats *services.ChatService, messages *services.Mess
|
||||
createMessage := createMessageHandler(chats, messages)
|
||||
listMessages := listMessagesHandler(chats, messages)
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if attachments != nil && strings.Contains(r.URL.Path, "/attachment-uploads") {
|
||||
directUploadHandler(chats, attachments)(w, r)
|
||||
return
|
||||
}
|
||||
if attachments != nil && strings.HasSuffix(r.URL.Path, "/attachments") {
|
||||
uploadAttachmentHandler(chats, attachments)(w, r)
|
||||
return
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/services"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func directUploadHandler(chats *services.ChatService, attachments *services.AttachmentService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodPost {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
user, ok := requestUserID(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
parts := strings.Split(strings.TrimPrefix(r.URL.Path, "/api/v1/chats/"), "/")
|
||||
if (len(parts) != 2 && (len(parts) != 4 || parts[3] != "complete")) || parts[1] != "attachment-uploads" {
|
||||
writeError(w, 404, "not_found", "upload endpoint not found", requestID)
|
||||
return
|
||||
}
|
||||
chat, err := uuid.Parse(parts[0])
|
||||
if err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid chat id", requestID)
|
||||
return
|
||||
}
|
||||
if _, err = chats.Get(r.Context(), user, chat.String()); err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 25*time.Second)
|
||||
defer cancel()
|
||||
if len(parts) == 4 {
|
||||
id, err := uuid.Parse(parts[2])
|
||||
if err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid upload id", requestID)
|
||||
return
|
||||
}
|
||||
a, err := attachments.CompleteDirect(ctx, user, chat.String(), id.String())
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 201, a)
|
||||
return
|
||||
}
|
||||
r.Body = http.MaxBytesReader(w, r.Body, 4096)
|
||||
var input services.DirectUploadInput
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
decoder.DisallowUnknownFields()
|
||||
err = decoder.Decode(&input)
|
||||
if err == nil {
|
||||
var extra any
|
||||
if next := decoder.Decode(&extra); !errors.Is(next, io.EOF) {
|
||||
err = errors.New("expected one JSON object")
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid upload JSON (maximum 4 KiB)", requestID)
|
||||
return
|
||||
}
|
||||
result, err := attachments.InitDirect(ctx, user, chat.String(), input)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 201, result)
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
)
|
||||
|
||||
@@ -44,6 +45,11 @@ func writeError(w http.ResponseWriter, status int, code, message, requestID stri
|
||||
}
|
||||
|
||||
func writeServiceError(w http.ResponseWriter, err error, requestID string) {
|
||||
var upload repositories.UploadError
|
||||
if errors.As(err, &upload) {
|
||||
writeError(w, upload.Status, upload.Code, upload.Message, requestID)
|
||||
return
|
||||
}
|
||||
var invalid services.InvalidInput
|
||||
switch {
|
||||
case errors.As(err, &invalid):
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
func (s *AttachmentStore) CleanupDirectUploads(ctx context.Context, dryRun bool, remove func(context.Context, string) error) (CleanupResult, error) {
|
||||
result := CleanupResult{DryRun: dryRun}
|
||||
rows, err := s.db.Query(ctx, `SELECT id::text FROM direct_uploads WHERE cleanup_after<=clock_timestamp() ORDER BY cleanup_after,id LIMIT 100`)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
ids := []string{}
|
||||
for rows.Next() {
|
||||
var id string
|
||||
if err = rows.Scan(&id); err != nil {
|
||||
rows.Close()
|
||||
return result, err
|
||||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
err = rows.Err()
|
||||
rows.Close()
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
result.Candidates = len(ids)
|
||||
for _, id := range ids {
|
||||
outcome, err := s.cleanupDirectUpload(ctx, id, dryRun, remove)
|
||||
if err != nil {
|
||||
result.Failed++
|
||||
continue
|
||||
}
|
||||
switch outcome {
|
||||
case "deleted":
|
||||
result.Deleted++
|
||||
case "eligible":
|
||||
result.Eligible++
|
||||
default:
|
||||
result.Skipped++
|
||||
}
|
||||
}
|
||||
if result.Failed > 0 {
|
||||
return result, fmt.Errorf("%d direct upload cleanup operations failed", result.Failed)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *AttachmentStore) cleanupDirectUpload(ctx context.Context, id string, dryRun bool, remove func(context.Context, string) error) (string, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||
defer cancel()
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer tx.Rollback(context.Background())
|
||||
var key, state string
|
||||
var barrier, complete, purge, now time.Time
|
||||
err = tx.QueryRow(ctx, `SELECT staging_key,state,complete_before+interval '600 seconds',complete_before,purge_after,clock_timestamp() FROM direct_uploads WHERE id=$1 AND cleanup_after<=clock_timestamp() FOR UPDATE SKIP LOCKED`, id).Scan(&key, &state, &barrier, &complete, &purge, &now)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return "skipped", nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
parsed, err := uuid.Parse(id)
|
||||
if err != nil || key != "staging/direct/"+parsed.String() {
|
||||
return "", errors.New("invalid direct staging key")
|
||||
}
|
||||
if state == "pending" && now.Before(barrier) {
|
||||
return "skipped", nil
|
||||
}
|
||||
if dryRun {
|
||||
return "eligible", nil
|
||||
}
|
||||
if err = remove(ctx, key); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if now.After(purge) && !now.Before(barrier) {
|
||||
_, err = tx.Exec(ctx, `DELETE FROM direct_uploads WHERE id=$1`, id)
|
||||
} else {
|
||||
if state == "pending" && !now.Before(complete) {
|
||||
state = "expired"
|
||||
}
|
||||
// An early cleanup is not final: a still valid POST can recreate staging.
|
||||
// Keep a second cleanup after the safety barrier, then retain the receipt 24h.
|
||||
next := barrier
|
||||
if !now.Before(barrier) {
|
||||
next = purge
|
||||
}
|
||||
_, err = tx.Exec(ctx, `UPDATE direct_uploads SET state=$2,cleanup_after=$3 WHERE id=$1`, id, state, next)
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return "deleted", nil
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
// UploadError is also persisted for terminal validation failures, so retries agree.
|
||||
type UploadError struct {
|
||||
Status int
|
||||
Code, Message string
|
||||
}
|
||||
|
||||
func (e UploadError) Error() string { return e.Message }
|
||||
|
||||
func DirectError(code string) UploadError {
|
||||
switch code {
|
||||
case "upload_not_uploaded":
|
||||
return UploadError{409, code, "file has not been uploaded"}
|
||||
case "upload_expired":
|
||||
return UploadError{409, code, "upload confirmation period has expired"}
|
||||
case "upload_size_mismatch":
|
||||
return UploadError{400, code, "uploaded size differs from declared size"}
|
||||
case "upload_checksum_mismatch":
|
||||
return UploadError{400, code, "uploaded SHA-256 differs from declared checksum"}
|
||||
case "payload_too_large":
|
||||
return UploadError{413, code, "image exceeds upload limits"}
|
||||
default:
|
||||
return UploadError{415, "unsupported_media_type", "a valid PNG or JPEG matching Content-Type is required"}
|
||||
}
|
||||
}
|
||||
|
||||
type DirectUpload struct {
|
||||
ID string `json:"upload_id"`
|
||||
AttachmentID string `json:"-"`
|
||||
OwnerID string `json:"-"`
|
||||
ChatID string `json:"-"`
|
||||
Filename string `json:"-"`
|
||||
ContentType string `json:"-"`
|
||||
SizeBytes int64 `json:"-"`
|
||||
SHA256 string `json:"-"`
|
||||
StagingKey string `json:"-"`
|
||||
FinalKey string `json:"-"`
|
||||
State string `json:"-"`
|
||||
ErrorCode *string `json:"-"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
CompleteBefore time.Time `json:"complete_before"`
|
||||
}
|
||||
|
||||
const directColumns = `d.id,d.attachment_id,d.owner_id,d.chat_id,d.filename,d.content_type,d.size_bytes,d.sha256,d.staging_key,d.final_key,d.state,d.error_code,d.expires_at,d.complete_before`
|
||||
|
||||
func scanDirect(row pgx.Row) (DirectUpload, error) {
|
||||
var u DirectUpload
|
||||
err := row.Scan(&u.ID, &u.AttachmentID, &u.OwnerID, &u.ChatID, &u.Filename, &u.ContentType, &u.SizeBytes, &u.SHA256, &u.StagingKey, &u.FinalKey, &u.State, &u.ErrorCode, &u.ExpiresAt, &u.CompleteBefore)
|
||||
return u, err
|
||||
}
|
||||
|
||||
func (s *AttachmentStore) CreateDirect(ctx context.Context, u DirectUpload) (DirectUpload, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
return scanDirect(s.db.QueryRow(ctx, `INSERT INTO direct_uploads AS d(id,attachment_id,owner_id,chat_id,filename,content_type,size_bytes,sha256,staging_key,final_key) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10) RETURNING `+directColumns, u.ID, u.AttachmentID, u.OwnerID, u.ChatID, u.Filename, u.ContentType, u.SizeBytes, u.SHA256, u.StagingKey, u.FinalKey))
|
||||
}
|
||||
|
||||
func (s *AttachmentStore) GetDirect(ctx context.Context, id, chat, user string) (DirectUpload, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
return scanDirect(s.db.QueryRow(ctx, `SELECT `+directColumns+` FROM direct_uploads d JOIN chat_members cm ON cm.chat_id=d.chat_id AND cm.user_id=$3 WHERE d.id=$1 AND d.chat_id=$2 AND d.owner_id=$3`, id, chat, user))
|
||||
}
|
||||
|
||||
// The session lock serializes complete across replicas. A separately committed
|
||||
// reservation survives rollback/crash, while its row lock protects the final key.
|
||||
func (s *AttachmentStore) CompleteDirect(ctx context.Context, id, chat, user string, validate func(context.Context, DirectUpload) error) (Attachment, error) {
|
||||
var a Attachment
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return a, err
|
||||
}
|
||||
defer tx.Rollback(context.Background())
|
||||
u, err := scanDirect(tx.QueryRow(ctx, `SELECT `+directColumns+` FROM direct_uploads d JOIN chat_members cm ON cm.chat_id=d.chat_id AND cm.user_id=$3 WHERE d.id=$1 AND d.chat_id=$2 AND d.owner_id=$3 FOR UPDATE OF d`, id, chat, user))
|
||||
if err != nil {
|
||||
return a, err
|
||||
}
|
||||
if u.StagingKey != "staging/direct/"+u.ID || u.FinalKey != "originals/"+u.AttachmentID {
|
||||
return a, errors.New("invalid direct upload object keys")
|
||||
}
|
||||
if u.State == "ready" {
|
||||
return s.GetForMember(ctx, u.AttachmentID, user)
|
||||
}
|
||||
if u.State == "rejected" && u.ErrorCode != nil {
|
||||
return a, DirectError(*u.ErrorCode)
|
||||
}
|
||||
var expired bool
|
||||
if err = tx.QueryRow(ctx, `SELECT clock_timestamp()>=$1`, u.CompleteBefore).Scan(&expired); err != nil {
|
||||
return a, err
|
||||
}
|
||||
if expired || u.State == "expired" {
|
||||
return a, DirectError("upload_expired")
|
||||
}
|
||||
grace, err := UploadCleanupGrace()
|
||||
if err != nil {
|
||||
return a, err
|
||||
}
|
||||
// ON CONFLICT handles retry after an interrupted COPY without losing tracking.
|
||||
_, err = s.db.Exec(ctx, `INSERT INTO upload_attempts(id,object_key,cleanup_after) VALUES($1,$2,clock_timestamp()+$3*interval '1 second') ON CONFLICT(id) DO UPDATE SET cleanup_after=GREATEST(upload_attempts.cleanup_after,EXCLUDED.cleanup_after) WHERE upload_attempts.object_key=EXCLUDED.object_key`, u.AttachmentID, u.FinalKey, grace.Seconds())
|
||||
if err != nil {
|
||||
return a, err
|
||||
}
|
||||
var key string
|
||||
if err = tx.QueryRow(ctx, `SELECT object_key FROM upload_attempts WHERE id=$1 AND cleanup_after>clock_timestamp() FOR UPDATE`, u.AttachmentID).Scan(&key); err != nil {
|
||||
return a, err
|
||||
}
|
||||
if key != u.FinalKey {
|
||||
return a, errors.New("direct upload reservation mismatch")
|
||||
}
|
||||
if err = validate(ctx, u); err != nil {
|
||||
var invalid UploadError
|
||||
if errors.As(err, &invalid) && (invalid.Status == 400 || invalid.Status == 413 || invalid.Status == 415) {
|
||||
_, saveErr := tx.Exec(ctx, `UPDATE direct_uploads SET state='rejected',error_code=$2,cleanup_after=clock_timestamp(),purge_after=clock_timestamp()+interval '24 hours' WHERE id=$1`, id, invalid.Code)
|
||||
if saveErr != nil {
|
||||
return a, saveErr
|
||||
}
|
||||
if saveErr = tx.Commit(ctx); saveErr != nil {
|
||||
return a, saveErr
|
||||
}
|
||||
}
|
||||
return a, err
|
||||
}
|
||||
now := time.Now().UTC().Truncate(time.Microsecond)
|
||||
a = Attachment{ID: u.AttachmentID, ChatID: u.ChatID, OwnerID: u.OwnerID, Filename: u.Filename, ContentType: u.ContentType, SizeBytes: u.SizeBytes, SHA256: u.SHA256, ObjectKey: u.FinalKey, Status: "ready", Variants: []any{}, CreatedAt: now, UpdatedAt: now}
|
||||
_, err = tx.Exec(ctx, `INSERT INTO attachments(id,chat_id,owner_id,filename,object_key,content_type,size_bytes,sha256,created_at,updated_at) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)`, a.ID, a.ChatID, a.OwnerID, a.Filename, a.ObjectKey, a.ContentType, a.SizeBytes, a.SHA256, a.CreatedAt, a.UpdatedAt)
|
||||
if err != nil {
|
||||
return Attachment{}, err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `UPDATE direct_uploads SET state='ready',cleanup_after=clock_timestamp(),purge_after=clock_timestamp()+interval '24 hours' WHERE id=$1`, id); err != nil {
|
||||
return Attachment{}, err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `DELETE FROM upload_attempts WHERE id=$1`, a.ID); err != nil {
|
||||
return Attachment{}, err
|
||||
}
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
return Attachment{}, err
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE direct_uploads;
|
||||
@@ -0,0 +1,20 @@
|
||||
CREATE TABLE direct_uploads (
|
||||
id uuid PRIMARY KEY,
|
||||
attachment_id uuid NOT NULL UNIQUE,
|
||||
owner_id uuid NOT NULL REFERENCES users(id),
|
||||
chat_id uuid NOT NULL REFERENCES chats(id),
|
||||
filename text NOT NULL,
|
||||
content_type text NOT NULL CHECK (content_type IN ('image/png','image/jpeg')),
|
||||
size_bytes bigint NOT NULL CHECK (size_bytes BETWEEN 1 AND 10485760),
|
||||
sha256 text NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'),
|
||||
staging_key text NOT NULL UNIQUE,
|
||||
final_key text NOT NULL UNIQUE,
|
||||
state text NOT NULL DEFAULT 'pending' CHECK (state IN ('pending','ready','rejected','expired')),
|
||||
error_code text,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
expires_at timestamptz NOT NULL DEFAULT clock_timestamp()+interval '120 seconds',
|
||||
complete_before timestamptz NOT NULL DEFAULT clock_timestamp()+interval '720 seconds',
|
||||
cleanup_after timestamptz NOT NULL DEFAULT clock_timestamp()+interval '1320 seconds',
|
||||
purge_after timestamptz NOT NULL DEFAULT clock_timestamp()+interval '24 hours'
|
||||
);
|
||||
CREATE INDEX direct_uploads_cleanup_idx ON direct_uploads(cleanup_after,id);
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
madmin "github.com/minio/madmin-go/v3"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
"github.com/minio/minio-go/v7/pkg/lifecycle"
|
||||
)
|
||||
|
||||
func readS3Secret(name string) (string, error) {
|
||||
@@ -58,13 +59,16 @@ func BootstrapS3(ctx context.Context) error {
|
||||
if err = store.Bootstrap(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = bootstrapDirectLifecycle(ctx, client, store.bucket); err != nil {
|
||||
return err
|
||||
}
|
||||
admin, err := madmin.New(endpoint.Host, rootAccess, rootSecret, endpoint.Scheme == "https")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
policy := map[string]any{"Version": "2012-10-17", "Statement": []any{
|
||||
map[string]any{"Effect": "Allow", "Action": []string{"s3:GetBucketLocation", "s3:ListBucket"}, "Resource": []string{"arn:aws:s3:::" + store.bucket}},
|
||||
map[string]any{"Effect": "Allow", "Action": []string{"s3:GetObject", "s3:PutObject", "s3:DeleteObject"}, "Resource": []string{"arn:aws:s3:::" + store.bucket + "/originals/*"}},
|
||||
map[string]any{"Effect": "Allow", "Action": []string{"s3:GetObject", "s3:PutObject", "s3:DeleteObject"}, "Resource": []string{"arn:aws:s3:::" + store.bucket + "/originals/*", "arn:aws:s3:::" + store.bucket + "/staging/direct/*"}},
|
||||
}}
|
||||
data, err := json.Marshal(policy)
|
||||
if err != nil {
|
||||
@@ -81,3 +85,23 @@ func BootstrapS3(ctx context.Context) error {
|
||||
// This idempotent API is supported by our pinned MinIO server release.
|
||||
return admin.SetPolicy(ctx, name, access, false)
|
||||
}
|
||||
|
||||
func bootstrapDirectLifecycle(ctx context.Context, client *minio.Client, bucket string) error {
|
||||
config, err := client.GetBucketLifecycle(ctx, bucket)
|
||||
if err != nil && minio.ToErrorResponse(err).Code != "NoSuchLifecycleConfiguration" {
|
||||
return err
|
||||
}
|
||||
if config == nil {
|
||||
config = lifecycle.NewConfiguration()
|
||||
}
|
||||
const id = "messenger-direct-staging-expiration"
|
||||
rules := make([]lifecycle.Rule, 0, len(config.Rules)+1)
|
||||
for _, rule := range config.Rules {
|
||||
if rule.ID != id {
|
||||
rules = append(rules, rule)
|
||||
}
|
||||
}
|
||||
rules = append(rules, lifecycle.Rule{ID: id, Status: "Enabled", RuleFilter: lifecycle.Filter{Prefix: "staging/direct/"}, Expiration: lifecycle.Expiration{Days: 1}})
|
||||
config.Rules = rules
|
||||
return client.SetBucketLifecycle(ctx, bucket, config)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/url"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
)
|
||||
|
||||
type PostUpload struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
Fields map[string]string `json:"fields"`
|
||||
}
|
||||
|
||||
type StoredObject struct {
|
||||
Size int64
|
||||
ETag string
|
||||
}
|
||||
|
||||
func IsObjectMissing(err error) bool {
|
||||
code := minio.ToErrorResponse(err).Code
|
||||
return code == "NoSuchKey" || code == "NoSuchObject" || code == "NotFound"
|
||||
}
|
||||
|
||||
func (s *ObjectStore) PresignDirect(ctx context.Context, key, mime string, size int64, expires time.Time) (PostUpload, error) {
|
||||
endpoint, err := url.Parse(os.Getenv("S3_PUBLIC_ENDPOINT"))
|
||||
if err != nil || endpoint.Host == "" || (endpoint.Scheme != "http" && endpoint.Scheme != "https") || endpoint.Path != "" || endpoint.RawQuery != "" || endpoint.User != nil {
|
||||
return PostUpload{}, errors.New("invalid S3_PUBLIC_ENDPOINT")
|
||||
}
|
||||
access, err := readS3Secret("S3_ACCESS_KEY_ID_FILE")
|
||||
if err != nil {
|
||||
return PostUpload{}, err
|
||||
}
|
||||
secret, err := readS3Secret("S3_SECRET_ACCESS_KEY_FILE")
|
||||
if err != nil {
|
||||
return PostUpload{}, err
|
||||
}
|
||||
signer, err := minio.New(endpoint.Host, &minio.Options{Creds: credentials.NewStaticV4(access, secret, ""), Secure: endpoint.Scheme == "https", Region: os.Getenv("S3_REGION"), BucketLookup: minio.BucketLookupPath})
|
||||
if err != nil {
|
||||
return PostUpload{}, err
|
||||
}
|
||||
policy := minio.NewPostPolicy()
|
||||
for _, err = range []error{policy.SetBucket(s.bucket), policy.SetKey(key), policy.SetExpires(expires), policy.SetContentType(mime), policy.SetContentLengthRange(size, size), policy.SetSuccessStatusAction("204")} {
|
||||
if err != nil {
|
||||
return PostUpload{}, err
|
||||
}
|
||||
}
|
||||
target, fields, err := signer.PresignedPostPolicy(ctx, policy)
|
||||
if err != nil {
|
||||
return PostUpload{}, errors.New("cannot sign upload policy")
|
||||
}
|
||||
return PostUpload{Method: "POST", URL: target.String(), Fields: fields}, nil
|
||||
}
|
||||
|
||||
func (s *ObjectStore) Stat(ctx context.Context, key string) (StoredObject, error) {
|
||||
object, err := s.client.StatObject(ctx, s.bucket, key, minio.StatObjectOptions{})
|
||||
return StoredObject{Size: object.Size, ETag: object.ETag}, err
|
||||
}
|
||||
|
||||
func (s *ObjectStore) Copy(ctx context.Context, source, destination, etag string) error {
|
||||
_, err := s.client.CopyObject(ctx, minio.CopyDestOptions{Bucket: s.bucket, Object: destination}, minio.CopySrcOptions{Bucket: s.bucket, Object: source, MatchETag: etag})
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
type DirectUploadInput struct {
|
||||
Filename string `json:"filename"`
|
||||
ContentType string `json:"content_type"`
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
SHA256 string `json:"sha256"`
|
||||
}
|
||||
|
||||
type DirectUploadResponse struct {
|
||||
repositories.DirectUpload
|
||||
Upload repositories.PostUpload `json:"upload"`
|
||||
}
|
||||
|
||||
type DirectObjects interface {
|
||||
AttachmentObjects
|
||||
PresignDirect(context.Context, string, string, int64, time.Time) (repositories.PostUpload, error)
|
||||
Stat(context.Context, string) (repositories.StoredObject, error)
|
||||
Copy(context.Context, string, string, string) error
|
||||
}
|
||||
|
||||
var checksumPattern = regexp.MustCompile(`^[a-f0-9]{64}$`)
|
||||
|
||||
func (s *AttachmentService) InitDirect(ctx context.Context, user, chat string, input DirectUploadInput) (DirectUploadResponse, error) {
|
||||
if err := ValidateFilename(input.Filename); err != nil {
|
||||
return DirectUploadResponse{}, err
|
||||
}
|
||||
if input.ContentType != "image/png" && input.ContentType != "image/jpeg" {
|
||||
return DirectUploadResponse{}, ErrUnsupportedMedia
|
||||
}
|
||||
if input.SizeBytes > MaxUploadBytes {
|
||||
return DirectUploadResponse{}, ErrPayloadTooLarge
|
||||
}
|
||||
if input.SizeBytes < 1 || !checksumPattern.MatchString(input.SHA256) {
|
||||
return DirectUploadResponse{}, InvalidInput{"size must be positive and sha256 must contain 64 lowercase hex characters"}
|
||||
}
|
||||
objects, ok := s.objects.(DirectObjects)
|
||||
if !ok {
|
||||
return DirectUploadResponse{}, errors.New("direct uploads unavailable")
|
||||
}
|
||||
id, attachment := uuid.NewString(), uuid.NewString()
|
||||
u, err := s.metadata.CreateDirect(ctx, repositories.DirectUpload{ID: id, AttachmentID: attachment, OwnerID: user, ChatID: chat, Filename: input.Filename, ContentType: input.ContentType, SizeBytes: input.SizeBytes, SHA256: input.SHA256, StagingKey: "staging/direct/" + id, FinalKey: "originals/" + attachment})
|
||||
if err != nil {
|
||||
return DirectUploadResponse{}, err
|
||||
}
|
||||
post, err := objects.PresignDirect(ctx, u.StagingKey, u.ContentType, u.SizeBytes, u.ExpiresAt)
|
||||
return DirectUploadResponse{DirectUpload: u, Upload: post}, err
|
||||
}
|
||||
|
||||
func (s *AttachmentService) CompleteDirect(ctx context.Context, user, chat, id string) (repositories.Attachment, error) {
|
||||
u, err := s.metadata.GetDirect(ctx, id, chat, user)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return repositories.Attachment{}, ErrAttachmentNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
if u.State == "ready" {
|
||||
return s.Get(ctx, u.AttachmentID, user)
|
||||
}
|
||||
objects, ok := s.objects.(DirectObjects)
|
||||
if !ok {
|
||||
return repositories.Attachment{}, errors.New("direct uploads unavailable")
|
||||
}
|
||||
select {
|
||||
case s.uploadSlot <- struct{}{}:
|
||||
defer func() { <-s.uploadSlot }()
|
||||
case <-ctx.Done():
|
||||
return repositories.Attachment{}, ctx.Err()
|
||||
}
|
||||
a, err := s.metadata.CompleteDirect(ctx, id, chat, user, func(ctx context.Context, u repositories.DirectUpload) error {
|
||||
info, err := objects.Stat(ctx, u.StagingKey)
|
||||
if repositories.IsObjectMissing(err) {
|
||||
return repositories.DirectError("upload_not_uploaded")
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if info.Size > MaxUploadBytes {
|
||||
return repositories.DirectError("payload_too_large")
|
||||
}
|
||||
if info.Size != u.SizeBytes {
|
||||
return repositories.DirectError("upload_size_mismatch")
|
||||
}
|
||||
// A signed staging key remains writable until expiry. Validate the isolated
|
||||
// final object, never a staging object that can change after validation.
|
||||
if err = objects.Copy(ctx, u.StagingKey, u.FinalKey, info.ETag); err != nil {
|
||||
return err
|
||||
}
|
||||
reader, err := objects.Get(ctx, u.FinalKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer reader.Close()
|
||||
file, err := os.CreateTemp(os.Getenv("UPLOAD_TMP_DIR"), "direct-*")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
if err = os.Remove(file.Name()); err != nil {
|
||||
return err
|
||||
}
|
||||
hash := sha256.New()
|
||||
size, err := io.CopyBuffer(io.MultiWriter(file, hash), io.LimitReader(reader, MaxUploadBytes+1), make([]byte, 32*1024))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if size > MaxUploadBytes {
|
||||
return repositories.DirectError("payload_too_large")
|
||||
}
|
||||
if size != u.SizeBytes {
|
||||
return repositories.DirectError("upload_size_mismatch")
|
||||
}
|
||||
if hex.EncodeToString(hash.Sum(nil)) != u.SHA256 {
|
||||
return repositories.DirectError("upload_checksum_mismatch")
|
||||
}
|
||||
if err = ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = file.Seek(0, io.SeekStart); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = ValidateImageReader(file, u.ContentType)
|
||||
if errors.Is(err, ErrPayloadTooLarge) {
|
||||
return repositories.DirectError("payload_too_large")
|
||||
}
|
||||
if errors.Is(err, ErrUnsupportedMedia) {
|
||||
return repositories.DirectError("unsupported_media_type")
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ctx.Err()
|
||||
})
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return a, ErrAttachmentNotFound
|
||||
}
|
||||
return a, err
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
)
|
||||
|
||||
func TestDirectUploadCleanupSafetyAndReplay(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
ctx := context.Background()
|
||||
data := uploadPNG(t)
|
||||
fresh, _ := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
expired, _ := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
locked, _ := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
ready, service := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
for _, u := range []string{fresh.ID, expired.ID, locked.ID, ready.ID} {
|
||||
record, err := store.GetDirect(ctx, u, chat, user)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = objects.Put(ctx, record.StagingKey, bytes.NewReader(data), int64(len(data)), "image/png"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
a, err := service.CompleteDirect(ctx, user, chat, ready.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = f.db.Exec(ctx, `UPDATE direct_uploads SET complete_before=clock_timestamp()-interval '601 seconds',cleanup_after=clock_timestamp()-interval '1 second' WHERE id=ANY($1::uuid[])`, []string{expired.ID, locked.ID}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tx, err := f.db.Begin(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
if _, err = tx.Exec(ctx, `SELECT id FROM direct_uploads WHERE id=$1 FOR UPDATE`, locked.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dry, err := store.CleanupDirectUploads(ctx, true, objects.Delete)
|
||||
if err != nil || dry.Eligible != 2 || dry.Deleted != 0 || dry.Skipped != 1 {
|
||||
t.Fatalf("dry: %+v %v", dry, err)
|
||||
}
|
||||
result, err := store.CleanupDirectUploads(ctx, false, objects.Delete)
|
||||
if err != nil || result.Deleted != 2 || result.Skipped != 1 {
|
||||
t.Fatalf("cleanup: %+v %v", result, err)
|
||||
}
|
||||
for _, id := range []string{fresh.ID, locked.ID} {
|
||||
u, _ := store.GetDirect(ctx, id, chat, user)
|
||||
if _, err = objects.Stat(ctx, u.StagingKey); err != nil {
|
||||
t.Fatal("fresh/locked staging removed", err)
|
||||
}
|
||||
}
|
||||
for _, id := range []string{ready.ID, expired.ID} {
|
||||
u, _ := store.GetDirect(ctx, id, chat, user)
|
||||
if _, err = objects.Stat(ctx, u.StagingKey); !repositories.IsObjectMissing(err) {
|
||||
t.Fatal("eligible staging remains", err)
|
||||
}
|
||||
}
|
||||
if _, err = objects.Stat(ctx, a.ObjectKey); err != nil {
|
||||
t.Fatal("ready original removed", err)
|
||||
}
|
||||
// A valid form can recreate staging after early cleanup. The receipt persists.
|
||||
if postDirect(t, ready.Upload, data, nil) != 204 {
|
||||
t.Fatal("replay failed")
|
||||
}
|
||||
if _, err = f.db.Exec(ctx, `UPDATE direct_uploads SET complete_before=clock_timestamp()-interval '601 seconds',cleanup_after=clock_timestamp()-interval '1 second' WHERE id=$1`, ready.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
again, err := store.CleanupDirectUploads(ctx, false, objects.Delete)
|
||||
if err != nil || again.Deleted != 1 {
|
||||
t.Fatalf("replay cleanup: %+v %v", again, err)
|
||||
}
|
||||
u, _ := store.GetDirect(ctx, ready.ID, chat, user)
|
||||
if _, err = objects.Stat(ctx, u.StagingKey); !repositories.IsObjectMissing(err) {
|
||||
t.Fatal("replayed staging remains")
|
||||
}
|
||||
same, err := service.CompleteDirect(ctx, user, chat, ready.ID)
|
||||
if err != nil || same.ID != a.ID {
|
||||
t.Fatal("cleanup broke idempotent complete", err)
|
||||
}
|
||||
tx.Rollback(ctx)
|
||||
if _, err = f.db.Exec(ctx, `UPDATE direct_uploads SET purge_after=clock_timestamp()-interval '1 second',cleanup_after=clock_timestamp()-interval '1 second' WHERE id=$1`, ready.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = store.CleanupDirectUploads(ctx, false, objects.Delete); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = objects.Stat(ctx, a.ObjectKey); err != nil {
|
||||
t.Fatal("receipt purge removed final", err)
|
||||
}
|
||||
if _, err = store.GetForMember(ctx, a.ID, user); err != nil {
|
||||
t.Fatal("receipt purge removed metadata", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirectUploadCleanupDependencyFailureAndMalformedKey(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
ctx := context.Background()
|
||||
data := uploadPNG(t)
|
||||
result, _ := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
u, _ := store.GetDirect(ctx, result.ID, chat, user)
|
||||
if err := objects.Put(ctx, u.StagingKey, bytes.NewReader(data), int64(len(data)), "image/png"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := f.db.Exec(ctx, `UPDATE direct_uploads SET complete_before=clock_timestamp()-interval '601 seconds',cleanup_after=clock_timestamp()-interval '1 second' WHERE id=$1`, result.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
failed, err := store.CleanupDirectUploads(ctx, false, func(context.Context, string) error { return errors.New("S3 unavailable") })
|
||||
if err == nil || failed.Failed != 1 {
|
||||
t.Fatal("failed DELETE was lost")
|
||||
}
|
||||
if _, err = objects.Stat(ctx, u.StagingKey); err != nil {
|
||||
t.Fatal("staging changed on failed DELETE", err)
|
||||
}
|
||||
cancelled, cancel := context.WithCancel(ctx)
|
||||
cancel()
|
||||
if _, err = store.CleanupDirectUploads(cancelled, false, objects.Delete); err == nil {
|
||||
t.Fatal("DB cancellation ignored")
|
||||
}
|
||||
if _, err = f.db.Exec(ctx, `UPDATE direct_uploads SET staging_key=$2 WHERE id=$1`, result.ID, "originals/"+u.AttachmentID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
called := false
|
||||
failed, err = store.CleanupDirectUploads(ctx, false, func(context.Context, string) error { called = true; return nil })
|
||||
if err == nil || failed.Failed != 1 || called {
|
||||
t.Fatal("malformed key reached S3 DELETE")
|
||||
}
|
||||
if _, err = f.db.Exec(ctx, `UPDATE direct_uploads SET staging_key=$2 WHERE id=$1`, result.ID, u.StagingKey); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
retried, err := store.CleanupDirectUploads(ctx, false, objects.Delete)
|
||||
if err != nil || retried.Deleted != 1 {
|
||||
t.Fatalf("retry: %+v %v", retried, err)
|
||||
}
|
||||
repeated, err := store.CleanupDirectUploads(ctx, false, objects.Delete)
|
||||
if err != nil || repeated.Deleted != 0 {
|
||||
t.Fatalf("repeat: %+v %v", repeated, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/lifecycle"
|
||||
)
|
||||
|
||||
func TestDirectUploadAccessPermissionsAndLifecycle(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
root := s3Client(t, true, false)
|
||||
app := s3Client(t, false, false)
|
||||
anon := s3Client(t, false, true)
|
||||
bucket := os.Getenv("S3_BUCKET")
|
||||
key := "staging/direct/" + uuid.NewString()
|
||||
t.Cleanup(func() { root.RemoveObject(ctx, bucket, key, minio.RemoveObjectOptions{}) })
|
||||
if _, err := app.PutObject(ctx, bucket, key, bytes.NewReader([]byte("x")), 1, minio.PutObjectOptions{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
object, err := anon.GetObject(ctx, bucket, key, minio.GetObjectOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = object.Stat()
|
||||
object.Close()
|
||||
denied(t, err)
|
||||
_, err = app.PutObject(ctx, bucket, "staging/forbidden/"+uuid.NewString(), bytes.NewReader([]byte("x")), 1, minio.PutObjectOptions{})
|
||||
denied(t, err)
|
||||
config, err := root.GetBucketLifecycle(ctx, bucket)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Add only our own harmless prefix rule, verify bootstrap merges rather than replaces.
|
||||
ruleID := "direct-test-" + uuid.NewString()
|
||||
config.Rules = append(config.Rules, lifecycle.Rule{ID: ruleID, Status: "Enabled", RuleFilter: lifecycle.Filter{Prefix: "test-lifecycle/" + ruleID + "/"}, Expiration: lifecycle.Expiration{Days: 7}})
|
||||
if err = root.SetBucketLifecycle(ctx, bucket, config); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
c, e := root.GetBucketLifecycle(ctx, bucket)
|
||||
if e != nil {
|
||||
t.Error(e)
|
||||
return
|
||||
}
|
||||
rules := []lifecycle.Rule{}
|
||||
for _, r := range c.Rules {
|
||||
if r.ID != ruleID {
|
||||
rules = append(rules, r)
|
||||
}
|
||||
}
|
||||
c.Rules = rules
|
||||
if e = root.SetBucketLifecycle(ctx, bucket, c); e != nil {
|
||||
t.Error(e)
|
||||
}
|
||||
})
|
||||
for i := 0; i < 2; i++ {
|
||||
if err = repositories.BootstrapS3(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
config, err = root.GetBucketLifecycle(ctx, bucket)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
managed, preserved := 0, false
|
||||
for _, r := range config.Rules {
|
||||
if r.ID == ruleID {
|
||||
preserved = true
|
||||
}
|
||||
if r.ID == "messenger-direct-staging-expiration" {
|
||||
managed++
|
||||
if r.RuleFilter.Prefix != "staging/direct/" || r.Expiration.Days != 1 || r.Status != "Enabled" {
|
||||
t.Fatal("unsafe staging lifecycle rule")
|
||||
}
|
||||
}
|
||||
}
|
||||
if managed != 1 || !preserved {
|
||||
t.Fatal("bootstrap is not an idempotent lifecycle merge")
|
||||
}
|
||||
denied(t, app.SetBucketLifecycle(ctx, bucket, config))
|
||||
}
|
||||
|
||||
func TestDirectUploadAccessCORS(t *testing.T) {
|
||||
if os.Getenv("S3_ENDPOINT") == "" {
|
||||
t.Skip("S3 integration")
|
||||
}
|
||||
for _, sample := range []struct {
|
||||
origin string
|
||||
allowed bool
|
||||
}{{"http://127.0.0.1:5173", true}, {"http://localhost:5173", true}, {"https://untrusted.example", false}} {
|
||||
request, err := http.NewRequest("OPTIONS", os.Getenv("S3_ENDPOINT")+"/"+os.Getenv("S3_BUCKET"), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
request.Header.Set("Origin", sample.origin)
|
||||
request.Header.Set("Access-Control-Request-Method", "POST")
|
||||
request.Header.Set("Access-Control-Request-Headers", "content-type")
|
||||
response, err := http.DefaultClient.Do(request)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
response.Body.Close()
|
||||
allowed := response.Header.Get("Access-Control-Allow-Origin") == sample.origin
|
||||
if allowed != sample.allowed || response.Header.Get("Access-Control-Allow-Origin") == "*" {
|
||||
t.Fatalf("unexpected CORS allowance for %s", sample.origin)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,474 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"hash/crc32"
|
||||
"image"
|
||||
"image/jpeg"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func directInput(data []byte, mime string) services.DirectUploadInput {
|
||||
sum := sha256.Sum256(data)
|
||||
return services.DirectUploadInput{Filename: "direct.png", ContentType: mime, SizeBytes: int64(len(data)), SHA256: hex.EncodeToString(sum[:])}
|
||||
}
|
||||
|
||||
func beginDirect(t *testing.T, f *testFixture, store *repositories.AttachmentStore, objects *repositories.ObjectStore, user, chat string, data []byte, mime string) (services.DirectUploadResponse, *services.AttachmentService) {
|
||||
t.Helper()
|
||||
t.Setenv("S3_PUBLIC_ENDPOINT", os.Getenv("S3_ENDPOINT"))
|
||||
service := services.NewAttachmentService(store, objects)
|
||||
result, err := service.InitDirect(context.Background(), user, chat, directInput(data, mime))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rememberDirect(t, store, objects, result.ID, chat, user)
|
||||
return result, service
|
||||
}
|
||||
|
||||
func rememberDirect(t *testing.T, store *repositories.AttachmentStore, objects *repositories.ObjectStore, id, chat, user string) {
|
||||
t.Helper()
|
||||
u, err := store.GetDirect(context.Background(), id, chat, user)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
objects.Delete(context.Background(), u.StagingKey)
|
||||
objects.Delete(context.Background(), u.FinalKey)
|
||||
})
|
||||
}
|
||||
|
||||
func postDirect(t *testing.T, p repositories.PostUpload, data []byte, change func(map[string]string)) int {
|
||||
t.Helper()
|
||||
fields := map[string]string{}
|
||||
for k, v := range p.Fields {
|
||||
fields[k] = v
|
||||
}
|
||||
if change != nil {
|
||||
change(fields)
|
||||
}
|
||||
var body bytes.Buffer
|
||||
writer := multipart.NewWriter(&body)
|
||||
for k, v := range fields {
|
||||
if err := writer.WriteField(k, v); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
file, err := writer.CreateFormFile("file", "image.png")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = file.Write(data); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = writer.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req, err := http.NewRequest("POST", p.URL, &body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
client := http.Client{Timeout: 10 * time.Second}
|
||||
response, err := client.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal("S3 form request failed")
|
||||
}
|
||||
defer response.Body.Close()
|
||||
io.Copy(io.Discard, response.Body)
|
||||
return response.StatusCode
|
||||
}
|
||||
|
||||
func requireDirectError(t *testing.T, err error, status int, code string) {
|
||||
t.Helper()
|
||||
var e repositories.UploadError
|
||||
if !errors.As(err, &e) || e.Status != status || e.Code != code {
|
||||
t.Fatalf("expected %d %s, got %v", status, code, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirectUploadFlow(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
png := append(uploadPNG(t), []byte("aaaa")...)
|
||||
var jpg bytes.Buffer
|
||||
if err := jpeg.Encode(&jpg, image.NewRGBA(image.Rect(0, 0, 8, 8)), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, sample := range []struct {
|
||||
data []byte
|
||||
mime string
|
||||
}{{png, "image/png"}, {jpg.Bytes(), "image/jpeg"}} {
|
||||
result, service := beginDirect(t, f, store, objects, user, chat, sample.data, sample.mime)
|
||||
if result.ExpiresAt.Sub(time.Now()) > 121*time.Second || result.CompleteBefore.Sub(result.ExpiresAt) < 599*time.Second {
|
||||
t.Fatal("wrong upload deadlines")
|
||||
}
|
||||
if status := postDirect(t, result.Upload, sample.data, nil); status != 204 {
|
||||
t.Fatalf("POST: %d", status)
|
||||
}
|
||||
a, err := service.CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
router := attachmentRouter(f, objects)
|
||||
content := attachmentCall(t, f, router, "GET", "/api/v1/attachments/"+a.ID+"/content", user, "", nil, 200)
|
||||
if !bytes.Equal(content.Body.Bytes(), sample.data) {
|
||||
t.Fatal("original bytes changed")
|
||||
}
|
||||
message := []byte(`{"text":"","attachment_ids":["` + a.ID + `"]}`)
|
||||
attachmentCall(t, f, router, "POST", "/api/v1/chats/"+chat+"/messages", user, "application/json", message, 201)
|
||||
// Lost success response is represented by ignoring a, then asking another service.
|
||||
again, err := services.NewAttachmentService(store, objects).CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
if err != nil || again.ID != a.ID || !again.CreatedAt.Equal(a.CreatedAt) {
|
||||
t.Fatal("complete is not idempotent", err)
|
||||
}
|
||||
replacement := append([]byte(nil), sample.data...)
|
||||
replacement[len(replacement)-1] ^= 1
|
||||
if status := postDirect(t, result.Upload, replacement, nil); status != 204 {
|
||||
t.Fatal("expected valid staging replay")
|
||||
}
|
||||
final, err := objects.Get(context.Background(), a.ObjectKey)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
original, err := io.ReadAll(final)
|
||||
final.Close()
|
||||
if err != nil || !bytes.Equal(original, sample.data) {
|
||||
t.Fatal("staging replay modified final")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirectUploadValidationHTTP(t *testing.T) {
|
||||
f, _, objects, user, chat := uploadFixture(t)
|
||||
t.Setenv("S3_PUBLIC_ENDPOINT", os.Getenv("S3_ENDPOINT"))
|
||||
router := attachmentRouter(f, objects)
|
||||
path := "/api/v1/chats/" + chat + "/attachment-uploads"
|
||||
input := directInput(uploadPNG(t), "image/png")
|
||||
valid, _ := json.Marshal(input)
|
||||
for _, sample := range []struct {
|
||||
body []byte
|
||||
status int
|
||||
}{
|
||||
{[]byte(`null`), 400}, {[]byte(`{}`), 400}, {append(append([]byte(nil), valid...), []byte(` {}`)...), 400},
|
||||
{[]byte(strings.Repeat("x", 4097)), 400}, {[]byte(`{"filename":"x","size_bytes":1.5}`), 400},
|
||||
} {
|
||||
attachmentCall(t, f, router, "POST", path, user, "application/json", sample.body, sample.status)
|
||||
}
|
||||
for _, sample := range []struct {
|
||||
mutate func(*services.DirectUploadInput)
|
||||
status int
|
||||
}{
|
||||
{func(i *services.DirectUploadInput) { i.Filename = "../image.png" }, 400},
|
||||
{func(i *services.DirectUploadInput) { i.SHA256 = "invalid" }, 400},
|
||||
{func(i *services.DirectUploadInput) { i.SizeBytes = 0 }, 400},
|
||||
{func(i *services.DirectUploadInput) { i.SizeBytes = 10485761 }, 413},
|
||||
{func(i *services.DirectUploadInput) { i.ContentType = "text/plain" }, 415},
|
||||
} {
|
||||
i := input
|
||||
sample.mutate(&i)
|
||||
body, _ := json.Marshal(i)
|
||||
attachmentCall(t, f, router, "POST", path, user, "application/json", body, sample.status)
|
||||
}
|
||||
store := repositories.NewAttachmentStore(f.db)
|
||||
w := attachmentCall(t, f, router, "POST", path, user, "application/json", valid, 201)
|
||||
var result services.DirectUploadResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rememberDirect(t, store, objects, result.ID, chat, user)
|
||||
if status := postDirect(t, result.Upload, uploadPNG(t), nil); status != 204 {
|
||||
t.Fatal("HTTP-created form failed")
|
||||
}
|
||||
attachmentCall(t, f, router, "POST", path+"/"+result.ID+"/complete", user, "", nil, 201)
|
||||
}
|
||||
|
||||
func TestDirectUploadValidationContents(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
png := uploadPNG(t)
|
||||
huge := append([]byte(nil), png...)
|
||||
binary.BigEndian.PutUint32(huge[16:20], 10001)
|
||||
binary.BigEndian.PutUint32(huge[20:24], 4000)
|
||||
binary.BigEndian.PutUint32(huge[29:33], crc32.ChecksumIEEE(huge[12:29]))
|
||||
for _, sample := range []struct {
|
||||
data []byte
|
||||
mime, code string
|
||||
status int
|
||||
}{
|
||||
{png[:40], "image/png", "unsupported_media_type", 415},
|
||||
{png, "image/jpeg", "unsupported_media_type", 415},
|
||||
{huge, "image/png", "payload_too_large", 413},
|
||||
} {
|
||||
result, service := beginDirect(t, f, store, objects, user, chat, sample.data, sample.mime)
|
||||
if postDirect(t, result.Upload, sample.data, nil) != 204 {
|
||||
t.Fatal("S3 refused matching declared policy")
|
||||
}
|
||||
_, err := service.CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
requireDirectError(t, err, sample.status, sample.code)
|
||||
_, err = service.CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
requireDirectError(t, err, sample.status, sample.code)
|
||||
}
|
||||
result, service := beginDirect(t, f, store, objects, user, chat, png, "image/png")
|
||||
wrong := append([]byte(nil), png...)
|
||||
wrong[len(wrong)-1] ^= 1
|
||||
if postDirect(t, result.Upload, wrong, nil) != 204 {
|
||||
t.Fatal("form POST failed")
|
||||
}
|
||||
_, err := service.CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
requireDirectError(t, err, 400, "upload_checksum_mismatch")
|
||||
result, service = beginDirect(t, f, store, objects, user, chat, png, "image/png")
|
||||
if postDirect(t, result.Upload, append(png, 1), nil) < 400 {
|
||||
t.Fatal("S3 did not enforce exact policy size")
|
||||
}
|
||||
u, _ := store.GetDirect(context.Background(), result.ID, chat, user)
|
||||
// Simulate a trusted-side fault that bypassed policy; complete must still check.
|
||||
if err = objects.Put(context.Background(), u.StagingKey, bytes.NewReader(png[:40]), 40, "image/png"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = service.CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
requireDirectError(t, err, 400, "upload_size_mismatch")
|
||||
}
|
||||
|
||||
func TestDirectUploadAccess(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
png := uploadPNG(t)
|
||||
result, service := beginDirect(t, f, store, objects, user, chat, png, "image/png")
|
||||
eve, _ := createTestUser(t, f.users, "eve", "Eve")
|
||||
bob, _ := createTestUser(t, f.users, "bob", "Bob")
|
||||
if _, err := f.db.Exec(context.Background(), `INSERT INTO chat_members(chat_id,user_id) VALUES($1,$2)`, chat, bob.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
other := createTestChat(t, f.chats, "other", []string{user})
|
||||
router := attachmentRouter(f, objects)
|
||||
path := "/api/v1/chats/" + chat + "/attachment-uploads"
|
||||
for _, actor := range []string{eve.ID, bob.ID} {
|
||||
attachmentCall(t, f, router, "POST", path+"/"+result.ID+"/complete", actor, "", nil, 404)
|
||||
}
|
||||
attachmentCall(t, f, router, "POST", path+"/"+result.ID+"/complete", "", "", nil, 401)
|
||||
attachmentCall(t, f, router, "POST", "/api/v1/chats/"+other.ID+"/attachment-uploads/"+result.ID+"/complete", user, "", nil, 404)
|
||||
body, _ := json.Marshal(directInput(png, "image/png"))
|
||||
attachmentCall(t, f, router, "POST", path, eve.ID, "application/json", body, 404)
|
||||
u, _ := store.GetDirect(context.Background(), result.ID, chat, user)
|
||||
attachmentCall(t, f, router, "POST", "/api/v1/chats/"+chat+"/messages", user, "application/json", []byte(`{"text":"","attachment_ids":["`+u.AttachmentID+`"]}`), 404)
|
||||
_, err := service.CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
requireDirectError(t, err, 409, "upload_not_uploaded")
|
||||
for _, change := range []func(map[string]string){
|
||||
func(fields map[string]string) { fields["key"] = "originals/" + uuid.NewString() },
|
||||
func(fields map[string]string) { fields["Content-Type"] = "image/jpeg" },
|
||||
func(fields map[string]string) { fields["x-amz-signature"] = strings.Repeat("0", 64) },
|
||||
} {
|
||||
if postDirect(t, result.Upload, png, change) < 400 {
|
||||
t.Fatal("S3 accepted tampered form")
|
||||
}
|
||||
}
|
||||
expired, err := objects.PresignDirect(context.Background(), u.StagingKey, "image/png", int64(len(png)), time.Now().Add(-time.Minute))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if postDirect(t, expired, png, nil) < 400 {
|
||||
t.Fatal("S3 accepted expired form")
|
||||
}
|
||||
if _, err = f.db.Exec(context.Background(), `UPDATE direct_uploads SET complete_before=clock_timestamp()-interval '1 second' WHERE id=$1`, result.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = service.CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
requireDirectError(t, err, 409, "upload_expired")
|
||||
}
|
||||
|
||||
type directFault struct {
|
||||
*repositories.ObjectStore
|
||||
afterCopy func() error
|
||||
failGet bool
|
||||
}
|
||||
|
||||
func (f directFault) Copy(ctx context.Context, source, dest, etag string) error {
|
||||
if err := f.ObjectStore.Copy(ctx, source, dest, etag); err != nil {
|
||||
return err
|
||||
}
|
||||
if f.afterCopy != nil {
|
||||
return f.afterCopy()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (f directFault) Get(ctx context.Context, key string) (io.ReadCloser, error) {
|
||||
if f.failGet {
|
||||
return nil, errors.New("injected GET failure")
|
||||
}
|
||||
return f.ObjectStore.Get(ctx, key)
|
||||
}
|
||||
|
||||
func TestDirectUploadRecoveryConcurrentComplete(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
data := uploadPNG(t)
|
||||
result, _ := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
if postDirect(t, result.Upload, data, nil) != 204 {
|
||||
t.Fatal("POST failed")
|
||||
}
|
||||
var wg sync.WaitGroup
|
||||
ids := make(chan string, 2)
|
||||
errs := make(chan error, 2)
|
||||
for i := 0; i < 2; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
a, err := services.NewAttachmentService(store, objects).CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
ids <- a.ID
|
||||
errs <- err
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
close(ids)
|
||||
close(errs)
|
||||
for err := range errs {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
first := <-ids
|
||||
if second := <-ids; first == "" || first != second {
|
||||
t.Fatal("concurrent complete produced different attachments")
|
||||
}
|
||||
var n int
|
||||
if err := f.db.QueryRow(context.Background(), `SELECT count(*) FROM attachments`).Scan(&n); err != nil || n != 1 {
|
||||
t.Fatal("unexpected metadata count", n, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirectUploadRecoveryFailures(t *testing.T) {
|
||||
for _, name := range []string{"copy-result-loss", "get-failure", "metadata-rejection", "database-disconnect"} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
data := uploadPNG(t)
|
||||
result, normal := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
if postDirect(t, result.Upload, data, nil) != 204 {
|
||||
t.Fatal("POST failed")
|
||||
}
|
||||
fault := directFault{ObjectStore: objects}
|
||||
switch name {
|
||||
case "copy-result-loss":
|
||||
fault.afterCopy = func() error { return errors.New("COPY acknowledgement lost") }
|
||||
case "get-failure":
|
||||
fault.failGet = true
|
||||
case "metadata-rejection":
|
||||
if _, err := f.db.Exec(context.Background(), `ALTER TABLE attachments ADD CONSTRAINT reject_direct CHECK (filename <> 'direct.png')`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
case "database-disconnect":
|
||||
fault.afterCopy = func() error {
|
||||
_, err := f.db.Exec(context.Background(), `SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE application_name=$1 AND state='idle in transaction' AND pid<>pg_backend_pid()`, f.db.Config().ConnConfig.RuntimeParams["application_name"])
|
||||
return err
|
||||
}
|
||||
}
|
||||
_, err := services.NewAttachmentService(store, fault).CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
if err == nil {
|
||||
t.Fatal("expected injected failure")
|
||||
}
|
||||
var count int
|
||||
if err = f.db.QueryRow(context.Background(), `SELECT count(*) FROM upload_attempts`).Scan(&count); err != nil || count != 1 {
|
||||
t.Fatal("durable journal missing", err)
|
||||
}
|
||||
if err = f.db.QueryRow(context.Background(), `SELECT count(*) FROM attachments`).Scan(&count); err != nil || count != 0 {
|
||||
t.Fatal("published incomplete attachment", err)
|
||||
}
|
||||
if name == "metadata-rejection" {
|
||||
if _, err = f.db.Exec(context.Background(), `ALTER TABLE attachments DROP CONSTRAINT reject_direct`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if _, err = normal.CompleteDirect(context.Background(), user, chat, result.ID); err != nil {
|
||||
t.Fatal("retry failed", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirectUploadRecoveryProcessExit(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
data := uploadPNG(t)
|
||||
result, _ := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
if postDirect(t, result.Upload, data, nil) != 204 {
|
||||
t.Fatal("POST failed")
|
||||
}
|
||||
child := exec.Command(os.Args[0], "-test.run=^TestDirectUploadCrashHelper$")
|
||||
child.Env = append(os.Environ(), "DIRECT_CRASH_HELPER=1", "UPLOAD_TEST_DATABASE="+f.db.Config().ConnString(), "UPLOAD_TEST_SCHEMA="+f.db.Config().ConnConfig.RuntimeParams["search_path"], "UPLOAD_TEST_USER="+user, "UPLOAD_TEST_CHAT="+chat, "DIRECT_UPLOAD_ID="+result.ID)
|
||||
output, err := child.CombinedOutput()
|
||||
var exit *exec.ExitError
|
||||
if !errors.As(err, &exit) || exit.ExitCode() != 23 {
|
||||
t.Fatalf("helper did not crash after COPY: %v %s", err, output)
|
||||
}
|
||||
ageAndClean(t, f, store, objects)
|
||||
}
|
||||
|
||||
func TestDirectUploadCrashHelper(t *testing.T) {
|
||||
if os.Getenv("DIRECT_CRASH_HELPER") != "1" {
|
||||
return
|
||||
}
|
||||
cfg, err := pgxpool.ParseConfig(os.Getenv("UPLOAD_TEST_DATABASE"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg.ConnConfig.RuntimeParams["search_path"] = os.Getenv("UPLOAD_TEST_SCHEMA")
|
||||
cfg.ConnConfig.RuntimeParams["application_name"] = os.Getenv("UPLOAD_TEST_SCHEMA")
|
||||
db, err := pgxpool.NewWithConfig(context.Background(), cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
objects := uploadObjects(t)
|
||||
fault := directFault{ObjectStore: objects, afterCopy: func() error { os.Exit(23); return nil }}
|
||||
_, err = services.NewAttachmentService(repositories.NewAttachmentStore(db), fault).CompleteDirect(context.Background(), os.Getenv("UPLOAD_TEST_USER"), os.Getenv("UPLOAD_TEST_CHAT"), os.Getenv("DIRECT_UPLOAD_ID"))
|
||||
t.Fatalf("helper failed before exit: %v", err)
|
||||
}
|
||||
|
||||
func TestDirectUploadRecoverySharedSlotAndCancellation(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
data := uploadPNG(t)
|
||||
result, _ := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
if postDirect(t, result.Upload, data, nil) != 204 {
|
||||
t.Fatal("POST failed")
|
||||
}
|
||||
entered, release := make(chan struct{}), make(chan struct{})
|
||||
fault := directFault{ObjectStore: objects, afterCopy: func() error {
|
||||
close(entered)
|
||||
<-release
|
||||
return errors.New("injected operation cancellation")
|
||||
}}
|
||||
service := services.NewAttachmentService(store, fault)
|
||||
done := make(chan error, 1)
|
||||
go func() { _, err := service.CompleteDirect(context.Background(), user, chat, result.ID); done <- err }()
|
||||
select {
|
||||
case <-entered:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("COPY not reached")
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
_, proxyErr := service.Upload(ctx, user, chat, "waiting.png", "image/png", bytes.NewReader(data))
|
||||
_, directErr := service.CompleteDirect(ctx, user, chat, result.ID)
|
||||
close(release)
|
||||
if err := <-done; err == nil {
|
||||
t.Fatal("injected failure ignored")
|
||||
}
|
||||
if !errors.Is(proxyErr, context.Canceled) || !errors.Is(directErr, context.Canceled) {
|
||||
t.Fatalf("waiting requests ignored cancellation: proxy=%v direct=%v", proxyErr, directErr)
|
||||
}
|
||||
ageAndClean(t, f, store, objects)
|
||||
}
|
||||
@@ -100,6 +100,13 @@ func newTestFixture(t *testing.T) *testFixture {
|
||||
if _, err = db.Exec(ctx, string(ddl)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ddl, err = os.ReadFile("../repositories/migrations/000006_direct_uploads.up.sql")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = db.Exec(ctx, string(ddl)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = redisClient.Close() })
|
||||
if err := redisClient.Ping(ctx).Err(); err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
Reference in New Issue
Block a user