// This file computes an adopted app's environment_binding: the digest // Home's signed activation carries of everything the runner observed. package adopt import ( "crypto/sha256" "encoding/base64" "encoding/json" "sort" "amadan.net/oficina/alta/carlosop" ) // bindingContext names the scheme, so a binding of these parts is never // mistaken for one of any other. const bindingContext = "oficina/existing-app-observation/v1" // BindingInput is everything one adopted app's binding commits to. // ArtifactSHA256 is the reviewed artifact's SHA-256 as 64 lowercase hex; // Version is the delivered version. type BindingInput struct { Before, After Identity BeforeVars, AfterVars []carlosop.EffectiveVar BeforeLayers, AfterLayers map[string]string TeamSQID, Issuer, ArtifactSHA256, Version string BeforeDelivery, AfterDelivery, NowDelivery Delivery Check DeliveryCheck } // EnvironmentBinding is base64url(SHA-256) of the JSON array, in order: // // 1. "oficina/existing-app-observation/v1"; // 2. the before and after identities; // 3. the before and after effective vars, each list as [key, kind, // digest, from] entries sorted (Shadows is not part of an entry); // 4. the before and after layer versions (JSON objects, keys sorted); // 5. the team SQID, the issuer, the artifact SHA-256 and the version; // 6. the before, after and now delivery observations; // 7. the delivery check: [app, identity, delivery, after version, // time], with the redeploy (from, to, artifact, reason) as a sixth // element only when the check records an approved redeploy. // // That is the spec's list (Home implementation/existing-team-adoption.md, // "Compute environment_binding") plus the delivery check, which the 522 // Plan 2 waiver puts in the signed observation; nothing else is bound. // The check holds the place Paul's witness held until his ruling of // 2026-11-17 (Home discussion 103); no binding with a witness was ever // signed. // // The delivery check and observations are inside the binding, so Home's // signed activation commits to them. The result is as sensitive as the // digests it covers: it is never printed or logged. func EnvironmentBinding(in BindingInput) string { c := in.Check check := []any{c.App, c.Identity, c.Delivery, c.AfterVersion, c.At} if c.Redeploy != nil { // A check on a redeployed release commits to the redeploy too; // one on the baseline's release binds exactly as before. check = append(check, c.Redeploy) } parts := []any{ bindingContext, in.Before, in.After, varEntries(in.BeforeVars), varEntries(in.AfterVars), layers(in.BeforeLayers), layers(in.AfterLayers), in.TeamSQID, in.Issuer, in.ArtifactSHA256, in.Version, in.BeforeDelivery, in.AfterDelivery, in.NowDelivery, check, } // Every part is a string, a time, a map of strings or a struct of // those: Marshal cannot fail on them. raw, err := json.Marshal(parts) if err != nil { panic("adopt: the binding's parts did not encode") } sum := sha256.Sum256(raw) return base64.RawURLEncoding.EncodeToString(sum[:]) } // varEntries is vars as sorted [key, kind, digest, from] entries, never // nil, leaving the caller's slice as it was. func varEntries(vars []carlosop.EffectiveVar) [][4]string { out := make([][4]string, 0, len(vars)) for _, v := range vars { out = append(out, [4]string{v.Key, v.Kind, v.Digest, v.From}) } sort.Slice(out, func(i, j int) bool { for k := range out[i] { if out[i][k] != out[j][k] { return out[i][k] < out[j][k] } } return false }) return out } // layers is m, or an empty object for none, so an absent map and an // empty one bind alike. func layers(m map[string]string) map[string]string { if m == nil { return map[string]string{} } return m }