// This file holds the 522 Plan 1 snapshots, the preservation check and // the CARLOS preflight that runs before any configuration write. package adopt import ( "context" "errors" "fmt" "net/http" "sort" "strings" "time" "amadan.net/oficina/alta/carlosop" "amadan.net/oficina/alta/provision" ) // HomeIssuerOrigin is the Home issuer the canonical apps trust: Home's // credential role. An app trusting another issuer is not one this // adoption's directory team can speak for. const HomeIssuerOrigin = "https://id.oficina.works" // The effective view's layer names, lowest first. A higher layer covers a // lower one for the keys it sets. var layerRank = map[string]int{"default": 0, "environment": 1, "overlay": 2} // layerName is the effective view's name for a write layer: "default" // for "", else "environment". func layerName(writeLayer string) string { if writeLayer == "" { return "default" } return "environment" } func findVar(vars []carlosop.EffectiveVar, key string) *carlosop.EffectiveVar { for i := range vars { if vars[i].Key == key { return &vars[i] } } return nil } // plainValue is key's value in a stored layer, if the layer holds it as a // plain var with exactly the digest the effective view reports. "" for a // secret, an absent key or a digest that disagrees. func plainValue(l carlosop.Layer, ev *carlosop.EffectiveVar) string { if ev == nil || ev.Kind != "env" { return "" } for _, v := range l.Vars { if v.Key == ev.Key && v.Kind == "env" && v.Digest == ev.Digest { return v.Value } } return "" } // canonicalRecord resolves app's canonical host as the platform routes // it: the canonical name is a domain claim, routed to one instance // record. It answers exactly one claim on the app whose host is host, and // exactly one record whose host is that claim's route, or the code that // refuses: domain-missing (no claim), domain-ambiguous (more than one), // instance-missing (no record at the route). A name a catch-all derives // (Via) and a catch-all's own row are not claims. Nothing else is tried: // a record whose own host is the canonical one, with no claim, is not // read as the canonical record. func canonicalRecord(host string, ds []carlosop.Domain, all []carlosop.Instance) (carlosop.Domain, *carlosop.Instance, string, error) { var claims []carlosop.Domain for _, d := range ds { if d.Host == host && d.Via == "" && !d.Instances { claims = append(claims, d) } } switch len(claims) { case 0: return carlosop.Domain{}, nil, "domain-missing", nil case 1: default: return carlosop.Domain{}, nil, "domain-ambiguous", nil } d := claims[0] var rec *carlosop.Instance for i := range all { if d.Route != "" && all[i].Host == d.Route { if rec != nil { return carlosop.Domain{}, nil, "", errors.New("adopt: more than one instance record answers for the canonical domain's route") } rec = &all[i] } } if rec == nil { return carlosop.Domain{}, nil, "instance-missing", nil } return d, rec, "", nil } // storedSecret reports whether a stored layer holds key as a secret with // exactly the digest the effective view reports. func storedSecret(l carlosop.Layer, ev *carlosop.EffectiveVar) bool { if ev == nil || ev.Kind != "secret" { return false } for _, v := range l.Vars { if v.Key == ev.Key && v.Kind == "secret" && v.Digest == ev.Digest { return true } } return false } // homeCodes lists how snap's Home issuer and consumer differ from the // ones app must trust. func homeCodes(app provision.App, snap Snapshot) []string { var codes []string if snap.HomeIssuer != HomeIssuerOrigin { codes = append(codes, "home-issuer-mismatch") } if snap.HomeConsumer != provision.ConsumerID(provision.AdoptionLabel, app.Slug) { codes = append(codes, "home-consumer-mismatch") } return codes } // observe reads one snapshot of app's canonical host, and the app's // instance list it found that host's record in. The codes are the // refusals that leave no snapshot to check: domain-missing, // domain-ambiguous, instance-missing, layer-overlay, and // home-issuer-mismatch when the host has no Home issuer to locate the // write layer by. domain-not-live, a claim that is not live, comes with // the complete snapshot but no delivery probe: the binding is known, and // a probe of a name not served would say nothing. With h nil it makes no // delivery probe, and the snapshot's Delivery is empty: the config and // identity alone. // // The Home issuer and consumer are a plain var's own value. Only a value // stored as a secret, which the console never returns, is taken from the // app's live sign-in redirect (signInIdentity), and so only with h: a plain // value that mismatches is never rescued by the redirect. func observe(ctx context.Context, c *carlosop.Client, h *http.Client, app provision.App) (Snapshot, []carlosop.Instance, []string, error) { ds, err := c.Domains(ctx, app.Slug) if err != nil { return Snapshot{}, nil, nil, err } all, err := c.Instances(ctx, app.Slug) if err != nil { return Snapshot{}, nil, nil, err } d, rec, code, err := canonicalRecord(CanonicalHost(app), ds, all) switch { case err != nil: return Snapshot{}, nil, nil, err case code != "": return Snapshot{}, all, []string{code}, nil } live := d.State == "live" if !live { h = nil } snap, codes, err := observeRecord(ctx, c, h, app, d, *rec) if err != nil { return Snapshot{}, nil, nil, err } if !live { codes = sortedSet(append(codes, "domain-not-live")) } return snap, all, codes, nil } // observeRecord is observe once the canonical record is resolved: rec, // served at d. func observeRecord(ctx context.Context, c *carlosop.Client, h *http.Client, app provision.App, d carlosop.Domain, rec carlosop.Instance) (Snapshot, []string, error) { eff, err := c.Effective(ctx, app.Slug, rec.Host) if err != nil { return Snapshot{}, nil, err } snap := Snapshot{Identity: IdentityOf(d, rec, eff), Layers: map[string]string{}, Vars: eff.Vars, At: time.Now().UTC()} for k, v := range eff.Layers { snap.Layers[k] = v } iss := findVar(eff.Vars, app.Prefix+"_HOME_ISSUER") switch { case iss == nil: return snap, []string{"home-issuer-mismatch"}, nil case iss.From == "overlay": return snap, []string{"layer-overlay"}, nil case iss.From == "environment": snap.WriteLayer = eff.Environment } read := func(name string) (carlosop.Layer, error) { env := "" if name == "environment" { env = eff.Environment } l, err := c.Config(ctx, app.Slug, env) if err != nil { return carlosop.Layer{}, err } // Two reads of one layer: if it moved between them, the // snapshot would describe no single moment. if l.Version != eff.Layers[name] { return carlosop.Layer{}, errors.New("adopt: the config changed while it was being read; read it again") } return l, nil } if snap.Layer, err = read(layerName(snap.WriteLayer)); err != nil { return Snapshot{}, nil, err } snap.HomeIssuer = plainValue(snap.Layer, iss) secretIssuer, secretConsumer := storedSecret(snap.Layer, iss), false // The overlay's plain values are not readable, so a consumer ID that // comes from it stays "" and is refused. if cons := findVar(eff.Vars, app.Prefix+"_HOME_CONSUMER_ID"); cons != nil && cons.From != "overlay" { src := snap.Layer if cons.From != layerName(snap.WriteLayer) { if src, err = read(cons.From); err != nil { return Snapshot{}, nil, err } } snap.HomeConsumer = plainValue(src, cons) secretConsumer = storedSecret(src, cons) } if h == nil { return snap, nil, nil } origin := provision.PublicOrigin(provision.AdoptionLabel, app.Slug) if secretIssuer || secretConsumer { issuer, consumer := signInIdentity(ctx, h, origin) if secretIssuer { snap.HomeIssuer = issuer } if secretConsumer { snap.HomeConsumer = consumer } } if snap.Delivery, err = Probe(ctx, h, origin); err != nil { return Snapshot{}, nil, err } return snap, nil, nil } // Observe reads one snapshot of app's canonical host: its instance // identity, the write layer and its stored vars, every layer's version, // the complete effective config by digest, the Home issuer and consumer // ID, and a delivery probe. A host whose write layer cannot be located is // an error naming the code. func Observe(ctx context.Context, c *carlosop.Client, h *http.Client, app provision.App) (Snapshot, error) { if h == nil { return Snapshot{}, errors.New("adopt: Observe needs a client for the delivery probe") } snap, _, codes, err := observe(ctx, c, h, app) if err != nil { return Snapshot{}, err } if len(codes) > 0 { return Snapshot{}, fmt.Errorf("adopt: cannot observe %s: %s", app.Slug, strings.Join(codes, ", ")) } return snap, nil } // TeamModeKeys names the team-mode keys among vars, sorted: those // provision.TeamKeyNames lists and any other
_TEAM_ key.
_TEAM
// itself (Memoria's) is not one.
func TeamModeKeys(app provision.App, vars []carlosop.EffectiveVar) []string {
named := map[string]bool{}
for _, k := range provision.TeamKeyNames(app) {
named[k] = true
}
var out []string
for _, v := range vars {
if named[v.Key] || strings.HasPrefix(v.Key, app.Prefix+"_TEAM_") {
out = append(out, v.Key)
}
}
return sortedSet(out)
}
// inherits reports whether host eff would receive any of keys from a
// write to writeLayer: the layer is in its chain, and nothing above it
// in that chain already sets the key. What else the host overrides is
// irrelevant.
func inherits(eff carlosop.Effective, writeLayer string, keys [2]string) bool {
switch {
case writeLayer == "" && eff.Owned:
return false // an owned environment never folds the default in
case writeLayer != "" && eff.Environment != writeLayer:
return false
}
at := layerRank[layerName(writeLayer)]
for _, k := range keys {
if v := findVar(eff.Vars, k); v == nil || layerRank[v.From] <= at {
return true
}
}
return false
}
// Preflight observes app and lists every reason its configuration may
// not be written, as fixed codes carrying key names and hosts only:
//
// - domain-missing, domain-ambiguous, domain-not-live, instance-missing
// (the canonical domain is not one live claim routed to a record);
// - instance-moving, owned-environment, layer-overlay;
// - home-issuer-mismatch, home-consumer-mismatch: a plain var's value,
// or for a secret the app's live sign-in redirect (observe);
// - team-mode-