diff --git a/cmd/stickd/main.go b/cmd/stickd/main.go index 5243173..a92630f 100644 --- a/cmd/stickd/main.go +++ b/cmd/stickd/main.go @@ -53,7 +53,10 @@ func mainContext(parent context.Context, args []string) (err error) { return fmt.Errorf("unexpected positional arguments: %v", flags.Args()) } - cfg, err := config.Load(*configPath) + cfg, err := config.Load(parent, + config.YAMLProvider{Path: *configPath}, + config.EnvironmentProvider{}, + ) if err != nil { return fmt.Errorf("config: %w", err) } diff --git a/go.mod b/go.mod index 21f5aa3..9f75b4d 100644 --- a/go.mod +++ b/go.mod @@ -78,7 +78,7 @@ require ( go.opentelemetry.io/otel v1.44.0 // indirect go.opentelemetry.io/otel/metric v1.44.0 // indirect go.opentelemetry.io/otel/trace v1.44.0 // indirect - golang.org/x/crypto v0.55.0 // indirect + golang.org/x/crypto v0.56.0 // indirect golang.org/x/sync v0.22.0 // indirect golang.org/x/sys v0.47.0 // indirect golang.org/x/text v0.41.0 // indirect diff --git a/go.sum b/go.sum index e6b6bbd..24bb65b 100644 --- a/go.sum +++ b/go.sum @@ -173,6 +173,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.55.0 h1:+KWHjbgOaAQ66dh/YlkZKHlz9ZUlq61AFirAR9ntP8M= golang.org/x/crypto v0.55.0/go.mod h1:uq0V9dE/fzQuJtbnL+2EhWOE63vo164FY8xqEnV9xis= +golang.org/x/crypto v0.56.0 h1:GUh5Ii4J5jtcseSMiRqr1jXCNHoxjeV9Fmekc2oLy6Y= +golang.org/x/crypto v0.56.0/go.mod h1:OMW5y6CY9l38uPLmxU6l6pwcXp1obtLo3e6gT7gQR2I= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.38.0 h1:MECBjubtXD7yj4HrhIUcywNaGeNVUdfVnxmPajOk4yk= golang.org/x/mod v0.38.0/go.mod h1:V6Xz0pq8TQ3dGqVQ1FVHuelZpAL0uNhSkk9ogYP3c40= diff --git a/internal/config/config.go b/internal/config/config.go index 39b47b8..2f92aaf 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -65,7 +65,9 @@ type NotificationsConfig struct { Teams []*TeamsConfig } -// Config is the normalized application configuration. +// Config is the normalized application configuration returned by Load. +// Providers populate a Config value before Load applies final defaults and +// validation. type Config struct { Server ServerConfig Database DatabaseConfig @@ -73,56 +75,3 @@ type Config struct { Timezone *time.Location Notifications NotificationsConfig } - -// rawConfig and its nested types are the YAML representation. Keeping these -// separate from Config prevents unvalidated source values from escaping the -// configuration boundary. -type rawConfig struct { - Server rawServerConfig `yaml:"server"` - Database string `yaml:"database"` - Auth rawAuthConfig `yaml:"auth"` - Timezone string `yaml:"timezone"` - Notifications rawNotificationsConfig `yaml:"notifications"` -} - -type rawServerConfig struct { - PublicURL string `yaml:"public_url"` - ListenAddr string `yaml:"listen_addr"` -} - -type rawAuthConfig struct { - OIDC rawOIDCConfig `yaml:"oidc"` - SessionSecret string `yaml:"session_secret"` - AdminEmails []string `yaml:"admin_emails"` -} - -type rawOIDCConfig struct { - Issuer string `yaml:"issuer"` - ClientID string `yaml:"client_id"` - ClientSecret string `yaml:"client_secret"` -} - -type rawNotificationsConfig struct { - SMTP rawSMTPConfigs `yaml:"smtp"` - Webhook rawWebhookConfigs `yaml:"webhook"` - Teams rawTeamsConfigs `yaml:"teams"` -} - -type rawSMTPConfig struct { - Host string `yaml:"host"` - Port int `yaml:"port"` - TLSMode string `yaml:"tls_mode"` - Username string `yaml:"username"` - Password string `yaml:"password"` - From string `yaml:"from"` - Subject string `yaml:"subject"` - Body string `yaml:"body"` -} - -type rawWebhookConfig struct { - URL string `yaml:"url"` -} - -type rawTeamsConfig struct { - URL string `yaml:"url"` -} diff --git a/internal/config/config_test.go b/internal/config/config_test.go index ac6e60e..629efd0 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -1,6 +1,7 @@ package config_test import ( + "context" "os" "path/filepath" "strings" @@ -63,7 +64,7 @@ func loadFromEnv(t *testing.T) config.Config { t.Fatal(err) } t.Cleanup(func() { _ = os.Chdir(original) }) - cfg, err := config.Load("") + cfg, err := config.Load(context.Background(), config.YAMLProvider{}, config.EnvironmentProvider{}) if err != nil { t.Fatal(err) } @@ -145,7 +146,7 @@ auth: timezone: America/New_York `) - cfg, err := config.Load(path) + cfg, err := config.Load(context.Background(), config.YAMLProvider{Path: path}, config.EnvironmentProvider{}) if err != nil { t.Fatalf("Load: %v", err) } @@ -181,7 +182,7 @@ auth: t.Setenv("STICK_SERVER_PUBLIC_URL", "https://env.example.com/stick/") t.Setenv("STICK_AUTH_ADMIN_EMAILS", " Alice@Example.com, bob@example.com ") - cfg, err := config.Load(path) + cfg, err := config.Load(context.Background(), config.YAMLProvider{Path: path}, config.EnvironmentProvider{}) if err != nil { t.Fatalf("Load: %v", err) } @@ -196,6 +197,42 @@ auth: } } +func TestLoad_ProvidersApplyInSuppliedOrder(t *testing.T) { + setRequiredEnv(t) + t.Setenv("STICK_SERVER_LISTEN_ADDR", ":7070") + path := writeConfig(t, ` +server: + listen_addr: :9090 +`) + + cfg, err := config.Load( + context.Background(), + config.YAMLProvider{Path: path}, + config.EnvironmentProvider{}, + ) + if err != nil { + t.Fatalf("Load with YAML then environment: %v", err) + } + if cfg.Server.ListenAddr != ":7070" { + t.Errorf("ListenAddr = %q, want environment value", cfg.Server.ListenAddr) + } + + cfg, err = config.Load( + context.Background(), + config.EnvironmentProvider{}, + config.YAMLProvider{Path: path}, + ) + if err != nil { + t.Fatalf("Load with environment then YAML: %v", err) + } + if cfg.Server.ListenAddr != ":9090" { + t.Errorf("ListenAddr = %q, want YAML value", cfg.Server.ListenAddr) + } + if cfg.Database.DSN != "/tmp/test.db" { + t.Errorf("Database.DSN = %q, want value preserved from environment", cfg.Database.DSN) + } +} + func TestLoad_NotificationConfig(t *testing.T) { clearConfigEnv(t) path := writeConfig(t, ` @@ -219,7 +256,7 @@ notifications: subject: "{{.StickName}} ready" body: "Hello {{.RecipientName}}" `) - cfg, err := config.Load(path) + cfg, err := config.Load(context.Background(), config.YAMLProvider{Path: path}, config.EnvironmentProvider{}) if err != nil { t.Fatalf("Load: %v", err) } @@ -262,7 +299,7 @@ notifications: teams: url: https://teams.example.com/webhook `) - cfg, err := config.Load(path) + cfg, err := config.Load(context.Background(), config.YAMLProvider{Path: path}, config.EnvironmentProvider{}) if err != nil { t.Fatalf("Load: %v", err) } @@ -289,7 +326,7 @@ auth: session_secret: yaml-session-secret-that-is-long-enough timezone: Not/ATimezone `) - if _, err := config.Load(path); err == nil || !strings.Contains(err.Error(), "timezone") { + if _, err := config.Load(context.Background(), config.YAMLProvider{Path: path}, config.EnvironmentProvider{}); err == nil || !strings.Contains(err.Error(), "timezone") { t.Fatalf("Load error = %v", err) } } @@ -297,7 +334,7 @@ timezone: Not/ATimezone func TestLoad_RejectsShortSessionSecret(t *testing.T) { setRequiredEnv(t) t.Setenv("STICK_AUTH_SESSION_SECRET", "too-short") - if _, err := config.Load(""); err == nil || !strings.Contains(err.Error(), "SESSION_SECRET") { + if _, err := config.Load(context.Background(), config.YAMLProvider{}, config.EnvironmentProvider{}); err == nil || !strings.Contains(err.Error(), "auth.session_secret") { t.Fatalf("Load error = %v", err) } } @@ -316,7 +353,7 @@ auth: session_secret: 0123456789abcdef0123456789abcdef unknown_setting: true `) - if _, err := config.Load(path); err == nil || !strings.Contains(err.Error(), "field unknown_setting not found") { + if _, err := config.Load(context.Background(), config.YAMLProvider{Path: path}, config.EnvironmentProvider{}); err == nil || !strings.Contains(err.Error(), "field unknown_setting not found") { t.Fatalf("unknown YAML field error = %v", err) } } @@ -335,7 +372,7 @@ func TestLoad_RejectsInvalidURLs(t *testing.T) { t.Run(test.name, func(t *testing.T) { setRequiredEnv(t) t.Setenv(test.key, test.value) - if _, err := config.Load(""); err == nil { + if _, err := config.Load(context.Background(), config.YAMLProvider{}, config.EnvironmentProvider{}); err == nil { t.Fatal("expected invalid URL to be rejected") } }) @@ -343,16 +380,20 @@ func TestLoad_RejectsInvalidURLs(t *testing.T) { } func TestLoad_ExplicitPathNotFound(t *testing.T) { - if _, err := config.Load("/nonexistent/path/config.yaml"); err == nil { + if _, err := config.Load(context.Background(), config.YAMLProvider{Path: "/nonexistent/path/config.yaml"}, config.EnvironmentProvider{}); err == nil { t.Fatal("expected error for explicit missing config") } } func TestLoad_MissingRequired(t *testing.T) { clearConfigEnv(t) - if _, err := loadFromEnvWithoutRequired(t); err == nil || !strings.Contains(err.Error(), "missing required config") { + _, err := loadFromEnvWithoutRequired(t) + if err == nil || !strings.Contains(err.Error(), "missing required config") { t.Fatalf("Load error = %v", err) } + if strings.Contains(err.Error(), "STICK_") { + t.Fatalf("Load error exposes provider-specific names: %v", err) + } } func loadFromEnvWithoutRequired(t *testing.T) (config.Config, error) { @@ -366,5 +407,5 @@ func loadFromEnvWithoutRequired(t *testing.T) (config.Config, error) { t.Fatal(err) } t.Cleanup(func() { _ = os.Chdir(original) }) - return config.Load("") + return config.Load(context.Background(), config.YAMLProvider{}, config.EnvironmentProvider{}) } diff --git a/internal/config/environment_provider.go b/internal/config/environment_provider.go new file mode 100644 index 0000000..60130a9 --- /dev/null +++ b/internal/config/environment_provider.go @@ -0,0 +1,129 @@ +package config + +import ( + "context" + "fmt" + "os" + "strconv" + "strings" +) + +// EnvironmentProvider applies non-empty STICK_* environment variable +// overrides to the accumulated configuration. +type EnvironmentProvider struct{} + +// Apply applies environment variable overrides. +func (EnvironmentProvider) Apply(_ context.Context, config *Config) error { + if value := os.Getenv("STICK_DATABASE"); value != "" { + if err := setDatabase(config, value); err != nil { + return err + } + } + if value := os.Getenv("STICK_SERVER_PUBLIC_URL"); value != "" { + if err := setPublicURL(config, value); err != nil { + return err + } + } + if value := os.Getenv("STICK_SERVER_LISTEN_ADDR"); value != "" { + config.Server.ListenAddr = strings.TrimSpace(value) + } + if value := os.Getenv("STICK_TIMEZONE"); value != "" { + if err := setTimezone(config, value); err != nil { + return err + } + } + if value := os.Getenv("STICK_AUTH_OIDC_ISSUER"); value != "" { + config.Auth.OIDC.Issuer = value + } + if value := os.Getenv("STICK_AUTH_OIDC_CLIENT_ID"); value != "" { + config.Auth.OIDC.ClientID = value + } + if value := os.Getenv("STICK_AUTH_OIDC_CLIENT_SECRET"); value != "" { + config.Auth.OIDC.ClientSecret = value + } + if value := os.Getenv("STICK_AUTH_SESSION_SECRET"); value != "" { + setSessionSecret(config, value) + } + if value := os.Getenv("STICK_AUTH_ADMIN_EMAILS"); value != "" { + config.Auth.AdminEmails = strings.Split(value, ",") + } + + if err := applySMTPEnvOverrides(config); err != nil { + return err + } + if value := os.Getenv("STICK_NOTIFICATIONS_WEBHOOK_URL"); value != "" { + if len(config.Notifications.Webhook) == 0 { + config.Notifications.Webhook = append(config.Notifications.Webhook, &WebhookConfig{}) + } + if config.Notifications.Webhook[0] == nil { + config.Notifications.Webhook[0] = &WebhookConfig{} + } + config.Notifications.Webhook[0].URL = strings.TrimSpace(value) + } + if value := os.Getenv("STICK_NOTIFICATIONS_TEAMS_URL"); value != "" { + if len(config.Notifications.Teams) == 0 { + config.Notifications.Teams = append(config.Notifications.Teams, &TeamsConfig{}) + } + if config.Notifications.Teams[0] == nil { + config.Notifications.Teams[0] = &TeamsConfig{} + } + config.Notifications.Teams[0].URL = strings.TrimSpace(value) + } + return nil +} + +func applySMTPEnvOverrides(config *Config) error { + keys := []string{ + "STICK_NOTIFICATIONS_SMTP_HOST", + "STICK_NOTIFICATIONS_SMTP_PORT", + "STICK_NOTIFICATIONS_SMTP_TLS_MODE", + "STICK_NOTIFICATIONS_SMTP_USERNAME", + "STICK_NOTIFICATIONS_SMTP_PASSWORD", + "STICK_NOTIFICATIONS_SMTP_FROM", + "STICK_NOTIFICATIONS_SMTP_SUBJECT", + "STICK_NOTIFICATIONS_SMTP_BODY", + } + configured := false + for _, key := range keys { + if os.Getenv(key) != "" { + configured = true + break + } + } + if !configured { + return nil + } + + var smtp SMTPConfig + if len(config.Notifications.SMTP) == 0 { + config.Notifications.SMTP = append(config.Notifications.SMTP, &smtp) + } else if config.Notifications.SMTP[0] != nil { + smtp = *config.Notifications.SMTP[0] + } else { + config.Notifications.SMTP[0] = &smtp + } + + set := func(key string, dst *string, normalize func(string) string) { + if value := os.Getenv(key); value != "" { + *dst = normalize(value) + } + } + set("STICK_NOTIFICATIONS_SMTP_HOST", &smtp.Host, strings.TrimSpace) + set("STICK_NOTIFICATIONS_SMTP_TLS_MODE", &smtp.TLSMode, func(value string) string { + return strings.ToLower(strings.TrimSpace(value)) + }) + set("STICK_NOTIFICATIONS_SMTP_USERNAME", &smtp.Username, func(value string) string { return value }) + set("STICK_NOTIFICATIONS_SMTP_PASSWORD", &smtp.Password, func(value string) string { return value }) + set("STICK_NOTIFICATIONS_SMTP_FROM", &smtp.From, strings.TrimSpace) + set("STICK_NOTIFICATIONS_SMTP_SUBJECT", &smtp.Subject, func(value string) string { return value }) + set("STICK_NOTIFICATIONS_SMTP_BODY", &smtp.Body, func(value string) string { return value }) + if value := os.Getenv("STICK_NOTIFICATIONS_SMTP_PORT"); value != "" { + port, err := strconv.Atoi(value) + if err != nil { + return fmt.Errorf("invalid STICK_NOTIFICATIONS_SMTP_PORT: %w", err) + } + smtp.Port = port + } + config.Notifications.SMTP[0] = &smtp + return nil +} diff --git a/internal/config/load.go b/internal/config/load.go index e1985f4..5885f10 100644 --- a/internal/config/load.go +++ b/internal/config/load.go @@ -1,133 +1,26 @@ package config import ( - "bytes" + "context" "fmt" - "io" - "os" - "strconv" - "strings" - - "gopkg.in/yaml.v3" ) -// Load reads configuration from YAML and applies non-empty STICK_* environment -// variable overrides. If path is empty, config.yaml in the working directory -// is optional and environment variables may provide the complete config. -func Load(path string) (Config, error) { - raw := rawConfig{} - - filePath := path - if filePath == "" { - filePath = "config.yaml" - } - - data, err := os.ReadFile(filePath) - if err != nil { - if !os.IsNotExist(err) || path != "" { - return Config{}, fmt.Errorf("reading config file %q: %w", filePath, err) - } - } else { - decoder := yaml.NewDecoder(bytes.NewReader(data)) - decoder.KnownFields(true) - if err := decoder.Decode(&raw); err != nil { - return Config{}, fmt.Errorf("parsing config file %q: %w", filePath, err) - } - var trailing any - if err := decoder.Decode(&trailing); err != io.EOF { - if err == nil { - err = fmt.Errorf("multiple YAML documents are not supported") - } - return Config{}, fmt.Errorf("parsing config file %q: %w", filePath, err) - } - } - - if err := applyEnvOverrides(&raw); err != nil { - return Config{}, err - } - return normalize(raw) +// Provider applies configuration values to the accumulated configuration. +// Providers are applied in the order supplied to Load. A provider must only +// change values present in its source and must not reset the accumulated +// configuration. +type Provider interface { + Apply(context.Context, *Config) error } -func applyEnvOverrides(raw *rawConfig) error { - set := func(key string, dst *string) { - if value := os.Getenv(key); value != "" { - *dst = value - } - } - - set("STICK_DATABASE", &raw.Database) - set("STICK_SERVER_PUBLIC_URL", &raw.Server.PublicURL) - set("STICK_SERVER_LISTEN_ADDR", &raw.Server.ListenAddr) - set("STICK_TIMEZONE", &raw.Timezone) - set("STICK_AUTH_OIDC_ISSUER", &raw.Auth.OIDC.Issuer) - set("STICK_AUTH_OIDC_CLIENT_ID", &raw.Auth.OIDC.ClientID) - set("STICK_AUTH_OIDC_CLIENT_SECRET", &raw.Auth.OIDC.ClientSecret) - set("STICK_AUTH_SESSION_SECRET", &raw.Auth.SessionSecret) - if value := os.Getenv("STICK_AUTH_ADMIN_EMAILS"); value != "" { - raw.Auth.AdminEmails = strings.Split(value, ",") - } - - if err := applySMTPEnvOverrides(raw); err != nil { - return err - } - if value := os.Getenv("STICK_NOTIFICATIONS_WEBHOOK_URL"); value != "" { - if len(raw.Notifications.Webhook) == 0 { - raw.Notifications.Webhook = append(raw.Notifications.Webhook, &rawWebhookConfig{}) - } - raw.Notifications.Webhook[0].URL = value - } - if value := os.Getenv("STICK_NOTIFICATIONS_TEAMS_URL"); value != "" { - if len(raw.Notifications.Teams) == 0 { - raw.Notifications.Teams = append(raw.Notifications.Teams, &rawTeamsConfig{}) - } - raw.Notifications.Teams[0].URL = value - } - return nil -} - -func applySMTPEnvOverrides(raw *rawConfig) error { - keys := []string{ - "STICK_NOTIFICATIONS_SMTP_HOST", - "STICK_NOTIFICATIONS_SMTP_PORT", - "STICK_NOTIFICATIONS_SMTP_TLS_MODE", - "STICK_NOTIFICATIONS_SMTP_USERNAME", - "STICK_NOTIFICATIONS_SMTP_PASSWORD", - "STICK_NOTIFICATIONS_SMTP_FROM", - "STICK_NOTIFICATIONS_SMTP_SUBJECT", - "STICK_NOTIFICATIONS_SMTP_BODY", - } - configured := false - for _, key := range keys { - if os.Getenv(key) != "" { - configured = true - break - } - } - if !configured { - return nil - } - if len(raw.Notifications.SMTP) == 0 { - raw.Notifications.SMTP = append(raw.Notifications.SMTP, &rawSMTPConfig{}) - } - smtp := raw.Notifications.SMTP[0] - set := func(key string, dst *string) { - if value := os.Getenv(key); value != "" { - *dst = value - } - } - set("STICK_NOTIFICATIONS_SMTP_HOST", &smtp.Host) - set("STICK_NOTIFICATIONS_SMTP_TLS_MODE", &smtp.TLSMode) - set("STICK_NOTIFICATIONS_SMTP_USERNAME", &smtp.Username) - set("STICK_NOTIFICATIONS_SMTP_PASSWORD", &smtp.Password) - set("STICK_NOTIFICATIONS_SMTP_FROM", &smtp.From) - set("STICK_NOTIFICATIONS_SMTP_SUBJECT", &smtp.Subject) - set("STICK_NOTIFICATIONS_SMTP_BODY", &smtp.Body) - if value := os.Getenv("STICK_NOTIFICATIONS_SMTP_PORT"); value != "" { - port, err := strconv.Atoi(value) - if err != nil { - return fmt.Errorf("invalid STICK_NOTIFICATIONS_SMTP_PORT: %w", err) +// Load applies providers in order and normalizes the resulting configuration. +// Later providers override values supplied by earlier providers. +func Load(ctx context.Context, providers ...Provider) (Config, error) { + config := Config{} + for _, provider := range providers { + if err := provider.Apply(ctx, &config); err != nil { + return Config{}, fmt.Errorf("%T: %w", provider, err) } - smtp.Port = port } - return nil + return normalize(config) } diff --git a/internal/config/normalize.go b/internal/config/normalize.go index 05996f2..1f0281b 100644 --- a/internal/config/normalize.go +++ b/internal/config/normalize.go @@ -7,98 +7,83 @@ import ( "strings" "time" - "stick/internal/auth" "stick/internal/netutil" "stick/internal/publicurl" ) const minSessionSecretBytes = 32 -func normalize(raw rawConfig) (Config, error) { - if err := validateRequired(raw); err != nil { - return Config{}, err +func normalize(config Config) (Config, error) { + config.Server.ListenAddr = strings.TrimSpace(config.Server.ListenAddr) + if config.Server.ListenAddr == "" { + config.Server.ListenAddr = ":8080" } - database, err := normalizeDatabase(raw.Database) - if err != nil { - return Config{}, err + if config.Timezone == nil { + config.Timezone = time.UTC } - publicURL, err := publicurl.Parse(raw.Server.PublicURL) + config.Auth.SessionSecret = []byte(strings.TrimSpace(string(config.Auth.SessionSecret))) + + notifications, err := normalizeNotifications(config.Notifications) if err != nil { return Config{}, err } - if !publicURL.IsHTTPS() && !publicURL.IsLoopback() { - return Config{}, fmt.Errorf("invalid PUBLIC_URL %q: HTTPS is required for non-local addresses", raw.Server.PublicURL) - } + config.Notifications = notifications - listenAddr := strings.TrimSpace(raw.Server.ListenAddr) - if listenAddr == "" { - listenAddr = ":8080" + if err := validateRequired(config); err != nil { + return Config{}, err } - - location := time.UTC - if raw.Timezone != "" { - location, err = time.LoadLocation(raw.Timezone) - if err != nil { - return Config{}, fmt.Errorf("invalid timezone %q: %w", raw.Timezone, err) - } + if err := validateDatabase(config.Database); err != nil { + return Config{}, err } - - notifications, err := normalizeNotifications(raw.Notifications) + if !config.Server.PublicURL.IsHTTPS() && !config.Server.PublicURL.IsLoopback() { + return Config{}, fmt.Errorf("invalid server.public_url %q: HTTPS is required for non-local addresses", config.Server.PublicURL) + } + issuer, err := parseHTTPURL("auth.oidc.issuer", config.Auth.OIDC.Issuer) if err != nil { return Config{}, err } - return Config{ - Server: ServerConfig{ - PublicURL: publicURL, - ListenAddr: listenAddr, - }, - Database: database, - Auth: AuthConfig{ - OIDC: auth.OIDCConfig{ - Issuer: raw.Auth.OIDC.Issuer, - ClientID: raw.Auth.OIDC.ClientID, - ClientSecret: raw.Auth.OIDC.ClientSecret, - }, - SessionSecret: []byte(strings.TrimSpace(raw.Auth.SessionSecret)), - AdminEmails: append([]string(nil), raw.Auth.AdminEmails...), - }, - Timezone: location, - Notifications: notifications, - }, nil + if issuer.RawQuery != "" || issuer.Fragment != "" { + return Config{}, fmt.Errorf("invalid auth.oidc.issuer %q: must not include a query or fragment", config.Auth.OIDC.Issuer) + } + if err := validateSessionSecret(config.Auth.SessionSecret); err != nil { + return Config{}, err + } + + config.Auth.AdminEmails = append([]string(nil), config.Auth.AdminEmails...) + return config, nil } -func validateRequired(raw rawConfig) error { - required := map[string]string{ - "STICK_DATABASE": raw.Database, - "STICK_SERVER_PUBLIC_URL": raw.Server.PublicURL, - "STICK_AUTH_OIDC_ISSUER": raw.Auth.OIDC.Issuer, - "STICK_AUTH_OIDC_CLIENT_ID": raw.Auth.OIDC.ClientID, - "STICK_AUTH_OIDC_CLIENT_SECRET": raw.Auth.OIDC.ClientSecret, - "STICK_AUTH_SESSION_SECRET": raw.Auth.SessionSecret, +func validateRequired(config Config) error { + required := map[string]bool{ + "database": strings.TrimSpace(config.Database.DSN) != "", + "server.public_url": config.Server.PublicURL.Validate() == nil, + "auth.oidc.issuer": strings.TrimSpace(config.Auth.OIDC.Issuer) != "", + "auth.oidc.client_id": strings.TrimSpace(config.Auth.OIDC.ClientID) != "", + "auth.oidc.client_secret": strings.TrimSpace(config.Auth.OIDC.ClientSecret) != "", + "auth.session_secret": len(config.Auth.SessionSecret) > 0, } var missing []string - for key, value := range required { - if strings.TrimSpace(value) == "" { - missing = append(missing, key) + for field, present := range required { + if !present { + missing = append(missing, field) } } if len(missing) > 0 { sort.Strings(missing) return fmt.Errorf("missing required config: %v", missing) } - if err := validateSessionSecret(raw.Auth.SessionSecret); err != nil { - return err - } - issuer, err := parseHTTPURL("STICK_AUTH_OIDC_ISSUER", raw.Auth.OIDC.Issuer) - if err != nil { - return err - } - if issuer.RawQuery != "" || issuer.Fragment != "" { - return fmt.Errorf("invalid STICK_AUTH_OIDC_ISSUER %q: must not include a query or fragment", raw.Auth.OIDC.Issuer) - } return nil } +func validateDatabase(config DatabaseConfig) error { + switch config.Driver { + case DatabaseDriverSQLite, DatabaseDriverPostgres, DatabaseDriverMongoDB: + return nil + default: + return fmt.Errorf("unsupported database driver %q", config.Driver) + } +} + func normalizeDatabase(value string) (DatabaseConfig, error) { dsn := strings.TrimSpace(value) parsed, err := url.Parse(dsn) @@ -117,21 +102,21 @@ func normalizeDatabase(value string) (DatabaseConfig, error) { } } -func normalizeNotifications(raw rawNotificationsConfig) (NotificationsConfig, error) { +func normalizeNotifications(config NotificationsConfig) (NotificationsConfig, error) { result := NotificationsConfig{} - for _, smtp := range raw.SMTP { + for _, smtp := range config.SMTP { if smtp == nil { return NotificationsConfig{}, fmt.Errorf("notifications.smtp entries must be mappings") } result.SMTP = append(result.SMTP, normalizeSMTP(smtp)) } - for _, webhook := range raw.Webhook { + for _, webhook := range config.Webhook { if webhook == nil { return NotificationsConfig{}, fmt.Errorf("notifications.webhook entries must be mappings") } result.Webhook = append(result.Webhook, &WebhookConfig{URL: strings.TrimSpace(webhook.URL)}) } - for _, teams := range raw.Teams { + for _, teams := range config.Teams { if teams == nil { return NotificationsConfig{}, fmt.Errorf("notifications.teams entries must be mappings") } @@ -140,17 +125,12 @@ func normalizeNotifications(raw rawNotificationsConfig) (NotificationsConfig, er return result, nil } -func normalizeSMTP(raw *rawSMTPConfig) *SMTPConfig { - return &SMTPConfig{ - Host: strings.TrimSpace(raw.Host), - Port: raw.Port, - TLSMode: strings.ToLower(strings.TrimSpace(raw.TLSMode)), - Username: raw.Username, - Password: raw.Password, - From: strings.TrimSpace(raw.From), - Subject: raw.Subject, - Body: raw.Body, - } +func normalizeSMTP(config *SMTPConfig) *SMTPConfig { + result := *config + result.Host = strings.TrimSpace(result.Host) + result.TLSMode = strings.ToLower(strings.TrimSpace(result.TLSMode)) + result.From = strings.TrimSpace(result.From) + return &result } func parseHTTPURL(name, value string) (*url.URL, error) { @@ -170,10 +150,40 @@ func parseHTTPURL(name, value string) (*url.URL, error) { return parsed, nil } -func validateSessionSecret(secret string) error { - secret = strings.TrimSpace(secret) - if len([]byte(secret)) < minSessionSecretBytes { - return fmt.Errorf("STICK_AUTH_SESSION_SECRET must be at least %d bytes", minSessionSecretBytes) +func validateSessionSecret(secret []byte) error { + if len(secret) < minSessionSecretBytes { + return fmt.Errorf("auth.session_secret must be at least %d bytes", minSessionSecretBytes) + } + return nil +} + +func setDatabase(config *Config, value string) error { + database, err := normalizeDatabase(value) + if err != nil { + return err } + config.Database = database return nil } + +func setPublicURL(config *Config, value string) error { + publicURL, err := publicurl.Parse(value) + if err != nil { + return err + } + config.Server.PublicURL = publicURL + return nil +} + +func setTimezone(config *Config, value string) error { + location, err := time.LoadLocation(value) + if err != nil { + return fmt.Errorf("invalid timezone %q: %w", value, err) + } + config.Timezone = location + return nil +} + +func setSessionSecret(config *Config, value string) { + config.Auth.SessionSecret = []byte(strings.TrimSpace(value)) +} diff --git a/internal/config/notification_yaml.go b/internal/config/notification_yaml.go deleted file mode 100644 index d20ffe9..0000000 --- a/internal/config/notification_yaml.go +++ /dev/null @@ -1,94 +0,0 @@ -package config - -import ( - "fmt" - - "gopkg.in/yaml.v3" -) - -// The notification fields accept both the original mapping form for one -// backend and a sequence form for multiple instances of that backend. -type rawSMTPConfigs []*rawSMTPConfig -type rawWebhookConfigs []*rawWebhookConfig -type rawTeamsConfigs []*rawTeamsConfig - -func (c *rawSMTPConfigs) UnmarshalYAML(node *yaml.Node) error { - items, err := decodeNotificationConfigs[rawSMTPConfig](node, "smtp", []string{ - "host", "port", "tls_mode", "username", "password", "from", "subject", "body", - }) - if err != nil { - return err - } - *c = items - return nil -} - -func (c *rawWebhookConfigs) UnmarshalYAML(node *yaml.Node) error { - items, err := decodeNotificationConfigs[rawWebhookConfig](node, "webhook", []string{"url"}) - if err != nil { - return err - } - *c = items - return nil -} - -func (c *rawTeamsConfigs) UnmarshalYAML(node *yaml.Node) error { - items, err := decodeNotificationConfigs[rawTeamsConfig](node, "teams", []string{"url"}) - if err != nil { - return err - } - *c = items - return nil -} - -func decodeNotificationConfigs[T any](node *yaml.Node, name string, fields []string) ([]*T, error) { - if node.Kind == yaml.ScalarNode && node.Tag == "!!null" { - return nil, nil - } - - if node.Kind == yaml.MappingNode { - if err := validateNotificationFields(node, name, fields); err != nil { - return nil, err - } - var item T - if err := node.Decode(&item); err != nil { - return nil, err - } - return []*T{&item}, nil - } - - if node.Kind != yaml.SequenceNode { - return nil, fmt.Errorf("notifications.%s must be a mapping or sequence", name) - } - for _, item := range node.Content { - if item.Kind == yaml.ScalarNode && item.Tag == "!!null" { - continue - } - if item.Kind != yaml.MappingNode { - return nil, fmt.Errorf("notifications.%s entries must be mappings", name) - } - if err := validateNotificationFields(item, name, fields); err != nil { - return nil, err - } - } - - var items []*T - if err := node.Decode(&items); err != nil { - return nil, err - } - return items, nil -} - -func validateNotificationFields(node *yaml.Node, name string, fields []string) error { - known := make(map[string]struct{}, len(fields)) - for _, field := range fields { - known[field] = struct{}{} - } - for i := 0; i < len(node.Content); i += 2 { - key := node.Content[i].Value - if _, ok := known[key]; !ok { - return fmt.Errorf("field %s not found in notifications.%s", key, name) - } - } - return nil -} diff --git a/internal/config/yaml_provider.go b/internal/config/yaml_provider.go new file mode 100644 index 0000000..fd6edfd --- /dev/null +++ b/internal/config/yaml_provider.go @@ -0,0 +1,297 @@ +package config + +import ( + "bytes" + "context" + "fmt" + "io" + "os" + "strings" + + "gopkg.in/yaml.v3" +) + +// YAMLProvider loads configuration from a YAML file. An empty Path uses +// config.yaml in the working directory; that default file is optional. A +// non-empty Path must exist. +type YAMLProvider struct { + Path string +} + +// Apply reads and strictly decodes the configured YAML document before +// applying the values present in it to config. The YAML representation is +// private to this provider; the rest of the application only sees Config. +func (p YAMLProvider) Apply(ctx context.Context, config *Config) error { + filePath := p.Path + optional := filePath == "" + if optional { + filePath = "config.yaml" + } + + data, err := os.ReadFile(filePath) + if err != nil { + if optional && os.IsNotExist(err) { + return nil + } + return fmt.Errorf("reading config file %q: %w", filePath, err) + } + if err := ctx.Err(); err != nil { + return err + } + + source := yamlConfig{} + decoder := yaml.NewDecoder(bytes.NewReader(data)) + decoder.KnownFields(true) + if err := decoder.Decode(&source); err != nil { + return fmt.Errorf("parsing config file %q: %w", filePath, err) + } + var trailing any + if err := decoder.Decode(&trailing); err != io.EOF { + if err == nil { + err = fmt.Errorf("multiple YAML documents are not supported") + } + return fmt.Errorf("parsing config file %q: %w", filePath, err) + } + + updated := *config + if err := applyYAMLConfig(source, &updated); err != nil { + return err + } + *config = updated + return nil +} + +type yamlConfig struct { + Server yamlServerConfig `yaml:"server"` + Database *string `yaml:"database"` + Auth yamlAuthConfig `yaml:"auth"` + Timezone *string `yaml:"timezone"` + Notifications yamlNotificationsConfig `yaml:"notifications"` +} + +type yamlServerConfig struct { + PublicURL *string `yaml:"public_url"` + ListenAddr *string `yaml:"listen_addr"` +} + +type yamlAuthConfig struct { + OIDC yamlOIDCConfig `yaml:"oidc"` + SessionSecret *string `yaml:"session_secret"` + AdminEmails *[]string `yaml:"admin_emails"` +} + +type yamlOIDCConfig struct { + Issuer *string `yaml:"issuer"` + ClientID *string `yaml:"client_id"` + ClientSecret *string `yaml:"client_secret"` +} + +type yamlNotificationsConfig struct { + SMTP yamlSMTPConfigs `yaml:"smtp"` + Webhook yamlWebhookConfigs `yaml:"webhook"` + Teams yamlTeamsConfigs `yaml:"teams"` +} + +type yamlSMTPConfigs []*yamlSMTPConfig +type yamlWebhookConfigs []*yamlWebhookConfig +type yamlTeamsConfigs []*yamlTeamsConfig + +type yamlSMTPConfig struct { + Host string `yaml:"host"` + Port int `yaml:"port"` + TLSMode string `yaml:"tls_mode"` + Username string `yaml:"username"` + Password string `yaml:"password"` + From string `yaml:"from"` + Subject string `yaml:"subject"` + Body string `yaml:"body"` +} + +type yamlWebhookConfig struct { + URL string `yaml:"url"` +} + +type yamlTeamsConfig struct { + URL string `yaml:"url"` +} + +func applyYAMLConfig(source yamlConfig, config *Config) error { + if source.Database != nil { + if err := setDatabase(config, *source.Database); err != nil { + return err + } + } + if source.Server.PublicURL != nil { + if err := setPublicURL(config, *source.Server.PublicURL); err != nil { + return err + } + } + if source.Server.ListenAddr != nil { + config.Server.ListenAddr = strings.TrimSpace(*source.Server.ListenAddr) + } + if source.Auth.OIDC.Issuer != nil { + config.Auth.OIDC.Issuer = *source.Auth.OIDC.Issuer + } + if source.Auth.OIDC.ClientID != nil { + config.Auth.OIDC.ClientID = *source.Auth.OIDC.ClientID + } + if source.Auth.OIDC.ClientSecret != nil { + config.Auth.OIDC.ClientSecret = *source.Auth.OIDC.ClientSecret + } + if source.Auth.SessionSecret != nil { + setSessionSecret(config, *source.Auth.SessionSecret) + } + if source.Auth.AdminEmails != nil { + config.Auth.AdminEmails = append([]string(nil), (*source.Auth.AdminEmails)...) + } + if source.Timezone != nil { + if err := setTimezone(config, *source.Timezone); err != nil { + return err + } + } + if source.Notifications.SMTP != nil { + smtp, err := applyYAMLSMTP(source.Notifications.SMTP) + if err != nil { + return err + } + config.Notifications.SMTP = smtp + } + if source.Notifications.Webhook != nil { + webhooks, err := applyYAMLWebhooks(source.Notifications.Webhook) + if err != nil { + return err + } + config.Notifications.Webhook = webhooks + } + if source.Notifications.Teams != nil { + teams, err := applyYAMLTeams(source.Notifications.Teams) + if err != nil { + return err + } + config.Notifications.Teams = teams + } + return nil +} + +func applyYAMLSMTP(source yamlSMTPConfigs) ([]*SMTPConfig, error) { + result := make([]*SMTPConfig, 0, len(source)) + for _, item := range source { + if item == nil { + return nil, fmt.Errorf("notifications.smtp entries must be mappings") + } + result = append(result, &SMTPConfig{ + Host: strings.TrimSpace(item.Host), + Port: item.Port, + TLSMode: strings.ToLower(strings.TrimSpace(item.TLSMode)), + Username: item.Username, + Password: item.Password, + From: strings.TrimSpace(item.From), + Subject: item.Subject, + Body: item.Body, + }) + } + return result, nil +} + +func applyYAMLWebhooks(source yamlWebhookConfigs) ([]*WebhookConfig, error) { + result := make([]*WebhookConfig, 0, len(source)) + for _, item := range source { + if item == nil { + return nil, fmt.Errorf("notifications.webhook entries must be mappings") + } + result = append(result, &WebhookConfig{URL: strings.TrimSpace(item.URL)}) + } + return result, nil +} + +func applyYAMLTeams(source yamlTeamsConfigs) ([]*TeamsConfig, error) { + result := make([]*TeamsConfig, 0, len(source)) + for _, item := range source { + if item == nil { + return nil, fmt.Errorf("notifications.teams entries must be mappings") + } + result = append(result, &TeamsConfig{URL: strings.TrimSpace(item.URL)}) + } + return result, nil +} + +func (c *yamlSMTPConfigs) UnmarshalYAML(node *yaml.Node) error { + items, err := decodeYAMLNotificationConfigs[yamlSMTPConfig](node, "smtp", []string{ + "host", "port", "tls_mode", "username", "password", "from", "subject", "body", + }) + if err != nil { + return err + } + *c = items + return nil +} + +func (c *yamlWebhookConfigs) UnmarshalYAML(node *yaml.Node) error { + items, err := decodeYAMLNotificationConfigs[yamlWebhookConfig](node, "webhook", []string{"url"}) + if err != nil { + return err + } + *c = items + return nil +} + +func (c *yamlTeamsConfigs) UnmarshalYAML(node *yaml.Node) error { + items, err := decodeYAMLNotificationConfigs[yamlTeamsConfig](node, "teams", []string{"url"}) + if err != nil { + return err + } + *c = items + return nil +} + +func decodeYAMLNotificationConfigs[T any](node *yaml.Node, name string, fields []string) ([]*T, error) { + if node.Kind == yaml.ScalarNode && node.Tag == "!!null" { + return nil, nil + } + + if node.Kind == yaml.MappingNode { + if err := validateYAMLNotificationFields(node, name, fields); err != nil { + return nil, err + } + var item T + if err := node.Decode(&item); err != nil { + return nil, err + } + return []*T{&item}, nil + } + + if node.Kind != yaml.SequenceNode { + return nil, fmt.Errorf("notifications.%s must be a mapping or sequence", name) + } + for _, item := range node.Content { + if item.Kind == yaml.ScalarNode && item.Tag == "!!null" { + continue + } + if item.Kind != yaml.MappingNode { + return nil, fmt.Errorf("notifications.%s entries must be mappings", name) + } + if err := validateYAMLNotificationFields(item, name, fields); err != nil { + return nil, err + } + } + + var items []*T + if err := node.Decode(&items); err != nil { + return nil, err + } + return items, nil +} + +func validateYAMLNotificationFields(node *yaml.Node, name string, fields []string) error { + known := make(map[string]struct{}, len(fields)) + for _, field := range fields { + known[field] = struct{}{} + } + for i := 0; i < len(node.Content); i += 2 { + key := node.Content[i].Value + if _, ok := known[key]; !ok { + return fmt.Errorf("field %s not found in notifications.%s", key, name) + } + } + return nil +}