Compare commits
1 commit
main
...
fix/ignore
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4af8d5e9ef |
5 changed files with 265 additions and 10 deletions
|
|
@ -347,6 +347,7 @@ go_test(
|
|||
"testmain_helper_sentinel_test.go",
|
||||
"testmain_test.go",
|
||||
"transaction_borrow_test.go",
|
||||
"transaction_ignored_checkout_test.go",
|
||||
"transaction_phase_test.go",
|
||||
"transaction_test.go",
|
||||
"transaction_wisp_lifecycle_persistence_test.go",
|
||||
|
|
|
|||
|
|
@ -41,8 +41,8 @@ type mockDriver struct {
|
|||
qmu sync.Mutex // guards queries
|
||||
queries []string // every query string passed to Prepare (borrow-path assertions)
|
||||
|
||||
// failCheckout makes Prepare of a DOLT_CHECKOUT statement fail, so borrow-path
|
||||
// tests can force beginTxOnConn to error and fall through to the fallback.
|
||||
// failCheckout makes Prepare of a DOLT_CHECKOUT statement fail, modelling a
|
||||
// user who is denied DOLT_CHECKOUT (a least-privilege operator).
|
||||
failCheckout atomic.Bool
|
||||
|
||||
// activeBranch is what a SELECT active_branch() query reports; empty means
|
||||
|
|
|
|||
|
|
@ -1092,6 +1092,25 @@ func (s *DoltStore) withReadTx(ctx context.Context, fn func(tx *sql.Tx) error) e
|
|||
// and a single pinned *sql.Conn (see recomputeAllBlocked/recomputeBlockedTx).
|
||||
type execer interface {
|
||||
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
|
||||
QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
|
||||
}
|
||||
|
||||
// checkoutBranchIfNeeded puts conn's session on branch, issuing DOLT_CHECKOUT
|
||||
// only when the session is on a different branch. A fresh session usually
|
||||
// already sits on the requested branch, and a least-privilege operator user
|
||||
// (EXECUTE on dolt_add/dolt_commit alone) is denied even a no-op checkout. conn
|
||||
// must be a single session: on a multi-connection *sql.DB the read and the
|
||||
// checkout could land on different connections.
|
||||
func checkoutBranchIfNeeded(ctx context.Context, conn execer, branch string) error {
|
||||
var active string
|
||||
if err := conn.QueryRowContext(ctx, "SELECT active_branch()").Scan(&active); err != nil {
|
||||
return fmt.Errorf("read active branch: %w", err)
|
||||
}
|
||||
if active == branch {
|
||||
return nil
|
||||
}
|
||||
_, err := conn.ExecContext(ctx, "CALL DOLT_CHECKOUT(?)", branch)
|
||||
return err
|
||||
}
|
||||
|
||||
// pinStoreBranch reproduces the store's real active branch on conn. Branch
|
||||
|
|
@ -1121,18 +1140,22 @@ type execer interface {
|
|||
// in practice rather than by construction. The s.branch fallback does not
|
||||
// close the gap either: it fires only when the query errors, not when it
|
||||
// succeeds with another connection's answer.
|
||||
//
|
||||
// conn must be a single session (a *sql.Conn, or a *sql.DB capped at one open
|
||||
// connection that keeps it idle-cached): the branch read and any checkout
|
||||
// must land on the same connection.
|
||||
func (s *DoltStore) pinStoreBranch(ctx context.Context, conn execer) error {
|
||||
var branch string
|
||||
if scanErr := s.db.QueryRowContext(ctx, "SELECT active_branch()").Scan(&branch); scanErr == nil {
|
||||
if branch != "" {
|
||||
if _, err := conn.ExecContext(ctx, "CALL DOLT_CHECKOUT(?)", branch); err != nil {
|
||||
if err := checkoutBranchIfNeeded(ctx, conn, branch); err != nil {
|
||||
return fmt.Errorf("checkout active branch %q: %w", branch, err)
|
||||
}
|
||||
}
|
||||
} else if s.branch != "" {
|
||||
// Fall back to the store's recorded branch rather than failing the
|
||||
// whole call outright.
|
||||
if _, err := conn.ExecContext(ctx, "CALL DOLT_CHECKOUT(?)", s.branch); err != nil {
|
||||
if err := checkoutBranchIfNeeded(ctx, conn, s.branch); err != nil {
|
||||
return fmt.Errorf("checkout fallback branch %q: %w", s.branch, err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -464,14 +464,17 @@ func beginBorrowedTx(ctx context.Context, conn *sql.Conn, branch string) (*sql.T
|
|||
return tx, nil
|
||||
}
|
||||
|
||||
// beginTxOnConn checks a connection out to branch and begins a transaction on
|
||||
// it. Only the fallback path uses it: the fallback owns a dedicated
|
||||
// beginTxOnConn puts a connection on branch and begins a transaction on it.
|
||||
// Only the fallback path uses it: the fallback owns a dedicated
|
||||
// single-connection pool, so checking its session out is safe. Every Dolt SQL
|
||||
// session has its own active branch, so the explicit checkout is required on
|
||||
// a fresh dial.
|
||||
// session has its own active branch, and a fresh dial lands on the database's
|
||||
// default branch — usually already the requested one. DOLT_CHECKOUT is issued
|
||||
// only when the session is on a different branch: a capped operator user may
|
||||
// be granted EXECUTE on dolt_add/dolt_commit alone, and an unconditional no-op
|
||||
// checkout would be denied and fail every wisp write on the fallback path.
|
||||
func beginTxOnConn(ctx context.Context, conn *sql.Conn, branch string) (*sql.Tx, error) {
|
||||
if _, err := conn.ExecContext(ctx, "CALL DOLT_CHECKOUT(?)", branch); err != nil {
|
||||
return nil, fmt.Errorf("failed to checkout ignored tx branch %s: %w", branch, err)
|
||||
if err := checkoutBranchIfNeeded(ctx, conn, branch); err != nil {
|
||||
return nil, fmt.Errorf("failed to put ignored tx conn on branch %s: %w", branch, err)
|
||||
}
|
||||
tx, err := conn.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
|
|
|
|||
228
internal/storage/dolt/transaction_ignored_checkout_test.go
Normal file
228
internal/storage/dolt/transaction_ignored_checkout_test.go
Normal file
|
|
@ -0,0 +1,228 @@
|
|||
package dolt
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
// TestBeginTxOnConnSkipsCheckoutWhenAlreadyOnBranch pins that the fresh-dial
|
||||
// fallback does not issue DOLT_CHECKOUT when the session already sits on the
|
||||
// requested branch. A fresh session lands on the database's default branch, so
|
||||
// this is the normal case — and a capped operator user that may EXECUTE only
|
||||
// dolt_add/dolt_commit is denied DOLT_CHECKOUT outright.
|
||||
func TestBeginTxOnConnSkipsCheckoutWhenAlreadyOnBranch(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
db, drv := openMockDB(t)
|
||||
drv.failCheckout.Store(true) // any checkout attempt fails, like the capped user
|
||||
|
||||
conn, err := db.Conn(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("acquire conn: %v", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
tx, err := beginTxOnConn(ctx, conn, "main")
|
||||
if err != nil {
|
||||
t.Fatalf("beginTxOnConn on the session's own branch: %v", err)
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
t.Fatalf("commit: %v", err)
|
||||
}
|
||||
if got := drv.countQuery("DOLT_CHECKOUT"); got != 0 {
|
||||
t.Fatalf("DOLT_CHECKOUT ran %d times, want 0 (session already on the requested branch)", got)
|
||||
}
|
||||
if got := drv.countQuery("active_branch"); got != 1 {
|
||||
t.Fatalf("active_branch ran %d times, want 1", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBeginTxOnConnChecksOutDifferentBranch pins that the fallback still
|
||||
// switches a fresh session that is NOT on the requested branch.
|
||||
func TestBeginTxOnConnChecksOutDifferentBranch(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
db, drv := openMockDB(t)
|
||||
drv.activeBranch.Store("main")
|
||||
|
||||
conn, err := db.Conn(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("acquire conn: %v", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
tx, err := beginTxOnConn(ctx, conn, "feature-x")
|
||||
if err != nil {
|
||||
t.Fatalf("beginTxOnConn on a different branch: %v", err)
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
t.Fatalf("commit: %v", err)
|
||||
}
|
||||
if got := drv.countQuery("DOLT_CHECKOUT"); got != 1 {
|
||||
t.Fatalf("DOLT_CHECKOUT ran %d times, want 1 (session must be switched to the requested branch)", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBeginTxOnConnSurfacesActiveBranchError pins that a failed branch read is
|
||||
// reported rather than papered over with a blind checkout.
|
||||
func TestBeginTxOnConnSurfacesActiveBranchError(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
db, drv := openMockDB(t)
|
||||
drv.failActiveBranch.Store(true)
|
||||
|
||||
conn, err := db.Conn(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("acquire conn: %v", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
tx, err := beginTxOnConn(ctx, conn, "main")
|
||||
if err == nil {
|
||||
_ = tx.Rollback()
|
||||
t.Fatal("expected an error when active_branch() fails")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "active branch") {
|
||||
t.Fatalf("error = %q, want it to name the active-branch read", err.Error())
|
||||
}
|
||||
if got := drv.countQuery("DOLT_CHECKOUT"); got != 0 {
|
||||
t.Fatalf("DOLT_CHECKOUT ran %d times, want 0", got)
|
||||
}
|
||||
}
|
||||
|
||||
// readOnlyMigrationTables are the schema-version tables a least-privilege operator
|
||||
// only reads: covered
|
||||
// by the db-level SELECT, never granted DML.
|
||||
var readOnlyMigrationTables = map[string]bool{
|
||||
"schema_migrations": true,
|
||||
"ignored_schema_migrations": true,
|
||||
}
|
||||
|
||||
// openCappedOperatorStore opens a store as a least-privilege operator user, as a
|
||||
// shared Dolt server may grant one: SELECT on the database, DML on the data tables,
|
||||
// and EXECUTE on only dolt_add/dolt_commit. The pool is pinned to one
|
||||
// connection, so the ignored tx must take the fresh-dial fallback. The caller
|
||||
// builds its own context after this returns (see openStoreWithOwnBudget).
|
||||
func openCappedOperatorStore(t *testing.T) (admin, op *DoltStore) {
|
||||
t.Helper()
|
||||
admin, cleanup := setupConcurrentTestStore(t)
|
||||
t.Cleanup(cleanup) // registered first so it runs after the DROP USER cleanup below
|
||||
|
||||
ctx, cancel := testContext(t)
|
||||
defer cancel()
|
||||
|
||||
dbName := admin.database
|
||||
user := "capped_op_" + strings.TrimPrefix(dbName, "testdb_")
|
||||
const password = "capped-pw"
|
||||
|
||||
exec := func(stmt string) {
|
||||
t.Helper()
|
||||
if _, err := admin.db.ExecContext(ctx, stmt); err != nil {
|
||||
t.Fatalf("%s: %v", stmt, err)
|
||||
}
|
||||
}
|
||||
exec(fmt.Sprintf("CREATE USER '%s'@'%%' IDENTIFIED BY '%s'", user, password))
|
||||
t.Cleanup(func() {
|
||||
_, _ = admin.db.ExecContext(context.Background(), fmt.Sprintf("DROP USER IF EXISTS '%s'@'%%'", user))
|
||||
})
|
||||
exec(fmt.Sprintf("GRANT SELECT ON `%s`.* TO '%s'@'%%'", dbName, user))
|
||||
|
||||
rows, err := admin.db.QueryContext(ctx, "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'")
|
||||
if err != nil {
|
||||
t.Fatalf("list tables: %v", err)
|
||||
}
|
||||
var tables []string
|
||||
for rows.Next() {
|
||||
var name, kind string
|
||||
if err := rows.Scan(&name, &kind); err != nil {
|
||||
_ = rows.Close()
|
||||
t.Fatalf("scan table: %v", err)
|
||||
}
|
||||
if !readOnlyMigrationTables[name] && !strings.HasPrefix(name, "dolt_") {
|
||||
tables = append(tables, name)
|
||||
}
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
t.Fatalf("iterate tables: %v", err)
|
||||
}
|
||||
_ = rows.Close()
|
||||
if len(tables) == 0 {
|
||||
t.Fatal("no data tables found to grant")
|
||||
}
|
||||
for _, table := range tables {
|
||||
exec(fmt.Sprintf("GRANT INSERT, UPDATE, DELETE ON `%s`.`%s` TO '%s'@'%%'", dbName, table, user))
|
||||
}
|
||||
for _, proc := range []string{"dolt_add", "dolt_commit"} {
|
||||
exec(fmt.Sprintf("GRANT EXECUTE ON PROCEDURE `%s`.`%s` TO '%s'@'%%'", dbName, proc, user))
|
||||
}
|
||||
|
||||
op, err = openStoreWithOwnBudget(t, &Config{
|
||||
Path: t.TempDir(),
|
||||
CommitterName: "test",
|
||||
CommitterEmail: "test@example.com",
|
||||
Database: dbName,
|
||||
ServerUser: user,
|
||||
ServerPassword: password,
|
||||
MaxOpenConns: 1,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("open store as capped operator: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = op.Close() })
|
||||
return admin, op
|
||||
}
|
||||
|
||||
// TestIgnoredTxFallbackUnderCappedOperatorUser reproduces the failure end to
|
||||
// end: writing a wisp in a transaction as a capped operator
|
||||
// used to fail with "failed to checkout ignored tx branch main: ... command
|
||||
// denied" (bd create --graph, bd mol wisp).
|
||||
func TestIgnoredTxFallbackUnderCappedOperatorUser(t *testing.T) {
|
||||
admin, op := openCappedOperatorStore(t)
|
||||
ctx, cancel := testContext(t)
|
||||
defer cancel()
|
||||
|
||||
wisp := &types.Issue{
|
||||
ID: "test-capped-wisp",
|
||||
Title: "wisp written by a capped operator",
|
||||
Status: types.StatusOpen,
|
||||
Priority: 2,
|
||||
IssueType: types.TypeTask,
|
||||
Ephemeral: true,
|
||||
}
|
||||
if err := op.RunInTransaction(ctx, "test: capped operator writes a wisp", func(tx storage.Transaction) error {
|
||||
return tx.CreateIssue(ctx, wisp, "tester")
|
||||
}); err != nil {
|
||||
t.Fatalf("RunInTransaction as capped operator: %v", err)
|
||||
}
|
||||
|
||||
assertWispCount(ctx, t, admin.db, wisp.ID, 1)
|
||||
}
|
||||
|
||||
// TestPinnedConnReadsUnderCappedOperatorUser covers the other fresh-connection
|
||||
// site, pinStoreBranch: the long-timeout history read and the pinned-conn
|
||||
// blocked recompute used to fail with "checkout active branch \"main\": ...
|
||||
// command denied" for the same capped operator.
|
||||
func TestPinnedConnReadsUnderCappedOperatorUser(t *testing.T) {
|
||||
_, op := openCappedOperatorStore(t)
|
||||
ctx, cancel := testContext(t)
|
||||
defer cancel()
|
||||
|
||||
issue := &types.Issue{
|
||||
ID: "test-capped-history",
|
||||
Title: "durable issue read back by a capped operator",
|
||||
Status: types.StatusOpen,
|
||||
Priority: 2,
|
||||
IssueType: types.TypeTask,
|
||||
}
|
||||
if err := op.CreateIssue(ctx, issue, "tester"); err != nil {
|
||||
t.Fatalf("CreateIssue as capped operator: %v", err)
|
||||
}
|
||||
if _, err := op.RecomputeAllBlocked(ctx); err != nil {
|
||||
t.Fatalf("RecomputeAllBlocked as capped operator: %v", err)
|
||||
}
|
||||
if _, err := op.History(ctx, issue.ID); err != nil {
|
||||
t.Fatalf("History as capped operator: %v", err)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue