| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo/chat" |
| 5 | "amadan.net/oficina/equipo/internal/equipo/oficina" |
| 6 | "amadan.net/oficina/equipo/internal/equipo/push" |
| 7 | "amadan.net/oficina/equipo/internal/websearch" |
| 8 | "amadan.net/oficina/home/connect" |
| 9 | "errors" |
| 10 | "log/slog" |
| 11 | "net" |
| 12 | "net/http" |
| 13 | "net/url" |
| 14 | "regexp" |
| 15 | "strconv" |
| 16 | "strings" |
| 17 | ) |
| 18 | |
| 19 | type Config struct { |
| 20 | GitHubEnabled bool |
| 21 | // GitHubTransport is a trusted embedding/test transport. URLs remain pinned. |
| 22 | GitHubTransport http.RoundTripper |
| 23 | AgentIssuer string |
| 24 | AgentTeam string |
| 25 | Home *connect.DecodedCredentials |
| 26 | RunnerReleaseDir string |
| 27 | ExecutionEnabled bool |
| 28 | ExecutionMarkerPath string |
| 29 | Origin string |
| 30 | InstanceKey string |
| 31 | Owners []string |
| 32 | AllowLoggedMail bool |
| 33 | // AllowLoggedPush (EQUIPO_ALLOW_LOGGED_PUSH=1) logs pushes instead of |
| 34 | // sending them; like logged mail, only on loopback or staging. |
| 35 | AllowLoggedPush bool |
| 36 | // NotificationActions is off until physical-device unlock and privacy checks. |
| 37 | // Enable explicitly with EQUIPO_NOTIFICATION_ACTIONS=1 after those checks. |
| 38 | NotificationActions bool |
| 39 | // Push is the push transport, built by Pusher on the serve path only |
| 40 | // (main's run) and set there; nil everywhere else: no outbox, no sender. |
| 41 | Push push.Transport |
| 42 | // MailConfigured is true when the platform gave Equipo a way to |
| 43 | // send: CARLOS mail submission (CARLOS_MAIL=submit) or SMTP |
| 44 | // (CARLOS_SMTP_HOST and CARLOS_SMTP_FROM). Mailer builds the sender. |
| 45 | MailConfigured bool |
| 46 | // DatabaseName: a new name is a fresh database (scripts/cutover-thin.sh). |
| 47 | DatabaseName string |
| 48 | // TestPeople are address suffixes of test identities |
| 49 | // (EQUIPO_TEST_PEOPLE, comma-separated; staging's ".invalid"): their |
| 50 | // tasks are listed only to themselves. |
| 51 | TestPeople []string |
| 52 | // AssistantHosts are the origins a person may register as their Base |
| 53 | // host (EQUIPO_ASSISTANT_HOSTS, comma-separated, lowercased): the |
| 54 | // Assistant calls no other address. |
| 55 | AssistantHosts []string |
| 56 | // OficinaConnectOrigin is oficina-connect's origin |
| 57 | // (EQUIPO_OFICINA_CONNECT_ORIGIN) and OficinaConnectClientID the |
| 58 | // public client it registered for this instance |
| 59 | // (EQUIPO_OFICINA_CONNECT_CLIENT_ID), whose redirect is |
| 60 | // EQUIPO_ORIGIN/oficina/connect/callback. Unset, the Assistant has no |
| 61 | // Oficina apps. |
| 62 | OficinaConnectOrigin string |
| 63 | OficinaConnectClientID string |
| 64 | // Abilities are the operator's fixed abilities (EQUIPO_ABILITIES, |
| 65 | // key=off or key=owners, comma-separated): they narrow for everyone |
| 66 | // on the instance (spec 2026-11-26-assistant-abilities §5.4). |
| 67 | Abilities chat.Fixed |
| 68 | // InstancePrefixes is the network this instance is on |
| 69 | // (EQUIPO_INSTANCE_PREFIXES, comma-separated prefixes), which no |
| 70 | // request to the public web may reach; an owner confirms it. |
| 71 | InstancePrefixes string |
| 72 | // WebSearch is the lookups' search backends, keys and monthly caps |
| 73 | // (EQUIPO_WEB_SEARCH, EQUIPO_WEB_SEARCH_FALLBACK, EQUIPO_<NAME>_KEY, |
| 74 | // EQUIPO_<NAME>_MONTHLY_CAP; plan AD3). Keys are never logged. |
| 75 | WebSearch websearch.Config |
| 76 | // MacEngineering (EQUIPO_MAC_ENGINEERING=1) places Engineering for Mac |
| 77 | // work on a Mac worker. Unset, it waits as before, so a release can |
| 78 | // deploy inert. |
| 79 | MacEngineering bool |
| 80 | } |
| 81 | |
| 82 | func LoadConfig(getenv func(string) string) (Config, error) { |
| 83 | cfg := Config{AgentIssuer: getenv("EQUIPO_AGENT_ISSUER"), AgentTeam: getenv("EQUIPO_AGENT_TEAM"), RunnerReleaseDir: getenv("EQUIPO_RUNNER_RELEASE_DIR"), |
| 84 | GitHubEnabled: getenv("EQUIPO_GITHUB") == "1", |
| 85 | ExecutionEnabled: getenv("EQUIPO_EXECUTION_ENABLED") == "1", |
| 86 | MacEngineering: getenv("EQUIPO_MAC_ENGINEERING") == "1", |
| 87 | ExecutionMarkerPath: getenv("EQUIPO_EXECUTION_MARKER_PATH"), |
| 88 | Origin: strings.TrimSpace(getenv("EQUIPO_ORIGIN")), |
| 89 | InstanceKey: strings.TrimSpace(getenv("EQUIPO_INSTANCE_KEY")), |
| 90 | AllowLoggedMail: getenv("EQUIPO_ALLOW_LOGGED_MAIL") == "1", |
| 91 | AllowLoggedPush: getenv("EQUIPO_ALLOW_LOGGED_PUSH") == "1", |
| 92 | NotificationActions: getenv("EQUIPO_NOTIFICATION_ACTIONS") == "1", |
| 93 | DatabaseName: strings.TrimSpace(getenv("EQUIPO_DATABASE")), |
| 94 | } |
| 95 | raw := connect.Credentials{Issuer: getenv("EQUIPO_HOME_ISSUER"), ConsumerID: getenv("EQUIPO_HOME_CONSUMER_ID"), KeysBase64: getenv("EQUIPO_HOME_KEYS_BASE64"), DEKBase64: getenv("EQUIPO_HOME_DEK_BASE64")} |
| 96 | if raw.Issuer != "" || raw.ConsumerID != "" || raw.KeysBase64 != "" || raw.DEKBase64 != "" { |
| 97 | var err error |
| 98 | cfg.Home, err = connect.DecodeCredentials(&raw, cfg.Origin) |
| 99 | if err != nil { |
| 100 | //lint:ignore ST1005 User-facing errors preserve the reviewed sentences in copy.go. |
| 101 | return Config{}, errors.New(copyIDConfig) |
| 102 | } |
| 103 | } |
| 104 | if cfg.DatabaseName == "" { |
| 105 | cfg.DatabaseName = "equipo.db" |
| 106 | } |
| 107 | for _, origin := range strings.Split(getenv("EQUIPO_ASSISTANT_HOSTS"), ",") { |
| 108 | if origin = strings.ToLower(strings.TrimSpace(origin)); origin != "" { |
| 109 | cfg.AssistantHosts = append(cfg.AssistantHosts, origin) |
| 110 | } |
| 111 | } |
| 112 | fixed, err := chat.ParseFixed(getenv("EQUIPO_ABILITIES")) |
| 113 | if err != nil { |
| 114 | // The operator's line says what is wrong; the error is the |
| 115 | // reviewed one for a value that can't be read. |
| 116 | slog.Default().Error("config", "err", err) |
| 117 | //lint:ignore ST1005 User-facing errors preserve the reviewed sentences in copy.go. |
| 118 | return Config{}, errors.New(copyInvalid) |
| 119 | } |
| 120 | cfg.Abilities = fixed |
| 121 | cfg.InstancePrefixes = strings.TrimSpace(getenv("EQUIPO_INSTANCE_PREFIXES")) |
| 122 | cfg.WebSearch = websearch.ParseConfig(getenv) |
| 123 | if _, err := chat.ParseInstancePrefixes(cfg.InstancePrefixes); err != nil { |
| 124 | slog.Default().Error("config", "err", err) |
| 125 | //lint:ignore ST1005 User-facing errors preserve the reviewed sentences in copy.go. |
| 126 | return Config{}, errors.New(copyInvalid) |
| 127 | } |
| 128 | if cfg.OficinaConnectOrigin = strings.TrimSpace(getenv("EQUIPO_OFICINA_CONNECT_ORIGIN")); cfg.OficinaConnectOrigin != "" { |
| 129 | cfg.OficinaConnectClientID = strings.TrimSpace(getenv("EQUIPO_OFICINA_CONNECT_CLIENT_ID")) |
| 130 | } |
| 131 | for _, suffix := range strings.Split(getenv("EQUIPO_TEST_PEOPLE"), ",") { |
| 132 | if suffix = strings.ToLower(strings.TrimSpace(suffix)); suffix != "" { |
| 133 | cfg.TestPeople = append(cfg.TestPeople, suffix) |
| 134 | } |
| 135 | } |
| 136 | if !databaseName.MatchString(cfg.DatabaseName) { |
| 137 | //lint:ignore ST1005 User-facing errors preserve the reviewed sentences in copy.go. |
| 138 | return Config{}, errors.New(copyInvalidDatabase) |
| 139 | } |
| 140 | for _, address := range strings.Split(getenv("EQUIPO_OWNERS"), ",") { |
| 141 | if address = strings.ToLower(strings.TrimSpace(address)); address != "" { |
| 142 | cfg.Owners = append(cfg.Owners, address) |
| 143 | } |
| 144 | } |
| 145 | if err := cfg.validate(); err != nil { |
| 146 | return Config{}, err |
| 147 | } |
| 148 | // Mail is either channel mail.FromCarlos takes: CARLOS mail |
| 149 | // submission, which the agent turns on with CARLOS_MAIL=submit, or |
| 150 | // CARLOS_SMTP_*. With neither, every sign-in link would go to the log. |
| 151 | submit := getenv("CARLOS_MAIL") == "submit" |
| 152 | host := strings.TrimSpace(getenv("CARLOS_SMTP_HOST")) |
| 153 | from := strings.TrimSpace(getenv("CARLOS_SMTP_FROM")) |
| 154 | cfg.MailConfigured = submit || (host != "" && from != "") |
| 155 | if !cfg.MailConfigured && !cfg.AllowLoggedMail { |
| 156 | //lint:ignore ST1005 User-facing errors preserve the reviewed sentences in copy.go. |
| 157 | return Config{}, errors.New(copyMissingSMTP) |
| 158 | } |
| 159 | // Owners are enforced by Bootstrap when the database has no team. |
| 160 | return cfg, nil |
| 161 | } |
| 162 | |
| 163 | func (cfg Config) validate() error { |
| 164 | if _, err := validatedAgentConfig(cfg); err != nil { |
| 165 | return err |
| 166 | } |
| 167 | if strings.TrimSpace(cfg.InstanceKey) == "" { |
| 168 | //lint:ignore ST1005 User-facing errors preserve the reviewed sentences in copy.go. |
| 169 | return errors.New(copyMissingKey) |
| 170 | } |
| 171 | u, err := url.Parse(cfg.Origin) |
| 172 | if err != nil || u.Hostname() == "" || u.User != nil || u.Opaque != "" || |
| 173 | u.Path != "" || u.RawQuery != "" || u.ForceQuery || u.Fragment != "" || strings.Contains(cfg.Origin, "#") { |
| 174 | //lint:ignore ST1005 User-facing errors preserve the reviewed sentences in copy.go. |
| 175 | return errors.New(copyInvalidOrigin) |
| 176 | } |
| 177 | if port := u.Port(); port != "" { |
| 178 | n, err := strconv.Atoi(port) |
| 179 | if err != nil || n < 1 || n > 65535 { |
| 180 | //lint:ignore ST1005 User-facing errors preserve the reviewed sentences in copy.go. |
| 181 | return errors.New(copyInvalidOrigin) |
| 182 | } |
| 183 | } |
| 184 | if strings.HasSuffix(u.Host, ":") { |
| 185 | //lint:ignore ST1005 User-facing errors preserve the reviewed sentences in copy.go. |
| 186 | return errors.New(copyInvalidOrigin) |
| 187 | } |
| 188 | host := strings.ToLower(u.Hostname()) |
| 189 | ip := net.ParseIP(host) |
| 190 | local := host == "localhost" || (ip != nil && ip.IsLoopback()) |
| 191 | if u.Scheme != "https" && !(u.Scheme == "http" && local) { |
| 192 | //lint:ignore ST1005 User-facing errors preserve the reviewed sentences in copy.go. |
| 193 | return errors.New(copyInvalidOrigin) |
| 194 | } |
| 195 | if cfg.AllowLoggedMail && !local && !stagingMailOrigin.MatchString(cfg.Origin) { |
| 196 | //lint:ignore ST1005 User-facing errors preserve the reviewed sentences in copy.go. |
| 197 | return errors.New(copyLoggedMailPublic) |
| 198 | } |
| 199 | if cfg.AllowLoggedPush && !local && !stagingMailOrigin.MatchString(cfg.Origin) { |
| 200 | //lint:ignore ST1005 User-facing errors preserve the reviewed sentences in copy.go. |
| 201 | return errors.New(copyLoggedPushPublic) |
| 202 | } |
| 203 | if cfg.OficinaConnectOrigin != "" { |
| 204 | if _, err := oficina.New(cfg.oficinaConfig(), oficinaStore{}, oficinaSealer{}); err != nil { |
| 205 | //lint:ignore ST1005 User-facing errors preserve the reviewed sentences in copy.go. |
| 206 | return errors.New(copyOficinaConfig) |
| 207 | } |
| 208 | } |
| 209 | return nil |
| 210 | } |
| 211 | |
| 212 | // oficinaConfig is the oficina-connect client this configuration names. |
| 213 | func (cfg Config) oficinaConfig() oficina.Config { |
| 214 | return oficina.Config{Origin: cfg.OficinaConnectOrigin, ClientID: cfg.OficinaConnectClientID, Redirect: cfg.Origin + oficinaCallbackPath} |
| 215 | } |
| 216 | |
| 217 | // Logged sign-in mail is allowed off loopback only for Oficina's staging |
| 218 | // app, so the headless suite can sign in. Production origins never are. |
| 219 | var stagingMailOrigin = regexp.MustCompile(`^https://equipo-staging(-[a-f0-9]{16})?\.bds\.oncarlos\.com$`) |
| 220 | |
| 221 | // A plain file name in the state directory: no paths, no hidden files. |
| 222 | var databaseName = regexp.MustCompile(`^[a-z0-9][a-z0-9-]{0,62}\.db$`) |
| 223 | |