149 lines
3.1 KiB
Go
149 lines
3.1 KiB
Go
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
|
|
}
|