| 1 | // This file computes an adopted app's environment_binding: the digest |
| 2 | // Home's signed activation carries of everything the runner observed. |
| 3 | package adopt |
| 4 | |
| 5 | import ( |
| 6 | "crypto/sha256" |
| 7 | "encoding/base64" |
| 8 | "encoding/json" |
| 9 | "sort" |
| 10 | |
| 11 | "amadan.net/oficina/alta/carlosop" |
| 12 | ) |
| 13 | |
| 14 | // bindingContext names the scheme, so a binding of these parts is never |
| 15 | // mistaken for one of any other. |
| 16 | const bindingContext = "oficina/existing-app-observation/v1" |
| 17 | |
| 18 | // BindingInput is everything one adopted app's binding commits to. |
| 19 | // ArtifactSHA256 is the reviewed artifact's SHA-256 as 64 lowercase hex; |
| 20 | // Version is the delivered version. |
| 21 | type BindingInput struct { |
| 22 | Before, After Identity |
| 23 | BeforeVars, AfterVars []carlosop.EffectiveVar |
| 24 | BeforeLayers, AfterLayers map[string]string |
| 25 | TeamSQID, Issuer, ArtifactSHA256, Version string |
| 26 | BeforeDelivery, AfterDelivery, NowDelivery Delivery |
| 27 | Check DeliveryCheck |
| 28 | } |
| 29 | |
| 30 | // EnvironmentBinding is base64url(SHA-256) of the JSON array, in order: |
| 31 | // |
| 32 | // 1. "oficina/existing-app-observation/v1"; |
| 33 | // 2. the before and after identities; |
| 34 | // 3. the before and after effective vars, each list as [key, kind, |
| 35 | // digest, from] entries sorted (Shadows is not part of an entry); |
| 36 | // 4. the before and after layer versions (JSON objects, keys sorted); |
| 37 | // 5. the team SQID, the issuer, the artifact SHA-256 and the version; |
| 38 | // 6. the before, after and now delivery observations; |
| 39 | // 7. the delivery check: [app, identity, delivery, after version, |
| 40 | // time], with the redeploy (from, to, artifact, reason) as a sixth |
| 41 | // element only when the check records an approved redeploy. |
| 42 | // |
| 43 | // That is the spec's list (Home implementation/existing-team-adoption.md, |
| 44 | // "Compute environment_binding") plus the delivery check, which the 522 |
| 45 | // Plan 2 waiver puts in the signed observation; nothing else is bound. |
| 46 | // The check holds the place Paul's witness held until his ruling of |
| 47 | // 2026-11-17 (Home discussion 103); no binding with a witness was ever |
| 48 | // signed. |
| 49 | // |
| 50 | // The delivery check and observations are inside the binding, so Home's |
| 51 | // signed activation commits to them. The result is as sensitive as the |
| 52 | // digests it covers: it is never printed or logged. |
| 53 | func EnvironmentBinding(in BindingInput) string { |
| 54 | c := in.Check |
| 55 | check := []any{c.App, c.Identity, c.Delivery, c.AfterVersion, c.At} |
| 56 | if c.Redeploy != nil { |
| 57 | // A check on a redeployed release commits to the redeploy too; |
| 58 | // one on the baseline's release binds exactly as before. |
| 59 | check = append(check, c.Redeploy) |
| 60 | } |
| 61 | parts := []any{ |
| 62 | bindingContext, |
| 63 | in.Before, in.After, |
| 64 | varEntries(in.BeforeVars), varEntries(in.AfterVars), |
| 65 | layers(in.BeforeLayers), layers(in.AfterLayers), |
| 66 | in.TeamSQID, in.Issuer, in.ArtifactSHA256, in.Version, |
| 67 | in.BeforeDelivery, in.AfterDelivery, in.NowDelivery, |
| 68 | check, |
| 69 | } |
| 70 | // Every part is a string, a time, a map of strings or a struct of |
| 71 | // those: Marshal cannot fail on them. |
| 72 | raw, err := json.Marshal(parts) |
| 73 | if err != nil { |
| 74 | panic("adopt: the binding's parts did not encode") |
| 75 | } |
| 76 | sum := sha256.Sum256(raw) |
| 77 | return base64.RawURLEncoding.EncodeToString(sum[:]) |
| 78 | } |
| 79 | |
| 80 | // varEntries is vars as sorted [key, kind, digest, from] entries, never |
| 81 | // nil, leaving the caller's slice as it was. |
| 82 | func varEntries(vars []carlosop.EffectiveVar) [][4]string { |
| 83 | out := make([][4]string, 0, len(vars)) |
| 84 | for _, v := range vars { |
| 85 | out = append(out, [4]string{v.Key, v.Kind, v.Digest, v.From}) |
| 86 | } |
| 87 | sort.Slice(out, func(i, j int) bool { |
| 88 | for k := range out[i] { |
| 89 | if out[i][k] != out[j][k] { |
| 90 | return out[i][k] < out[j][k] |
| 91 | } |
| 92 | } |
| 93 | return false |
| 94 | }) |
| 95 | return out |
| 96 | } |
| 97 | |
| 98 | // layers is m, or an empty object for none, so an absent map and an |
| 99 | // empty one bind alike. |
| 100 | func layers(m map[string]string) map[string]string { |
| 101 | if m == nil { |
| 102 | return map[string]string{} |
| 103 | } |
| 104 | return m |
| 105 | } |
| 106 | |