- OpenBucket validates public_url (absolute http/https, host, no user info, query or fragment; the error names the key) and records it next to public_path_prefix - a set public_url logs one start-up line: the bucket behind it must hold only public files, since it bypasses the is_public check - attach.PublicURLBase; PublicURL, File.URL and thumbnail URLs use the base when set and keep the public_path_prefix proxy URLs otherwise - TestPublicURLSmoke; storage Public URL base section, attachments, configuration and lagoon README
107 lines
3.5 KiB
Go
107 lines
3.5 KiB
Go
package attach
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.golem15.com/golem15/summercms/modules/compass"
|
|
)
|
|
|
|
// openTestConfig writes storage.yaml into a temp config dir and opens it.
|
|
func openTestConfig(t *testing.T, storageYAML string) *compass.Config {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
if err := os.WriteFile(filepath.Join(dir, "storage.yaml"), []byte(storageYAML), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cfg, err := compass.Open(compass.Options{
|
|
Dir: dir,
|
|
Env: "development",
|
|
Environ: []string{"SUMMER_ENV=development"},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return cfg
|
|
}
|
|
|
|
// TestOpenBucketS3Smoke runs in both tag modes: without summercms_s3 an
|
|
// s3:// bucket_url fails boot naming the tag, with it the bucket opens
|
|
// (s3blob connects lazily, so no network is needed).
|
|
func TestOpenBucketS3Smoke(t *testing.T) {
|
|
t.Setenv("AWS_ACCESS_KEY_ID", "smoke-access-key")
|
|
t.Setenv("AWS_SECRET_ACCESS_KEY", "smoke-secret-key")
|
|
t.Setenv("AWS_REGION", "us-east-1")
|
|
t.Setenv("AWS_EC2_METADATA_DISABLED", "true")
|
|
t.Cleanup(func() { setPublicPathPrefix(defaultPublicPathPrefix) })
|
|
|
|
cfg := openTestConfig(t, "uploads:\n bucket_url: \"s3://smoke-bucket?region=us-east-1\"\n")
|
|
bucket, err := OpenBucket(t.Context(), cfg)
|
|
if !s3Enabled {
|
|
if err == nil {
|
|
_ = bucket.Close()
|
|
t.Fatal("OpenBucket opened s3:// without the summercms_s3 tag")
|
|
}
|
|
for _, want := range []string{S3BuildTag, "build.tags", "-tags " + S3BuildTag} {
|
|
if !strings.Contains(err.Error(), want) {
|
|
t.Fatalf("error %q does not name %q", err, want)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("OpenBucket with the summercms_s3 tag: %v", err)
|
|
}
|
|
if err := bucket.Close(); err != nil {
|
|
t.Fatalf("close: %v", err)
|
|
}
|
|
}
|
|
|
|
// TestPublicURLSmoke checks storage.uploads.public_url (D-12): set, URLs
|
|
// are the base plus the partition path; unset, they keep the proxy prefix;
|
|
// malformed, OpenBucket fails naming the key.
|
|
func TestPublicURLSmoke(t *testing.T) {
|
|
t.Cleanup(func() {
|
|
setPublicPathPrefix(defaultPublicPathPrefix)
|
|
setPublicURLBase("")
|
|
})
|
|
key := BlobKey("651a2b3c4d5e61234567.png")
|
|
|
|
cfg := openTestConfig(t, "uploads:\n bucket_url: \"mem://\"\n public_url: \"https://cdn.example.com/uploads/\"\n")
|
|
bucket, err := OpenBucket(t.Context(), cfg)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_ = bucket.Close()
|
|
if got, want := PublicURL(key), "https://cdn.example.com/uploads/"+key; got != want {
|
|
t.Fatalf("PublicURL with public_url = %q, want %q", got, want)
|
|
}
|
|
if got, want := (&File{DiskName: "651a2b3c4d5e61234567.png"}).URL(), "https://cdn.example.com/uploads/651/a2b/3c4/651a2b3c4d5e61234567.png"; got != want {
|
|
t.Fatalf("File.URL with public_url = %q, want %q", got, want)
|
|
}
|
|
|
|
cfg = openTestConfig(t, "uploads:\n bucket_url: \"mem://\"\n")
|
|
bucket, err = OpenBucket(t.Context(), cfg)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_ = bucket.Close()
|
|
if PublicURLBase() != "" || !strings.HasPrefix(PublicURL(key), "/storage/uploads/") {
|
|
t.Fatalf("without public_url: base %q, URL %q", PublicURLBase(), PublicURL(key))
|
|
}
|
|
|
|
for _, bad := range []string{"ftp://x", "https://u:p@cdn.example.com", "/storage/uploads", "https://cdn.example.com/?v=1"} {
|
|
cfg = openTestConfig(t, "uploads:\n bucket_url: \"mem://\"\n public_url: \""+bad+"\"\n")
|
|
bucket, err = OpenBucket(t.Context(), cfg)
|
|
if err == nil {
|
|
_ = bucket.Close()
|
|
t.Fatalf("public_url %q accepted", bad)
|
|
}
|
|
if !strings.Contains(err.Error(), "storage.uploads.public_url") || strings.Contains(err.Error(), "u:p") {
|
|
t.Fatalf("public_url %q: error %q", bad, err)
|
|
}
|
|
}
|
|
}
|