Store public uploads in a Garage S3 bucket, with garage-webui at /garage/
Opt-in and built so Garage can never take the site down: - the backend writes to the bucket only once S3_ENDPOINT is set, and falls back to the uploads volume (with a log line) if a write fails; - nginx serves /uploads/ from the volume first and Garage's anonymous website endpoint second, so every existing URL keeps working and nginx boots with no garage container; - nothing depends_on garage and no variable is required, so a missing secret cannot fail a deploy. Private attachments stay on disk, since nginx cannot gate a bucket. garage/bootstrap.sh sets up the layout, bucket, key and website access; the runbook is "Garage" in DEPLOYMENT.md. Also lifts the body limit on the internal auth subrequest, which 413'd large uploads to gated locations, and reserves the garage and dozzle page slugs. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Covered here: which store NewUploadStore picks, the disk store, and the
|
||||
// fallback path — including against a real S3 client talking to a server
|
||||
// that refuses the write, since "Garage said no" is the case the fallback
|
||||
// exists for.
|
||||
//
|
||||
// Not covered: a successful PutObject against Garage. That needs the garage
|
||||
// container; it is checked by hand in the stack (see DEPLOYMENT.md, "Garage").
|
||||
|
||||
func TestNewUploadStore(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
|
||||
if _, ok := NewUploadStore(S3Config{}, dir).(DiskFileStore); !ok {
|
||||
t.Error("no S3 endpoint should mean the disk store")
|
||||
}
|
||||
if _, ok := NewUploadStore(S3Config{Endpoint: "http://garage:3900", Bucket: "uploads"}, dir).(DiskFileStore); !ok {
|
||||
t.Error("an endpoint without credentials should fall back to the disk store")
|
||||
}
|
||||
full := S3Config{Endpoint: "http://garage:3900", Region: "garage", Bucket: "uploads", AccessKeyID: "GK0", SecretAccessKey: "s"}
|
||||
if _, ok := NewUploadStore(full, dir).(FallbackFileStore); !ok {
|
||||
t.Error("a full S3 config should mean the bucket with a disk fallback")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiskFileStorePut(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
body := []byte("hello")
|
||||
if err := (DiskFileStore{Dir: dir}).Put(context.Background(), "a.txt", "text/plain", bytes.NewReader(body), int64(len(body))); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := os.ReadFile(filepath.Join(dir, "a.txt"))
|
||||
if err != nil || !bytes.Equal(got, body) {
|
||||
t.Fatalf("read back %q, %v; want %q", got, err, body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFallbackWhenBucketRefuses(t *testing.T) {
|
||||
// 403 rather than 500: the SDK retries 5xx with backoff, which would only
|
||||
// make the test slow. Either way PutObject returns an error.
|
||||
var hits int
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
hits++
|
||||
// Drain the body so a test failure can't be a half-read request.
|
||||
_, _ = io.Copy(io.Discard, r.Body)
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
dir := t.TempDir()
|
||||
store := NewUploadStore(S3Config{Endpoint: srv.URL, Region: "garage", Bucket: "uploads", AccessKeyID: "GK0", SecretAccessKey: "s"}, dir)
|
||||
|
||||
body := bytes.Repeat([]byte("x"), 4096)
|
||||
if err := store.Put(context.Background(), "f.bin", "application/octet-stream", bytes.NewReader(body), int64(len(body))); err != nil {
|
||||
t.Fatalf("Put should succeed through the fallback, got %v", err)
|
||||
}
|
||||
if hits == 0 {
|
||||
t.Fatal("the bucket was never tried")
|
||||
}
|
||||
// The whole file, not just what was left after the S3 attempt read it:
|
||||
// this is what the rewind in FallbackFileStore is for.
|
||||
got, err := os.ReadFile(filepath.Join(dir, "f.bin"))
|
||||
if err != nil || !bytes.Equal(got, body) {
|
||||
t.Fatalf("fallback file has %d bytes (%v), want %d", len(got), err, len(body))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user