| new file mode 100644 |
| index 000000000..d1284bb7f |
| --- /dev/null |
| +++ b/internal/instancectl/egress_test.go |
| @@ -0,0 +1,416 @@ |
| +package instancectl |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "net/netip" |
| + "reflect" |
| + "strconv" |
| + "strings" |
| + "sync" |
| + "testing" |
| +) |
| + |
| +const egressHost = "shop-a.example.com" |
| + |
| +// egressCtl is a fake systemctl for the asserter: it records every call |
| +// and answers `show -p IPAddressAllow --value <slice>` with what the slice |
| +// holds, which a `set-property --runtime <slice> IPAddressAllow= ...` |
| +// replaces (the reset first, then the list, as systemd applies them). |
| +type egressCtl struct { |
| + mu sync.Mutex |
| + calls []string |
| + held map[string]string // slice -> show's answer |
| + ignore bool // accept set-property but change nothing |
| +} |
| + |
| +func newEgressCtl() *egressCtl { return &egressCtl{held: map[string]string{}} } |
| + |
| +func (f *egressCtl) run(_ context.Context, args ...string) (string, error) { |
| + f.mu.Lock() |
| + defer f.mu.Unlock() |
| + f.calls = append(f.calls, strings.Join(args, " ")) |
| + switch { |
| + case len(args) == 5 && args[0] == "show" && args[1] == "-p" && args[2] == "IPAddressAllow" && args[3] == "--value": |
| + return f.held[args[4]] + "\n", nil |
| + case len(args) >= 4 && args[0] == "set-property" && args[1] == "--runtime": |
| + if f.ignore { |
| + return "", nil |
| + } |
| + var v string |
| + for _, a := range args[3:] { |
| + val, ok := strings.CutPrefix(a, "IPAddressAllow=") |
| + if !ok { |
| + return "", errors.New("unexpected property " + a) |
| + } |
| + v = val // "" is the reset; the next assignment appends to it |
| + } |
| + f.held[args[2]] = v |
| + return "", nil |
| + } |
| + return "", errors.New("unexpected call: " + strings.Join(args, " ")) |
| +} |
| + |
| +func (f *egressCtl) setHeld(slice, v string) { |
| + f.mu.Lock() |
| + defer f.mu.Unlock() |
| + f.held[slice] = v |
| +} |
| + |
| +func (f *egressCtl) takeCalls() []string { |
| + f.mu.Lock() |
| + defer f.mu.Unlock() |
| + c := f.calls |
| + f.calls = nil |
| + return c |
| +} |
| + |
| +func (f *egressCtl) sets() []string { |
| + f.mu.Lock() |
| + defer f.mu.Unlock() |
| + var out []string |
| + for _, c := range f.calls { |
| + if strings.HasPrefix(c, "set-property ") { |
| + out = append(out, c) |
| + } |
| + } |
| + return out |
| +} |
| + |
| +// staticSource answers every host with one policy (or one error). |
| +func staticSource(p EgressPolicy, err error) EgressSource { |
| + return func(context.Context, string) (EgressPolicy, error) { return p, err } |
| +} |
| + |
| +func newEgressAsserter(ctl *egressCtl, src EgressSource) *EgressAsserter { |
| + return &EgressAsserter{ |
| + Systemctl: ctl.run, |
| + Source: src, |
| + LocalAddrs: func() ([]netip.Prefix, error) { return []netip.Prefix{netip.MustParsePrefix("127.0.0.0/8")}, nil }, |
| + Logf: func(string, ...any) {}, |
| + } |
| +} |
| + |
| +func showCall(host string) string { return "show -p IPAddressAllow --value " + SliceName(host) } |
| + |
| +func setCall(host string, list ...string) string { |
| + c := "set-property --runtime " + SliceName(host) + " IPAddressAllow=" |
| + if len(list) > 0 { |
| + c += " IPAddressAllow=" + strings.Join(list, " ") |
| + } |
| + return c |
| +} |
| + |
| +func mustAssert(t *testing.T, e *EgressAsserter, host string) EgressReport { |
| + t.Helper() |
| + if err := e.Assert(context.Background(), host); err != nil { |
| + t.Fatalf("Assert(%s): %v", host, err) |
| + } |
| + return e.Reports()[host] |
| +} |
| + |
| +func wantCalls(t *testing.T, got []string, want ...string) { |
| + t.Helper() |
| + if !reflect.DeepEqual(got, want) { |
| + t.Fatalf("systemctl calls:\n got %q\n want %q", got, want) |
| + } |
| +} |
| + |
| +func TestEgressNoPolicyWritesNothing(t *testing.T) { |
| + ctl := newEgressCtl() |
| + e := newEgressAsserter(ctl, staticSource(EgressPolicy{}, nil)) |
| + rep := mustAssert(t, e, egressHost) |
| + wantCalls(t, ctl.takeCalls(), showCall(egressHost)) |
| + if !reflect.DeepEqual(rep, EgressReport{}) { |
| + t.Fatalf("report = %+v, want {}", rep) |
| + } |
| +} |
| + |
| +func TestEgressWritesResetThenList(t *testing.T) { |
| + ctl := newEgressCtl() |
| + e := newEgressAsserter(ctl, staticSource(EgressPolicy{Allow: []string{"fd00:5::20", "10.0.5.20", "10.0.5.20"}}, nil)) |
| + rep := mustAssert(t, e, egressHost) |
| + wantCalls(t, ctl.takeCalls(), |
| + showCall(egressHost), |
| + setCall(egressHost, "10.0.5.20/32", "fd00:5::20/128"), |
| + showCall(egressHost)) |
| + if want := (EgressReport{Allowed: []string{"10.0.5.20", "fd00:5::20"}}); !reflect.DeepEqual(rep, want) { |
| + t.Fatalf("report = %+v, want %+v", rep, want) |
| + } |
| +} |
| + |
| +func TestEgressRefusesEachBadEntryWithItsReason(t *testing.T) { |
| + ctl := newEgressCtl() |
| + e := newEgressAsserter(ctl, staticSource(EgressPolicy{Allow: []string{ |
| + "10.0.5.20", "127.0.0.1", "8.8.8.8", "10.0.5.20:5432", "::ffff:10.0.5.21", |
| + }}, nil)) |
| + rep := mustAssert(t, e, egressHost) |
| + wantCalls(t, ctl.sets(), setCall(egressHost, "10.0.5.20/32", "10.0.5.21/32")) |
| + // egressaddr's errors already name the address (C4): carried as-is. |
| + want := []string{ |
| + "127.0.0.1 is a loopback address", |
| + "8.8.8.8 is a public address and is already reachable. No allow is needed", |
| + `"10.0.5.20:5432" has a port. An allow opens an address, never a port`, |
| + } |
| + if !reflect.DeepEqual(rep.Refused, want) { |
| + t.Fatalf("Refused = %q, want %q", rep.Refused, want) |
| + } |
| + if want := []string{"10.0.5.20", "10.0.5.21"}; !reflect.DeepEqual(rep.Allowed, want) || rep.State != "" { |
| + t.Fatalf("report = %+v, want Allowed %q and no state", rep, want) |
| + } |
| +} |
| + |
| +func TestEgressDropsTheBoxsOwnAddress(t *testing.T) { |
| + ctl := newEgressCtl() |
| + e := newEgressAsserter(ctl, staticSource(EgressPolicy{Allow: []string{"10.0.5.20", "10.0.5.21"}}, nil)) |
| + e.LocalAddrs = func() ([]netip.Prefix, error) { |
| + return []netip.Prefix{netip.MustParsePrefix("127.0.0.0/8"), netip.MustParsePrefix("10.0.5.20/32")}, nil |
| + } |
| + rep := mustAssert(t, e, egressHost) |
| + wantCalls(t, ctl.sets(), setCall(egressHost, "10.0.5.21/32")) |
| + if want := []string{"10.0.5.20: an address of this box (10.0.5.20/32)"}; !reflect.DeepEqual(rep.Refused, want) { |
| + t.Fatalf("Refused = %q, want %q", rep.Refused, want) |
| + } |
| + if want := []string{"10.0.5.21"}; !reflect.DeepEqual(rep.Allowed, want) { |
| + t.Fatalf("Allowed = %q, want %q", rep.Allowed, want) |
| + } |
| +} |
| + |
| +func TestEgressClosesWhenTheBoxsAddressesCannotBeRead(t *testing.T) { |
| + ctl := newEgressCtl() |
| + ctl.setHeld(SliceName(egressHost), "10.0.5.20/32") |
| + e := newEgressAsserter(ctl, staticSource(EgressPolicy{Allow: []string{"10.0.5.20", "fd00:5::20"}}, nil)) |
| + e.LocalAddrs = func() ([]netip.Prefix, error) { return nil, errors.New("/proc/net/fib_trie: permission denied") } |
| + rep := mustAssert(t, e, egressHost) |
| + wantCalls(t, ctl.sets(), setCall(egressHost)) |
| + want := []string{ |
| + "10.0.5.20: this box's addresses could not be read", |
| + "fd00:5::20: this box's addresses could not be read", |
| + } |
| + if !reflect.DeepEqual(rep.Refused, want) { |
| + t.Fatalf("Refused = %q, want %q", rep.Refused, want) |
| + } |
| + if rep.Allowed != nil { |
| + t.Fatalf("Allowed = %q, want none", rep.Allowed) |
| + } |
| +} |
| + |
| +func TestEgressMoreThanEightClosesAll(t *testing.T) { |
| + ctl := newEgressCtl() |
| + ctl.setHeld(SliceName(egressHost), "10.0.5.20/32") |
| + var nine []string |
| + for i := 20; i < 29; i++ { |
| + nine = append(nine, "10.0.5."+strconv.Itoa(i)) |
| + } |
| + e := newEgressAsserter(ctl, staticSource(EgressPolicy{Allow: nine}, nil)) |
| + rep := mustAssert(t, e, egressHost) |
| + wantCalls(t, ctl.sets(), setCall(egressHost)) |
| + if rep.State != EgressUnreadable || !strings.Contains(rep.Detail, "more than 8") { |
| + t.Fatalf("report = %+v, want state %q and a detail saying more than 8", rep, EgressUnreadable) |
| + } |
| + if rep.Allowed != nil { |
| + t.Fatalf("Allowed = %q, want none", rep.Allowed) |
| + } |
| +} |
| + |
| +func TestEgressClosedPolicyResets(t *testing.T) { |
| + for _, state := range []string{EgressExpired, EgressNotNested, EgressUnderClaims} { |
| + t.Run(state, func(t *testing.T) { |
| + ctl := newEgressCtl() |
| + ctl.setHeld(SliceName(egressHost), "10.0.5.20/32") |
| + e := newEgressAsserter(ctl, staticSource(EgressPolicy{Allow: []string{"10.0.5.20"}, Closed: state}, nil)) |
| + rep := mustAssert(t, e, egressHost) |
| + wantCalls(t, ctl.sets(), setCall(egressHost)) |
| + if rep.State != state || rep.Allowed != nil { |
| + t.Fatalf("report = %+v, want state %q and nothing allowed", rep, state) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestEgressClosedPolicyCarriesItsDetail(t *testing.T) { |
| + ctl := newEgressCtl() |
| + detail := "the policy expired at 2026-10-24T00:00:00Z" |
| + e := newEgressAsserter(ctl, staticSource(EgressPolicy{Closed: EgressExpired, Detail: detail}, nil)) |
| + rep := mustAssert(t, e, egressHost) |
| + if rep.State != "expired" || rep.Detail != detail { |
| + t.Fatalf("report = %+v, want state expired and detail %q", rep, detail) |
| + } |
| + |
| + e = newEgressAsserter(ctl, staticSource(EgressPolicy{}, errors.New("registry: locked"))) |
| + rep = mustAssert(t, e, egressHost) |
| + if rep.State != EgressUnreadable || !strings.Contains(rep.Detail, "registry: locked") { |
| + t.Fatalf("report = %+v, want state %q and a detail naming the source error", rep, EgressUnreadable) |
| + } |
| +} |
| + |
| +func TestEgressUnderClaimsResetsAHeldAllow(t *testing.T) { |
| + ctl := newEgressCtl() |
| + held, empty := "shop-a.example.com", "shop-b.example.com" |
| + ctl.setHeld(SliceName(held), "10.0.5.20/32") |
| + e := newEgressAsserter(ctl, staticSource(EgressPolicy{Closed: EgressUnderClaims}, nil)) |
| + |
| + rep := mustAssert(t, e, held) |
| + wantCalls(t, ctl.takeCalls(), showCall(held), setCall(held), showCall(held)) |
| + if rep.State != EgressUnderClaims || rep.Allowed != nil { |
| + t.Fatalf("held slice: report = %+v, want state %q and nothing allowed", rep, EgressUnderClaims) |
| + } |
| + |
| + rep = mustAssert(t, e, empty) |
| + wantCalls(t, ctl.takeCalls(), showCall(empty)) |
| + if rep.State != EgressUnderClaims || rep.Allowed != nil { |
| + t.Fatalf("empty slice: report = %+v, want state %q and nothing allowed", rep, EgressUnderClaims) |
| + } |
| +} |
| + |
| +func TestEgressReadBackFormsAreOneSet(t *testing.T) { |
| + ctl := newEgressCtl() |
| + ctl.setHeld(SliceName(egressHost), "fd00:5:0:0:0:0:0:20/128 10.0.5.20") |
| + e := newEgressAsserter(ctl, staticSource(EgressPolicy{Allow: []string{"10.0.5.20", "fd00:5::20"}}, nil)) |
| + rep := mustAssert(t, e, egressHost) |
| + wantCalls(t, ctl.takeCalls(), showCall(egressHost)) |
| + if want := (EgressReport{Allowed: []string{"10.0.5.20", "fd00:5::20"}}); !reflect.DeepEqual(rep, want) { |
| + t.Fatalf("report = %+v, want %+v", rep, want) |
| + } |
| +} |
| + |
| +func TestEgressReadBackUnparseableTokenIsADifference(t *testing.T) { |
| + ctl := newEgressCtl() |
| + ctl.setHeld(SliceName(egressHost), "10.0.5.20/32 localhost") |
| + e := newEgressAsserter(ctl, staticSource(EgressPolicy{Allow: []string{"10.0.5.20"}}, nil)) |
| + rep := mustAssert(t, e, egressHost) |
| + wantCalls(t, ctl.sets(), setCall(egressHost, "10.0.5.20/32")) |
| + if want := []string{"10.0.5.20"}; !reflect.DeepEqual(rep.Allowed, want) { |
| + t.Fatalf("Allowed = %q, want %q", rep.Allowed, want) |
| + } |
| +} |
| + |
| +func TestEgressFailingAfterAWriteThatDoesNotHold(t *testing.T) { |
| + ctl := newEgressCtl() |
| + ctl.ignore = true |
| + ctl.setHeld(SliceName(egressHost), "10.0.5.21/32") |
| + e := newEgressAsserter(ctl, staticSource(EgressPolicy{Allow: []string{"10.0.5.20"}}, nil)) |
| + if err := e.Assert(context.Background(), egressHost); err == nil { |
| + t.Fatal("Assert = nil, want an error when the write does not hold") |
| + } |
| + rep := e.Reports()[egressHost] |
| + if rep.State != EgressFailing { |
| + t.Fatalf("State = %q, want %q", rep.State, EgressFailing) |
| + } |
| + if want := []string{"10.0.5.21"}; !reflect.DeepEqual(rep.Allowed, want) { |
| + t.Fatalf("Allowed = %q, want what show returned, %q", rep.Allowed, want) |
| + } |
| +} |
| + |
| +func TestEgressSourceErrorKeepsTheLastPolicy(t *testing.T) { |
| + ctl := newEgressCtl() |
| + var srcErr error |
| + e := newEgressAsserter(ctl, func(context.Context, string) (EgressPolicy, error) { |
| + if srcErr != nil { |
| + return EgressPolicy{}, srcErr |
| + } |
| + return EgressPolicy{Allow: []string{"10.0.5.20"}}, nil |
| + }) |
| + mustAssert(t, e, egressHost) |
| + ctl.takeCalls() |
| + |
| + srcErr = errors.New("registry: locked") |
| + rep := mustAssert(t, e, egressHost) |
| + wantCalls(t, ctl.takeCalls(), showCall(egressHost)) |
| + if rep.State != EgressUnreadable || !reflect.DeepEqual(rep.Allowed, []string{"10.0.5.20"}) { |
| + t.Fatalf("report = %+v, want state %q and Allowed [10.0.5.20]", rep, EgressUnreadable) |
| + } |
| +} |
| + |
| +func TestEgressNoCacheAndSourceErrorCloses(t *testing.T) { |
| + ctl := newEgressCtl() |
| + ctl.setHeld(SliceName(egressHost), "10.0.5.20/32") |
| + var srcErr = errors.New("registry: locked") |
| + e := newEgressAsserter(ctl, func(context.Context, string) (EgressPolicy, error) { |
| + if srcErr != nil { |
| + return EgressPolicy{}, srcErr |
| + } |
| + return EgressPolicy{Allow: []string{"10.0.5.20"}}, nil |
| + }) |
| + rep := mustAssert(t, e, egressHost) |
| + wantCalls(t, ctl.sets(), setCall(egressHost)) |
| + if rep.State != EgressUnreadable || rep.Allowed != nil { |
| + t.Fatalf("report = %+v, want state %q and nothing allowed", rep, EgressUnreadable) |
| + } |
| + ctl.takeCalls() |
| + |
| + srcErr = nil |
| + rep = mustAssert(t, e, egressHost) |
| + wantCalls(t, ctl.sets(), setCall(egressHost, "10.0.5.20/32")) |
| + if want := (EgressReport{Allowed: []string{"10.0.5.20"}}); !reflect.DeepEqual(rep, want) { |
| + t.Fatalf("report = %+v, want %+v", rep, want) |
| + } |
| +} |
| + |
| +func TestEgressNilAsserterIsANoOp(t *testing.T) { |
| + var e *EgressAsserter |
| + if err := e.Assert(context.Background(), egressHost); err != nil { |
| + t.Fatalf("nil Assert = %v, want nil", err) |
| + } |
| + if r := e.Reports(); r != nil { |
| + t.Fatalf("nil Reports = %v, want nil", r) |
| + } |
| +} |
| + |
| +func TestEgressReportsIsACopy(t *testing.T) { |
| + ctl := newEgressCtl() |
| + e := newEgressAsserter(ctl, staticSource(EgressPolicy{Allow: []string{"10.0.5.20"}}, nil)) |
| + rep := mustAssert(t, e, egressHost) |
| + rep.Allowed[0] = "10.0.5.99" |
| + r := e.Reports() |
| + delete(r, egressHost) |
| + if got := e.Reports()[egressHost].Allowed; !reflect.DeepEqual(got, []string{"10.0.5.20"}) { |
| + t.Fatalf("Reports after the caller changed its copy: Allowed = %q", got) |
| + } |
| +} |
| + |
| +func TestEgressReadBackErrorStillWrites(t *testing.T) { |
| + ctl := newEgressCtl() |
| + ctl.setHeld(SliceName(egressHost), "10.0.5.21/32") |
| + e := newEgressAsserter(ctl, staticSource(EgressPolicy{}, nil)) |
| + first := true |
| + e.Systemctl = func(ctx context.Context, args ...string) (string, error) { |
| + out, err := ctl.run(ctx, args...) |
| + if args[0] == "show" && first { |
| + first = false |
| + return "Failed to get properties", errors.New("exit status 1") |
| + } |
| + return out, err |
| + } |
| + rep := mustAssert(t, e, egressHost) |
| + wantCalls(t, ctl.sets(), setCall(egressHost)) |
| + if !reflect.DeepEqual(rep, EgressReport{}) { |
| + t.Fatalf("report = %+v, want {}", rep) |
| + } |
| +} |
| + |
| +func TestEgressWriteErrorIsFailing(t *testing.T) { |
| + ctl := newEgressCtl() |
| + e := newEgressAsserter(ctl, staticSource(EgressPolicy{Allow: []string{"10.0.5.20"}}, nil)) |
| + e.Systemctl = func(ctx context.Context, args ...string) (string, error) { |
| + if args[0] == "set-property" { |
| + return "Failed to set unit properties", errors.New("exit status 1") |
| + } |
| + return ctl.run(ctx, args...) |
| + } |
| + if err := e.Assert(context.Background(), egressHost); err == nil { |
| + t.Fatal("Assert = nil, want the set-property error") |
| + } |
| + if rep := e.Reports()[egressHost]; rep.State != EgressFailing || !strings.Contains(rep.Detail, "Failed to set unit properties") { |
| + t.Fatalf("report = %+v, want state %q naming systemctl's output", rep, EgressFailing) |
| + } |
| +} |
| + |
| +func TestEgressRefusesABadHost(t *testing.T) { |
| + ctl := newEgressCtl() |
| + e := newEgressAsserter(ctl, staticSource(EgressPolicy{Allow: []string{"10.0.5.20"}}, nil)) |
| + if err := e.Assert(context.Background(), "../x y"); err == nil { |
| + t.Fatal("Assert(bad host) = nil, want an error") |
| + } |
| + wantCalls(t, ctl.takeCalls()) |
| +} |