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.
71 lines
2.5 KiB
Go
71 lines
2.5 KiB
Go
package db
|
|
|
|
import "database/sql"
|
|
|
|
// egressExecer is satisfied by *DB, *sql.DB and *sql.Tx.
|
|
type egressExecer interface {
|
|
Exec(query string, args ...any) (sql.Result, error)
|
|
}
|
|
|
|
// ScopeEgress is one materialized egress entry an operator approval granted.
|
|
type ScopeEgress struct {
|
|
Kind string // domain|cidr
|
|
Value string
|
|
}
|
|
|
|
// snapshotCompanyEgressSQL freezes the domains and CIDRs a company grant covers
|
|
// right now: its operator-approved company_scope domain and IP/CIDR rules. The
|
|
// sandbox proxy reads task_scope_egress, so inventory written after this point
|
|
// cannot widen an approved task. Assets and agent-written company_scope rows
|
|
// (add_company_scope, ScopeSentry sync) are never copied: a worker controls
|
|
// both, and the next operator approval would otherwise promote them.
|
|
const snapshotCompanyEgressSQL = `
|
|
INSERT INTO task_scope_egress(task_id, kind, value, company_id)
|
|
SELECT $1, e.kind, e.value, e.company_id
|
|
FROM (
|
|
SELECT 'domain' AS kind, lower(trim(cs.domain)) AS value, cs.company_id
|
|
FROM company_scope cs
|
|
JOIN unnest($2::bigint[]) AS ids(id) ON ids.id = cs.company_id
|
|
WHERE cs.kind = 'domain' AND COALESCE(cs.domain, '') <> ''
|
|
AND cs.approved_by_operator = TRUE
|
|
UNION ALL
|
|
SELECT 'cidr', cs.net::text, cs.company_id
|
|
FROM company_scope cs
|
|
JOIN unnest($2::bigint[]) AS ids(id) ON ids.id = cs.company_id
|
|
WHERE cs.kind IN ('ip', 'cidr') AND cs.net IS NOT NULL
|
|
AND cs.approved_by_operator = TRUE
|
|
) e
|
|
WHERE e.value <> ''
|
|
ON CONFLICT DO NOTHING`
|
|
|
|
// snapshotCompanyEgress records the current inventory of every given company as
|
|
// task egress. Call it only from an operator-approval path.
|
|
func snapshotCompanyEgress(ex egressExecer, taskID int64, companyIDs []int64) error {
|
|
if taskID <= 0 || len(companyIDs) == 0 {
|
|
return nil
|
|
}
|
|
_, err := ex.Exec(snapshotCompanyEgressSQL, taskID, companyIDs)
|
|
return err
|
|
}
|
|
|
|
// CompanyEgressForTask returns the frozen entries one operator-approved company
|
|
// grant gave this task. A company without entries grants no egress.
|
|
func (d *DB) CompanyEgressForTask(taskID, companyID int64) ([]ScopeEgress, error) {
|
|
rows, err := d.Query(`
|
|
SELECT kind, value FROM task_scope_egress
|
|
WHERE task_id=$1 AND company_id=$2
|
|
ORDER BY kind, value`, taskID, companyID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var out []ScopeEgress
|
|
for rows.Next() {
|
|
var e ScopeEgress
|
|
if err := rows.Scan(&e.Kind, &e.Value); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, e)
|
|
}
|
|
return out, rows.Err()
|
|
}
|