Добавьте S3-вложения с дедупликацией и безопасной сверкой хранилищ
This commit is contained in:
@@ -15,6 +15,9 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Allow a 30s binary request, its independent 3s cleanup, and 5s margin.
|
||||
const defaultShutdownTimeout = 38 * time.Second
|
||||
|
||||
type Logging struct {
|
||||
Env string
|
||||
Level slog.Level
|
||||
@@ -43,6 +46,7 @@ type Config struct {
|
||||
IdempotencyTTL time.Duration
|
||||
DBMaxConns int32
|
||||
DebugAddr string
|
||||
S3 S3
|
||||
}
|
||||
|
||||
type Migrate struct {
|
||||
@@ -58,7 +62,7 @@ func Load(lookup Lookup) (Config, error) {
|
||||
cfg := Config{
|
||||
Host: "0.0.0.0",
|
||||
Port: 8080,
|
||||
ShutdownTimeout: 8 * time.Second,
|
||||
ShutdownTimeout: defaultShutdownTimeout,
|
||||
MaxBodyBytes: 64 * 1024,
|
||||
SessionTTL: time.Hour,
|
||||
DrainDelay: 2 * time.Second,
|
||||
@@ -136,6 +140,11 @@ func Load(lookup Lookup) (Config, error) {
|
||||
if cfg.SessionStoreURL, err = secret(lookup, "SESSION_STORE_URL_FILE"); err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
if _, ok := lookup("S3_ENDPOINT"); ok {
|
||||
if cfg.S3, err = LoadS3(lookup); err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -56,7 +56,7 @@ func TestDefaults(t *testing.T) {
|
||||
if cfg.Env != "lab" || cfg.Level != slog.LevelInfo {
|
||||
t.Fatalf("cfg = %+v", cfg)
|
||||
}
|
||||
if cfg.ShutdownTimeout != 8*time.Second || cfg.MaxBodyBytes != 65536 {
|
||||
if cfg.ShutdownTimeout != 38*time.Second || cfg.MaxBodyBytes != 65536 {
|
||||
t.Fatalf("cfg = %+v", cfg)
|
||||
}
|
||||
if cfg.DatabaseURL != databaseURL || cfg.SessionStoreURL != sessionStoreURL {
|
||||
@@ -70,6 +70,19 @@ func TestDefaults(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultShutdownTimeoutCoversBinaryLifecycle(t *testing.T) {
|
||||
cfg, err := config.Load(env(required(t, nil)))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Binary requests can use 30s, followed by 3s independent cleanup. Keep
|
||||
// another 5s for shutdown scheduling and closing the remaining resources.
|
||||
budget := 30*time.Second + 3*time.Second + 5*time.Second
|
||||
if cfg.ShutdownTimeout < budget {
|
||||
t.Fatalf("default shutdown budget %v cannot cover binary request and cleanup budget %v", cfg.ShutdownTimeout, budget)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSecretFilesAreRequiredAndNeverEchoed(t *testing.T) {
|
||||
for _, key := range []string{"DATABASE_URL_FILE", "SESSION_STORE_URL_FILE"} {
|
||||
cases := map[string]func(map[string]string){
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type S3 struct {
|
||||
Endpoint, Region, Bucket string
|
||||
AccessKey, SecretKey string
|
||||
MaxBytes, MaxPixels int64
|
||||
}
|
||||
|
||||
func LoadS3(lookup Lookup) (S3, error) {
|
||||
cfg := S3{Endpoint: "http://s3:8333", Region: "us-east-1", Bucket: "messenger-attachments", MaxBytes: 10485760, MaxPixels: 40000000}
|
||||
for key, dest := range map[string]*string{"S3_ENDPOINT": &cfg.Endpoint, "S3_REGION": &cfg.Region, "S3_BUCKET": &cfg.Bucket} {
|
||||
if v, ok := lookup(key); ok {
|
||||
*dest = v
|
||||
}
|
||||
}
|
||||
var err error
|
||||
if cfg.AccessKey, err = secret(lookup, "S3_ACCESS_KEY_ID_FILE"); err != nil {
|
||||
return S3{}, err
|
||||
}
|
||||
if cfg.SecretKey, err = secret(lookup, "S3_SECRET_ACCESS_KEY_FILE"); err != nil {
|
||||
return S3{}, err
|
||||
}
|
||||
for _, p := range []struct {
|
||||
key string
|
||||
dest *int64
|
||||
max int64
|
||||
}{{"MAX_UPLOAD_BYTES", &cfg.MaxBytes, 10485760}, {"MAX_IMAGE_PIXELS", &cfg.MaxPixels, 40000000}} {
|
||||
if v, ok := lookup(p.key); ok {
|
||||
n, e := strconv.ParseInt(v, 10, 64)
|
||||
if e != nil || n < 1 || n > p.max {
|
||||
return S3{}, fmt.Errorf("%s must be between 1 and %d", p.key, p.max)
|
||||
}
|
||||
*p.dest = n
|
||||
}
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
// ReconcileS3 selects the separate listing identity used by background scans.
|
||||
func ReconcileS3(lookup Lookup) Lookup {
|
||||
return func(key string) (string, bool) {
|
||||
if key == "S3_ACCESS_KEY_ID_FILE" {
|
||||
return lookup("S3_RECONCILE_ACCESS_KEY_ID_FILE")
|
||||
}
|
||||
if key == "S3_SECRET_ACCESS_KEY_FILE" {
|
||||
return lookup("S3_RECONCILE_SECRET_ACCESS_KEY_FILE")
|
||||
}
|
||||
return lookup(key)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package config_test
|
||||
|
||||
import (
|
||||
"messenger/internal/platform/config"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestS3LimitsAndSecretFiles(t *testing.T) {
|
||||
values := map[string]string{"S3_ACCESS_KEY_ID_FILE": secretFile(t, "access\n"), "S3_SECRET_ACCESS_KEY_FILE": secretFile(t, "private\n")}
|
||||
cfg, err := config.LoadS3(env(values))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg.AccessKey != "access" || cfg.SecretKey != "private" || cfg.MaxBytes != 10485760 || cfg.MaxPixels != 40000000 {
|
||||
t.Fatal("S3 defaults or secret trimming broken")
|
||||
}
|
||||
for _, tc := range []struct{ k, v string }{{"MAX_UPLOAD_BYTES", "0"}, {"MAX_UPLOAD_BYTES", "10485761"}, {"MAX_IMAGE_PIXELS", "40000001"}, {"MAX_IMAGE_PIXELS", "x"}} {
|
||||
v := map[string]string{}
|
||||
for k, x := range values {
|
||||
v[k] = x
|
||||
}
|
||||
v[tc.k] = tc.v
|
||||
if _, err := config.LoadS3(env(v)); err == nil {
|
||||
t.Fatalf("accepted %s=%s", tc.k, tc.v)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user