Add admin and customer interfaces, orders, signed activation and leases, protected package delivery, keyring verification, migrations, security controls, tests and deployment documentation. Co-Authored-By: Craft Agent <agents-noreply@craft.do>
99 lines
3.1 KiB
Go
99 lines
3.1 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Config struct {
|
|
HTTPAddr string
|
|
TrustedProxies []string
|
|
ReadHeaderTimeout time.Duration
|
|
ReadTimeout time.Duration
|
|
WriteTimeout time.Duration
|
|
IdleTimeout time.Duration
|
|
ShutdownTimeout time.Duration
|
|
SigningPrivateKeyPEM string
|
|
}
|
|
|
|
func Load(getenv func(string) string) (Config, error) {
|
|
c := Config{HTTPAddr: "127.0.0.1:18080"}
|
|
if v := getenv("HTTP_ADDR"); v != "" {
|
|
c.HTTPAddr = v
|
|
}
|
|
_, port, err := net.SplitHostPort(c.HTTPAddr)
|
|
if err != nil {
|
|
return Config{}, fmt.Errorf("HTTP_ADDR must be host:port")
|
|
}
|
|
p, err := strconv.Atoi(port)
|
|
if err != nil || p < 1 || p > 65535 {
|
|
return Config{}, fmt.Errorf("HTTP_ADDR port must be between 1 and 65535")
|
|
}
|
|
for _, setting := range []struct {
|
|
name string
|
|
value *time.Duration
|
|
fallback time.Duration
|
|
}{
|
|
{"HTTP_READ_HEADER_TIMEOUT", &c.ReadHeaderTimeout, 5 * time.Second},
|
|
{"HTTP_READ_TIMEOUT", &c.ReadTimeout, 30 * time.Second},
|
|
{"HTTP_WRITE_TIMEOUT", &c.WriteTimeout, 30 * time.Second},
|
|
{"HTTP_IDLE_TIMEOUT", &c.IdleTimeout, 60 * time.Second},
|
|
{"HTTP_SHUTDOWN_TIMEOUT", &c.ShutdownTimeout, 10 * time.Second},
|
|
} {
|
|
*setting.value = setting.fallback
|
|
if v := getenv(setting.name); v != "" {
|
|
d, err := time.ParseDuration(v)
|
|
if err != nil || d <= 0 || d > 5*time.Minute {
|
|
return Config{}, fmt.Errorf("%s must be a duration greater than zero and at most 5m", setting.name)
|
|
}
|
|
*setting.value = d
|
|
}
|
|
}
|
|
if value := getenv("HTTP_TRUSTED_PROXY_CIDRS"); value != "" {
|
|
for _, value := range strings.Split(value, ",") {
|
|
value = strings.TrimSpace(value)
|
|
_, network, err := net.ParseCIDR(value)
|
|
if err != nil {
|
|
return Config{}, fmt.Errorf("trusted proxies must be explicit CIDRs")
|
|
}
|
|
ones, _ := network.Mask.Size()
|
|
if ones == 0 {
|
|
return Config{}, fmt.Errorf("trust-all proxies are forbidden")
|
|
}
|
|
c.TrustedProxies = append(c.TrustedProxies, value)
|
|
}
|
|
}
|
|
if value := getenv("SESSION_COOKIE_SECURE"); value != "" && value != "true" && value != "false" {
|
|
return Config{}, fmt.Errorf("SESSION_COOKIE_SECURE must be true or false")
|
|
}
|
|
c.SigningPrivateKeyPEM = getenv("LICENSE_SIGNING_PRIVATE_KEY_PEM")
|
|
if path := getenv("LICENSE_SIGNING_PRIVATE_KEY_FILE"); path != "" {
|
|
if c.SigningPrivateKeyPEM != "" {
|
|
return Config{}, fmt.Errorf("only one signing key source may be configured")
|
|
}
|
|
if !filepath.IsAbs(path) {
|
|
return Config{}, fmt.Errorf("LICENSE_SIGNING_PRIVATE_KEY_FILE must be absolute")
|
|
}
|
|
data, err := os.ReadFile(path)
|
|
if err != nil || len(data) == 0 || len(data) > 16384 {
|
|
return Config{}, fmt.Errorf("signing key file must be readable and between 1 and 16384 bytes")
|
|
}
|
|
c.SigningPrivateKeyPEM = string(data)
|
|
}
|
|
if root := getenv("PACKAGE_STORAGE_ROOT"); root != "" {
|
|
if !filepath.IsAbs(root) {
|
|
return Config{}, fmt.Errorf("PACKAGE_STORAGE_ROOT must be absolute")
|
|
}
|
|
info, err := os.Stat(root)
|
|
if err != nil || !info.IsDir() {
|
|
return Config{}, fmt.Errorf("PACKAGE_STORAGE_ROOT must be an existing directory")
|
|
}
|
|
}
|
|
return c, nil
|
|
}
|