refactor(s3): read S3 secrets through app config
This commit is contained in:
+9
-58
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user