alta-adopt: bind live checks to the agent issuer the apps trust
RunLive refuses any connect origin but adopt.Issuer, the AGENT_ISSUER value configure writes, before contacting or recording anything, dry run included (LiveNeedIssuer, draft copy pending review). Checks against another deployment can no longer unlock Plan C. Each check now records the origin it was made against, sealed under alta/adopt/adopt_checks/<name> (migration 0002), since it is a plain var's value. LiveGate closes on any check whose origin is not Issuer, is missing (a row from before the column) or does not open. SaveCheck takes the origin and refuses anything but a plain https origin. The fake connect now answers as Issuer through a dial-rewriting transport, so the tests run the real origin.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
10 files changed,
+255
−58
adopt/gate_adverse_test.go+8 −8adopt/live.go+17 −4adopt/live_test.go+103 −18adopt/migrations/0002_check_origin.sql+6 −0adopt/observer_test.go+3 −3adopt/rehearsal_test.go+3 −3adopt/store.go+42 −8adopt/store_test.go+59 −11adopt/words.go+1 −0cmd/alta-adopt/main_test.go+13 −3
diff --git a/adopt/gate_adverse_test.go b/adopt/gate_adverse_test.go| index a13c9d0..a816f14 100644 |
| --- a/adopt/gate_adverse_test.go |
| +++ b/adopt/gate_adverse_test.go |
| @@ -257,7 +257,7 @@ func (g *gate) observer(w *writeFixture) *observerFixture { |
| func passLiveChecks(t *testing.T, s *Store) { |
| t.Helper() |
| for _, c := range RequiredLiveChecks { |
| - setupOK(t, s.SaveCheck(context.Background(), c, "passed")) |
| + setupOK(t, s.SaveCheck(context.Background(), Issuer, c, "passed")) |
| } |
| } |
| @@ -357,7 +357,7 @@ func TestAdversePlanCRefusedWithoutLiveSuccess(t *testing.T) { |
| {name: "none recorded", checks: func(*testing.T, *Store) {}}, |
| {name: "one missing", checks: func(t *testing.T, s *Store) { |
| for _, c := range RequiredLiveChecks[1:] { |
| - setupOK(t, s.SaveCheck(context.Background(), c, "passed")) |
| + setupOK(t, s.SaveCheck(context.Background(), Issuer, c, "passed")) |
| } |
| }}, |
| {name: "one inconclusive", checks: func(t *testing.T, s *Store) { |
| @@ -367,28 +367,28 @@ func TestAdversePlanCRefusedWithoutLiveSuccess(t *testing.T) { |
| if c == unsure { |
| outcome = "inconclusive" |
| } |
| - setupOK(t, s.SaveCheck(context.Background(), c, outcome)) |
| + setupOK(t, s.SaveCheck(context.Background(), Issuer, c, outcome)) |
| } |
| }}, |
| {name: "all inconclusive", checks: func(t *testing.T, s *Store) { |
| for _, c := range RequiredLiveChecks { |
| - setupOK(t, s.SaveCheck(context.Background(), c, "inconclusive")) |
| + setupOK(t, s.SaveCheck(context.Background(), Issuer, c, "inconclusive")) |
| } |
| }}, |
| {name: "one failed", checks: func(t *testing.T, s *Store) { |
| passLiveChecks(t, s) |
| - setupOK(t, s.SaveCheck(context.Background(), "revoked-membership-refused", "failed:allowed")) |
| + setupOK(t, s.SaveCheck(context.Background(), Issuer, "revoked-membership-refused", "failed:allowed")) |
| }}, |
| {name: "failed, then passed again", checks: func(t *testing.T, s *Store) { |
| - setupOK(t, s.SaveCheck(context.Background(), "owner-allowed", "failed:denied")) |
| + setupOK(t, s.SaveCheck(context.Background(), Issuer, "owner-allowed", "failed:denied")) |
| passLiveChecks(t, s) |
| }}, |
| {name: "local permission check failed", checks: func(t *testing.T, s *Store) { |
| passLiveChecks(t, s) |
| - setupOK(t, s.SaveCheck(context.Background(), "member-no-local-permission-refused", "failed:allowed")) |
| + setupOK(t, s.SaveCheck(context.Background(), Issuer, "member-no-local-permission-refused", "failed:allowed")) |
| }}, |
| {name: "local permission check failed, rest passed after", checks: func(t *testing.T, s *Store) { |
| - setupOK(t, s.SaveCheck(context.Background(), "member-no-local-permission-refused", "failed:allowed")) |
| + setupOK(t, s.SaveCheck(context.Background(), Issuer, "member-no-local-permission-refused", "failed:allowed")) |
| passLiveChecks(t, s) |
| }}, |
| } |
diff --git a/adopt/live.go b/adopt/live.go| index d7e029a..08d5fcc 100644 |
| --- a/adopt/live.go |
| +++ b/adopt/live.go |
| @@ -317,12 +317,21 @@ func mustSucceed(answer string) string { |
| // failed:<code> with a fixed lower-case code. |
| var liveOutcome = regexp.MustCompile(`^(passed|inconclusive|failed:[a-z][a-z_-]*)$`) |
| +// httpsOrigin reports whether s is exactly a plain https origin, |
| +// https://host[:port], and nothing more. |
| +func httpsOrigin(s string) bool { |
| + u, err := url.Parse(s) |
| + return err == nil && u.Scheme == "https" && u.Host != "" && s == u.Scheme+"://"+u.Host |
| +} |
| + |
| // errLiveSignOut is a connection connect did not revoke. |
| var errLiveSignOut = errors.New("adopt: live-sign-out-failed") |
| // RunLive runs the live checks against connect at server for the team |
| -// sqid, recording each outcome with SaveCheck: passed, failed:<code> or |
| -// inconclusive. It stops at the first check that does not pass; a |
| +// sqid, recording each outcome with SaveCheck, with server's origin: |
| +// passed, failed:<code> or inconclusive. server must be Issuer, the |
| +// origin the apps were told to trust; any other is refused before |
| +// anything is contacted. It stops at the first check that does not pass; a |
| // failure keeps Plan C refused for good, an inconclusive check only until |
| // it passes on a later run. Only the owner's revocation check repeats on |
| // its own, with a fresh connection, up to liveOwnerAttempts times. |
| @@ -340,6 +349,10 @@ func RunLive(ctx context.Context, out io.Writer, server, sqid string, p Prompter |
| switch { |
| case perr != nil || u.Scheme != "https" || u.Host == "" || u.User != nil || u.RawQuery != "" || u.Fragment != "" || u.Path != "" && u.Path != "/": |
| return errors.New("adopt: " + LiveNeedOrigin) |
| + case u.Scheme+"://"+u.Host != Issuer: |
| + // Plan C trusts these checks for the apps that trust Issuer: |
| + // checks against any other deployment prove nothing about it. |
| + return errors.New("adopt: " + LiveNeedIssuer) |
| case sqid == "": |
| return errors.New("adopt: " + LiveNeedTeam) |
| } |
| @@ -380,7 +393,7 @@ func (r *liveRun) say(line string) { fmt.Fprintln(r.out, line) } |
| // record saves name's outcome. Anything but passed stops the run, with an |
| // error naming the check and the outcome only. |
| func (r *liveRun) record(ctx context.Context, name, outcome string) error { |
| - if err := r.s.SaveCheck(ctx, name, outcome); err != nil { |
| + if err := r.s.SaveCheck(ctx, r.server, name, outcome); err != nil { |
| return err |
| } |
| if outcome != livePassed { |
| @@ -585,7 +598,7 @@ func (r *liveRun) owner(ctx context.Context) (string, error) { |
| return "", err |
| } |
| outcome := r.replay(ctx, c) |
| - if err := r.s.SaveCheck(ctx, "owner-revoked-refused", outcome); err != nil { |
| + if err := r.s.SaveCheck(ctx, r.server, "owner-revoked-refused", outcome); err != nil { |
| return "", err |
| } |
| return outcome, nil |
diff --git a/adopt/live_test.go b/adopt/live_test.go| index 6ec453e..a947f40 100644 |
| --- a/adopt/live_test.go |
| +++ b/adopt/live_test.go |
| @@ -8,6 +8,7 @@ import ( |
| "encoding/json" |
| "errors" |
| "io" |
| + "net" |
| "net/http" |
| "net/http/httptest" |
| "net/url" |
| @@ -54,6 +55,9 @@ type fakeConnect struct { |
| t *testing.T |
| srv *httptest.Server |
| mu sync.Mutex |
| + // origin is the origin the fake answers as: production connect, the |
| + // agent issuer, reached through transport. |
| + origin string |
| team string // the adopted team's SQID: liveSQID unless set |
| members map[string]bool |
| @@ -84,12 +88,26 @@ func newFakeConnect(t *testing.T) *fakeConnect { |
| logins: []string{subjTest, subjTest, subjOwner}, |
| codes: map[string]string{}, |
| tokens: map[string]*fakeToken{}, |
| + origin: Issuer, |
| } |
| f.srv = httptest.NewTLSServer(f) |
| t.Cleanup(f.srv.Close) |
| return f |
| } |
| +// transport reaches the fake for every https origin, whatever its name: |
| +// the live checks name production connect, and land here. |
| +func (f *fakeConnect) transport() http.RoundTripper { |
| + tr := f.srv.Client().Transport.(*http.Transport).Clone() |
| + addr := f.srv.Listener.Addr().String() |
| + tr.DialContext = func(ctx context.Context, network, _ string) (net.Conn, error) { |
| + var d net.Dialer |
| + return d.DialContext(ctx, network, addr) |
| + } |
| + tr.TLSClientConfig.ServerName = "example.com" |
| + return tr |
| +} |
| + |
| func randHex() string { |
| var b [16]byte |
| if _, err := rand.Read(b[:]); err != nil { |
| @@ -109,7 +127,7 @@ func writeJSONTest(w http.ResponseWriter, status int, v any) { |
| func (f *fakeConnect) agentFail(w http.ResponseWriter, status int, code string) { |
| w.Header().Set("Cache-Control", "no-store") |
| if status == http.StatusUnauthorized { |
| - w.Header().Set("WWW-Authenticate", `Bearer resource_metadata="`+f.srv.URL+`/.well-known/oauth-protected-resource"`) |
| + w.Header().Set("WWW-Authenticate", `Bearer resource_metadata="`+f.origin+`/.well-known/oauth-protected-resource"`) |
| } |
| writeJSONTest(w, status, map[string]any{"error": map[string]string{"code": code, "message": "message-canary"}}) |
| } |
| @@ -121,8 +139,8 @@ func (f *fakeConnect) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| switch { |
| case r.Method == "GET" && r.URL.Path == "/.well-known/oauth-authorization-server": |
| writeJSONTest(w, 200, map[string]any{ |
| - "issuer": f.srv.URL, "authorization_endpoint": f.srv.URL + "/oauth/authorize", |
| - "token_endpoint": f.srv.URL + "/oauth/token", "code_challenge_methods_supported": []string{"S256"}, |
| + "issuer": f.origin, "authorization_endpoint": f.origin + "/oauth/authorize", |
| + "token_endpoint": f.origin + "/oauth/token", "code_challenge_methods_supported": []string{"S256"}, |
| "scopes_supported": []string{"calendar:read", "docs:read"}, |
| }) |
| case r.Method == "POST" && r.URL.Path == "/oauth/token": |
| @@ -182,7 +200,7 @@ func (f *fakeConnect) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| if status, code := f.answer(op); status != 0 { |
| switch { |
| case status >= 300 && status < 400: |
| - w.Header().Set("Location", f.srv.URL+"/followed") |
| + w.Header().Set("Location", f.origin+"/followed") |
| w.WriteHeader(status) |
| case status == 200 && code == "malformed": |
| w.Header().Set("Content-Type", "application/json") |
| @@ -282,7 +300,7 @@ func (f *fakeConnect) open(raw string) error { |
| if q.Get("code_challenge") == "" || q.Get("scope") != "calendar:read" { |
| f.t.Errorf("sign-in asked for %q without PKCE or with other scopes", q.Get("scope")) |
| } |
| - cb := q.Get("redirect_uri") + "?" + url.Values{"code": {code}, "state": {q.Get("state")}, "iss": {f.srv.URL}}.Encode() |
| + cb := q.Get("redirect_uri") + "?" + url.Values{"code": {code}, "state": {q.Get("state")}, "iss": {f.origin}}.Encode() |
| resp, err := http.Get(cb) |
| if err != nil { |
| return err |
| @@ -355,7 +373,7 @@ func newLive(t *testing.T) *liveFixture { |
| f := newFakeConnect(t) |
| x := &liveFixture{f: f, p: &fakePrompter{f: f}, s: newJournal(t), out: &bytes.Buffer{}} |
| oldT, oldNow := liveTransport, liveNow |
| - liveTransport = f.srv.Client().Transport |
| + liveTransport = f.transport() |
| liveNow = func() time.Time { return time.Now().Add(time.Duration(f.skew.Load())) } |
| t.Cleanup(func() { liveTransport, liveNow = oldT, oldNow }) |
| return x |
| @@ -366,7 +384,7 @@ func (x *liveFixture) run(t *testing.T) error { |
| ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| defer cancel() |
| x.p.cancel = nil |
| - return RunLive(ctx, x.out, x.f.srv.URL, liveSQID, x.p, x.s, x.f.open, liveEvent, true) |
| + return RunLive(ctx, x.out, x.f.origin, liveSQID, x.p, x.s, x.f.open, liveEvent, true) |
| } |
| // checks lists the journal's live checks in the order recorded. |
| @@ -836,7 +854,7 @@ func TestLiveCloseZeroesEveryCapturedRequest(t *testing.T) { |
| x := newLive(t) |
| brk(x) |
| ev, _ := strconv.ParseInt(liveEvent, 10, 64) |
| - r := &liveRun{out: x.out, server: x.f.srv.URL, sqid: liveSQID, event: ev, p: x.p, s: x.s, open: x.f.open} |
| + r := &liveRun{out: x.out, server: x.f.origin, sqid: liveSQID, event: ev, p: x.p, s: x.s, open: x.f.open} |
| ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| defer cancel() |
| _ = r.run(ctx) |
| @@ -1015,7 +1033,7 @@ func TestLiveAlwaysLogsOut(t *testing.T) { |
| defer cancel() |
| x.p.failOn = LiveRemovedMember |
| x.p.cancel = cancel |
| - if err := RunLive(ctx, x.out, x.f.srv.URL, liveSQID, x.p, x.s, x.f.open, liveEvent, true); err == nil { |
| + if err := RunLive(ctx, x.out, x.f.origin, liveSQID, x.p, x.s, x.f.open, liveEvent, true); err == nil { |
| t.Fatal("a cancelled run went on") |
| } |
| allRevoked(t, x.f) |
| @@ -1049,10 +1067,10 @@ func TestLiveDryRunContactsNothing(t *testing.T) { |
| x := newLive(t) |
| open := func(string) error { t.Fatal("dry run opened a browser"); return nil } |
| p := askFunc(func(context.Context, string) error { t.Fatal("dry run asked Paul"); return nil }) |
| - if err := RunLive(context.Background(), x.out, x.f.srv.URL, liveSQID, p, x.s, open, liveEvent, false); err != nil { |
| + if err := RunLive(context.Background(), x.out, x.f.origin, liveSQID, p, x.s, open, liveEvent, false); err != nil { |
| t.Fatal(err) |
| } |
| - if err := RunLive(context.Background(), x.out, x.f.srv.URL, liveSQID, p, nil, open, liveEvent, false); err != nil { |
| + if err := RunLive(context.Background(), x.out, x.f.origin, liveSQID, p, nil, open, liveEvent, false); err != nil { |
| t.Fatalf("dry run needs the journal: %v", err) |
| } |
| _, _, _, paths, _ := x.f.state() |
| @@ -1077,11 +1095,11 @@ func TestLiveRefusesBadInputs(t *testing.T) { |
| "http server": {"http://connect.oficina.works", liveSQID, liveEvent}, |
| "server path": {"https://connect.oficina.works/x", liveSQID, liveEvent}, |
| "no server": {"", liveSQID, liveEvent}, |
| - "no sqid": {x.f.srv.URL, "", liveEvent}, |
| - "event zero": {x.f.srv.URL, liveSQID, "0"}, |
| - "event negative": {x.f.srv.URL, liveSQID, "-4"}, |
| - "event text": {x.f.srv.URL, liveSQID, "abc"}, |
| - "event spaced": {x.f.srv.URL, liveSQID, " 4"}, |
| + "no sqid": {x.f.origin, "", liveEvent}, |
| + "event zero": {x.f.origin, liveSQID, "0"}, |
| + "event negative": {x.f.origin, liveSQID, "-4"}, |
| + "event text": {x.f.origin, liveSQID, "abc"}, |
| + "event spaced": {x.f.origin, liveSQID, " 4"}, |
| } { |
| for _, apply := range []bool{false, true} { |
| if err := RunLive(context.Background(), x.out, tc.server, tc.sqid, x.p, x.s, open, tc.event, apply); err == nil { |
| @@ -1089,10 +1107,10 @@ func TestLiveRefusesBadInputs(t *testing.T) { |
| } |
| } |
| } |
| - if err := RunLive(context.Background(), x.out, x.f.srv.URL, liveSQID, nil, x.s, open, liveEvent, true); err == nil { |
| + if err := RunLive(context.Background(), x.out, x.f.origin, liveSQID, nil, x.s, open, liveEvent, true); err == nil { |
| t.Fatal("ran with no prompter") |
| } |
| - if err := RunLive(context.Background(), x.out, x.f.srv.URL, liveSQID, x.p, nil, open, liveEvent, true); err == nil { |
| + if err := RunLive(context.Background(), x.out, x.f.origin, liveSQID, x.p, nil, open, liveEvent, true); err == nil { |
| t.Fatal("ran with no journal") |
| } |
| _, _, _, paths, _ := x.f.state() |
| @@ -1101,6 +1119,73 @@ func TestLiveRefusesBadInputs(t *testing.T) { |
| } |
| } |
| +// The live checks run against production connect only: the origin the |
| +// apps are told to trust, Issuer. Another https origin, even one that |
| +// answers as connect does, is refused before anything is contacted or |
| +// recorded, in a dry run too. |
| +func TestLiveRefusesAnotherConnectOrigin(t *testing.T) { |
| + x := newLive(t) |
| + open := func(string) error { t.Fatal("opened a browser"); return nil } |
| + for _, server := range []string{ |
| + "https://connect.staging.oficina.works", |
| + "https://connect.oficina.works.example", |
| + "https://connect.oficina.works:8443", |
| + "https://oficina.works", |
| + } { |
| + for _, apply := range []bool{false, true} { |
| + err := RunLive(context.Background(), x.out, server, liveSQID, x.p, x.s, open, liveEvent, apply) |
| + if err == nil || !strings.Contains(err.Error(), LiveNeedIssuer) { |
| + t.Fatalf("%s (apply %v): err = %v", server, apply, err) |
| + } |
| + } |
| + } |
| + _, _, _, paths, _ := x.f.state() |
| + if len(paths) != 0 || x.out.Len() != 0 { |
| + t.Fatalf("a refused run contacted connect or printed: %q %q", paths, x.out.String()) |
| + } |
| + wantChecks(t, x.s) |
| + // The issuer itself, with or without its trailing slash, is accepted. |
| + for _, server := range []string{Issuer, Issuer + "/"} { |
| + if err := RunLive(context.Background(), io.Discard, server, liveSQID, x.p, x.s, open, liveEvent, false); err != nil { |
| + t.Fatalf("%s: %v", server, err) |
| + } |
| + } |
| +} |
| + |
| +// Each check is recorded with the origin it was made against. |
| +func TestLiveRecordsTheOriginOfEachCheck(t *testing.T) { |
| + x := newLive(t) |
| + if err := x.run(t); err != nil { |
| + t.Fatal(err) |
| + } |
| + rows, err := x.s.db.Query("SELECT name, origin FROM adopt_checks") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer rows.Close() |
| + n := 0 |
| + for rows.Next() { |
| + var name string |
| + var sealed []byte |
| + if err := rows.Scan(&name, &sealed); err != nil { |
| + t.Fatal(err) |
| + } |
| + if bytes.Contains(sealed, []byte(Issuer)) { |
| + t.Fatalf("%s: the origin is in the clear", name) |
| + } |
| + if got := x.s.checkOrigin(name, sealed); got != Issuer { |
| + t.Fatalf("%s: origin %q", name, got) |
| + } |
| + n++ |
| + } |
| + if n != len(RequiredLiveChecks) { |
| + t.Fatalf("%d checks recorded", n) |
| + } |
| + if err := x.s.LiveGate(context.Background(), "docs"); err != nil { |
| + t.Fatalf("gate after a passing run: %v", err) |
| + } |
| +} |
| + |
| // MemoryStore keeps the credentials in memory only, hands out copies, |
| // and forgets them on Delete. |
| func TestLiveMemoryStore(t *testing.T) { |
diff --git a/adopt/migrations/0002_check_origin.sql b/adopt/migrations/0002_check_origin.sql| new file mode 100644 |
| index 0000000..e2a75ec |
| --- /dev/null |
| +++ b/adopt/migrations/0002_check_origin.sql |
| @@ -0,0 +1,6 @@ |
| +-- Each live check records the connect origin it was made against, |
| +-- sealed under alta/adopt/adopt_checks/<name> (it is the agent issuer's |
| +-- value, a plain var's, so never in the clear). The gate counts checks |
| +-- against the agent issuer only; a row from before this column has no |
| +-- origin, and closes the gate. |
| +ALTER TABLE adopt_checks ADD COLUMN origin BLOB; |
diff --git a/adopt/observer_test.go b/adopt/observer_test.go| index 1dec345..611acd5 100644 |
| --- a/adopt/observer_test.go |
| +++ b/adopt/observer_test.go |
| @@ -267,9 +267,9 @@ func TestObserverRefusals(t *testing.T) { |
| {name: "live checks missing", app: "docs", want: []string{"live-checks-missing"}, exact: true}, |
| {name: "live check failed", app: "docs", edit: func(t *testing.T, x *observerFixture) { |
| for _, c := range RequiredLiveChecks { |
| - setupOK(t, x.s.SaveCheck(ctx, c, "passed")) |
| + setupOK(t, x.s.SaveCheck(ctx, Issuer, c, "passed")) |
| } |
| - setupOK(t, x.s.SaveCheck(ctx, "member-no-local-permission-refused", "failed:allowed")) |
| + setupOK(t, x.s.SaveCheck(ctx, Issuer, "member-no-local-permission-refused", "failed:allowed")) |
| }, want: []string{"live-checks-missing"}, exact: true}, |
| {name: "artifact not reviewed", edit: func(t *testing.T, x *observerFixture) { x.o.Artifacts = nil }, |
| want: []string{"artifact-unreviewed"}, exact: true}, |
| @@ -385,7 +385,7 @@ func TestObserverRefusesUnknownAppAndBadTeam(t *testing.T) { |
| func TestObserverPassesNonCanaryWithLiveChecks(t *testing.T) { |
| x := newObserverFixture(t, "docs") |
| for _, c := range RequiredLiveChecks { |
| - if err := x.s.SaveCheck(context.Background(), c, "passed"); err != nil { |
| + if err := x.s.SaveCheck(context.Background(), Issuer, c, "passed"); err != nil { |
| t.Fatal(err) |
| } |
| } |
diff --git a/adopt/rehearsal_test.go b/adopt/rehearsal_test.go| index bee74b0..9774931 100644 |
| --- a/adopt/rehearsal_test.go |
| +++ b/adopt/rehearsal_test.go |
| @@ -190,7 +190,7 @@ func newRehearsal(t *testing.T) *rehearsal { |
| r.connect = newFakeConnect(t) |
| oldT, oldNow := liveTransport, liveNow |
| - liveTransport = r.connect.srv.Client().Transport |
| + liveTransport = r.connect.transport() |
| liveNow = time.Now |
| t.Cleanup(func() { liveTransport, liveNow = oldT, oldNow }) |
| @@ -699,7 +699,7 @@ func (r *rehearsal) live() { |
| ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| defer cancel() |
| p := &fakePrompter{f: r.connect} |
| - if err := RunLive(ctx, io.Discard, r.connect.srv.URL, r.sqid, p, r.js, r.connect.open, liveEvent, true); err != nil { |
| + if err := RunLive(ctx, io.Discard, r.connect.origin, r.sqid, p, r.js, r.connect.open, liveEvent, true); err != nil { |
| r.t.Fatalf("live: %v", err) |
| } |
| allRevoked(r.t, r.connect) |
| @@ -1069,7 +1069,7 @@ func TestRehearsalDeadProcessTouchesNothing(t *testing.T) { |
| return errors.New("the PUT was answered") |
| } |
| // Whatever a dead process would do next. |
| - return js.SaveCheck(context.Background(), RequiredLiveChecks[0], "passed") |
| + return js.SaveCheck(context.Background(), Issuer, RequiredLiveChecks[0], "passed") |
| }) |
| if died, err := x.await(); !died { |
| t.Fatalf("the process finished (%v) instead of dying in its PUT", err) |
diff --git a/adopt/store.go b/adopt/store.go| index 5f2ea61..259b377 100644 |
| --- a/adopt/store.go |
| +++ b/adopt/store.go |
| @@ -86,6 +86,7 @@ const ( |
| tRollbacks = "adopt_rollbacks" |
| tAfters = "adopt_afters" |
| tWitnesses = "adopt_witnesses" |
| + tChecks = "adopt_checks" |
| ) |
| func purpose(table, key string) string { return "alta/adopt/" + table + "/" + key } |
| @@ -291,31 +292,59 @@ func (s *Store) Witness(ctx context.Context, app string) (Witness, bool, error) |
| return w, ok, err |
| } |
| -// SaveCheck appends one live check's outcome: "passed", "inconclusive", |
| -// or "failed:<code>" with a fixed lower-case code. Every outcome is kept: |
| -// a failure is never overwritten by a later pass. |
| -func (s *Store) SaveCheck(ctx context.Context, name, outcome string) error { |
| +// SaveCheck appends one live check's outcome, with the connect origin it |
| +// was made against (a plain https origin): "passed", "inconclusive", or |
| +// "failed:<code>" with a fixed lower-case code. Every outcome is kept: a |
| +// failure is never overwritten by a later pass. |
| +func (s *Store) SaveCheck(ctx context.Context, origin, name, outcome string) error { |
| if !slices.Contains(RequiredLiveChecks, name) { |
| return errors.New("adopt: not a live check") |
| } |
| if !liveOutcome.MatchString(outcome) { |
| return errors.New("adopt: not a live check outcome") |
| } |
| - _, err := s.exec(ctx, "INSERT INTO adopt_checks(name,outcome) VALUES(?,?)", name, outcome) |
| + if !httpsOrigin(origin) { |
| + return errors.New("adopt: not a connect origin") |
| + } |
| + sealed, err := s.sealed(tChecks, name, origin) |
| + if err != nil { |
| + return err |
| + } |
| + _, err = s.exec(ctx, "INSERT INTO adopt_checks(origin,name,outcome) VALUES(?,?,?)", sealed, name, outcome) |
| return err |
| } |
| +// checkOrigin opens a check's sealed origin; "" when it has none or it |
| +// does not open. |
| +func (s *Store) checkOrigin(name string, sealed []byte) string { |
| + if len(sealed) == 0 { |
| + return "" |
| + } |
| + b, err := s.ps.OpenAdoption(purpose(tChecks, name), sealed) |
| + if err != nil { |
| + return "" |
| + } |
| + var origin string |
| + if json.Unmarshal(b, &origin) != nil { |
| + return "" |
| + } |
| + return origin |
| +} |
| + |
| // LiveGate is the live-check rule, the one implementation of it. The |
| // canary app activates before the checks run, so it is never gated. |
| // Every other app is admitted only when each of RequiredLiveChecks has |
| // passed and no check has ever failed; otherwise ErrLiveChecksMissing. |
| // An inconclusive outcome neither passes nor fails; any outcome not |
| -// known here counts as a failure. |
| +// known here counts as a failure. Only checks made against production |
| +// connect, the agent issuer the apps were told to trust (Issuer), count: |
| +// a check recorded against any other origin, or none, or one whose |
| +// origin does not open, closes the gate. |
| func (s *Store) LiveGate(ctx context.Context, app string) error { |
| if app == CanaryApp { |
| return nil |
| } |
| - rows, err := s.db.QueryContext(ctx, "SELECT name,outcome FROM adopt_checks") |
| + rows, err := s.db.QueryContext(ctx, "SELECT origin,name,outcome FROM adopt_checks") |
| if err != nil { |
| return err |
| } |
| @@ -323,10 +352,15 @@ func (s *Store) LiveGate(ctx context.Context, app string) error { |
| passed := map[string]bool{} |
| failed := false |
| for rows.Next() { |
| + var sealed []byte |
| var name, outcome string |
| - if err := rows.Scan(&name, &outcome); err != nil { |
| + if err := rows.Scan(&sealed, &name, &outcome); err != nil { |
| return err |
| } |
| + if s.checkOrigin(name, sealed) != Issuer { |
| + failed = true |
| + continue |
| + } |
| switch outcome { |
| case livePassed: |
| passed[name] = true |
diff --git a/adopt/store_test.go b/adopt/store_test.go| index 74fd2a1..dbb408b 100644 |
| --- a/adopt/store_test.go |
| +++ b/adopt/store_test.go |
| @@ -293,13 +293,13 @@ func TestLiveGateCanaryAndRest(t *testing.T) { |
| } |
| gated("none recorded") |
| for _, name := range RequiredLiveChecks[:len(RequiredLiveChecks)-1] { |
| - if err := s.SaveCheck(ctx, name, "passed"); err != nil { |
| + if err := s.SaveCheck(ctx, Issuer, name, "passed"); err != nil { |
| t.Fatal(err) |
| } |
| } |
| gated("one missing") |
| last := RequiredLiveChecks[len(RequiredLiveChecks)-1] |
| - if err := s.SaveCheck(ctx, last, "passed"); err != nil { |
| + if err := s.SaveCheck(ctx, Issuer, last, "passed"); err != nil { |
| t.Fatal(err) |
| } |
| for _, app := range rest { |
| @@ -312,22 +312,22 @@ func TestLiveGateCanaryAndRest(t *testing.T) { |
| } |
| // A failure stays on the record: passing again later does not open |
| // the gate. |
| - if err := s.SaveCheck(ctx, "owner-revoked-refused", "failed:allowed"); err != nil { |
| + if err := s.SaveCheck(ctx, Issuer, "owner-revoked-refused", "failed:allowed"); err != nil { |
| t.Fatal(err) |
| } |
| gated("one failed") |
| - if err := s.SaveCheck(ctx, "owner-revoked-refused", "passed"); err != nil { |
| + if err := s.SaveCheck(ctx, Issuer, "owner-revoked-refused", "passed"); err != nil { |
| t.Fatal(err) |
| } |
| gated("failed then passed") |
| if err := s.LiveGate(ctx, "calendar"); err != nil { |
| t.Fatalf("canary gated by the rest's checks: %v", err) |
| } |
| - if err := s.SaveCheck(ctx, "made-up", "passed"); err == nil { |
| + if err := s.SaveCheck(ctx, Issuer, "made-up", "passed"); err == nil { |
| t.Fatal("unknown check recorded") |
| } |
| for _, outcome := range []string{"", "pass", "skipped", "failed", "failed:", "failed:Allowed", "failed:a b", "failed:allowed\n", "inconclusive:x"} { |
| - if err := s.SaveCheck(ctx, "member-allowed", outcome); err == nil { |
| + if err := s.SaveCheck(ctx, Issuer, "member-allowed", outcome); err == nil { |
| t.Fatalf("outcome %q recorded", outcome) |
| } |
| } |
| @@ -335,11 +335,59 @@ func TestLiveGateCanaryAndRest(t *testing.T) { |
| // An inconclusive check neither passes nor fails: it leaves the gate as |
| // it was, and a later pass of the same check still counts. |
| +// The gate counts checks made against production connect only, the |
| +// origin the apps trust (Issuer). A check recorded against any other |
| +// origin, or against none (a row older than the origin), closes it. |
| +func TestLiveGateRefusesChecksFromAnotherOrigin(t *testing.T) { |
| + ctx := context.Background() |
| + const other = "https://connect.staging.oficina.works" |
| + passAll := func(s *Store, origin string) { |
| + t.Helper() |
| + for _, name := range RequiredLiveChecks { |
| + if err := s.SaveCheck(ctx, origin, name, "passed"); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + } |
| + s := newJournal(t) |
| + passAll(s, Issuer) |
| + if err := s.LiveGate(ctx, "docs"); err != nil { |
| + t.Fatalf("every check passed against connect: %v", err) |
| + } |
| + if err := s.SaveCheck(ctx, other, "member-allowed", "passed"); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := s.LiveGate(ctx, "docs"); !errors.Is(err, ErrLiveChecksMissing) { |
| + t.Fatalf("a check against another origin: gate %v", err) |
| + } |
| + |
| + s = newJournal(t) |
| + passAll(s, other) |
| + if err := s.LiveGate(ctx, "docs"); !errors.Is(err, ErrLiveChecksMissing) { |
| + t.Fatalf("every check passed against another origin: gate %v", err) |
| + } |
| + |
| + s = newJournal(t) |
| + passAll(s, Issuer) |
| + if _, err := s.db.ExecContext(ctx, "INSERT INTO adopt_checks(name,outcome) VALUES('member-allowed','passed')"); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := s.LiveGate(ctx, "docs"); !errors.Is(err, ErrLiveChecksMissing) { |
| + t.Fatalf("a check with no origin: gate %v", err) |
| + } |
| + |
| + for _, bad := range []string{"", "connect.oficina.works", "http://connect.oficina.works", "https://connect.oficina.works/x", "https://user@connect.oficina.works", "https://connect.oficina.works?x=1"} { |
| + if err := s.SaveCheck(ctx, bad, "member-allowed", "passed"); err == nil { |
| + t.Fatalf("origin %q accepted", bad) |
| + } |
| + } |
| +} |
| + |
| func TestLiveGateInconclusive(t *testing.T) { |
| ctx := context.Background() |
| s := newJournal(t) |
| for _, name := range RequiredLiveChecks { |
| - if err := s.SaveCheck(ctx, name, "inconclusive"); err != nil { |
| + if err := s.SaveCheck(ctx, Issuer, name, "inconclusive"); err != nil { |
| t.Fatal(err) |
| } |
| } |
| @@ -347,17 +395,17 @@ func TestLiveGateInconclusive(t *testing.T) { |
| t.Fatalf("inconclusive checks opened the gate: %v", err) |
| } |
| for _, name := range RequiredLiveChecks { |
| - if err := s.SaveCheck(ctx, name, "passed"); err != nil { |
| + if err := s.SaveCheck(ctx, Issuer, name, "passed"); err != nil { |
| t.Fatal(err) |
| } |
| } |
| - if err := s.SaveCheck(ctx, "owner-revoked-refused", "inconclusive"); err != nil { |
| + if err := s.SaveCheck(ctx, Issuer, "owner-revoked-refused", "inconclusive"); err != nil { |
| t.Fatal(err) |
| } |
| if err := s.LiveGate(ctx, "docs"); err != nil { |
| t.Fatalf("an inconclusive repeat closed the gate: %v", err) |
| } |
| - if err := s.SaveCheck(ctx, "member-allowed", "failed:forbidden"); err != nil { |
| + if err := s.SaveCheck(ctx, Issuer, "member-allowed", "failed:forbidden"); err != nil { |
| t.Fatal(err) |
| } |
| if err := s.LiveGate(ctx, "docs"); !errors.Is(err, ErrLiveChecksMissing) { |
| @@ -401,7 +449,7 @@ func TestAdoptRowsAreSealed(t *testing.T) { |
| s.SaveRollback(ctx, RollbackRecord{App: "docs", State: "sent", Receipt: receipt()}), |
| s.SaveAfter(ctx, "docs", after), |
| s.SaveWitness(ctx, Witness{App: "docs", Typed: "docs", Identity: after.Identity, Delivery: after.Delivery, AfterVersion: versionB, At: at}), |
| - s.SaveCheck(ctx, "member-allowed", "passed"), |
| + s.SaveCheck(ctx, Issuer, "member-allowed", "passed"), |
| } |
| for i, err := range steps { |
| if err != nil { |
diff --git a/adopt/words.go b/adopt/words.go| index 437bec6..71aa3f3 100644 |
| --- a/adopt/words.go |
| +++ b/adopt/words.go |
| @@ -63,6 +63,7 @@ var LiveSteps = []string{ |
| // What RunLive says when it refuses to start, after "adopt: ". |
| const ( |
| LiveNeedOrigin = "the live checks need connect's https origin" |
| + LiveNeedIssuer = "the live checks run only against the connect the apps trust" |
| LiveNeedTeam = "the live checks need the team" |
| LiveNeedEvent = "the foreign event is not an event id" |
| LiveNeedRunners = "the live checks need the journal, a prompter and a browser" |
diff --git a/cmd/alta-adopt/main_test.go b/cmd/alta-adopt/main_test.go| index 500aa67..30715ff 100644 |
| --- a/cmd/alta-adopt/main_test.go |
| +++ b/cmd/alta-adopt/main_test.go |
| @@ -515,9 +515,19 @@ func rehearse(t *testing.T) *rig { |
| r.tick(homeop.WindowLifetime * time.Second) |
| // The live checks' steps come through the command's own writer. |
| - if out := r.dry("", false, "live", "--event", "7", "--connect", r.connect.URL); !strings.Contains(out, strings.Join(adopt.LiveSteps, "\n")) { |
| + if out := r.dry("", false, "live", "--event", "7"); !strings.Contains(out, strings.Join(adopt.LiveSteps, "\n")) { |
| t.Fatalf("live without --apply did not list its steps\n%s", out) |
| } |
| + // They run against production connect only, the issuer the apps |
| + // trust: another deployment is refused, applied or not, and never |
| + // contacted. |
| + for _, apply := range [][]string{nil, {"--apply"}} { |
| + before := r.writes() |
| + r.refused(adopt.LiveNeedIssuer, "", false, append([]string{"live", "--event", "7", "--connect", r.connect.URL}, apply...)...) |
| + if after := r.writes(); after != before { |
| + t.Fatalf("a refused live wrote or contacted connect: %+v %+v", before, after) |
| + } |
| + } |
| // The rollback's dry run names the keys it would remove, the bundle |
| // and the host it would restart. |
| @@ -728,7 +738,7 @@ func (r *rig) saveChecks(during ...func()) { |
| r.t.Fatal(err) |
| } |
| for _, c := range adopt.RequiredLiveChecks { |
| - if err := js.SaveCheck(context.Background(), c, "passed"); err != nil { |
| + if err := js.SaveCheck(context.Background(), adopt.Issuer, c, "passed"); err != nil { |
| r.t.Fatal(err) |
| } |
| } |
| @@ -767,7 +777,7 @@ func TestLiveRefusedBeforeCanaryActive(t *testing.T) { |
| r := newRig(t) |
| r.throughPlanA() |
| before := r.writes() |
| - r.refused("canary-not-active", "", false, "live", "--event", "7", "--connect", r.connect.URL, "--apply") |
| + r.refused("canary-not-active", "", false, "live", "--event", "7", "--apply") |
| if after := r.writes(); after != before { |
| t.Fatalf("a refused live wrote: %+v %+v", before, after) |
| } |