console, cli: plan warning line on CLI commands (plans 4a)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
6 files changed,
+230
−0
auth.go+1 −0internal/console/apicli.go+10 −0internal/console/plangrace_cli_test.go+80 −0internal/console/plangracepass.go+51 −0planwarnings.go+46 −0planwarnings_test.go+42 −0
diff --git a/auth.go b/auth.go| index 47565dc94..81b995ed1 100644 |
| --- a/auth.go |
| +++ b/auth.go |
| @@ -403,6 +403,7 @@ func newConsoleClient(base, token string) *consoleClient { |
| return &consoleClient{base: base, token: token, httpc: &http.Client{ |
| Timeout: 30 * time.Second, |
| CheckRedirect: neverFollow, |
| + Transport: &planWarningTransport{out: os.Stderr}, |
| }} |
| } |
diff --git a/internal/console/apicli.go b/internal/console/apicli.go| index 77c6e47dc..1fc5ea997 100644 |
| --- a/internal/console/apicli.go |
| +++ b/internal/console/apicli.go |
| @@ -207,6 +207,16 @@ func mainGateCopy() gateCopy { |
| // apiPrincipalAccountSaying is apiPrincipalAccount with the sentences of c. |
| func (s *Server) apiPrincipalAccountSaying(w http.ResponseWriter, r *http.Request, c gateCopy) (Principal, *Account, bool) { |
| + p, acct, ok := s.apiPrincipalAccountResolve(w, r, c) |
| + if ok { |
| + s.writePlanWarnings(w, r, p, acct) |
| + } |
| + return p, acct, ok |
| +} |
| + |
| +// apiPrincipalAccountResolve is the gate's resolution; the wrapper above |
| +// adds the account's plan warnings (plans 4a) to every reply it admits. |
| +func (s *Server) apiPrincipalAccountResolve(w http.ResponseWriter, r *http.Request, c gateCopy) (Principal, *Account, bool) { |
| p, ok := principalFrom(r.Context()) |
| if !ok { |
| // Only reachable from a door that skipped cliRoute, which |
diff --git a/internal/console/plangrace_cli_test.go b/internal/console/plangrace_cli_test.go| new file mode 100644 |
| index 000000000..6f4b07efd |
| --- /dev/null |
| +++ b/internal/console/plangrace_cli_test.go |
| @@ -0,0 +1,80 @@ |
| +package console |
| + |
| +import ( |
| + "net/http" |
| + "strings" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/svcrole" |
| +) |
| + |
| +// cliWarnings GETs an account-scoped CLI door and returns its plan warning |
| +// header values. |
| +func cliWarnings(t *testing.T, fx *consoleFixture, token string, accept bool) []string { |
| + t.Helper() |
| + req, err := http.NewRequest(http.MethodGet, fx.ts.URL+"/api/cli/apps/bab/list", nil) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + req.Header.Set("Authorization", "Bearer "+token) |
| + if accept { |
| + req.Header.Set(planWarningsAccept, "1") |
| + } |
| + resp, err := http.DefaultClient.Do(req) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + resp.Body.Close() |
| + if resp.StatusCode != http.StatusOK { |
| + t.Fatalf("GET list = %d", resp.StatusCode) |
| + } |
| + return resp.Header.Values(planWarningHeader) |
| +} |
| + |
| +func TestCLIWarningHeaderUnderAPlan(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + overRow(t, fx) |
| + token := cliToken(t, fx, "paul@keymail.dev", "code-cli-warn") |
| + got := cliWarnings(t, fx, token, true) |
| + if len(got) != 1 || !strings.HasPrefix(got[0], "this account is over its plan: 2 GB of 1 GB object storage") { |
| + t.Fatalf("warning header = %q", got) |
| + } |
| +} |
| + |
| +func TestCLIWarningNoticeNeedsTheDeclaration(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + acct := overRow(t, fx) |
| + token := cliToken(t, fx, "paul@keymail.dev", "code-cli-decl") |
| + if got := cliWarnings(t, fx, token, false); len(got) != 0 { |
| + t.Fatalf("warning sent to a CLI that does not print it: %q", got) |
| + } |
| + if st, _ := capRow(t, fx, acct, capStorage); !st.NoticeAt.IsZero() { |
| + t.Fatal("notice recorded for a CLI that does not print warnings") |
| + } |
| + cliWarnings(t, fx, token, true) |
| + if st, _ := capRow(t, fx, acct, capStorage); st.NoticeAt.IsZero() { |
| + t.Fatal("an owner's declaring CLI recorded no notice") |
| + } |
| +} |
| + |
| +func TestCLIWarningServiceEarnsNoNotice(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + acct := overRow(t, fx) |
| + token := serviceToken(t, fx.st, acct.ID, "ci", "", svcrole.Publish) |
| + if got := cliWarnings(t, fx, token, true); len(got) != 1 { |
| + t.Fatalf("service saw %q, want the warning", got) |
| + } |
| + if st, _ := capRow(t, fx, acct, capStorage); !st.NoticeAt.IsZero() { |
| + t.Fatal("a service credential recorded notice") |
| + } |
| +} |
| + |
| +func TestCLIWarningWithoutACatalogUnchanged(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + overRow(t, fx) |
| + fx.srv.cfg.PlanCatalog = nil |
| + token := cliToken(t, fx, "paul@keymail.dev", "code-cli-off") |
| + if got := cliWarnings(t, fx, token, true); len(got) != 0 { |
| + t.Fatalf("warning with plans off: %q", got) |
| + } |
| +} |
diff --git a/internal/console/plangracepass.go b/internal/console/plangracepass.go| index 0ed21a52a..1ee97b3c5 100644 |
| --- a/internal/console/plangracepass.go |
| +++ b/internal/console/plangracepass.go |
| @@ -4,6 +4,7 @@ import ( |
| "context" |
| "fmt" |
| "log" |
| + "net/http" |
| "net/url" |
| "strconv" |
| "strings" |
| @@ -244,3 +245,53 @@ func (s *Server) planBanner(acct *Account, role string, now time.Time) []string |
| } |
| return lines |
| } |
| + |
| +// planWarningsAccept is the request header by which a CLI says it prints |
| +// plan warnings; planWarningHeader carries one warning per value back. |
| +const ( |
| + planWarningsAccept = "X-Carlos-Plan-Warnings" |
| + planWarningHeader = "X-Carlos-Plan-Warning" |
| +) |
| + |
| +// writePlanWarnings adds the account's plan warnings to a CLI reply, but |
| +// only for a CLI that declared it prints them: an older one would drop the |
| +// header, and must earn no notice. A person who owns the account receiving |
| +// a cap that is over is notice received (spec §6); a service is not. |
| +func (s *Server) writePlanWarnings(w http.ResponseWriter, r *http.Request, p Principal, acct *Account) { |
| + if s.cfg.PlanCatalog == nil || r.Header.Get(planWarningsAccept) != "1" { |
| + return |
| + } |
| + states, err := s.store.CapStates(acct.ID) |
| + if err != nil { |
| + log.Printf("console: plan warnings %s: %v", acct.Sqid, err) |
| + return |
| + } |
| + var over []capID |
| + for _, n := range planNoticesOf(states) { |
| + w.Header().Add(planWarningHeader, planNoticeText(n, "cli")) |
| + if n.Level.over() { |
| + over = append(over, n.Cap) |
| + } |
| + } |
| + if len(over) == 0 || p.Kind != PrincipalUser || !s.isOwner(acct, p.Address) { |
| + return |
| + } |
| + if err := s.store.RecordCapNotice(acct.ID, over, time.Now()); err != nil { |
| + log.Printf("console: plan warnings %s: notice: %v", acct.Sqid, err) |
| + } |
| +} |
| + |
| +// isOwner is whether addr owns acct. A lookup failure is "no". |
| +func (s *Server) isOwner(acct *Account, addr string) bool { |
| + members, err := s.store.Members(acct.ID) |
| + if err != nil { |
| + log.Printf("console: plan warnings %s: members: %v", acct.Sqid, err) |
| + return false |
| + } |
| + for _, m := range members { |
| + if m.Role == RoleOwner && strings.EqualFold(m.Name, addr) { |
| + return true |
| + } |
| + } |
| + return false |
| +} |
diff --git a/planwarnings.go b/planwarnings.go| new file mode 100644 |
| index 000000000..3b0802034 |
| --- /dev/null |
| +++ b/planwarnings.go |
| @@ -0,0 +1,46 @@ |
| +package main |
| + |
| +import ( |
| + "fmt" |
| + "io" |
| + "net/http" |
| + "sync" |
| +) |
| + |
| +// planWarningTransport tells the console this CLI prints plan warnings, |
| +// and prints each warning the console sends back, once per run (plans 4a). |
| +// The console counts a warning an owner's CLI printed as notice received, |
| +// so the request header is a promise this transport keeps. |
| +type planWarningTransport struct { |
| + base http.RoundTripper |
| + out io.Writer |
| + |
| + mu sync.Mutex |
| + seen map[string]bool |
| +} |
| + |
| +func (t *planWarningTransport) RoundTrip(req *http.Request) (*http.Response, error) { |
| + req = req.Clone(req.Context()) |
| + req.Header.Set("X-Carlos-Plan-Warnings", "1") |
| + base := t.base |
| + if base == nil { |
| + base = http.DefaultTransport |
| + } |
| + resp, err := base.RoundTrip(req) |
| + if err != nil { |
| + return resp, err |
| + } |
| + t.mu.Lock() |
| + defer t.mu.Unlock() |
| + for _, w := range resp.Header.Values("X-Carlos-Plan-Warning") { |
| + if t.seen == nil { |
| + t.seen = map[string]bool{} |
| + } |
| + if t.seen[w] { |
| + continue |
| + } |
| + t.seen[w] = true |
| + fmt.Fprintf(t.out, "warning: %s\n", w) |
| + } |
| + return resp, nil |
| +} |
diff --git a/planwarnings_test.go b/planwarnings_test.go| new file mode 100644 |
| index 000000000..985703649 |
| --- /dev/null |
| +++ b/planwarnings_test.go |
| @@ -0,0 +1,42 @@ |
| +package main |
| + |
| +import ( |
| + "bytes" |
| + "io" |
| + "net/http" |
| + "strings" |
| + "testing" |
| +) |
| + |
| +type stubRT struct { |
| + got []http.Header |
| +} |
| + |
| +func (s *stubRT) RoundTrip(req *http.Request) (*http.Response, error) { |
| + s.got = append(s.got, req.Header.Clone()) |
| + h := http.Header{} |
| + h.Add("X-Carlos-Plan-Warning", "this account has used 4 of 5 apps and instances") |
| + return &http.Response{StatusCode: 200, Header: h, Body: io.NopCloser(strings.NewReader("{}")), Request: req}, nil |
| +} |
| + |
| +func TestPlanWarningTransportPrintsOnce(t *testing.T) { |
| + var out bytes.Buffer |
| + stub := &stubRT{} |
| + tr := &planWarningTransport{base: stub, out: &out} |
| + for i := 0; i < 2; i++ { |
| + req, _ := http.NewRequest(http.MethodGet, "http://console.example/api/cli/apps", nil) |
| + resp, err := tr.RoundTrip(req) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + resp.Body.Close() |
| + } |
| + if got := out.String(); got != "warning: this account has used 4 of 5 apps and instances\n" { |
| + t.Fatalf("printed %q, want the warning once", got) |
| + } |
| + for _, h := range stub.got { |
| + if h.Get("X-Carlos-Plan-Warnings") != "1" { |
| + t.Fatalf("request did not declare that this CLI prints warnings: %v", h) |
| + } |
| + } |
| +} |