ARTEX/db/task_scope_approval_test.go
BaaRai ed70ea3220 security: promote company scope egress only when approved by the operator
An operator company approval froze the company's whole inventory into
task_scope_egress: every company_scope domain and IP/CIDR row plus the
root_domain, domain and ip of every asset attributed to the company. The
agent writes both (insert_assets, add_company_scope), so the next operator
selection or re-approval promoted worker-chosen hosts into the sandbox
egress allowlist for the life of the task.

The snapshot now reads only company_scope domain and IP/CIDR rules with
approved_by_operator = TRUE; the asset branches are gone. Migration 0025
adds the column and backfills existing rows as approved. The flag is an
explicit parameter of insertScopeRulesTx and the CompanyStore scope
writers: the operator company API passes true, add_company_scope and the
ScopeSentry sync pass false. An operator write of a rule the agent stored
first approves the existing row.

AddOperatorScope outside a transaction now commits the task_scope row and
the egress snapshot together.

Existing task_scope_egress rows are not purged.
2026-12-01 20:44:37 +01:00

91 lines
3.6 KiB
Go

package db
import (
"testing"
)
// TestTaskScopeOperatorApprovalBackfillMigration0023 verifies that migration
// 0023 backfills approved_by_operator=true ONLY for legacy source='manual'
// rows while leaving source='agent' and source='auto' rows false.
func TestTaskScopeOperatorApprovalBackfillMigration0023(t *testing.T) {
dsn := scratchDSN(t, "ts_approval_0023")
d, err := Open(dsn)
if err != nil {
t.Fatalf("initial Open: %v", err)
}
coID, _, _, _, _, err := d.Companies().CreateCompanyWithScope("Mig23 Corp", "", []ScopeInput{{Kind: "domain", Value: "mig23.test"}}, "test", true)
if err != nil {
t.Fatalf("CreateCompanyWithScope: %v", err)
}
task, err := d.CreateTaskWithOptions("mig23-task", "goal", TaskCreateOptions{})
if err != nil {
t.Fatalf("CreateTaskWithOptions: %v", err)
}
// Insert legacy pre-0023 rows directly with approved_by_operator=FALSE:
// - manual domain, IP, and company rows -> MUST become TRUE
// - agent domain and IP rows -> MUST stay FALSE
// - auto domain and IP rows -> MUST stay FALSE
stmts := []struct {
sql string
args []any
}{
{`INSERT INTO task_scope(task_id, kind, domain, source, approved_by_operator) VALUES ($1, 'root_domain', 'manual-legacy.test', 'manual', FALSE)`, []any{task.ID}},
{`INSERT INTO task_scope(task_id, kind, net, source, approved_by_operator) VALUES ($1, 'ip', '198.51.100.10/32'::cidr, 'manual', FALSE)`, []any{task.ID}},
{`INSERT INTO task_scope(task_id, kind, company_id, source, approved_by_operator) VALUES ($1, 'company', $2, 'manual', FALSE)`, []any{task.ID, coID}},
{`INSERT INTO task_scope(task_id, kind, domain, source, approved_by_operator) VALUES ($1, 'root_domain', 'agent-legacy.test', 'agent', FALSE)`, []any{task.ID}},
{`INSERT INTO task_scope(task_id, kind, net, source, approved_by_operator) VALUES ($1, 'ip', '198.51.100.20/32'::cidr, 'agent', FALSE)`, []any{task.ID}},
{`INSERT INTO task_scope(task_id, kind, domain, source, approved_by_operator) VALUES ($1, 'root_domain', 'auto-legacy.test', 'auto', FALSE)`, []any{task.ID}},
{`INSERT INTO task_scope(task_id, kind, net, source, approved_by_operator) VALUES ($1, 'ip', '198.51.100.30/32'::cidr, 'auto', FALSE)`, []any{task.ID}},
}
for _, st := range stmts {
if _, err := d.Exec(st.sql, st.args...); err != nil {
t.Fatalf("seed legacy row: %v", err)
}
}
if _, err := d.Exec(`DELETE FROM schema_migrations WHERE version >= 23`); err != nil {
t.Fatalf("delete migration 23 marker: %v", err)
}
d.Close()
// Re-open so migration 0023 executes against the legacy rows.
d2, err := Open(dsn)
if err != nil {
t.Fatalf("re-Open with migration 0023: %v", err)
}
defer d2.Close()
rows, err := d2.Assets().ListTaskScope(task.ID)
if err != nil {
t.Fatalf("ListTaskScope: %v", err)
}
if len(rows) != 7 {
t.Fatalf("expected 7 task_scope rows, got %d", len(rows))
}
manualCount, agentCount, autoCount := 0, 0, 0
for _, r := range rows {
switch r.Source {
case "manual":
manualCount++
if !r.ApprovedByOperator {
t.Errorf("legacy manual row %+v was not backfilled to approved_by_operator=true", r)
}
case "agent":
agentCount++
if r.ApprovedByOperator {
t.Errorf("legacy agent row %+v was unexpectedly marked approved_by_operator=true", r)
}
case "auto":
autoCount++
if r.ApprovedByOperator {
t.Errorf("legacy auto row %+v was unexpectedly marked approved_by_operator=true", r)
}
default:
t.Errorf("unexpected source %q", r.Source)
}
}
if manualCount != 3 || agentCount != 2 || autoCount != 2 {
t.Fatalf("unexpected source counts manual=%d agent=%d auto=%d", manualCount, agentCount, autoCount)
}
}