feat: restricted S3 credentials, orphan reconciler, upload limits (grade 4)

This commit is contained in:
2026-11-20 04:30:58 +03:00
parent 89e9e7b4d4
commit 1c3011bf28
42 changed files with 1483 additions and 130 deletions
+54 -34
View File
@@ -10,19 +10,21 @@ import (
)
type Config struct {
Host string
Port string
Env string
SessionTTL time.Duration
DatabaseURL string
CacheURL string
S3Endpoint string
S3Region string
S3Bucket string
S3AccessKeyID string
S3SecretAccessKey string
MaxUploadBytes int64
MaxImagePixels int64
Host string
Port string
Env string
SessionTTL time.Duration
DatabaseURL string
CacheURL string
S3Endpoint string
S3Region string
S3Bucket string
S3AccessKeyID string
S3SecretAccessKey string
MaxUploadBytes int64
MaxImagePixels int64
MaxConcurrentUploads int
MaxConcurrentDecodes int
}
func Load() (Config, error) {
@@ -30,27 +32,35 @@ func Load() (Config, error) {
if err != nil || ttl <= 0 {
return Config{}, fmt.Errorf("SESSION_TTL_SECONDS must be a positive integer")
}
maxUploadBytes, err := positiveInt("MAX_UPLOAD_BYTES", "10485760")
maxUploadBytes, err := positiveInt64("MAX_UPLOAD_BYTES", "10485760")
if err != nil {
return Config{}, err
}
maxImagePixels, err := positiveInt("MAX_IMAGE_PIXELS", "40000000")
maxImagePixels, err := positiveInt64("MAX_IMAGE_PIXELS", "40000000")
if err != nil {
return Config{}, err
}
databaseURL, err := readSecret("DATABASE_URL_FILE", "/run/secrets/database_url")
maxConcurrentUploads, err := positiveInt("MAX_CONCURRENT_UPLOADS", "8")
if err != nil {
return Config{}, err
}
cacheURL, err := readSecret("CACHE_URL_FILE", "/run/secrets/cache_url")
maxConcurrentDecodes, err := positiveInt("MAX_CONCURRENT_DECODES", "1")
if err != nil {
return Config{}, err
}
accessKeyID, err := readSecret("S3_ACCESS_KEY_ID_FILE", "/run/secrets/s3_access_key_id")
databaseURL, err := ReadSecret("DATABASE_URL_FILE", "/run/secrets/database_url")
if err != nil {
return Config{}, err
}
secretAccessKey, err := readSecret("S3_SECRET_ACCESS_KEY_FILE", "/run/secrets/s3_secret_access_key")
cacheURL, err := ReadSecret("CACHE_URL_FILE", "/run/secrets/cache_url")
if err != nil {
return Config{}, err
}
accessKeyID, err := ReadSecret("S3_ACCESS_KEY_ID_FILE", "/run/secrets/s3_access_key_id")
if err != nil {
return Config{}, err
}
secretAccessKey, err := ReadSecret("S3_SECRET_ACCESS_KEY_FILE", "/run/secrets/s3_secret_access_key")
if err != nil {
return Config{}, err
}
@@ -66,25 +76,27 @@ func Load() (Config, error) {
return Config{}, fmt.Errorf("S3_ENDPOINT must not be empty")
}
return Config{
Host: getenv("APP_HOST", "0.0.0.0"),
Port: getenv("APP_PORT", "8080"),
Env: getenv("APP_ENV", "lab"),
SessionTTL: time.Duration(ttl) * time.Second,
DatabaseURL: databaseURL,
CacheURL: cacheURL,
S3Endpoint: endpoint,
S3Region: getenv("S3_REGION", "us-east-1"),
S3Bucket: bucket,
S3AccessKeyID: accessKeyID,
S3SecretAccessKey: secretAccessKey,
MaxUploadBytes: maxUploadBytes,
MaxImagePixels: maxImagePixels,
Host: getenv("APP_HOST", "0.0.0.0"),
Port: getenv("APP_PORT", "8080"),
Env: getenv("APP_ENV", "lab"),
SessionTTL: time.Duration(ttl) * time.Second,
DatabaseURL: databaseURL,
CacheURL: cacheURL,
S3Endpoint: endpoint,
S3Region: getenv("S3_REGION", "us-east-1"),
S3Bucket: bucket,
S3AccessKeyID: accessKeyID,
S3SecretAccessKey: secretAccessKey,
MaxUploadBytes: maxUploadBytes,
MaxImagePixels: maxImagePixels,
MaxConcurrentUploads: maxConcurrentUploads,
MaxConcurrentDecodes: maxConcurrentDecodes,
}, nil
}
func (c Config) Addr() string { return net.JoinHostPort(c.Host, c.Port) }
func positiveInt(key, def string) (int64, error) {
func positiveInt64(key, def string) (int64, error) {
value, err := strconv.ParseInt(getenv(key, def), 10, 64)
if err != nil || value <= 0 {
return 0, fmt.Errorf("%s must be a positive integer", key)
@@ -92,7 +104,15 @@ func positiveInt(key, def string) (int64, error) {
return value, nil
}
func readSecret(key, def string) (string, error) {
func positiveInt(key, def string) (int, error) {
value, err := strconv.Atoi(getenv(key, def))
if err != nil || value <= 0 {
return 0, fmt.Errorf("%s must be a positive integer", key)
}
return value, nil
}
func ReadSecret(key, def string) (string, error) {
path := getenv(key, def)
b, err := os.ReadFile(path)
if err != nil {