diff --git a/.env.example b/.env.example index 8cbc810..242ebdf 100644 --- a/.env.example +++ b/.env.example @@ -23,5 +23,7 @@ S3_REGION=us-east-1 S3_BUCKET=messenger-attachments S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_access_key_id S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_secret_access_key +S3_BOOTSTRAP_ACCESS_KEY_ID_FILE=/run/secrets/s3_bootstrap_access_key_id +S3_BOOTSTRAP_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_bootstrap_secret_access_key MAX_UPLOAD_BYTES=10485760 MAX_IMAGE_PIXELS=40000000 diff --git a/Dockerfile.s3config b/Dockerfile.s3config new file mode 100644 index 0000000..153468b --- /dev/null +++ b/Dockerfile.s3config @@ -0,0 +1,13 @@ +FROM golang:1.27.1-alpine3.24 AS build +ENV CGO_ENABLED=0 +WORKDIR /src + +COPY go.mod go.sum ./ +RUN go mod download + +COPY . . +RUN go build -o /out/s3config ./cmd/s3config + +FROM alpine:3.24 +COPY --from=build /out/s3config /usr/local/bin/s3config +ENTRYPOINT ["/usr/local/bin/s3config"] diff --git a/cmd/s3config/main.go b/cmd/s3config/main.go new file mode 100644 index 0000000..2fd917e --- /dev/null +++ b/cmd/s3config/main.go @@ -0,0 +1,15 @@ +package main + +import ( + "fmt" + "os" + + "mtusi2026-messenger/internal/s3" +) + +func main() { + if err := s3.WriteConfig("/config/s3.json"); err != nil { + fmt.Fprintln(os.Stderr, "s3config:", err) + os.Exit(1) + } +} diff --git a/compose.yaml b/compose.yaml index ebd5e5f..fe24811 100644 --- a/compose.yaml +++ b/compose.yaml @@ -9,6 +9,8 @@ services: secrets: - database_url - cache_url + - s3_access_key_id + - s3_secret_access_key deploy: replicas: ${API_REPLICAS:-2} resources: @@ -18,6 +20,8 @@ services: depends_on: migrate: condition: service_completed_successfully + s3: + condition: service_healthy restart: unless-stopped postgres: @@ -75,6 +79,62 @@ services: memory: 128M restart: unless-stopped + s3-config-init: + build: + context: . + dockerfile: Dockerfile.s3config + image: mtusi-messenger-s3config:lab4 + entrypoint: ["/usr/local/bin/s3config"] + user: "0:0" + env_file: + - .env + secrets: + - s3_access_key_id + - s3_secret_access_key + - s3_bootstrap_access_key_id + - s3_bootstrap_secret_access_key + volumes: + - s3config:/config + deploy: + resources: + limits: + cpus: "0.25" + memory: 64M + restart: "no" + + s3: + image: chrislusf/seaweedfs:4.51 + command: + - "server" + - "-dir=/data" + - "-s3" + - "-s3.port=9000" + - "-s3.iam=false" + - "-filer.disableHttp" + - "-s3.port.iceberg=0" + - "-s3.port.lance=0" + - "-s3.config=/config/s3.json" + depends_on: + s3-config-init: + condition: service_completed_successfully + volumes: + - s3config:/config:ro + - seaweedfs:/data + healthcheck: + test: + - CMD-SHELL + - wget -qO- http://127.0.0.1:9000/healthz >/dev/null && wget -qO- http://127.0.0.1:9333/dir/status | grep -q '"Url":' + interval: 10s + timeout: 5s + retries: 5 + start_period: 20s + deploy: + resources: + limits: + cpus: "0.5" + memory: 256M + restart: unless-stopped + load_balancer: image: nginx:1.31.7-alpine3.24 ports: @@ -98,6 +158,16 @@ secrets: file: ./secrets/database_url.txt cache_url: file: ./secrets/cache_url.txt + s3_access_key_id: + file: ./secrets/s3_access_key_id.txt + s3_secret_access_key: + file: ./secrets/s3_secret_access_key.txt + s3_bootstrap_access_key_id: + file: ./secrets/s3_bootstrap_access_key_id.txt + s3_bootstrap_secret_access_key: + file: ./secrets/s3_bootstrap_secret_access_key.txt volumes: pgdata: + seaweedfs: + s3config: diff --git a/internal/s3/config.go b/internal/s3/config.go new file mode 100644 index 0000000..4a7b83c --- /dev/null +++ b/internal/s3/config.go @@ -0,0 +1,148 @@ +package s3 + +import ( + "encoding/json" + "fmt" + "os" + "path/filepath" + "strings" +) + +const ( + seaweedUID = 1000 + seaweedGID = 1000 + + defaultConfigPath = "/config/s3.json" +) + +type Credential struct { + AccessKey string `json:"accessKey"` + SecretKey string `json:"secretKey"` +} + +type Identity struct { + Name string `json:"name"` + Credentials []Credential `json:"credentials"` + Actions []string `json:"actions"` +} + +type Config struct { + Identities []Identity `json:"identities"` +} + +func WriteConfig(output string) error { + if output == "" { + output = defaultConfigPath + } + + apiAccessKey, err := readSecret("S3_ACCESS_KEY_ID_FILE", "/run/secrets/s3_access_key_id") + if err != nil { + return err + } + + apiSecretKey, err := readSecret("S3_SECRET_ACCESS_KEY_FILE", "/run/secrets/s3_secret_access_key") + if err != nil { + return err + } + + bootstrapAccessKey, err := readSecret( + "S3_BOOTSTRAP_ACCESS_KEY_ID_FILE", + "/run/secrets/s3_bootstrap_access_key_id", + ) + if err != nil { + return err + } + + bootstrapSecretKey, err := readSecret( + "S3_BOOTSTRAP_SECRET_ACCESS_KEY_FILE", + "/run/secrets/s3_bootstrap_secret_access_key", + ) + if err != nil { + return err + } + + cfg := Config{ + Identities: []Identity{ + { + Name: "bootstrap", + Credentials: []Credential{ + {AccessKey: bootstrapAccessKey, SecretKey: bootstrapSecretKey}, + }, + Actions: []string{"Admin", "Read", "Write", "List", "Tagging"}, + }, + { + Name: "api", + Credentials: []Credential{ + {AccessKey: apiAccessKey, SecretKey: apiSecretKey}, + }, + Actions: []string{"Read", "Write", "List", "Tagging"}, + }, + }, + } + + data, err := json.MarshalIndent(cfg, "", " ") + if err != nil { + return fmt.Errorf("marshal s3 config: %w", err) + } + + if err := os.MkdirAll(filepath.Dir(output), 0o755); err != nil { + return fmt.Errorf("create config directory: %w", err) + } + + tmp := output + ".tmp" + if err := os.WriteFile(tmp, append(data, '\n'), 0o600); err != nil { + return fmt.Errorf("write config: %w", err) + } + + if err := os.Chown(tmp, seaweedUID, seaweedGID); err != nil { + return fmt.Errorf("chown config: %w", err) + } + + if err := os.Chmod(tmp, 0o400); err != nil { + return fmt.Errorf("chmod config: %w", err) + } + + if err := os.Rename(tmp, output); err != nil { + return fmt.Errorf("replace config: %w", err) + } + + return nil +} + +// LoadConfig reads a config previously written by WriteConfig. +func LoadConfig(path string) (Config, error) { + if path == "" { + path = defaultConfigPath + } + + data, err := os.ReadFile(path) + if err != nil { + return Config{}, fmt.Errorf("read s3 config %s: %w", path, err) + } + + var cfg Config + if err := json.Unmarshal(data, &cfg); err != nil { + return Config{}, fmt.Errorf("parse s3 config %s: %w", path, err) + } + + return cfg, nil +} + +func readSecret(envKey, defaultPath string) (string, error) { + path := defaultPath + if value := os.Getenv(envKey); value != "" { + path = value + } + + data, err := os.ReadFile(path) + if err != nil { + return "", fmt.Errorf("read secret %s: %w", path, err) + } + + value := strings.TrimSpace(string(data)) + if value == "" { + return "", fmt.Errorf("secret %s is empty", path) + } + + return value, nil +}