oficina / memoria Public

Clone
git clone https://amadan.net/oficina/memoria

Plain git — no account needed to clone.

Download

Download this file

1package memoria
2
3import (
4 "encoding/base64"
5 "errors"
6 "fmt"
7 "net/http"
8
9 "amadan.net/oficina/home/connect"
10 "amadan.net/oficina/home/roster"
11 "amadan.net/oficina/home/teammode"
12
13 "amadan.net/oficina/memoria/internal/memoria/seal"
14)
15
16// Config is everything memoria needs from platform config.
17//
18// This is a type with a constructor, rather than a run of os.Getenv calls
19// in main, for one reason: the refusals below are a safety property and a
20// safety property needs a test. An earlier version of this validation
21// lived inline in main.go and was "checked" by grepping the source for
22// os.Exit near each variable name — which passed even when the refusal
23// was deleted, because a neighbouring error path supplied the os.Exit the
24// grep was looking for. A guard that cannot fail is not a guard.
25type Config struct {
26 Origin string
27 InstanceKey string
28 Owners []string
29 Sealer *seal.Sealer
30 Home *connect.DecodedCredentials
31 AgentIssuer string
32 AgentTeam string
33 // AllowLoggedMail permits booting with no SMTP, which makes
34 // rastrillo substitute mail.Logged — and mail.Logged writes the
35 // magic link, a live credential, into the process log at Warn.
36 // `carlos logs` is platform-held storage we cannot grep, so that is
37 // a credential we cannot retract. Opt in explicitly for local dev.
38 AllowLoggedMail bool
39 // MailConfigured is true when the platform gave memoria a way to
40 // send: CARLOS mail submission or SMTP. Mailer builds the sender.
41 MailConfigured bool
42
43 // AI is the team owner's opt-in to the model layer (decision 4). With
44 // it off, nothing is ever sent to a provider and §8.9 does nothing.
45 AI bool
46 // TensorXKey is the provider credential. Required when AI is on,
47 // ignored when it is off. Never logged, never in an error.
48 TensorXKey string
49
50 // Team is roster team mode's configuration: the five MEMORIA_TEAM_*
51 // variables, nil when all five are unset (team_config.go). It needs
52 // Home, and it makes MEMORIA_OWNERS optional: the roster decides who
53 // may use the instance, and the owners list only admits a local
54 // sign-in. Verified in AppWith, against Home and the origin.
55 Team *teammode.Config
56 // teamTransport and teamClock are the roster's test seams: nil in
57 // production, where teammode uses its own client and the real clock.
58 teamTransport http.RoundTripper
59 teamClock roster.Clock
60}
61
62// ErrMissing is returned when required platform config is absent.
63var ErrMissing = errors.New("memoria: required configuration is missing")
64
65// LoadConfig reads and validates configuration. getenv is a parameter so
66// a test can supply an environment without mutating the process's.
67//
68// Every failure here is fatal by design. There is deliberately no
69// generated-fallback DEK: a fallback would seal this boot's corpus under
70// a key the next boot cannot derive, losing the data quietly instead of
71// refusing loudly. And there is no default owner list, because an empty
72// admission gate on a public hostname admits anyone who can receive mail.
73func LoadConfig(getenv func(string) string) (Config, error) {
74 var c Config
75
76 dekRaw := getenv("MEMORIA_DEK")
77 if dekRaw == "" {
78 return c, fmt.Errorf("%w: MEMORIA_DEK", ErrMissing)
79 }
80 dek, err := base64.StdEncoding.DecodeString(dekRaw)
81 if err != nil {
82 return c, fmt.Errorf("memoria: MEMORIA_DEK is not valid base64: %w", err)
83 }
84 sealer, err := seal.New(dek)
85 if err != nil {
86 return c, fmt.Errorf("memoria: MEMORIA_DEK rejected: %w", err)
87 }
88 c.Sealer = sealer
89
90 if c.InstanceKey = getenv("MEMORIA_INSTANCE_KEY"); c.InstanceKey == "" {
91 return c, fmt.Errorf("%w: MEMORIA_INSTANCE_KEY", ErrMissing)
92 }
93
94 // Team mode first, because it decides whether MEMORIA_OWNERS is
95 // required. One of the five set is a configuration, and an invalid
96 // one; AppWith's Verify refuses it, so it never falls back to the
97 // owners list.
98 c.Team = teammode.ConfigFromLookup("MEMORIA", getenv)
99
100 // In team mode the roster is the admission gate and the owners list
101 // only admits a local sign-in, so an empty one is valid there and
102 // means "Home sign-in only" (Alta's shape). Outside it, the list is
103 // the gate, and an empty gate admits anyone who can receive mail.
104 if c.Owners = ParseOwners(getenv("MEMORIA_OWNERS")); len(c.Owners) == 0 && c.Team == nil {
105 return c, fmt.Errorf("%w: MEMORIA_OWNERS (no admission gate)", ErrMissing)
106 }
107
108 // Refuse to log credentials by default.
109 //
110 // The prefix is CARLOS, not MEMORIA: rastrillo's mail.FromEnv takes
111 // the prefix as an argument and the suite passes "CARLOS", so the
112 // platform config is CARLOS_SMTP_*. An earlier version of this check
113 // read MEMORIA_SMTP_HOST, which no deployment sets — so it would have
114 // refused to boot on a correctly configured instance, and the obvious
115 // fix under deadline would have been to delete the check.
116 //
117 // Both HOST and FROM, because mail.FromEnv falls back to the logging
118 // sender unless it has both. Checking only HOST would let a
119 // half-configured instance log links while this function reported
120 // everything fine.
121 //
122 // Mail is either channel rastrillo's mail.FromCarlos takes (Mailer,
123 // mailer.go): CARLOS mail submission, which the agent turns on with
124 // CARLOS_MAIL=submit beside its control socket, or CARLOS_SMTP_*.
125 c.AllowLoggedMail = getenv("MEMORIA_ALLOW_LOGGED_MAIL") == "1"
126 submit := getenv("CARLOS_MAIL") == "submit"
127 smtp := getenv("CARLOS_SMTP_HOST") != "" && getenv("CARLOS_SMTP_FROM") != ""
128 c.MailConfigured = submit || smtp
129 if !c.MailConfigured && !c.AllowLoggedMail {
130 return c, fmt.Errorf("%w: CARLOS_MAIL=submit, or CARLOS_SMTP_HOST and CARLOS_SMTP_FROM (or MEMORIA_ALLOW_LOGGED_MAIL=1 to log sign-in links instead, which puts a live credential in a log nobody can delete)", ErrMissing)
131 }
132
133 // The model layer: off unless the owner says "on", in exactly those
134 // words (decision 4 — opt-in per team).
135 //
136 // **Only "on" and "" are accepted.** A typo — "yes", "true", "On" —
137 // is a refusal, not a silent off: an owner who meant to switch AI on
138 // and got nothing would be told nothing, and an owner who meant to
139 // switch it OFF and wrote "false" must not be read as meaning on.
140 // Refusing both is the only reading that cannot be wrong.
141 //
142 // And a key with AI off does NOT turn AI on. Presence of a
143 // credential is not consent; the switch is.
144 switch ai := getenv("MEMORIA_AI"); ai {
145 case "":
146 case "on":
147 c.AI = true
148 // MEMORIA_TENSORX_KEY, because that is the name the credential was
149 // already given on the platform (set by hand for the 2026-09-05
150 // live-key checks). This code first read MEMORIA_TENSORX_API_KEY,
151 // which would have meant a second copy of the key under a new name —
152 // and a platform secret cannot be read back to make one.
153 if c.TensorXKey = getenv("MEMORIA_TENSORX_KEY"); c.TensorXKey == "" {
154 return c, fmt.Errorf("%w: MEMORIA_TENSORX_KEY (MEMORIA_AI=on names a provider but gives no credential for it)", ErrMissing)
155 }
156 default:
157 return c, fmt.Errorf("memoria: MEMORIA_AI must be \"on\" or unset; refusing to guess what %q means", ai)
158 }
159
160 // Origin is the only one with a default, and only because the
161 // default is a loopback address that cannot be reached from
162 // anywhere else. It decides the CSRF yardstick and the cookie
163 // attributes, so a silent production default would be a real bug —
164 // hence the caller logs a warning when it is used.
165 if c.Origin = getenv("MEMORIA_ORIGIN"); c.Origin == "" {
166 c.Origin = "http://localhost:8080"
167 }
168 home := connect.Credentials{Issuer: getenv("MEMORIA_HOME_ISSUER"), ConsumerID: getenv("MEMORIA_HOME_CONSUMER_ID"), KeysBase64: getenv("MEMORIA_HOME_KEYS_BASE64"), DEKBase64: getenv("MEMORIA_HOME_DEK_BASE64")}
169 if home.Issuer != "" || home.ConsumerID != "" || home.KeysBase64 != "" || home.DEKBase64 != "" {
170 // The hub directory "All apps" links to: unprefixed, the name and
171 // value Home itself reads, as connect.CredentialsFromEnv does. It
172 // is optional and enables nothing, so it does not count toward
173 // "Home is configured"; DecodeCredentials validates it.
174 home.Directory = getenv("HOME_DIRECTORY_ORIGIN")
175 c.Home, err = connect.DecodeCredentials(&home, c.Origin)
176 if err != nil {
177 return c, err
178 }
179 }
180 c.AgentIssuer, c.AgentTeam = getenv("MEMORIA_AGENT_ISSUER"), getenv("MEMORIA_AGENT_TEAM")
181 if (c.AgentIssuer != "" || c.AgentTeam != "") && (c.AgentIssuer == "" || c.AgentTeam == "" || c.Home == nil) {
182 return c, errors.New("memoria: agent access requires MEMORIA_AGENT_ISSUER, MEMORIA_AGENT_TEAM and Home")
183 }
184 // Team mode exists only beside Home: the roster names Home subjects,
185 // and nobody could sign in to be asked about.
186 if c.Team != nil && c.Home == nil {
187 return c, fmt.Errorf("%w: MEMORIA_HOME_* (the MEMORIA_TEAM_* variables are set, and team mode needs Home sign-in)", ErrMissing)
188 }
189 return c, nil
190}
191
192// UsedDefaultOrigin reports whether the origin was defaulted, so main can
193// say so out loud.
194func (c Config) UsedDefaultOrigin() bool { return c.Origin == "http://localhost:8080" }
195