mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-03 13:11:05 +00:00
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.
115 lines
4.4 KiB
Go
115 lines
4.4 KiB
Go
package db
|
|
|
|
import (
|
|
"database/sql"
|
|
"testing"
|
|
)
|
|
|
|
// TestTargetGateBackfillMigration0022 proves that migration 0022 backfills
|
|
// target_gate=true on existing active scoped tasks (company, intercept rules,
|
|
// or task_scope) while leaving finished/unscoped tasks and explicitly opted-out
|
|
// tasks with target_gate=false.
|
|
func TestTargetGateBackfillMigration0022(t *testing.T) {
|
|
dsn := scratchDSN(t, "tg_backfill_0022")
|
|
|
|
d, err := Open(dsn)
|
|
if err != nil {
|
|
t.Fatalf("initial Open: %v", err)
|
|
}
|
|
coID, _, _, _, _, err := d.Companies().CreateCompanyWithScope("Backfill Target Corp", "", []ScopeInput{{Kind: "domain", Value: "backfill.example.com"}}, "test", true)
|
|
if err != nil {
|
|
t.Fatalf("create company: %v", err)
|
|
}
|
|
|
|
off := false
|
|
// 1) Active company-scoped task
|
|
tCompany, err := d.CreateTaskWithOptions("active-company", "goal", TaskCreateOptions{CompanyIDs: []int64{coID}})
|
|
if err != nil {
|
|
t.Fatalf("create company task: %v", err)
|
|
}
|
|
// 2) Active intercept-rule-scoped task
|
|
tRule, err := d.CreateTaskWithOptions("active-rule", "goal", TaskCreateOptions{
|
|
InterceptRules: []TaskInterceptRuleInput{{Kind: "exact_domain", Pattern: "target.example.com", Action: "allow", Enabled: true}},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create rule task: %v", err)
|
|
}
|
|
// 3) Active task_scope-scoped task
|
|
tScope, err := d.CreateTaskWithOptions("active-task-scope", "goal", TaskCreateOptions{})
|
|
if err != nil {
|
|
t.Fatalf("create scope task: %v", err)
|
|
}
|
|
if _, err := d.Exec(`INSERT INTO task_scope(task_id, kind, domain, source) VALUES ($1, 'root_domain', 'scoped.example.com', 'manual')`, tScope.ID); err != nil {
|
|
t.Fatalf("insert task_scope: %v", err)
|
|
}
|
|
// 4) Finished scoped task -> must stay false
|
|
tDone, err := d.CreateTaskWithOptions("done-company", "goal", TaskCreateOptions{CompanyIDs: []int64{coID}})
|
|
if err != nil {
|
|
t.Fatalf("create done task: %v", err)
|
|
}
|
|
// 5) Unscoped active task -> must stay false
|
|
tUnscoped, err := d.CreateTaskWithOptions("active-unscoped", "goal", TaskCreateOptions{})
|
|
if err != nil {
|
|
t.Fatalf("create unscoped task: %v", err)
|
|
}
|
|
// 6) Explicitly opted-out active scoped task -> must stay false
|
|
tOptOut, err := d.CreateTaskWithOptions("explicit-opt-out", "goal", TaskCreateOptions{
|
|
CompanyIDs: []int64{coID},
|
|
TargetGate: &off,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create opt-out task: %v", err)
|
|
}
|
|
// 7) Active scoped task later explicitly turned off via UpdateTask -> must stay false
|
|
tUpdatedOff, err := d.CreateTaskWithOptions("updated-opt-out", "goal", TaskCreateOptions{CompanyIDs: []int64{coID}})
|
|
if err != nil {
|
|
t.Fatalf("create updated-opt-out task: %v", err)
|
|
}
|
|
if _, err := d.UpdateTask(tUpdatedOff.ID, TaskPatch{TargetGate: &off}); err != nil {
|
|
t.Fatalf("UpdateTask opt-out: %v", err)
|
|
}
|
|
|
|
// Simulate legacy pre-0022 state for tasks 1..5 (target_gate=false, opt_out=false, done on #4).
|
|
for _, id := range []int64{tCompany.ID, tRule.ID, tScope.ID, tDone.ID, tUnscoped.ID} {
|
|
if _, err := d.Exec(`UPDATE tasks SET target_gate=FALSE, target_gate_opt_out=FALSE WHERE id=$1`, id); err != nil {
|
|
t.Fatalf("reset legacy task %d: %v", id, err)
|
|
}
|
|
}
|
|
if _, err := d.Exec(`UPDATE tasks SET status='done' WHERE id=$1`, tDone.ID); err != nil {
|
|
t.Fatalf("mark done: %v", err)
|
|
}
|
|
// Remove migration 0022 marker so Open(dsn) re-runs 0022_target_gate_backfill.sql.
|
|
if _, err := d.Exec(`DELETE FROM schema_migrations WHERE version >= 22`); err != nil {
|
|
t.Fatalf("delete migration 22 marker: %v", err)
|
|
}
|
|
d.Close()
|
|
|
|
// Re-open: migration 0022 executes.
|
|
d2, err := Open(dsn)
|
|
if err != nil {
|
|
t.Fatalf("re-Open with migration 0022: %v", err)
|
|
}
|
|
defer d2.Close()
|
|
|
|
check := func(name string, id int64, wantGate, wantOptOut bool) {
|
|
t.Helper()
|
|
var gate, optOut bool
|
|
if err := d2.QueryRow(`SELECT target_gate, target_gate_opt_out FROM tasks WHERE id=$1`, id).Scan(&gate, &optOut); err != nil {
|
|
if err == sql.ErrNoRows {
|
|
t.Fatalf("%s: missing row", name)
|
|
}
|
|
t.Fatalf("%s: query: %v", name, err)
|
|
}
|
|
if gate != wantGate || optOut != wantOptOut {
|
|
t.Errorf("%s (id=%d): got target_gate=%v opt_out=%v, want %v %v", name, id, gate, optOut, wantGate, wantOptOut)
|
|
}
|
|
}
|
|
|
|
check("active-company", tCompany.ID, true, false)
|
|
check("active-rule", tRule.ID, true, false)
|
|
check("active-task-scope", tScope.ID, true, false)
|
|
check("done-company", tDone.ID, false, false)
|
|
check("active-unscoped", tUnscoped.ID, false, false)
|
|
check("explicit-opt-out", tOptOut.ID, false, true)
|
|
check("updated-opt-out", tUpdatedOff.ID, false, true)
|
|
}
|