// Package config reads Base's settings from BASE_* environment variables. package config import ( "crypto/elliptic" "encoding/base64" "errors" "fmt" "net" "net/url" "regexp" "strings" "time" _ "time/tzdata" // the release binary must not depend on host zoneinfo "amadan.net/oficina/home/connect" "amadan.net/rastrillo/rastrillo/assertion" "amadan.net/rastrillo/rastrillo/crypto" "amadan.net/oficina/base/internal/catalog" ) // A host label is "-", at most 32 characters. App ids in the // catalogue are at most 12 characters, so the slug gets what is left. const ( maxLabel = 32 maxAppID = 12 maxSlug = maxLabel - 1 - maxAppID ) var slugRE = regexp.MustCompile(`^[a-z0-9]([a-z0-9-]*[a-z0-9])?$`) type Config struct { Origin string Name string Slug string Owner string AppsDomain string Timezone *time.Location PlatformConsole string PlatformAccount string InstanceToken string OperateToken string AssertionKey *crypto.Keypair AllowLoggedMail bool // InstanceKey seals sign-in state (BASE_INSTANCE_KEY). Keep it secret and // stable: changing it invalidates links and sign-ins in flight. InstanceKey string // EquipoOrigin is the Equipo instance whose coordinator answers "Ask // Base anything" (BASE_EQUIPO_ORIGIN). Empty leaves the chat off. EquipoOrigin string // OficinaApps are the team's Oficina apps, in the order Home lists // them (BASE_OFICINA_APPS). Empty leaves them off Home. OficinaApps []OficinaApp // Home turns on sign-in with Oficina ID. Nil (no BASE_HOME_* settings) // leaves it off; LoadHome sets it. Home *connect.DecodedCredentials } // Load reads the configuration. getenv is os.Getenv in production. func Load(getenv func(string) string) (Config, error) { get := func(k string) string { return strings.TrimSpace(getenv("BASE_" + k)) } cfg := Config{ Origin: strings.TrimRight(get("ORIGIN"), "/"), Name: get("NAME"), Slug: get("SLUG"), Owner: strings.ToLower(get("OWNER")), AppsDomain: strings.ToLower(get("APPS_DOMAIN")), PlatformConsole: strings.TrimRight(get("PLATFORM_CONSOLE"), "/"), PlatformAccount: get("PLATFORM_ACCOUNT"), InstanceToken: get("PLATFORM_INSTANCE_TOKEN"), OperateToken: get("PLATFORM_OPERATE_TOKEN"), AllowLoggedMail: get("ALLOW_LOGGED_MAIL") == "1", InstanceKey: get("INSTANCE_KEY"), } if cfg.Origin == "" { return Config{}, errors.New("BASE_ORIGIN is required") } if cfg.Owner == "" { return Config{}, errors.New("BASE_OWNER is required") } if cfg.Name == "" { cfg.Name = "Base" } if cfg.Slug == "" { cfg.Slug = "base" } if len(cfg.Slug) > maxSlug || !slugRE.MatchString(cfg.Slug) { return Config{}, fmt.Errorf("BASE_SLUG %q must be 1 to %d characters of a-z, 0-9 and -, not starting or ending with -", cfg.Slug, maxSlug) } local, err := originIsLocal(cfg.Origin) if err != nil { return Config{}, err } if cfg.AllowLoggedMail && !local { return Config{}, errors.New("BASE_ALLOW_LOGGED_MAIL=1 is only allowed with a loopback BASE_ORIGIN") } tz := get("TIMEZONE") if tz == "" { tz = "Europe/Dublin" } if cfg.Timezone, err = time.LoadLocation(tz); err != nil { return Config{}, fmt.Errorf("BASE_TIMEZONE: %w", err) } if raw := get("ASSERTION_KEY"); raw != "" { b, err := base64.StdEncoding.DecodeString(raw) if err != nil { return Config{}, fmt.Errorf("BASE_ASSERTION_KEY is not base64: %w", err) } if cfg.AssertionKey, err = crypto.UnmarshalKeypair(b); err != nil { return Config{}, fmt.Errorf("BASE_ASSERTION_KEY: %w", err) } } if cfg.EquipoOrigin, err = equipoOrigin(get("EQUIPO_ORIGIN")); err != nil { return Config{}, err } if cfg.OficinaApps, err = parseOficinaApps(get("OFICINA_APPS")); err != nil { return Config{}, err } if cfg.AppsDomain != "" { if err := checkAssertionOrigins(cfg, local); err != nil { return Config{}, err } } return cfg, nil } // checkAssertionOrigins holds, with installs configured, Base's origin and // each catalogue app's origin to the rules rastrillo/assertion applies to an // assertion's issuer and audience (https, a DNS hostname, no :443), by asking // it to build a verifier for each pair. A loopback http Base origin is the // local-run mode and is not checked; handoffs cannot work there. func checkAssertionOrigins(cfg Config, local bool) error { issuer := cfg.Origin if local && strings.HasPrefix(issuer, "http://") { issuer = "https://base.invalid" // stands in, so the app origins are still checked } else if _, err := assertion.NewVerifier(issuer, "https://app.invalid", probeKeys); err != nil { return fmt.Errorf("BASE_ORIGIN %q cannot issue assertions: it must be https with a DNS hostname and no :443", cfg.Origin) } for _, e := range catalog.All() { app := "https://" + cfg.Slug + "-" + e.ID + "." + cfg.AppsDomain if _, err := assertion.NewVerifier(issuer, app, probeKeys); err != nil { return fmt.Errorf("BASE_APPS_DOMAIN %q gives %s the origin %q, which cannot receive assertions: it must be a DNS hostname with no :443", cfg.AppsDomain, e.ID, app) } } return nil } // probeKeys is a key set the verifier accepts (P-256's base point), for // checking origins only; nothing is ever verified with it. var probeKeys = func() map[string][]byte { p := elliptic.P256().Params() pub := make([]byte, 65) pub[0] = 4 p.Gx.FillBytes(pub[1:33]) p.Gy.FillBytes(pub[33:]) return map[string][]byte{"probe": pub} }() // originIsLocal checks BASE_ORIGIN is https, or http on a loopback host, // and reports whether it is loopback. func originIsLocal(origin string) (bool, error) { u, err := url.Parse(origin) if err != nil || u.Host == "" || u.Path != "" || u.User != nil || u.RawQuery != "" || u.Fragment != "" { return false, fmt.Errorf("BASE_ORIGIN %q must be a scheme and host only", origin) } host := strings.ToLower(u.Hostname()) ip := net.ParseIP(host) local := host == "localhost" || (ip != nil && ip.IsLoopback()) if ip != nil && u.Scheme == "https" { return false, fmt.Errorf("BASE_ORIGIN %q must use a hostname, not an IP address", origin) } if u.Scheme != "https" && !(u.Scheme == "http" && local) { return false, fmt.Errorf("BASE_ORIGIN %q must be https (http only on loopback)", origin) } return local, nil } // equipoOrigin checks BASE_EQUIPO_ORIGIN: empty, or a scheme and host only, // https (http only on a loopback host, for a local Equipo). func equipoOrigin(raw string) (string, error) { raw = strings.TrimRight(raw, "/") if raw == "" { return "", nil } if _, err := originIsLocal(raw); err != nil { return "", fmt.Errorf("BASE_EQUIPO_ORIGIN %q must be an https origin: a scheme and host only (http only on loopback)", raw) } return raw, nil }