refactor(s3): read S3 secrets through app config

This commit is contained in:
kk10
2026-11-02 15:12:11 +03:00
parent 635620d627
commit 9b38e090ee
3 changed files with 59 additions and 59 deletions
+5 -1
View File
@@ -4,11 +4,15 @@ import (
"fmt"
"os"
"mtusi2026-messenger/internal/config"
"mtusi2026-messenger/internal/s3"
)
func main() {
if err := s3.WriteConfig("/config/s3.json"); err != nil {
cfg := config.LoadS3()
err := s3.WriteConfig("/config/s3.json", cfg)
if err != nil {
fmt.Fprintln(os.Stderr, "s3config:", err)
os.Exit(1)
}
+45
View File
@@ -20,6 +20,7 @@ type Config struct {
Logging LoggingConfig
Database DatabaseConfig
Redis RedisConfig
S3 S3Config
SessionStore string
}
@@ -56,6 +57,34 @@ func (c RedisConfig) URL() (string, error) {
return readSecretFile(c.URLFile)
}
type S3Config struct {
Endpoint string
Region string
Bucket string
APIAccessKeyFile string
APISecretKeyFile string
BootstrapAccessKeyFile string
BootstrapSecretKeyFile string
MaxUploadBytes int
MaxImagePixels int
}
func (c S3Config) APIAccessKey() (string, error) {
return readSecretFile(c.APIAccessKeyFile)
}
func (c S3Config) APISecretKey() (string, error) {
return readSecretFile(c.APISecretKeyFile)
}
func (c S3Config) BootstrapAccessKey() (string, error) {
return readSecretFile(c.BootstrapAccessKeyFile)
}
func (c S3Config) BootstrapSecretKey() (string, error) {
return readSecretFile(c.BootstrapSecretKeyFile)
}
func Load() Config {
return Config{
Server: ServerConfig{
@@ -79,10 +108,26 @@ func Load() Config {
URLFile: getEnv("CACHE_URL_FILE", "/run/secrets/cache_url"),
},
S3: LoadS3(),
SessionStore: getEnvSessionStore("SESSION_STORE", SessionStorePostgres),
}
}
func LoadS3() S3Config {
return S3Config{
Endpoint: getEnv("S3_ENDPOINT", "http://s3:9000"),
Region: getEnv("S3_REGION", "us-east-1"),
Bucket: getEnv("S3_BUCKET", "messenger-attachments"),
APIAccessKeyFile: getEnv("S3_ACCESS_KEY_ID_FILE", "/run/secrets/s3_access_key_id"),
APISecretKeyFile: getEnv("S3_SECRET_ACCESS_KEY_FILE", "/run/secrets/s3_secret_access_key"),
BootstrapAccessKeyFile: getEnv("S3_BOOTSTRAP_ACCESS_KEY_ID_FILE", "/run/secrets/s3_bootstrap_access_key_id"),
BootstrapSecretKeyFile: getEnv("S3_BOOTSTRAP_SECRET_ACCESS_KEY_FILE", "/run/secrets/s3_bootstrap_secret_access_key"),
MaxUploadBytes: getEnvPositiveInt("MAX_UPLOAD_BYTES", 10485760),
MaxImagePixels: getEnvPositiveInt("MAX_IMAGE_PIXELS", 40000000),
}
}
func getEnv(key, def string) string {
value := os.Getenv(key)
if value == "" {
+9 -58
View File
@@ -5,14 +5,13 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"mtusi2026-messenger/internal/config"
)
const (
seaweedUID = 1000
seaweedGID = 1000
defaultConfigPath = "/config/s3.json"
)
type Credential struct {
@@ -30,38 +29,28 @@ 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")
func WriteConfig(output string, cfg config.S3Config) error {
apiAccessKey, err := cfg.APIAccessKey()
if err != nil {
return err
}
apiSecretKey, err := readSecret("S3_SECRET_ACCESS_KEY_FILE", "/run/secrets/s3_secret_access_key")
apiSecretKey, err := cfg.APISecretKey()
if err != nil {
return err
}
bootstrapAccessKey, err := readSecret(
"S3_BOOTSTRAP_ACCESS_KEY_ID_FILE",
"/run/secrets/s3_bootstrap_access_key_id",
)
bootstrapAccessKey, err := cfg.BootstrapAccessKey()
if err != nil {
return err
}
bootstrapSecretKey, err := readSecret(
"S3_BOOTSTRAP_SECRET_ACCESS_KEY_FILE",
"/run/secrets/s3_bootstrap_secret_access_key",
)
bootstrapSecretKey, err := cfg.BootstrapSecretKey()
if err != nil {
return err
}
cfg := Config{
config := Config{
Identities: []Identity{
{
Name: "bootstrap",
@@ -80,7 +69,7 @@ func WriteConfig(output string) error {
},
}
data, err := json.MarshalIndent(cfg, "", " ")
data, err := json.MarshalIndent(config, "", " ")
if err != nil {
return fmt.Errorf("marshal s3 config: %w", err)
}
@@ -108,41 +97,3 @@ func WriteConfig(output string) error {
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
}