mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-03 13:11:05 +00:00
egress-guard reads hand-edited lists/scope-domains.txt and scope-cidrs.txt, while the sandbox egress proxy builds its policy from PostgreSQL. Nothing connected the two, so the overlay could drift from the approved scope (#421). egress-guard/export-scope.sh regenerates both lists through the new artex -export-egress-guard-scope. The export calls taskSandboxRunInfoChecked (the error-returning form of taskSandboxRunInfo) and sandbox.NewEgressPolicy, the derivation and parser of the per-task proxy, so only operator-approved task scope, frozen company grants and operator task allow rules are exported. Agent-added, auto-added and expanded rows are not. Fail closed: taskSandboxRunInfoChecked reports the read errors the proxy path ignores, any error fails the whole export, and the script touches neither list until a complete, well-formed export was read; each list is then replaced by rename. An empty export is refused unless --allow-empty, because empty lists with EGRESS_MODE unset make the guard fall back to monitor. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
145 lines
4.4 KiB
Go
145 lines
4.4 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"regexp"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/Autumn-27/artex/db"
|
|
"github.com/Autumn-27/artex/sandbox"
|
|
)
|
|
|
|
// EgressGuardScope is the operator-approved scope of a set of tasks in the
|
|
// form the egress-guard overlay reads (egress-guard/lists/scope-domains.txt
|
|
// and scope-cidrs.txt). The overlay has one allowlist for the whole ARTEX
|
|
// network, so this is the union over the tasks.
|
|
type EgressGuardScope struct {
|
|
TaskIDs []int64
|
|
Domains []string // domain suffixes, sorted
|
|
CIDRs []string // IPv4 prefixes, sorted
|
|
Skipped []string // entries the overlay cannot express, with the reason
|
|
}
|
|
|
|
// egressGuardDomainRE is what may be written as a dnsmasq "ipset=/<suffix>/..."
|
|
// line: dot-separated labels of letters, digits, "-" and "_". Anything else
|
|
// could not match a hostname in the proxy either, and must not reach the
|
|
// overlay's dnsmasq configuration.
|
|
var egressGuardDomainRE = regexp.MustCompile(`^[a-z0-9_]([a-z0-9_-]*[a-z0-9_])?(\.[a-z0-9_]([a-z0-9_-]*[a-z0-9_])?)*$`)
|
|
|
|
// ExportEgressGuardScope derives the egress-guard allowlist from the same
|
|
// source as the per-task sandbox egress proxy: taskSandboxRunInfo, parsed by
|
|
// sandbox.NewEgressPolicy. Only operator-approved task scope, frozen company
|
|
// grants and operator task allow rules are exported; agent-added, auto-added
|
|
// and expanded rows are not.
|
|
//
|
|
// With no taskIDs, every task that is not in a terminal state is exported.
|
|
// Any read or parse error fails the whole export: a partial list must not
|
|
// replace the one the overlay already has.
|
|
func ExportEgressGuardScope(pg *db.DB, taskIDs []int64) (EgressGuardScope, error) {
|
|
var out EgressGuardScope
|
|
if pg == nil {
|
|
return out, fmt.Errorf("no database")
|
|
}
|
|
if len(taskIDs) == 0 {
|
|
tasks, err := pg.ListTasks()
|
|
if err != nil {
|
|
return out, fmt.Errorf("list tasks: %w", err)
|
|
}
|
|
for _, t := range tasks {
|
|
if !db.IsTerminal(t.Status) {
|
|
taskIDs = append(taskIDs, t.ID)
|
|
}
|
|
}
|
|
} else {
|
|
for _, id := range taskIDs {
|
|
t, err := pg.GetTask(id)
|
|
if err != nil {
|
|
return out, fmt.Errorf("task %d: %w", id, err)
|
|
}
|
|
if t == nil {
|
|
return out, fmt.Errorf("task %d does not exist", id)
|
|
}
|
|
}
|
|
}
|
|
|
|
seenTask := map[int64]bool{}
|
|
seenDom := map[string]bool{}
|
|
seenCIDR := map[string]bool{}
|
|
seenSkip := map[string]bool{}
|
|
skip := func(entry, reason string) {
|
|
s := fmt.Sprintf("%q: %s", entry, reason)
|
|
if !seenSkip[s] {
|
|
seenSkip[s] = true
|
|
out.Skipped = append(out.Skipped, s)
|
|
}
|
|
}
|
|
for _, id := range taskIDs {
|
|
if seenTask[id] {
|
|
continue
|
|
}
|
|
seenTask[id] = true
|
|
out.TaskIDs = append(out.TaskIDs, id)
|
|
info, err := taskSandboxRunInfoChecked(pg, "", strconv.FormatInt(id, 10))
|
|
if err != nil {
|
|
return EgressGuardScope{}, fmt.Errorf("task %d: read scope: %w", id, err)
|
|
}
|
|
pol, err := sandbox.NewEgressPolicy(info.TaskID, info.ScopeDomains, info.ScopeCIDRs, nil)
|
|
if err != nil {
|
|
return EgressGuardScope{}, fmt.Errorf("task %d: %w", id, err)
|
|
}
|
|
for _, d := range pol.Domains {
|
|
if !egressGuardDomainRE.MatchString(d) {
|
|
skip(d, "not a hostname suffix")
|
|
continue
|
|
}
|
|
if !seenDom[d] {
|
|
seenDom[d] = true
|
|
out.Domains = append(out.Domains, d)
|
|
}
|
|
}
|
|
for _, p := range pol.CIDRs {
|
|
c := p.String()
|
|
if !p.Addr().Is4() {
|
|
skip(c, "IPv6, egress-guard is IPv4-only")
|
|
continue
|
|
}
|
|
if !seenCIDR[c] {
|
|
seenCIDR[c] = true
|
|
out.CIDRs = append(out.CIDRs, c)
|
|
}
|
|
}
|
|
}
|
|
sort.Slice(out.TaskIDs, func(i, j int) bool { return out.TaskIDs[i] < out.TaskIDs[j] })
|
|
sort.Strings(out.Domains)
|
|
sort.Strings(out.CIDRs)
|
|
sort.Strings(out.Skipped)
|
|
return out, nil
|
|
}
|
|
|
|
// Render writes the scope as the line protocol egress-guard/export-scope.sh
|
|
// reads: "domain <suffix>" and "cidr <prefix>" lines, then a trailer carrying
|
|
// both counts so a truncated stream is detectable. Comment lines start with "#".
|
|
func (s EgressGuardScope) Render(w io.Writer) error {
|
|
var b strings.Builder
|
|
ids := make([]string, 0, len(s.TaskIDs))
|
|
for _, id := range s.TaskIDs {
|
|
ids = append(ids, strconv.FormatInt(id, 10))
|
|
}
|
|
b.WriteString("# artex egress-guard scope export v1\n")
|
|
fmt.Fprintf(&b, "# tasks: %s\n", strings.Join(ids, ","))
|
|
for _, d := range s.Domains {
|
|
fmt.Fprintf(&b, "domain %s\n", d)
|
|
}
|
|
for _, c := range s.CIDRs {
|
|
fmt.Fprintf(&b, "cidr %s\n", c)
|
|
}
|
|
for _, sk := range s.Skipped {
|
|
fmt.Fprintf(&b, "# skipped %s\n", sk)
|
|
}
|
|
fmt.Fprintf(&b, "# end domains=%d cidrs=%d\n", len(s.Domains), len(s.CIDRs))
|
|
_, err := io.WriteString(w, b.String())
|
|
return err
|
|
}
|