Repository Public
1// Package config loads service configuration from MCC_* environment variables.2// Secrets are read from files (MCC_SECRETS_DIR, mode 0600), never from the repo.3package config45import (6 "fmt"7 "net/url"8 "os"9 "path/filepath"10 "strconv"11 "strings"12 "time"13)1415type Env string1617const (18 Dev Env = "dev"19 Test Env = "test"20 Prod Env = "prod"21)2223type Config struct {24 Env Env25 DatabaseURL string26 SecretsDir string2728 // control-api29 APIListen string30 PublicOrigin string // https://clustr.spboucher.ai31 RPID string // clustr.spboucher.ai32 WebUpstream string // http://127.0.0.1:8733 ; empty = no web proxy33 TrustedProxies []string34 AppleAppIDs []string // <TeamID>.<bundle id>, served in the AASA file3536 // telemetry-ingest37 IngestListen string38 IngestServerName string39 IngestAdvertise []string // host:port endpoints handed to agents at enrollment40 // IngestHealthListen is the plain-HTTP, loopback-only health endpoint of telemetry-ingest.41 IngestHealthListen string42 // IngestSignedListen is the plain-HTTP listener of the signed-request agent43 // transport. It sits behind control-api (reverse proxy of /agent/v1/) and44 // is bound to loopback by default.45 IngestSignedListen string4647 // action-orchestrator48 OrchestratorListen string4950 // push notifications (APNs); all empty = provider inactive51 APNsKeyFile string // .p8 token signing key52 APNsKeyID string53 APNsTeamID string54 APNsTopicIOS string55 APNsTopicMacOS string5657 // sessions58 SessionIdle time.Duration59 SessionAbsolute time.Duration60 AccessTTL time.Duration61 RefreshTTL time.Duration62 StepUpWindow time.Duration6364 // telemetry65 FreshAfter time.Duration // older than this: stale66 OfflineAfter time.Duration // older than this: offline67 RetainRaw time.Duration68 Retain1m time.Duration69 Retain1h time.Duration70}7172func Load() (*Config, error) {73 c := &Config{74 Env: Env(get("MCC_ENV", "dev")),75 DatabaseURL: get("MCC_DATABASE_URL", "postgres://127.0.0.1/mcc_dev?sslmode=disable"),76 SecretsDir: get("MCC_SECRETS_DIR", defaultSecretsDir()),77 APIListen: get("MCC_API_LISTEN", "127.0.0.1:8730"),78 PublicOrigin: strings.TrimRight(get("MCC_PUBLIC_ORIGIN", "http://localhost:8730"), "/"),79 RPID: get("MCC_RP_ID", "localhost"),80 WebUpstream: get("MCC_WEB_UPSTREAM", "http://127.0.0.1:8733"),81 TrustedProxies: list("MCC_TRUSTED_PROXIES", "127.0.0.1"),82 AppleAppIDs: list("MCC_APPLE_APP_IDS", ""),83 IngestListen: get("MCC_INGEST_LISTEN", "127.0.0.1:8731"),84 IngestServerName: get("MCC_INGEST_SERVER_NAME", "ingest.mcc.internal"),85 IngestAdvertise: list("MCC_INGEST_ADVERTISE", "127.0.0.1:8731"),86 IngestHealthListen: get("MCC_INGEST_HEALTH_LISTEN", "127.0.0.1:8741"),87 IngestSignedListen: get("MCC_INGEST_SIGNED_LISTEN", "127.0.0.1:8734"),88 OrchestratorListen: get("MCC_ORCHESTRATOR_LISTEN", "127.0.0.1:8732"),89 APNsKeyFile: get("MCC_APNS_KEY_FILE", ""),90 APNsKeyID: get("MCC_APNS_KEY_ID", ""),91 APNsTeamID: get("MCC_APNS_TEAM_ID", ""),92 APNsTopicIOS: get("MCC_APNS_TOPIC_IOS", ""),93 APNsTopicMacOS: get("MCC_APNS_TOPIC_MACOS", ""),94 }95 var err error96 durs := []struct {97 dst *time.Duration98 key string99 def time.Duration100 }{101 {&c.SessionIdle, "MCC_SESSION_IDLE", 30 * time.Minute},102 {&c.SessionAbsolute, "MCC_SESSION_ABSOLUTE", 12 * time.Hour},103 {&c.AccessTTL, "MCC_ACCESS_TTL", 10 * time.Minute},104 {&c.RefreshTTL, "MCC_REFRESH_TTL", 30 * 24 * time.Hour},105 {&c.StepUpWindow, "MCC_STEPUP_WINDOW", 5 * time.Minute},106 {&c.FreshAfter, "MCC_FRESH_AFTER", 30 * time.Second},107 {&c.OfflineAfter, "MCC_OFFLINE_AFTER", 120 * time.Second},108 {&c.RetainRaw, "MCC_RETAIN_RAW", 7 * 24 * time.Hour},109 {&c.Retain1m, "MCC_RETAIN_1M", 30 * 24 * time.Hour},110 {&c.Retain1h, "MCC_RETAIN_1H", 400 * 24 * time.Hour},111 }112 for _, d := range durs {113 if *d.dst, err = dur(d.key, d.def); err != nil {114 return nil, err115 }116 }117 return c, c.validate()118}119120func (c *Config) validate() error {121 switch c.Env {122 case Dev, Test, Prod:123 default:124 return fmt.Errorf("MCC_ENV must be dev, test or prod, got %q", c.Env)125 }126 u, err := url.Parse(c.PublicOrigin)127 if err != nil || u.Host == "" || u.Path != "" {128 return fmt.Errorf("MCC_PUBLIC_ORIGIN must be an origin (scheme://host[:port]), got %q", c.PublicOrigin)129 }130 if c.Env == Prod {131 if u.Scheme != "https" {132 return fmt.Errorf("MCC_PUBLIC_ORIGIN must be https in prod")133 }134 if c.RPID == "localhost" {135 return fmt.Errorf("MCC_RP_ID must be set in prod")136 }137 }138 // The RP ID must be the origin host or a registrable suffix of it.139 if h := u.Hostname(); h != c.RPID && !strings.HasSuffix(h, "."+c.RPID) {140 return fmt.Errorf("MCC_RP_ID %q is not a suffix of the origin host %q", c.RPID, h)141 }142 if c.OfflineAfter <= c.FreshAfter {143 return fmt.Errorf("MCC_OFFLINE_AFTER must exceed MCC_FRESH_AFTER")144 }145 return nil146}147148// Secure reports whether cookies must carry the Secure attribute and __Host- prefix.149func (c *Config) Secure() bool { return strings.HasPrefix(c.PublicOrigin, "https://") }150151// SecretPath returns the path of a named secret file inside SecretsDir.152func (c *Config) SecretPath(name string) string { return filepath.Join(c.SecretsDir, name) }153154func defaultSecretsDir() string {155 home, _ := os.UserHomeDir()156 return filepath.Join(home, ".config", "mcc")157}158159func get(k, def string) string {160 if v := strings.TrimSpace(os.Getenv(k)); v != "" {161 return v162 }163 return def164}165166func list(k, def string) []string {167 var out []string168 for _, p := range strings.Split(get(k, def), ",") {169 if p = strings.TrimSpace(p); p != "" {170 out = append(out, p)171 }172 }173 return out174}175176func dur(k string, def time.Duration) (time.Duration, error) {177 v := strings.TrimSpace(os.Getenv(k))178 if v == "" {179 return def, nil180 }181 if n, err := strconv.Atoi(v); err == nil {182 return time.Duration(n) * time.Second, nil183 }184 d, err := time.ParseDuration(v)185 if err != nil || d <= 0 {186 return 0, fmt.Errorf("%s: invalid duration %q", k, v)187 }188 return d, nil189}190