link: report a pending rotation end to end; links ls removing states
Astra's final review, Important 3. When every registry sync after a
rotation fails, the serving door still answers the new key but adds
"revocation":"pending": the hub registry keeps the old hash and the hub
may still accept the old key. The home console saves the new key,
records rotate_pending on its link row (additive column, not hashed),
and answers {"rotated", "revocation":"pending", "console"}. carlos link
rotate prints a draft pending line instead of LD-C64, and carlos link
status reports key_revocation pending until a serving call succeeds,
which proves the sync applied (linkRoute syncs first), and clears it.
Fable's minor: carlos links ls now shows the removing states the API
reports (offline, may still answer, not checked) instead of one word.
copyLinksFlagAccountID loses its em dash for the reviewed draft.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
12 files changed,
+310
−49
apimode.go+27 −14docs/site/linked-deployments.md+2 −0internal/console/apilinks.go+8 −2internal/console/apilinks_test.go+79 −4internal/console/homelink.go+29 −2internal/console/linkclient.go+11 −7internal/console/view.go+1 −0internal/registry/homelink.go+40 −11internal/registry/homelink_test.go+45 −0internal/registry/remote_dispatch.go+2 −0link_cli.go+30 −4link_cli_test.go+36 −5
diff --git a/apimode.go b/apimode.go| index 25eaf8a43..00fa4dabf 100644 |
| --- a/apimode.go |
| +++ b/apimode.go |
| @@ -5917,16 +5917,19 @@ func (s *apiSession) setCopies(ctx context.Context, a apiAcct, app string, n int |
| // apiHomeLink is GET /api/cli/link. |
| type apiHomeLink struct { |
| - Linked bool `json:"linked"` |
| - Console string `json:"console"` |
| - Account string `json:"account"` |
| - Name string `json:"name"` // "" when the serving console could not be asked |
| - Box string `json:"box"` |
| - State string `json:"state"` |
| - Apps []apiHomeLinkApp `json:"apps"` |
| - Revocation string `json:"revocation"` |
| - ServingError string `json:"serving_error"` |
| - Start *apiHomeLinkStart `json:"start"` |
| + Linked bool `json:"linked"` |
| + Console string `json:"console"` |
| + Account string `json:"account"` |
| + Name string `json:"name"` // "" when the serving console could not be asked |
| + Box string `json:"box"` |
| + State string `json:"state"` |
| + Apps []apiHomeLinkApp `json:"apps"` |
| + Revocation string `json:"revocation"` |
| + // KeyRevocation is "pending" while the last rotation waits for the |
| + // serving console's hub to stop accepting the old key. |
| + KeyRevocation string `json:"key_revocation"` |
| + ServingError string `json:"serving_error"` |
| + Start *apiHomeLinkStart `json:"start"` |
| } |
| type apiHomeLinkApp struct { |
| @@ -6056,16 +6059,26 @@ func (s *apiSession) homeLinkUnpublish(ctx context.Context, name string) (apiLin |
| return out, decodeLinkAnswer(body, &out) |
| } |
| +// apiLinkRotated is POST /api/cli/link/rotate's answer: {"rotated": true}, |
| +// with "revocation": "pending" and the serving console's address while that |
| +// console's hub may still accept the old key. |
| +type apiLinkRotated struct { |
| + Rotated bool `json:"rotated"` |
| + Revocation string `json:"revocation"` |
| + Console string `json:"console"` |
| +} |
| + |
| // homeLinkRotate is POST /api/cli/link/rotate. |
| -func (s *apiSession) homeLinkRotate(ctx context.Context) error { |
| +func (s *apiSession) homeLinkRotate(ctx context.Context) (apiLinkRotated, error) { |
| + var out apiLinkRotated |
| status, body, err := s.postJSON(ctx, "/api/cli/link/rotate", struct{}{}) |
| if err != nil { |
| - return err |
| + return out, err |
| } |
| if status != http.StatusOK { |
| - return apiErrorFromBody(status, body) |
| + return out, apiErrorFromBody(status, body) |
| } |
| - return nil |
| + return out, decodeLinkAnswer(body, &out) |
| } |
| // homeLinkRemove is DELETE /api/cli/link, with ?local=1 to forget the link |
diff --git a/docs/site/linked-deployments.md b/docs/site/linked-deployments.md| index 2b80736e8..2bfc2adfc 100644 |
| --- a/docs/site/linked-deployments.md |
| +++ b/docs/site/linked-deployments.md |
| @@ -126,6 +126,8 @@ carlos link rotate |
| This replaces the link's key, and the old key stops working. Nobody ever sees the key: it goes straight from Carloku into your registry file. Rotate it if you think it has leaked, for example if a copy of the registry file got out. |
| +Sometimes Carloku can't finish the change straight away. The command then says Carloku still accepts the old key, and Carloku keeps trying by itself. `carlos link status` says so too, until the change is done. |
| + |
| Your box reconnects with the new key, so visitors may see the offline page for a moment. |
| ## Unlink |
diff --git a/internal/console/apilinks.go b/internal/console/apilinks.go| index 65d8b82ba..e19225231 100644 |
| --- a/internal/console/apilinks.go |
| +++ b/internal/console/apilinks.go |
| @@ -435,7 +435,11 @@ const linkSyncTimeout = 10 * time.Second |
| // hash, which makes the hub close the connection on the old one (Task 3's |
| // SetDynamicAuth). The new token is answered even when the sync keeps |
| // failing: the old one is already revoked here, and linkSyncLoop moves the |
| -// channel later. |
| +// channel later. Until it does, the hub registry still holds the old hash |
| +// and the hub may still accept the old token, so that answer says so: |
| +// 200 {"token", "revocation":"pending"}, as unlink's 202 does. Every later |
| +// link door syncs before it answers (linkRoute), so the home learns the |
| +// rotation has applied from its next successful call. |
| func (s *Server) handleLinkRotate(w http.ResponseWriter, r *http.Request) { |
| _, l, _, ok := s.linkCaller(w, r) |
| if !ok { |
| @@ -454,7 +458,9 @@ func (s *Server) handleLinkRotate(w http.ResponseWriter, r *http.Request) { |
| ctx, cancel := context.WithTimeout(context.WithoutCancel(r.Context()), linkSyncTimeout) |
| defer cancel() |
| if err := s.syncLinkRetry(ctx, l.ID); err != nil { |
| - log.Printf("console: link %s: rotate: channel sync: %v (linkSyncLoop retries)", l.ID, err) |
| + log.Printf("console: link %s: rotate: channel sync: %v (linkSyncLoop retries; the old credential may still be accepted until it does)", l.ID, err) |
| + writeJSON(w, http.StatusOK, map[string]string{"token": raw, "revocation": "pending"}) |
| + return |
| } |
| log.Printf("console: link %s rotated its credential", l.ID) |
| writeJSON(w, http.StatusOK, map[string]string{"token": raw}) |
diff --git a/internal/console/apilinks_test.go b/internal/console/apilinks_test.go| index c9fdda1b9..ae1090b05 100644 |
| --- a/internal/console/apilinks_test.go |
| +++ b/internal/console/apilinks_test.go |
| @@ -1054,6 +1054,76 @@ func (f *linkGenFault) RevokeLinkChannel(id string) (bool, error) { |
| return f.consoleRegistry.RevokeLinkChannel(id) |
| } |
| +// TestRotatePendingCarriedToTheHome is the rotation's whole output contract |
| +// while the serving console's registry still holds the old key (Astra final |
| +// review, Important 3): a real home console rotating through a real serving |
| +// console whose registry refuses the new hash. The serving door answers the |
| +// new key with "revocation":"pending"; the home saves the key and answers |
| +// pending with the serving console's address (the CLI prints its pending |
| +// line from that, not LD-C64; link_cli_test.go); the home's status reports |
| +// key_revocation pending while the serving console cannot be asked, and |
| +// stops once a serving call shows the change applied. Throughout, the hub |
| +// registry's channel holds the old key's hash: the old key still |
| +// authenticates at the hub. |
| +func TestRotatePendingCarriedToTheHome(t *testing.T) { |
| + serving := newLinkFixture(t, "tunnel.example.com") |
| + l := mintLink(t, serving, signInPaul(t, serving), "shop-home") |
| + fault := &linkGenFault{consoleRegistry: linkRegistryOf(t, serving), gen: 2, putFail: 1 << 20} |
| + serving.srv.linkRegistry = func() (consoleRegistry, error) { return fault, nil } |
| + hubHash := func() string { |
| + auth, err := linkRegistryOf(t, serving).LinkChannelAuth() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return auth[registry.LinkChannelName(l.id)] |
| + } |
| + |
| + home := newHomeFixture(t) |
| + op := cliToken(t, home, "paul@keymail.dev", "code-home-rotate-pending") |
| + if err := homeRegistry(t, home).PutHomeLink(registry.HomeLink{ID: l.id, ServingURL: serving.ts.URL, |
| + TunnelURL: "https://tunnel.example.com/register", Token: l.token, Account: serving.sqid, AppsDomain: "apps.example.com"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + |
| + status, body := homeReq(t, home, http.MethodPost, "/api/cli/link/rotate", op, "") |
| + var out map[string]any |
| + if err := json.Unmarshal([]byte(body), &out); status != http.StatusOK || err != nil || |
| + out["rotated"] != true || out["revocation"] != "pending" || out["console"] != serving.ts.URL { |
| + t.Fatalf("home rotate = %d %s; want 200 rotated, revocation pending, console %s", status, body, serving.ts.URL) |
| + } |
| + stored, err := homeRegistry(t, home).HomeLink() |
| + if err != nil || stored == nil || stored.Token == l.token || !stored.RotatePending { |
| + t.Fatalf("after rotate the home holds %+v, %v; want the new key, rotation pending", stored, err) |
| + } |
| + if h := hubHash(); h != hashToken(l.token) { |
| + t.Fatalf("hub channel hash = %q, want the old key's: the test would prove nothing", h) |
| + } |
| + if st := homeStatus(t, home, op); st.KeyRevocation != "pending" || st.ServingError == "" { |
| + t.Fatalf("status while the sync fails = %+v; want key_revocation pending and the serving error", st) |
| + } |
| + if h := hubHash(); h != hashToken(l.token) { |
| + t.Fatalf("hub channel hash = %q after the status, want still the old key's", h) |
| + } |
| + |
| + // The registry takes the new hash: the next serving call syncs it, and |
| + // the status stops reporting the rotation. |
| + fault.mu.Lock() |
| + fault.putFail = 0 |
| + fault.mu.Unlock() |
| + if st := homeStatus(t, home, op); st.KeyRevocation != "" || st.ServingError != "" { |
| + t.Fatalf("status once the sync applies = %+v; want no pending rotation", st) |
| + } |
| + if h := hubHash(); h != hashToken(stored.Token) { |
| + t.Fatalf("hub channel hash = %q, want the new key's", h) |
| + } |
| + if l, _ := homeRegistry(t, home).HomeLink(); l == nil || l.RotatePending { |
| + t.Fatalf("the home still records the rotation pending: %+v", l) |
| + } |
| + if st := homeStatus(t, home, op); st.KeyRevocation != "" { |
| + t.Fatalf("a later status = %+v", st) |
| + } |
| +} |
| + |
| // TestRotateAndUnlinkRetryTheirSync: the rotate and unlink doors retry their |
| // own sync three times before returning; past that the change stands and |
| // linkSyncPass finishes it. |
| @@ -1065,12 +1135,15 @@ func TestRotateAndUnlinkRetryTheirSync(t *testing.T) { |
| fx.srv.linkRegistry = func() (consoleRegistry, error) { return fault, nil } |
| return fx, l, fault |
| } |
| - rotate := func(t *testing.T, fx *consoleFixture, l linked) string { |
| + rotate := func(t *testing.T, fx *consoleFixture, l linked, wantRevocation string) string { |
| status, body := linkDoor(t, fx, http.MethodPost, "/api/link/rotate", l.token, "") |
| var out map[string]string |
| if err := json.Unmarshal([]byte(body), &out); status != http.StatusOK || err != nil || out["token"] == "" { |
| t.Fatalf("rotate = %d %s", status, body) |
| } |
| + if out["revocation"] != wantRevocation { |
| + t.Fatalf("rotate answered revocation %q, want %q: %s", out["revocation"], wantRevocation, body) |
| + } |
| return out["token"] |
| } |
| channel := func(t *testing.T, fx *consoleFixture, id string) (string, bool) { |
| @@ -1084,15 +1157,17 @@ func TestRotateAndUnlinkRetryTheirSync(t *testing.T) { |
| t.Run("rotate heals within its retries", func(t *testing.T) { |
| fx, l, fault := setup(t) |
| fault.putFail = 3 |
| - tokenB := rotate(t, fx, l) |
| + tokenB := rotate(t, fx, l, "") |
| if h, _ := channel(t, fx, l.id); h != hashToken(tokenB) || fault.putCalls != 4 { |
| t.Fatalf("after rotate the channel = %q after %d tries, want the new hash after 4", h, fault.putCalls) |
| } |
| }) |
| - t.Run("rotate answers the token when every try fails", func(t *testing.T) { |
| + t.Run("rotate answers the token, revocation pending, when every try fails", func(t *testing.T) { |
| fx, l, fault := setup(t) |
| fault.putFail = 4 |
| - tokenB := rotate(t, fx, l) |
| + // The answer must not claim the old key is dead: the hub registry |
| + // still holds its hash, so the hub still takes it. |
| + tokenB := rotate(t, fx, l, "pending") |
| if fault.putCalls != 4 { |
| t.Fatalf("rotate tried the sync %d times, want 4", fault.putCalls) |
| } |
diff --git a/internal/console/homelink.go b/internal/console/homelink.go| index c19cafd8f..3b12d289b 100644 |
| --- a/internal/console/homelink.go |
| +++ b/internal/console/homelink.go |
| @@ -374,6 +374,9 @@ type homeLinkWire struct { |
| // Revocation is "pending" while a remove waits for the serving console |
| // to cut the connection. |
| Revocation string `json:"revocation,omitempty"` |
| + // KeyRevocation is "pending" while the last key rotation waits for the |
| + // serving console's hub to stop accepting the old key. |
| + KeyRevocation string `json:"key_revocation,omitempty"` |
| // ServingError says why the serving console could not be asked. |
| ServingError string `json:"serving_error,omitempty"` |
| Start *homeLinkStartWire `json:"start,omitempty"` |
| @@ -440,11 +443,23 @@ func (s *Server) handleAPIHomeLinkStatus(w http.ResponseWriter, r *http.Request) |
| defer cancel() |
| if st, err := newLinkClient(l.ServingURL, l.Token).Status(ctx); err != nil { |
| out.ServingError = homeLinkServingSentence(l.ServingURL, err) |
| + if l.RotatePending { |
| + out.KeyRevocation = "pending" |
| + } |
| } else { |
| out.Name = st.Name |
| for _, p := range st.Publications { |
| urls[p.App] = p.URL |
| } |
| + // The serving console answers a link door only after syncing the |
| + // link's channel (linkRoute), so this answer, with the new key, is |
| + // the rotation applied: the hub registry no longer holds the old |
| + // key. |
| + if l.RotatePending { |
| + if err := reg.SetHomeLinkRotatePending(l.ID, false); err != nil { |
| + log.Printf("console: home link %s: clear the pending rotation: %v", l.ID, err) |
| + } |
| + } |
| } |
| for _, a := range apps { |
| u := urls[a.Name] |
| @@ -696,7 +711,10 @@ func (s *Server) handleAPIHomeLinkUnpublish(w http.ResponseWriter, r *http.Reque |
| // credential on the serving console, then here. If the new one cannot be |
| // saved here, the old one is already dead there: the answer is LD-C49, |
| // whose recovery (remove --local, then unlink on the Fleet tab) is the way |
| -// out. |
| +// out. When the serving console says the old key's revocation is pending |
| +// (its hub may still accept it), the answer says so too, 200 {"rotated", |
| +// "revocation":"pending", "console"}, and the link keeps the flag until a |
| +// later serving call shows the change applied (handleAPIHomeLinkStatus). |
| func (s *Server) handleAPIHomeLinkRotate(w http.ResponseWriter, r *http.Request) { |
| if _, _, ok := s.apiSystemRoller(w, r); !ok { |
| return |
| @@ -711,7 +729,7 @@ func (s *Server) handleAPIHomeLinkRotate(w http.ResponseWriter, r *http.Request) |
| if !ok { |
| return |
| } |
| - tok, err := newLinkClient(l.ServingURL, l.Token).Rotate(homeLinkContext(r)) |
| + tok, pending, err := newLinkClient(l.ServingURL, l.Token).Rotate(homeLinkContext(r)) |
| if err != nil { |
| homeLinkServingFailed(w, l.ServingURL, err) |
| return |
| @@ -723,6 +741,15 @@ func (s *Server) handleAPIHomeLinkRotate(w http.ResponseWriter, r *http.Request) |
| jsonError(w, http.StatusInternalServerError, linkCopy(copyLD49, "console", l.ServingURL)) |
| return |
| } |
| + // Kept in the link's row, so a restarted console still reports it. |
| + if err := reg.SetHomeLinkRotatePending(l.ID, pending); err != nil { |
| + log.Printf("console: home link %s: record the rotation pending=%v: %v", l.ID, pending, err) |
| + } |
| + if pending { |
| + log.Printf("console: home link %s: key rotated; %s has not stopped accepting the old key yet", l.ID, l.ServingURL) |
| + writeJSON(w, http.StatusOK, map[string]any{"rotated": true, "revocation": "pending", "console": l.ServingURL}) |
| + return |
| + } |
| log.Printf("console: home link %s: key rotated", l.ID) |
| writeJSON(w, http.StatusOK, map[string]bool{"rotated": true}) |
| } |
diff --git a/internal/console/linkclient.go b/internal/console/linkclient.go| index f71da1c78..06eedc418 100644 |
| --- a/internal/console/linkclient.go |
| +++ b/internal/console/linkclient.go |
| @@ -229,19 +229,23 @@ func (c *linkClient) Unpublish(ctx context.Context, app string) error { |
| return err |
| } |
| -// Rotate replaces the link credential and returns the new one. The old one |
| -// stops working on the serving side as soon as this answers. |
| -func (c *linkClient) Rotate(ctx context.Context) (string, error) { |
| +// Rotate replaces the link credential and returns the new one. The serving |
| +// console stops taking the old one at its doors as soon as this answers; |
| +// pending is its "revocation":"pending": its hub registry had not yet |
| +// dropped the old one, so the hub may still accept it until the serving |
| +// console's own retry gets the change through. |
| +func (c *linkClient) Rotate(ctx context.Context) (token string, pending bool, err error) { |
| var out struct { |
| - Token string `json:"token"` |
| + Token string `json:"token"` |
| + Revocation string `json:"revocation"` |
| } |
| if _, err := c.do(ctx, http.MethodPost, "/api/link/rotate", nil, &out, true); err != nil { |
| - return "", err |
| + return "", false, err |
| } |
| if out.Token == "" { |
| - return "", errors.New("serving console: rotate answered no key") |
| + return "", false, errors.New("serving console: rotate answered no key") |
| } |
| - return out.Token, nil |
| + return out.Token, out.Revocation == "pending", nil |
| } |
| // Remove unlinks on the serving console. pending is its 202: the link is |
diff --git a/internal/console/view.go b/internal/console/view.go| index ff5926faf..3161b2074 100644 |
| --- a/internal/console/view.go |
| +++ b/internal/console/view.go |
| @@ -562,6 +562,7 @@ type consoleRegistry interface { |
| HomeLink() (*registry.HomeLink, error) |
| PutHomeLink(registry.HomeLink) error |
| SetHomeLinkUnlinkPending(id string, pending bool) error |
| + SetHomeLinkRotatePending(id string, pending bool) error |
| DeleteHomeLink() error |
| HomeLinkApps() ([]registry.HomeLinkApp, error) |
| PutHomeLinkApp(registry.HomeLinkApp) error |
diff --git a/internal/registry/homelink.go b/internal/registry/homelink.go| index 3c79b3b66..6be15206a 100644 |
| --- a/internal/registry/homelink.go |
| +++ b/internal/registry/homelink.go |
| @@ -35,14 +35,18 @@ import ( |
| // Fingerprint. UnlinkPending records that the serving console answered an |
| // unlink with 202 (revoked there, its connection not cut yet), so the home |
| // keeps the link and a later remove finishes it; set by the home console, |
| -// not hashed by Fingerprint. ConnGen counts the changes that must replace the link's |
| +// not hashed by Fingerprint. RotatePending records the same for a key |
| +// rotation: the serving console answered the new key but its registry had |
| +// not yet stopped accepting the old one (its "revocation":"pending"); set |
| +// and cleared by the home console, not hashed by Fingerprint. ConnGen |
| +// counts the changes that must replace the link's |
| // tunnel connection (DeleteHomeLinkApp, and PutHomeLinkApp retargeting a |
| // name); it is hashed, so each one reloads the agent. |
| type HomeLink struct { |
| ID, ServingURL, TunnelURL, Token, Account, AppsDomain string |
| BoxLabel, State string |
| ConnGen uint64 |
| - UnlinkPending bool |
| + UnlinkPending, RotatePending bool |
| CreatedAt time.Time |
| } |
| @@ -123,9 +127,12 @@ CREATE TABLE IF NOT EXISTS home_link ( |
| );`); err != nil { |
| return err |
| } |
| - // unlink_pending: additive, beside the brief's columns (SetHomeLinkUnlinkPending). |
| - if _, err := r.sql.Exec(`ALTER TABLE home_link ADD COLUMN unlink_pending INTEGER NOT NULL DEFAULT 0`); err != nil && !strings.Contains(err.Error(), "duplicate column") { |
| - return err |
| + // unlink_pending and rotate_pending: additive, beside the brief's |
| + // columns (SetHomeLinkUnlinkPending, SetHomeLinkRotatePending). |
| + for _, col := range []string{"unlink_pending", "rotate_pending"} { |
| + if _, err := r.sql.Exec(`ALTER TABLE home_link ADD COLUMN ` + col + ` INTEGER NOT NULL DEFAULT 0`); err != nil && !strings.Contains(err.Error(), "duplicate column") { |
| + return err |
| + } |
| } |
| _, err := r.sql.Exec(` |
| CREATE TABLE IF NOT EXISTS home_link_apps ( |
| @@ -139,17 +146,18 @@ CREATE TABLE IF NOT EXISTS home_link_apps ( |
| } |
| const homeLinkCols = `id, COALESCE(serving_url, ''), COALESCE(tunnel_url, ''), COALESCE(token, ''), |
| - COALESCE(account, ''), COALESCE(apps_domain, ''), box_label, state, conn_gen, unlink_pending, COALESCE(created_at, 0)` |
| + COALESCE(account, ''), COALESCE(apps_domain, ''), box_label, state, conn_gen, unlink_pending, rotate_pending, COALESCE(created_at, 0)` |
| func scanHomeLink(scan func(...any) error) (*HomeLink, error) { |
| var l HomeLink |
| - var gen, pending, created int64 |
| + var gen, pending, rotating, created int64 |
| if err := scan(&l.ID, &l.ServingURL, &l.TunnelURL, &l.Token, &l.Account, &l.AppsDomain, |
| - &l.BoxLabel, &l.State, &gen, &pending, &created); err != nil { |
| + &l.BoxLabel, &l.State, &gen, &pending, &rotating, &created); err != nil { |
| return nil, err |
| } |
| l.ConnGen = uint64(gen) |
| l.UnlinkPending = pending != 0 |
| + l.RotatePending = rotating != 0 |
| if created != 0 { |
| l.CreatedAt = time.Unix(created, 0) |
| } |
| @@ -293,6 +301,26 @@ func (r *Registry) SetHomeLinkUnlinkPending(id string, pending bool) error { |
| return err |
| } |
| +// SetHomeLinkRotatePending records whether link id's last key rotation is |
| +// still pending on the serving console (it answered the new key with |
| +// "revocation":"pending": its registry may still accept the old one). |
| +// Another ID, or no link, changes nothing. Not hashed by Fingerprint. |
| +func (r *Registry) SetHomeLinkRotatePending(id string, pending bool) error { |
| + if !ValidLinkID(id) { |
| + return fmt.Errorf("registry: invalid link id %q", id) |
| + } |
| + if r.remote != nil { |
| + _, err := r.remote.call(nil, remoteRequest{Method: "SetHomeLinkRotatePending", LinkID: id, On: pending}, nil) |
| + return err |
| + } |
| + v := 0 |
| + if pending { |
| + v = 1 |
| + } |
| + _, err := r.sql.Exec(`UPDATE home_link SET rotate_pending = ? WHERE id = ?`, v, id) |
| + return err |
| +} |
| + |
| // HomeLinkApps returns the published set, ordered by name. Fresh |
| // connection. |
| func (r *Registry) HomeLinkApps() ([]HomeLinkApp, error) { |
| @@ -431,9 +459,10 @@ func bumpHomeLinkConnGen(tx txer) error { |
| // its own section marker written only when a row exists, so a registry that |
| // holds no link digests as it did before the tables existed. home_link |
| // hashes everything the agent's link loop acts on, conn_gen and the token's |
| -// sha256 included (never the token itself); state, unlink_pending and |
| -// created_at are not hashed: the agent writes state on every connect and |
| -// disconnect, and unlink_pending is the home console's own bookkeeping. |
| +// sha256 included (never the token itself); state, unlink_pending, |
| +// rotate_pending and created_at are not hashed: the agent writes state on |
| +// every connect and disconnect, and the two pending flags are the home |
| +// console's own bookkeeping. |
| func fingerprintHomeLink(db *sql.DB, h hash.Hash) error { |
| rows, err := db.Query(`SELECT ` + homeLinkCols + ` FROM home_link ORDER BY id`) |
| if err != nil { |
diff --git a/internal/registry/homelink_test.go b/internal/registry/homelink_test.go| index 911c82105..c6169e14e 100644 |
| --- a/internal/registry/homelink_test.go |
| +++ b/internal/registry/homelink_test.go |
| @@ -413,6 +413,45 @@ func TestHomeLinkUnlinkPending(t *testing.T) { |
| } |
| } |
| +// TestHomeLinkRotatePending: the rotation flag round-trips on its own link |
| +// only, survives the rotation's own PutHomeLink, and does not move the |
| +// fingerprint. |
| +func TestHomeLinkRotatePending(t *testing.T) { |
| + reg := openHomeRegistry(t) |
| + mustPutHomeLink(t, reg, testHomeLink()) |
| + before, err := reg.Fingerprint() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := reg.SetHomeLinkRotatePending(testLinkB, true); err != nil { |
| + t.Fatal(err) |
| + } |
| + if l, _ := reg.HomeLink(); l.RotatePending { |
| + t.Fatal("another link's ID set the flag") |
| + } |
| + if err := reg.SetHomeLinkRotatePending(testLinkA, true); err != nil { |
| + t.Fatal(err) |
| + } |
| + if after, _ := reg.Fingerprint(); after != before { |
| + t.Fatal("the rotation flag moved the fingerprint") |
| + } |
| + rotated := testHomeLink() |
| + rotated.Token = "rotated-token" |
| + mustPutHomeLink(t, reg, rotated) |
| + if l, _ := reg.HomeLink(); l == nil || !l.RotatePending || l.UnlinkPending || l.Token != "rotated-token" { |
| + t.Fatalf("after the rotation's save HomeLink = %+v; want the flag kept", l) |
| + } |
| + if err := reg.SetHomeLinkRotatePending(testLinkA, false); err != nil { |
| + t.Fatal(err) |
| + } |
| + if l, _ := reg.HomeLink(); l.RotatePending { |
| + t.Fatal("the flag was not cleared") |
| + } |
| + if err := reg.SetHomeLinkRotatePending("nope", true); err == nil { |
| + t.Fatal("a malformed ID was taken") |
| + } |
| +} |
| + |
| func TestHomeLinkCallsOverRemote(t *testing.T) { |
| reg, path := remoteTestRegistry(t) |
| chmodRegistry(t, path, 0o644) |
| @@ -439,6 +478,12 @@ func TestHomeLinkCallsOverRemote(t *testing.T) { |
| if l, _ := reg.HomeLink(); !l.UnlinkPending { |
| t.Fatal("remote SetHomeLinkUnlinkPending did not record the flag") |
| } |
| + if err := reg.SetHomeLinkRotatePending(testLinkA, true); err != nil { |
| + t.Fatal(err) |
| + } |
| + if l, _ := reg.HomeLink(); !l.RotatePending { |
| + t.Fatal("remote SetHomeLinkRotatePending did not record the flag") |
| + } |
| shop := HomeLinkApp{Name: "shop", HomeAccount: "acct1", HomeApp: "shop", HomeHost: "h"} |
| if err := reg.PutHomeLinkApp(shop); err != nil { |
| t.Fatal(err) |
diff --git a/internal/registry/remote_dispatch.go b/internal/registry/remote_dispatch.go| index 3bba7de2e..11506d4ee 100644 |
| --- a/internal/registry/remote_dispatch.go |
| +++ b/internal/registry/remote_dispatch.go |
| @@ -301,6 +301,8 @@ func (r *Registry) dispatchRemote(ctx context.Context, q remoteRequest, callback |
| err = r.SetHomeLinkState(q.Status) |
| case "SetHomeLinkUnlinkPending": |
| err = r.SetHomeLinkUnlinkPending(q.LinkID, q.On) |
| + case "SetHomeLinkRotatePending": |
| + err = r.SetHomeLinkRotatePending(q.LinkID, q.On) |
| case "HomeLinkApps": |
| out.HomeLinkApps, err = r.HomeLinkApps() |
| case "HomeLinkAppByName": |
diff --git a/link_cli.go b/link_cli.go| index 7661d6d59..67efe08ed 100644 |
| --- a/link_cli.go |
| +++ b/link_cli.go |
| @@ -69,7 +69,7 @@ const ( |
| copyLinkFlagPublishName = "the name to publish it under (default: the app's own name)" |
| copyLinkFlagLocal = "forget the link here without asking the other console" |
| copyLinksFlagAccount = "account (sqid or name) to list linked deployments for (default: your only account, or the configured/chosen one)" |
| - copyLinksFlagAccountID = "account sqid, matched as a sqid ONLY — supersedes --account" |
| + copyLinksFlagAccountID = "account sqid, matched as a sqid only; overrides --account" |
| copyLinksFlagConsole = "console to list through (default: the only logged-in one)" |
| // Refused in direct bucket mode: links live in consoles only. |
| copyLinkDirectMode = "carlos {verb} works through a console, and this shell is in direct bucket mode\n" + |
| @@ -88,6 +88,13 @@ const ( |
| // status: a remove got 202 and the serving console has not cut the |
| // connection yet. |
| copyLinkStatusUnlinkPending = "unlinking: {console} has not cut the connection yet. run carlos link remove again to finish" |
| + // rotate: the serving console answered the new key, but its hub may |
| + // still accept the old one until its own retry gets the change through |
| + // (Astra final review, Important 3). In place of LD-C64. |
| + copyLinkRotatePending = "replaced the link's key here. {console} still accepts the old key until it finishes the change, which it keeps trying by itself. run carlos link status to check" |
| + // status: the same, until a call to the serving console shows the |
| + // change applied. |
| + copyLinkStatusRotatePending = "key rotation: {console} still accepts the old key until it finishes the change, which it keeps trying by itself" |
| // remove: 202 with no message of the console's own. |
| copyLinkRemovePending = "Carloku has not cut this deployment's connection yet. run carlos link remove again to finish here" |
| // publish and unpublish when the console sent no URL. |
| @@ -97,7 +104,13 @@ const ( |
| copyLinksYes = "yes" |
| copyLinksNo = "no" |
| copyLinksUnknown = "unknown" |
| - copyLinksRemoving = "removing" |
| + // links ls's REACHABLE cell for a removing link, by the revocation the |
| + // console reports (as the Fleet tab's card lines tell them apart): the |
| + // hub has cut it (offline), has not yet (its apps may still answer), or |
| + // could not be read. |
| + copyLinksRemoving = "removing, offline" |
| + copyLinksRemovingPending = "removing, may still answer" |
| + copyLinksRemovingUnknown = "removing, not checked" |
| copyLinksAgo = "{ago} ago" |
| // An answer this CLI could not decode. |
| copyLinkUnreadable = "the console's answer could not be read" |
| @@ -340,6 +353,9 @@ func linkStatus(ctx context.Context, sess *apiSession, out io.Writer) error { |
| if l.Revocation == "pending" { |
| fmt.Fprintln(out, fillLinkCopy(copyLinkStatusUnlinkPending, "console", l.Console)) |
| } |
| + if l.KeyRevocation == "pending" { |
| + fmt.Fprintln(out, fillLinkCopy(copyLinkStatusRotatePending, "console", l.Console)) |
| + } |
| if l.Box == "" { |
| fmt.Fprintln(out, copyLD58) |
| } else { |
| @@ -484,11 +500,17 @@ func runLinkRotate(args []string) { |
| } |
| // linkRotate replaces the link's credential, on the serving console and |
| -// then at home. |
| +// then at home. LD-C64 says the old key no longer works; while the serving |
| +// console's hub may still accept it, the pending line says that instead. |
| func linkRotate(ctx context.Context, sess *apiSession, out io.Writer) error { |
| - if err := sess.homeLinkRotate(ctx); err != nil { |
| + r, err := sess.homeLinkRotate(ctx) |
| + if err != nil { |
| return err |
| } |
| + if r.Revocation == "pending" { |
| + fmt.Fprintln(out, fillLinkCopy(copyLinkRotatePending, "console", r.Console)) |
| + return nil |
| + } |
| fmt.Fprintln(out, copyLD64) |
| return nil |
| } |
| @@ -567,6 +589,10 @@ func linksList(ctx context.Context, sess *apiSession, acct apiAcct, out io.Write |
| for _, l := range links { |
| reachable := copyLinksUnknown |
| switch { |
| + case l.Removing && l.Revocation == "pending": |
| + reachable = copyLinksRemovingPending |
| + case l.Removing && l.Revocation == "unknown": |
| + reachable = copyLinksRemovingUnknown |
| case l.Removing: |
| reachable = copyLinksRemoving |
| case l.Reachable == nil: |
diff --git a/link_cli_test.go b/link_cli_test.go| index 15691fb44..5ea335da3 100644 |
| --- a/link_cli_test.go |
| +++ b/link_cli_test.go |
| @@ -338,6 +338,14 @@ func TestLinkStatusStates(t *testing.T) { |
| "could not reach " + testServing + ". try again\n" + |
| "connection: connected (box box-1)\n" + |
| "no apps published\n"}, |
| + {"rotation pending", map[string]any{"linked": true, "console": testServing, "account": "shop", |
| + "box": "box-1", "state": "connected", "apps": []any{}, "key_revocation": "pending", |
| + "serving_error": "Carloku couldn't update this link just now. Try again in a minute."}, |
| + "linked to shop on " + testServing + "\n" + |
| + "Carloku couldn't update this link just now. Try again in a minute.\n" + |
| + strings.ReplaceAll(copyLinkStatusRotatePending, "{console}", testServing) + "\n" + |
| + "connection: connected (box box-1)\n" + |
| + "no apps published\n"}, |
| {"unlink pending", map[string]any{"linked": true, "console": testServing, "account": "shop", "name": "pi", |
| "box": "box-1", "state": "connected", "apps": []any{}, "revocation": "pending"}, |
| "linked to shop on " + testServing + " as pi\n" + |
| @@ -444,6 +452,21 @@ func TestLinkRotate(t *testing.T) { |
| if want := "replaced the link's key. the old key no longer works\n"; out.String() != want { |
| t.Fatalf("output %q, want %q", out.String(), want) |
| } |
| + // The serving console's hub may still accept the old key (Astra final |
| + // review, Important 3): the home console's pending answer, exactly as |
| + // handleAPIHomeLinkRotate writes it (TestRotatePendingCarriedToTheHome), |
| + // prints the pending line and never LD-C64. |
| + fc.on("POST /api/cli/link/rotate", http.StatusOK, map[string]any{"rotated": true, "revocation": "pending", "console": testServing}) |
| + out.Reset() |
| + if err := linkRotate(context.Background(), fc.session(), &out); err != nil { |
| + t.Fatal(err) |
| + } |
| + if want := strings.ReplaceAll(copyLinkRotatePending, "{console}", testServing) + "\n"; out.String() != want { |
| + t.Fatalf("pending output %q, want %q", out.String(), want) |
| + } |
| + if strings.Contains(out.String(), copyLD64) || strings.Contains(out.String(), "no longer works") { |
| + t.Fatalf("a pending rotation printed LD-C64: %q", out.String()) |
| + } |
| msg := "this deployment is not linked. link it with carlos link create --to <console URL>" |
| fc.on("POST /api/cli/link/rotate", http.StatusConflict, map[string]string{"error": msg}) |
| if err := linkRotate(context.Background(), fc.session(), io.Discard); err == nil || err.Error() != msg { |
| @@ -535,16 +558,24 @@ func TestLinksList(t *testing.T) { |
| "removing": false, "apps": []any{}}, |
| {"id": "0123456789ad", "name": "old", "created_by": "a@example.com", "created_at": now, "reachable": nil, |
| "removing": true, "revocation": "pending", "apps": []map[string]string{{"app": "shop", "url": "https://shop.bab.example.com"}}}, |
| + {"id": "0123456789ae", "name": "gone", "created_by": "a@example.com", "created_at": now, "reachable": no, |
| + "removing": true, "apps": []map[string]string{{"app": "wiki", "url": "https://wiki.bab.example.com"}}}, |
| + {"id": "0123456789af", "name": "dim", "created_by": "a@example.com", "created_at": now, "reachable": nil, |
| + "removing": true, "revocation": "unknown", "apps": []any{}}, |
| }}) |
| var out bytes.Buffer |
| if err := linksList(context.Background(), fc.session(), apiAcct{Seg: "bab"}, &out, now); err != nil { |
| t.Fatal(err) |
| } |
| - want := "DEPLOYMENT REACHABLE LAST SEEN APP URL\n" + |
| - "pi yes 1m30s ago blog https://blog.bab.example.com\n" + |
| - "pi yes 1m30s ago demo https://demo.bab.example.com\n" + |
| - "nas no - - -\n" + |
| - "old removing - shop https://shop.bab.example.com\n" |
| + // The three removing states stay apart (Fable final review): the hub |
| + // has not cut "old" yet, has cut "gone", and could not be read for "dim". |
| + want := "DEPLOYMENT REACHABLE LAST SEEN APP URL\n" + |
| + "pi yes 1m30s ago blog https://blog.bab.example.com\n" + |
| + "pi yes 1m30s ago demo https://demo.bab.example.com\n" + |
| + "nas no - - -\n" + |
| + "old removing, may still answer - shop https://shop.bab.example.com\n" + |
| + "gone removing, offline - wiki https://wiki.bab.example.com\n" + |
| + "dim removing, not checked - - -\n" |
| if out.String() != want { |
| t.Fatalf("output:\n%s\nwant:\n%s", out.String(), want) |
| } |