| 1 | package adopt |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "amadan.net/oficina/alta/carlosop" |
| 10 | ) |
| 11 | |
| 12 | // checkFixture is an app configured and restarted: its baseline taken |
| 13 | // with the operations door absent, the agent keys stored, the door now |
| 14 | // answering agent-access's challenge, and the after snapshot recorded. |
| 15 | // artifact is the reviewed artifact SHA-256 of the release it serves. |
| 16 | type checkFixture struct { |
| 17 | *carlosFixture |
| 18 | s *Store |
| 19 | after Snapshot |
| 20 | artifact string |
| 21 | } |
| 22 | |
| 23 | func newCheckFixture(t *testing.T, slug string) *checkFixture { |
| 24 | t.Helper() |
| 25 | x := newCarlosFixture(t, slug) |
| 26 | s := newJournal(t) |
| 27 | ctx := context.Background() |
| 28 | base, err := Observe(ctx, x.c, x.h, x.app) |
| 29 | if err != nil { |
| 30 | t.Fatalf("Observe baseline: %v", err) |
| 31 | } |
| 32 | if err := s.SaveBaseline(ctx, x.app.Slug, base); err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | keys := AgentKeys(x.app) |
| 36 | x.f.SetVar(x.app.Slug, "", keys[0], Issuer) |
| 37 | x.f.SetVar(x.app.Slug, "", keys[1], "tm_fixture_sqid") |
| 38 | x.site.ops = agent401 |
| 39 | after, err := Observe(ctx, x.c, x.h, x.app) |
| 40 | if err != nil { |
| 41 | t.Fatalf("Observe after: %v", err) |
| 42 | } |
| 43 | if err := s.SaveAfter(ctx, x.app.Slug, after); err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | return &checkFixture{carlosFixture: x, s: s, after: after, artifact: sha256Hex(valueVersion)} |
| 47 | } |
| 48 | |
| 49 | func (w *checkFixture) check() error { |
| 50 | return CheckDelivery(context.Background(), w.s, w.c, w.h, w.app, w.artifact) |
| 51 | } |
| 52 | |
| 53 | func (w *checkFixture) noCheck(t *testing.T) { |
| 54 | t.Helper() |
| 55 | if _, ok, err := w.s.DeliveryCheck(context.Background(), w.app.Slug); err != nil || ok { |
| 56 | t.Fatalf("a delivery check was recorded (ok %v, err %v)", ok, err) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // The check asks Paul for nothing: it observes, compares and records. |
| 61 | func TestDeliveryCheckRecordsBoundObservation(t *testing.T) { |
| 62 | w := newCheckFixture(t, "docs") |
| 63 | if err := w.check(); err != nil { |
| 64 | t.Fatalf("CheckDelivery: %v", err) |
| 65 | } |
| 66 | got, ok, err := w.s.DeliveryCheck(context.Background(), "docs") |
| 67 | if err != nil || !ok { |
| 68 | t.Fatalf("no delivery check (ok %v, err %v)", ok, err) |
| 69 | } |
| 70 | if got.App != "docs" || got.At.IsZero() { |
| 71 | t.Fatalf("delivery check app %q at %v", got.App, got.At) |
| 72 | } |
| 73 | if codes := SameIdentity(got.Identity, w.after.Identity); len(codes) > 0 { |
| 74 | t.Fatalf("check identity differs from the after snapshot's: %v", codes) |
| 75 | } |
| 76 | if got.AfterVersion == "" || got.AfterVersion != w.after.Layer.Version { |
| 77 | t.Fatal("the check is not bound to the after snapshot's write-layer version") |
| 78 | } |
| 79 | if got.Delivery.Ops != OpsAgent401 || got.Delivery.Version != valueVersion || got.Delivery.Header != valueVersion || got.Delivery.Health != 200 || got.Delivery.Redirected { |
| 80 | t.Fatalf("check delivery ops %q health %d", got.Delivery.Ops, got.Delivery.Health) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // Slides and Calls answer their own 401 before the change: only |
| 85 | // agent-access's challenge after it counts. |
| 86 | func TestDeliveryCheckAcceptsApp401BecomingAgentChallenge(t *testing.T) { |
| 87 | x := newCarlosFixture(t, "slides") |
| 88 | x.site.ops = app401 |
| 89 | s := newJournal(t) |
| 90 | ctx := context.Background() |
| 91 | base, err := Observe(ctx, x.c, x.h, x.app) |
| 92 | if err != nil || base.Delivery.Ops != OpsApp401 { |
| 93 | t.Fatalf("baseline ops %q, %v", base.Delivery.Ops, err) |
| 94 | } |
| 95 | setupOK(t, s.SaveBaseline(ctx, "slides", base)) |
| 96 | keys := AgentKeys(x.app) |
| 97 | x.f.SetVar("slides", "", keys[0], Issuer) |
| 98 | x.f.SetVar("slides", "", keys[1], "tm_fixture_sqid") |
| 99 | x.site.ops = agent401 |
| 100 | after, err := Observe(ctx, x.c, x.h, x.app) |
| 101 | if err != nil { |
| 102 | t.Fatal(err) |
| 103 | } |
| 104 | setupOK(t, s.SaveAfter(ctx, "slides", after)) |
| 105 | w := &checkFixture{carlosFixture: x, s: s, after: after, artifact: sha256Hex(valueVersion)} |
| 106 | // Still the app's own 401: nothing was delivered. |
| 107 | x.site.ops = app401 |
| 108 | if err := w.check(); err == nil || !strings.HasPrefix(err.Error(), "adopt: check-not-delivered") { |
| 109 | t.Fatalf("app 401 after the restart: err = %v", err) |
| 110 | } |
| 111 | w.noCheck(t) |
| 112 | x.site.ops = agent401 |
| 113 | if err := w.check(); err != nil { |
| 114 | t.Fatalf("agent challenge after the restart: %v", err) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | func TestDeliveryCheckRefusesWithoutAfterSnapshot(t *testing.T) { |
| 119 | x := newCarlosFixture(t, "docs") |
| 120 | s := newJournal(t) |
| 121 | base, err := Observe(context.Background(), x.c, x.h, x.app) |
| 122 | if err != nil { |
| 123 | t.Fatal(err) |
| 124 | } |
| 125 | if err := s.SaveBaseline(context.Background(), "docs", base); err != nil { |
| 126 | t.Fatal(err) |
| 127 | } |
| 128 | x.site.ops = agent401 |
| 129 | err = CheckDelivery(context.Background(), s, x.c, x.h, x.app, sha256Hex(valueVersion)) |
| 130 | if err == nil || err.Error() != "adopt: check-after-missing" { |
| 131 | t.Fatalf("err = %v", err) |
| 132 | } |
| 133 | if _, ok, _ := s.DeliveryCheck(context.Background(), "docs"); ok { |
| 134 | t.Fatal("recorded a delivery check without an after snapshot") |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | func TestDeliveryCheckRefusesWithoutBaseline(t *testing.T) { |
| 139 | w := newCheckFixture(t, "docs") |
| 140 | s := newJournal(t) |
| 141 | setupOK(t, s.SaveAfter(context.Background(), "docs", w.after)) |
| 142 | w.s = s |
| 143 | if err := w.check(); err == nil || err.Error() != "adopt: check-baseline-missing" { |
| 144 | t.Fatalf("err = %v", err) |
| 145 | } |
| 146 | w.noCheck(t) |
| 147 | } |
| 148 | |
| 149 | // The reviewed artifact is checked before anything is read, and the |
| 150 | // served release must be it. |
| 151 | func TestDeliveryCheckRefusesUnreviewedOrOtherArtifact(t *testing.T) { |
| 152 | for _, artifact := range []string{"", "nothex", strings.ToUpper(sha256Hex(valueVersion))} { |
| 153 | w := newCheckFixture(t, "docs") |
| 154 | w.artifact = artifact |
| 155 | n := len(w.f.Requests()) |
| 156 | if err := w.check(); err == nil || err.Error() != "adopt: check-artifact-unreviewed" { |
| 157 | t.Fatalf("artifact %q: err = %v", artifact, err) |
| 158 | } |
| 159 | if got := w.f.Requests(); len(got) != n { |
| 160 | t.Fatalf("read the console for an unreviewed artifact: %q", got[n:]) |
| 161 | } |
| 162 | w.noCheck(t) |
| 163 | } |
| 164 | w := newCheckFixture(t, "docs") |
| 165 | w.artifact = sha256Hex("v1.3.9") |
| 166 | if err := w.check(); err == nil || err.Error() != "adopt: check-artifact-mismatch" { |
| 167 | t.Fatalf("other artifact: err = %v", err) |
| 168 | } |
| 169 | w.noCheck(t) |
| 170 | } |
| 171 | |
| 172 | func TestDeliveryCheckRefusesWithoutAgentShape(t *testing.T) { |
| 173 | cases := map[string]func(w *checkFixture){ |
| 174 | "absent": func(w *checkFixture) { w.site.ops = nil }, |
| 175 | "app-401": func(w *checkFixture) { w.site.ops = app401 }, |
| 176 | "unhealthy": func(w *checkFixture) { w.site.health = 503 }, |
| 177 | "version moved": func(w *checkFixture) { |
| 178 | w.site.version, w.site.header = "v1.5.0", "v1.5.0" |
| 179 | }, |
| 180 | "redirect": func(w *checkFixture) { w.site.redirectVersion = true }, |
| 181 | } |
| 182 | for name, change := range cases { |
| 183 | t.Run(name, func(t *testing.T) { |
| 184 | w := newCheckFixture(t, "docs") |
| 185 | change(w) |
| 186 | if err := w.check(); err == nil || !strings.HasPrefix(err.Error(), "adopt: check-not-delivered") { |
| 187 | t.Fatalf("err = %v, want check-not-delivered", err) |
| 188 | } |
| 189 | w.noCheck(t) |
| 190 | }) |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | func TestDeliveryCheckRefusesIdentityChangedSinceAfter(t *testing.T) { |
| 195 | cases := map[string]func(in *carlosop.Instance){ |
| 196 | "record": func(in *carlosop.Instance) { in.RecordSeq++ }, |
| 197 | "storage": func(in *carlosop.Instance) { in.Slug = "s-docs-recreated" }, |
| 198 | "region": func(in *carlosop.Instance) { in.Region = "us" }, |
| 199 | } |
| 200 | for name, change := range cases { |
| 201 | t.Run(name, func(t *testing.T) { |
| 202 | w := newCheckFixture(t, "docs") |
| 203 | in := carlosop.Instance{ |
| 204 | Host: routeOf(w.app), App: "docs", Slug: "s-docs", Channel: "stable", Region: "eu", |
| 205 | CreatedAt: w.after.Identity.CreatedAt, RecordSeq: w.after.Identity.RecordSeq, |
| 206 | ReplicaAssignment: []byte(w.after.Identity.ReplicaAssignment), |
| 207 | } |
| 208 | change(&in) |
| 209 | w.f.AddInstance(in) |
| 210 | err := w.check() |
| 211 | if err == nil || err.Error() != "adopt: check-identity-"+name { |
| 212 | t.Fatalf("err = %v, want check-identity-%s", err, name) |
| 213 | } |
| 214 | w.noCheck(t) |
| 215 | }) |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | // A check run again on an unchanged app records it again: configure's |
| 220 | // resume and re-check. |
| 221 | func TestDeliveryCheckRepeats(t *testing.T) { |
| 222 | w := newCheckFixture(t, "docs") |
| 223 | setupOK(t, w.check()) |
| 224 | setupOK(t, w.check()) |
| 225 | if _, ok, err := w.s.DeliveryCheck(context.Background(), "docs"); err != nil || !ok { |
| 226 | t.Fatalf("no delivery check after a repeat (ok %v, err %v)", ok, err) |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | // The check is bound to the after snapshot's config: any change since, |
| 231 | // in any key's digest, kind or source, or any layer's version, refuses. |
| 232 | func TestDeliveryCheckRefusesConfigChangedSinceAfter(t *testing.T) { |
| 233 | cases := map[string]struct { |
| 234 | change func(w *checkFixture) |
| 235 | codes []string |
| 236 | }{ |
| 237 | "unrelated key added": {func(w *checkFixture) { |
| 238 | w.f.SetVar("docs", "", "DOCS_EXTRA", "x") |
| 239 | }, []string{"added:DOCS_EXTRA", "layer:default"}}, |
| 240 | "secret resealed": {func(w *checkFixture) { |
| 241 | w.f.SetSecret("docs", "", "DOCS_INSTANCE_KEY") |
| 242 | }, []string{"changed:DOCS_INSTANCE_KEY", "layer:default"}}, |
| 243 | "agent key rewritten": {func(w *checkFixture) { |
| 244 | w.f.SetVar("docs", "", "DOCS_AGENT_TEAM", "tm_other") |
| 245 | }, []string{"changed:DOCS_AGENT_TEAM", "layer:default"}}, |
| 246 | "key unset": {func(w *checkFixture) { |
| 247 | w.f.Unset("docs", "", "DOCS_ORIGIN") |
| 248 | }, []string{"gone:DOCS_ORIGIN", "layer:default"}}, |
| 249 | "overlay moved": {func(w *checkFixture) { |
| 250 | w.f.SetOverlay("docs", docsRoute, "DOCS_ORIGIN", "https://elsewhere.example") |
| 251 | }, []string{"from:DOCS_ORIGIN", "layer:overlay"}}, |
| 252 | } |
| 253 | for name, tc := range cases { |
| 254 | t.Run(name, func(t *testing.T) { |
| 255 | w := newCheckFixture(t, "docs") |
| 256 | tc.change(w) |
| 257 | err := w.check() |
| 258 | if err == nil || !strings.HasPrefix(err.Error(), "adopt: check-config-") { |
| 259 | t.Fatalf("err = %v, want check-config-<code>", err) |
| 260 | } |
| 261 | for _, code := range tc.codes { |
| 262 | if !strings.Contains(err.Error(), "check-config-"+code) { |
| 263 | t.Fatalf("err = %v, want it to name %s", err, code) |
| 264 | } |
| 265 | } |
| 266 | for _, leak := range []string{"vd1:", "cv1:", "tm_other", "elsewhere"} { |
| 267 | if strings.Contains(err.Error(), leak) { |
| 268 | t.Fatalf("err = %v carries a value", err) |
| 269 | } |
| 270 | } |
| 271 | w.noCheck(t) |
| 272 | }) |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | // app401 is the operations answer Slides and Calls give before adoption: |
| 277 | // their own middleware's 401, not agent-access's challenge. |
| 278 | func app401(rw http.ResponseWriter) { |
| 279 | rw.WriteHeader(401) |
| 280 | _, _ = rw.Write([]byte(`{"error":"unauthorized"}`)) |
| 281 | } |
| 282 | |
| 283 | func TestSameConfigComparesEveryPart(t *testing.T) { |
| 284 | a := afterSnap() |
| 285 | if codes := sameConfig(a, afterSnap()); len(codes) > 0 { |
| 286 | t.Fatalf("identical snapshots differ: %v", codes) |
| 287 | } |
| 288 | cases := map[string]struct { |
| 289 | change func(s *Snapshot) |
| 290 | want string |
| 291 | }{ |
| 292 | "effective digest": {func(s *Snapshot) { s.Vars[0].Digest = "vd1:other" }, "changed:" + a.Vars[0].Key}, |
| 293 | "effective kind": {func(s *Snapshot) { s.Vars[0].Kind = "secret" }, "kind:" + a.Vars[0].Key}, |
| 294 | "effective from": {func(s *Snapshot) { s.Vars[0].From = "environment" }, "from:" + a.Vars[0].Key}, |
| 295 | "effective gone": {func(s *Snapshot) { s.Vars = s.Vars[1:] }, "gone:" + a.Vars[0].Key}, |
| 296 | "stored digest": {func(s *Snapshot) { s.Layer.Vars[0].Digest = "vd1:other" }, "changed:" + a.Layer.Vars[0].Key}, |
| 297 | "stored added": {func(s *Snapshot) { |
| 298 | s.Layer.Vars = append(s.Layer.Vars, carlosop.Var{Key: "DOCS_NEW", Kind: "env", Digest: "vd1:x"}) |
| 299 | }, "added:DOCS_NEW"}, |
| 300 | "write layer version": {func(s *Snapshot) { s.Layer.Version = "cv1:other" }, "layer:default"}, |
| 301 | "write layer": {func(s *Snapshot) { s.WriteLayer = "staging" }, "write-layer"}, |
| 302 | "other layer": {func(s *Snapshot) { s.Layers["environment"] = "cv1:other" }, "layer:environment"}, |
| 303 | "layer gone": {func(s *Snapshot) { delete(s.Layers, "overlay") }, "layer:overlay"}, |
| 304 | } |
| 305 | for name, tc := range cases { |
| 306 | t.Run(name, func(t *testing.T) { |
| 307 | b := afterSnap() |
| 308 | tc.change(&b) |
| 309 | codes := sameConfig(a, b) |
| 310 | found := false |
| 311 | for _, c := range codes { |
| 312 | found = found || c == tc.want |
| 313 | } |
| 314 | if !found { |
| 315 | t.Fatalf("codes = %q, want %q among them", codes, tc.want) |
| 316 | } |
| 317 | }) |
| 318 | } |
| 319 | } |
| 320 | |