oficina / alta Public

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

Plain git — no account needed to clone.

Download

Download this file

1// This file is the activation gate: an adopted app's re-observation
2// immediately before Home activates it. Nothing activates without the
3// journal's complete evidence and a fresh read that still matches it.
4package adopt
5
6import (
7 "context"
8 "crypto/sha256"
9 "encoding/base64"
10 "encoding/hex"
11 "errors"
12 "net/http"
13 "regexp"
14 "strings"
15 "time"
16
17 "amadan.net/oficina/alta/carlosop"
18 "amadan.net/oficina/alta/provision"
19)
20
21// Refusal is the gate's refusal: fixed codes, sorted, carrying key names,
22// layer names and identity fields only.
23type Refusal struct{ Codes []string }
24
25func (r *Refusal) Error() string {
26 return "adopt: observation refused: " + strings.Join(r.Codes, ", ")
27}
28
29// RefusalCodes answers the codes to provision's engine, which parks an
30// adopted app's activation with them (provision.ObserverRefusal).
31func (r *Refusal) RefusalCodes() []string { return append([]string(nil), r.Codes...) }
32
33var _ provision.ObserverRefusal = (*Refusal)(nil)
34
35func refuse(codes ...string) error { return &Refusal{Codes: sortedSet(codes)} }
36
37// appVersion is the app_version Home's directory.activate-consumer
38// admits, as provision's step 10 mirrors it (Home 0cee9ba,
39// internal/operator/verification.go observedVersion).
40var appVersion = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._+-]{0,127}$`)
41
42// artifactShape is a reviewed artifact SHA-256: 64 lowercase hex.
43var artifactShape = regexp.MustCompile(`^[0-9a-f]{64}$`)
44
45// Observer is the CARLOS half's provision.Observer. Artifacts holds each
46// app's reviewed artifact SHA-256 as 64 lowercase hex: the SHA-256 of the
47// release name Paul reviewed, Alta's artifact scheme (provision
48// artifactDigest). Now stamps the fresh delivery; it is no age limit.
49type Observer struct {
50 C *carlosop.Client
51 H *http.Client
52 S *Store
53 Artifacts map[string]string
54 Now func() time.Time
55}
56
57var _ provision.Observer = (*Observer)(nil)
58
59func (o *Observer) now() time.Time {
60 if o.Now == nil {
61 return time.Now().UTC()
62 }
63 return o.Now().UTC()
64}
65
66// Observe re-observes app and answers its activation evidence. It reads
67// and probes; it writes nothing, to CARLOS or the journal. It refuses
68// (a *Refusal) unless every one of these holds:
69//
70// - evidence: the pre-plan-a Home preflight, the baseline, a landed
71// write, the after snapshot and the delivery check (missing-<x>); no
72// rollback (rollback-recorded);
73// - the write sent Issuer and teamSQID (issuer-mismatch,
74// team-mismatch);
75// - Plan C: S.CanaryGate passes (canary-not-active);
76// - the app's reviewed artifact SHA-256 is configured
77// (artifact-unreviewed);
78// - identity: a fresh read's equals the baseline's, the after
79// snapshot's and the delivery check's (identity-<code>);
80// - preservation: Preserved(baseline, after) is empty
81// (after-not-preserved), and so is Preserved(baseline, fresh)
82// (drift-<code>);
83// - config: the fresh read equals the after snapshot, every key's
84// digest, kind and source and every layer's version
85// (config-<code>); no team-mode key (team-mode-<KEY>); the Home
86// issuer and consumer are still the ones the app must trust
87// (home-issuer-mismatch, home-consumer-mismatch); both agent
88// keys are plain vars from the write layer carrying the sent
89// digests (agent-key-<KEY>);
90// - the delivery check is current: its after version is the fresh
91// write layer's, and its delivered version and header are the fresh
92// ones (delivery-check-superseded). There is no wall-clock limit;
93// - delivery: the fresh probe passes Delivered against the baseline
94// (not-delivered); or, when the delivery check records an approved
95// redeploy, it is that redeploy, from the baseline's release to the
96// check's, under the approved artifact, and the fresh probe passes
97// redelivered (not-delivered: checkDelivered); the version body and X-Carlos-Version name the
98// same release, SameRelease (version-header-mismatch), and Home
99// admits the body
100// (version-unacceptable);
101// - artifact: the SHA-256 of the served release name (X-Carlos-Version)
102// is the reviewed one (artifact-mismatch).
103//
104// Stored evidence is checked before anything is read. A read that fails
105// is an error, not a refusal.
106func (o *Observer) Observe(ctx context.Context, app, teamSQID string) (provision.Observation, error) {
107 if o == nil || o.C == nil || o.H == nil || o.S == nil {
108 return provision.Observation{}, errors.New("adopt: the observer needs a console, a client and the journal")
109 }
110 a, ok := provision.AppBySlug(app)
111 if !ok {
112 return provision.Observation{}, refuse("unknown-app")
113 }
114 if !sqidShape.MatchString(teamSQID) {
115 return provision.Observation{}, refuse("team-not-sqid")
116 }
117 ev, err := o.evidence(ctx, a, teamSQID)
118 if err != nil {
119 return provision.Observation{}, err
120 }
121
122 fresh, err := Observe(ctx, o.C, o.H, a)
123 if err != nil {
124 return provision.Observation{}, err
125 }
126 fresh.Delivery.At = o.now()
127
128 var codes []string
129 addAll := func(prefix string, cs ...string) {
130 for _, c := range cs {
131 codes = append(codes, prefix+c)
132 }
133 }
134 add := func(code string) { codes = append(codes, code) }
135 keys := AgentKeys(a)
136 addAll("identity-", SameIdentity(ev.base.Identity, fresh.Identity)...)
137 addAll("identity-", SameIdentity(ev.after.Identity, fresh.Identity)...)
138 addAll("identity-", SameIdentity(ev.check.Identity, fresh.Identity)...)
139 if len(Preserved(ev.base, ev.after, keys)) > 0 {
140 add("after-not-preserved")
141 }
142 addAll("drift-", Preserved(ev.base, fresh, keys)...)
143 addAll("config-", sameConfig(ev.after, fresh)...)
144 addAll("team-mode-", TeamModeKeys(a, fresh.Vars)...)
145 addAll("", homeCodes(a, fresh)...)
146 for _, k := range keys {
147 v := findVar(fresh.Vars, k)
148 if v == nil || v.Kind != "env" || v.From != layerName(ev.base.WriteLayer) || v.Digest != carlosop.EnvDigest(ev.write.Sent[k]) {
149 add("agent-key-" + k)
150 }
151 }
152 chk := ev.check
153 if chk.AfterVersion != fresh.Layer.Version || chk.Delivery.Version != fresh.Delivery.Version || chk.Delivery.Header != fresh.Delivery.Header {
154 add("delivery-check-superseded")
155 }
156 d := fresh.Delivery
157 if checkDelivered(a, ev.base.Delivery, chk, ev.artifact, d) != nil {
158 add("not-delivered")
159 }
160 if !SameRelease(d.Version, d.Header) {
161 add("version-header-mismatch")
162 }
163 if !appVersion.MatchString(d.Version) {
164 add("version-unacceptable")
165 }
166 // The served release name is X-Carlos-Version, the release the edge
167 // served from. Controller amendment (Task 3 ruling, 2026-11-16):
168 // ChannelDigest is dropped and artifact_digest is Alta's existing
169 // scheme, base64url SHA-256 of the served release name, as ordinary
170 // activation sends it (provision/steps.go:700 records serving, :780 artifactDigest hashes
171 // it, :816 stepActivate sends it).
172 sum := sha256.Sum256([]byte(d.Header))
173 artifact := hex.EncodeToString(sum[:])
174 if artifact != ev.artifact {
175 add("artifact-mismatch")
176 }
177 if len(codes) > 0 {
178 return provision.Observation{}, refuse(codes...)
179 }
180
181 return provision.Observation{
182 AppVersion: d.Version,
183 ArtifactDigest: base64.RawURLEncoding.EncodeToString(sum[:]),
184 EnvironmentBinding: EnvironmentBinding(BindingInput{
185 Before: ev.base.Identity, After: ev.after.Identity,
186 BeforeVars: ev.base.Vars, AfterVars: ev.after.Vars,
187 BeforeLayers: ev.base.Layers, AfterLayers: ev.after.Layers,
188 TeamSQID: teamSQID, Issuer: Issuer, ArtifactSHA256: artifact, Version: d.Version,
189 BeforeDelivery: ev.base.Delivery, AfterDelivery: ev.after.Delivery, NowDelivery: d,
190 Check: chk,
191 }),
192 }, nil
193}
194
195// gateEvidence is the journal's records for one app's activation.
196type gateEvidence struct {
197 base, after Snapshot
198 write WriteRecord
199 check DeliveryCheck
200 artifact string
201}
202
203// evidence reads and checks the stored evidence, before anything is
204// read from CARLOS or the app.
205func (o *Observer) evidence(ctx context.Context, a provision.App, teamSQID string) (gateEvidence, error) {
206 var ev gateEvidence
207 var missing []string
208 have := func(ok bool, err error, what string) error {
209 if err == nil && !ok {
210 missing = append(missing, "missing-"+what)
211 }
212 return err
213 }
214 _, ok, err := o.S.HomePreflight(ctx, PhasePrePlanA)
215 if err := have(ok, err, "home-preflight"); err != nil {
216 return ev, err
217 }
218 base, ok, err := o.S.Baseline(ctx, a.Slug)
219 if err := have(ok, err, "baseline"); err != nil {
220 return ev, err
221 }
222 write, ok, err := o.S.Write(ctx, a.Slug)
223 if err := have(ok && write.State == "landed", err, "write"); err != nil {
224 return ev, err
225 }
226 after, ok, err := o.S.After(ctx, a.Slug)
227 if err := have(ok, err, "after"); err != nil {
228 return ev, err
229 }
230 check, ok, err := o.S.DeliveryCheck(ctx, a.Slug)
231 if err := have(ok, err, "delivery-check"); err != nil {
232 return ev, err
233 }
234 if len(missing) > 0 {
235 return ev, refuse(missing...)
236 }
237 if _, ok, err := o.S.Rollback(ctx, a.Slug); err != nil {
238 return ev, err
239 } else if ok {
240 return ev, refuse("rollback-recorded")
241 }
242 keys := AgentKeys(a)
243 var codes []string
244 if write.Sent[keys[0]] != Issuer {
245 codes = append(codes, "issuer-mismatch")
246 }
247 if write.Sent[keys[1]] != teamSQID {
248 codes = append(codes, "team-mismatch")
249 }
250 if len(codes) > 0 {
251 return ev, refuse(codes...)
252 }
253 if err := o.S.CanaryGate(ctx, a.Slug); err != nil {
254 if errors.Is(err, ErrCanaryNotActive) {
255 return ev, refuse("canary-not-active")
256 }
257 return ev, err
258 }
259 artifact := o.Artifacts[a.Slug]
260 if !artifactShape.MatchString(artifact) {
261 return ev, refuse("artifact-unreviewed")
262 }
263 return gateEvidence{base: base, after: after, write: write, check: check, artifact: artifact}, nil
264}
265