console: forward a pull, and audit it (DBF-19, DBF-20, pending)
The forward marker offers pull, the reads route admits a pull with no SQL and refuses one carrying SQL, and the audit row names the pull and its --at time. DBF-19 and DBF-20 are wired pending a Luna pass.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+102
−20
approved_db_forward_copy_test.go+6 −3internal/console/apidbforward.go+37 −12internal/console/apidbforward_test.go+59 −5
diff --git a/approved_db_forward_copy_test.go b/approved_db_forward_copy_test.go| index b2ac42826..33fcd17bc 100644 |
| --- a/approved_db_forward_copy_test.go |
| +++ b/approved_db_forward_copy_test.go |
| @@ -57,9 +57,12 @@ var dbForwardCopyBuilt = []string{ |
| // that has not turned forwarding on. The copy review moves each id from here |
| // to dbForwardCopyBuilt, applying Paul's edits in the same commit. Docs rows |
| // are public the moment the site syncs, so they are never wired. Luna |
| -// approved every row on 2026-10-23, so the list is empty and every row is |
| -// built. |
| -var dbForwardCopyWired = []string{} |
| +// approved every plan 2 row on 2026-10-23. Plan 3's pull rows are wired |
| +// pending a Luna pass: forwarding is on for both deployments, so this |
| +// branch must not land before they are reviewed. |
| +var dbForwardCopyWired = []string{ |
| + "DBF-19", "DBF-20", // the console's pull audit details |
| +} |
| func mustParseDBForwardRecord(t *testing.T) []copyRow { |
| t.Helper() |
diff --git a/internal/console/apidbforward.go b/internal/console/apidbforward.go| index 6a2181415..d1bc5cd01 100644 |
| --- a/internal/console/apidbforward.go |
| +++ b/internal/console/apidbforward.go |
| @@ -144,7 +144,7 @@ func (s *Server) dbResidency(w http.ResponseWriter, r *http.Request, rec provisi |
| } |
| writeJSON(w, http.StatusConflict, dbdoor.ForwardBody{ |
| Error: fmt.Sprintf(dbfUpdateCarlos, rec.Host, rec.Region), Forward: 1, |
| - Region: rec.Region, ForwardOps: []string{dbreads.OpQuery}, |
| + Region: rec.Region, ForwardOps: []string{dbreads.OpQuery, dbreads.OpPull}, |
| }) |
| } |
| @@ -157,11 +157,26 @@ const ( |
| dbfAuditAt = "forwarded a database query to region %s, as of %s. the query was %d bytes, sha256 %s (cli)" |
| ) |
| +// The pull's audit details (copy record DBF-19, DBF-20), pending a copy |
| +// review: wired through dbForwardCopyWired, reached only from a console |
| +// with CARLOS_DB_FORWARD on. |
| +const ( |
| + dbfAuditPull = "forwarded a database pull to region %s (cli)" |
| + dbfAuditPullAt = "forwarded a database pull to region %s, as of %s (cli)" |
| +) |
| + |
| const dbReadRetryAfter = "30" |
| -// dbForwardAuditDetail is the db-read row's detail (spec §9.3): the SQL's |
| -// length and the first 16 hex digits of its SHA-256, never its text. |
| -func dbForwardAuditDetail(region, sqlText string, at time.Time) string { |
| +// dbForwardAuditDetail is the db-read row's detail (spec §9.3): for a query |
| +// the SQL's length and the first 16 hex digits of its SHA-256, never its |
| +// text; a pull carries no SQL. |
| +func dbForwardAuditDetail(op, region, sqlText string, at time.Time) string { |
| + if op == dbreads.OpPull { |
| + if at.IsZero() { |
| + return fmt.Sprintf(dbfAuditPull, region) |
| + } |
| + return fmt.Sprintf(dbfAuditPullAt, region, at.UTC().Format(time.RFC3339)) |
| + } |
| sum := sha256.Sum256([]byte(sqlText)) |
| sha := hex.EncodeToString(sum[:])[:16] |
| if at.IsZero() { |
| @@ -326,7 +341,15 @@ func (s *Server) admitDBRead(w http.ResponseWriter, r *http.Request) (dbReadAdmi |
| jsonError(w, http.StatusConflict, fmt.Sprintf(dbfMoving, host)) |
| return none, "", false |
| } |
| - if body.Op != dbreads.OpQuery { |
| + switch body.Op { |
| + case dbreads.OpQuery: |
| + case dbreads.OpPull: |
| + // A pull runs nothing: SQL in one is a request no CLI builds. |
| + if body.SQL != "" { |
| + http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) |
| + return none, "", false |
| + } |
| + default: |
| http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) |
| return none, "", false |
| } |
| @@ -340,13 +363,15 @@ func (s *Server) admitDBRead(w http.ResponseWriter, r *http.Request) (dbReadAdmi |
| http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) |
| return none, "", false |
| } |
| - if err := dbquery.CheckSQL(body.SQL); err != nil || body.SQL == "" { |
| - msg := http.StatusText(http.StatusBadRequest) |
| - if err != nil { |
| - msg = err.Error() |
| + if body.Op == dbreads.OpQuery { |
| + if err := dbquery.CheckSQL(body.SQL); err != nil || body.SQL == "" { |
| + msg := http.StatusText(http.StatusBadRequest) |
| + if err != nil { |
| + msg = err.Error() |
| + } |
| + jsonError(w, http.StatusBadRequest, msg) |
| + return none, "", false |
| } |
| - jsonError(w, http.StatusBadRequest, msg) |
| - return none, "", false |
| } |
| var at time.Time |
| if body.At != "" { |
| @@ -430,7 +455,7 @@ func (s *Server) admitDBRead(w http.ResponseWriter, r *http.Request) (dbReadAdmi |
| return none, "", false |
| } |
| if err := s.store.RecordActivityContext(ctx, acct.ID, app, actor, Activity{Action: dbAuditAction, |
| - TargetType: "instance", Target: host, Detail: dbForwardAuditDetail(rec.Region, body.SQL, at)}, time.Now()); err != nil { |
| + TargetType: "instance", Target: host, Detail: dbForwardAuditDetail(body.Op, rec.Region, body.SQL, at)}, time.Now()); err != nil { |
| return fail("audit", err) |
| } |
| if ended("request", false) { |
diff --git a/internal/console/apidbforward_test.go b/internal/console/apidbforward_test.go| index 07c395eb5..cf69f06f2 100644 |
| --- a/internal/console/apidbforward_test.go |
| +++ b/internal/console/apidbforward_test.go |
| @@ -206,7 +206,7 @@ func TestListDoorMarksACapableRegion(t *testing.T) { |
| var got dbdoor.ForwardBody |
| json.Unmarshal([]byte(body), &got) |
| if status != http.StatusConflict || got.Forward != 1 || got.Region != "uk" || |
| - !slices.Equal(got.ForwardOps, []string{dbreads.OpQuery}) || |
| + !slices.Equal(got.ForwardOps, []string{dbreads.OpQuery, dbreads.OpPull}) || |
| got.Error != fmt.Sprintf(dbfUpdateCarlos, dbfHost, "uk") { |
| t.Fatalf("%d %s", status, body) |
| } |
| @@ -297,8 +297,10 @@ func TestDBReadRouteRefusesBeforeAdmitting(t *testing.T) { |
| Move: &provision.MoveState{ID: "mv_x", To: "ie", State: "requested"}}) |
| two := good |
| two.SQL = "SELECT 1; SELECT 2" |
| - pull := good |
| - pull.Op, pull.SQL = dbreads.OpPull, "" |
| + shell := good |
| + shell.Op = "shell" |
| + pullSQL := good |
| + pullSQL.Op = dbreads.OpPull |
| badKey := good |
| badKey.ReplyKey = "AAAA" |
| future := good |
| @@ -313,7 +315,8 @@ func TestDBReadRouteRefusesBeforeAdmitting(t *testing.T) { |
| "no such instance": {"nope.example.com", good, http.StatusNotFound, ""}, |
| "moving": {"moving.example.com", good, http.StatusConflict, fmt.Sprintf(dbfMoving, "moving.example.com")}, |
| "two statements": {dbfHost, two, http.StatusBadRequest, dbquery.ErrOneStatement.Error()}, |
| - "an op this console does not forward": {dbfHost, pull, http.StatusBadRequest, ""}, |
| + "an op this console does not forward": {dbfHost, shell, http.StatusBadRequest, ""}, |
| + "a pull carrying SQL": {dbfHost, pullSQL, http.StatusBadRequest, ""}, |
| "a reply key that is not a key": {dbfHost, badKey, http.StatusBadRequest, ""}, |
| "a time in the future": {dbfHost, future, http.StatusBadRequest, ""}, |
| } { |
| @@ -394,7 +397,7 @@ func TestDBReadQueuesASignedRequestAndAudits(t *testing.T) { |
| t.Fatalf("the serial was not bumped: %v", err) |
| } |
| evs, _ := fx.st.Events(OpsAccountID, "shop", 10) |
| - want := dbForwardAuditDetail("uk", body.SQL, time.Time{}) |
| + want := dbForwardAuditDetail(dbreads.OpQuery, "uk", body.SQL, time.Time{}) |
| found := false |
| for _, e := range evs { |
| found = found || (e.Action == "db-read" && e.Target == dbfHost && e.Detail == want) |
| @@ -649,6 +652,57 @@ func openAnswer(t *testing.T, key *ecdh.PrivateKey, lines []dbreads.Line) ([]byt |
| return rows.Bytes(), s |
| } |
| +// A pull through the console: the route admits it, relays the box's ltx |
| +// stream and its summary's file list, and audits it with DBF-19; with --at, |
| +// DBF-20. |
| +func TestDBReadPullStreamsTheFiles(t *testing.T) { |
| + fx := newDBFFixture(t, true) |
| + dir := t.TempDir() |
| + dbdoortest.Replicate(t, dir, 3) |
| + fx.startBox(t, dir) |
| + key, _ := dbreads.NewReplyKey() |
| + res := fx.openRead(t, dbfHost, fx.member, dbreads.ReadBody{Op: dbreads.OpPull, ReplyKey: dbreads.EncodeReplyKey(key.PublicKey())}) |
| + lines := readLines(t, res.Body) |
| + res.Body.Close() |
| + last := lines[len(lines)-1].Result |
| + if last == nil || last.State != dbreads.StateDone { |
| + t.Fatalf("lines %+v", lines) |
| + } |
| + s, err := dbreads.OpenSummary(key, lines[0].Admitted, last.Sealed) |
| + if err != nil || len(s.Files) == 0 || s.TXID != s.Files[len(s.Files)-1].MaxTXID { |
| + t.Fatalf("summary %+v %v", s, err) |
| + } |
| + h := sha256.New() |
| + for _, l := range lines { |
| + if l.Chunk == 0 { |
| + continue |
| + } |
| + plain, err := dbreads.OpenChunk(key, lines[0].Admitted, l.Chunk, dbreads.StreamLTX, l.Chunk == last.LastSeq, l.Data) |
| + if err != nil { |
| + t.Fatalf("chunk %d: %v", l.Chunk, err) |
| + } |
| + h.Write(plain) |
| + } |
| + if hex.EncodeToString(h.Sum(nil)) != s.SHA256 { |
| + t.Fatal("the pulled bytes do not add up to the summary") |
| + } |
| + at := time.Now().Add(-time.Second).UTC().Truncate(time.Second) |
| + res = fx.openRead(t, dbfHost, fx.member, dbreads.ReadBody{Op: dbreads.OpPull, At: at.Format(time.RFC3339), |
| + ReplyKey: dbreads.EncodeReplyKey(key.PublicKey())}) |
| + readLines(t, res.Body) |
| + res.Body.Close() |
| + evs, _ := fx.st.Events(OpsAccountID, "shop", 10) |
| + for _, want := range []string{fmt.Sprintf(dbfAuditPull, "uk"), fmt.Sprintf(dbfAuditPullAt, "uk", at.Format(time.RFC3339))} { |
| + found := false |
| + for _, e := range evs { |
| + found = found || (e.Action == "db-read" && e.Target == dbfHost && e.Detail == want) |
| + } |
| + if !found { |
| + t.Errorf("no %q audit row in %+v", want, evs) |
| + } |
| + } |
| +} |
| + |
| func TestDBReadStreamsAQueryEndToEnd(t *testing.T) { |
| fx := newDBFFixture(t, true) |
| dir := t.TempDir() |