| new file mode 100644 |
| index 000000000..57c470316 |
| --- /dev/null |
| +++ b/internal/console/egressfleet_test.go |
| @@ -0,0 +1,359 @@ |
| +package console |
| + |
| +import ( |
| + "context" |
| + "encoding/json" |
| + "errors" |
| + "net/http" |
| + "net/netip" |
| + "slices" |
| + "strings" |
| + "sync" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/edgesfile" |
| + "github.com/carlosframework/platform/internal/registry" |
| + "github.com/carlosframework/platform/internal/release" |
| + "github.com/carlosframework/platform/internal/status" |
| +) |
| + |
| +// putEdgesAddrs writes control/edges/<box>.json as a box publishes it, with |
| +// addrs as given; nil leaves the field out (an older box). |
| +func putEdgesAddrs(t *testing.T, s release.Store, box string, addrs []string) { |
| + t.Helper() |
| + f := map[string]any{"v": 1, "box": box, "eip": "192.0.2.1", "hosts": map[string]any{}} |
| + if addrs != nil { |
| + f["addrs"] = addrs |
| + } |
| + body, err := json.Marshal(f) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + putFixtureObject(t, s, edgesfile.Key(box), body) |
| +} |
| + |
| +// edgesCountingStore counts the requests the grant check makes under |
| +// control/edges/ and status/, forwards ETagLister as the production stores |
| +// answer it, and (failing set) refuses every one of them. |
| +type edgesCountingStore struct { |
| + release.Store |
| + mu sync.Mutex |
| + failing bool |
| + lists map[string]int // by prefix |
| + listETags map[string]int |
| + gets map[string]int // by key |
| +} |
| + |
| +func newEdgesCountingStore(s release.Store) *edgesCountingStore { |
| + return &edgesCountingStore{Store: s, lists: map[string]int{}, listETags: map[string]int{}, gets: map[string]int{}} |
| +} |
| + |
| +func watchedKey(key string) bool { |
| + return strings.HasPrefix(key, edgesfile.Prefix) || strings.HasPrefix(key, "status/") || strings.HasPrefix(key, "fleets/") |
| +} |
| + |
| +func (c *edgesCountingStore) fail(on bool) { |
| + c.mu.Lock() |
| + defer c.mu.Unlock() |
| + c.failing = on |
| +} |
| + |
| +func (c *edgesCountingStore) count(m map[string]int, key string) bool { |
| + c.mu.Lock() |
| + defer c.mu.Unlock() |
| + if !watchedKey(key) { |
| + return false |
| + } |
| + m[key]++ |
| + return c.failing |
| +} |
| + |
| +func (c *edgesCountingStore) List(ctx context.Context, prefix string) ([]string, error) { |
| + if c.count(c.lists, prefix) { |
| + return nil, errors.New("list refused") |
| + } |
| + return c.Store.List(ctx, prefix) |
| +} |
| + |
| +func (c *edgesCountingStore) ListETags(ctx context.Context, prefix string) ([]release.KeyETag, error) { |
| + if c.count(c.listETags, prefix) { |
| + return nil, errors.New("list refused") |
| + } |
| + l, ok := c.Store.(release.ETagLister) |
| + if !ok { |
| + return nil, errors.ErrUnsupported |
| + } |
| + return l.ListETags(ctx, prefix) |
| +} |
| + |
| +func (c *edgesCountingStore) Get(ctx context.Context, key string) ([]byte, string, error) { |
| + if c.count(c.gets, key) { |
| + return nil, "", errors.New("get refused") |
| + } |
| + return c.Store.Get(ctx, key) |
| +} |
| + |
| +// requests is every counted request, then the counts are zeroed. |
| +func (c *edgesCountingStore) requests() (lists, listETags, gets int) { |
| + c.mu.Lock() |
| + defer c.mu.Unlock() |
| + lists, listETags, gets = countSum(c.lists), countSum(c.listETags), countSum(c.gets) |
| + c.lists, c.listETags, c.gets = map[string]int{}, map[string]int{}, map[string]int{} |
| + return lists, listETags, gets |
| +} |
| + |
| +func countSum(m map[string]int) int { |
| + n := 0 |
| + for _, v := range m { |
| + n += v |
| + } |
| + return n |
| +} |
| + |
| +// edgesGets is the Gets of edges files alone, then all counts are kept. |
| +func (c *edgesCountingStore) edgesGets() int { |
| + c.mu.Lock() |
| + defer c.mu.Unlock() |
| + n := 0 |
| + for k, v := range c.gets { |
| + if strings.HasPrefix(k, edgesfile.Prefix) { |
| + n += v |
| + } |
| + } |
| + return n |
| +} |
| + |
| +// grantServer is a bare console over a fresh bucket (no registry, so no |
| +// fleets): enough for egressGrantCheck, which reads only the store. |
| +func grantServer(t *testing.T) (*Server, *edgesCountingStore) { |
| + t.Helper() |
| + cs := newEdgesCountingStore(release.NewDirStore(t.TempDir())) |
| + return &Server{cfg: Config{Releases: cs}, policyLog: func(string, ...any) {}}, cs |
| +} |
| + |
| +func addrs(t *testing.T, ss ...string) []netip.Addr { |
| + t.Helper() |
| + out := make([]netip.Addr, 0, len(ss)) |
| + for _, s := range ss { |
| + out = append(out, netip.MustParseAddr(s)) |
| + } |
| + return out |
| +} |
| + |
| +// An added address any edges file lists is refused, naming the box, |
| +// whatever the file's age: a retired box's retained file, whose status |
| +// snapshot has long gone stale, still excludes its addresses. |
| +func TestEgressGrantRefusesAFleetAddress(t *testing.T) { |
| + t.Parallel() |
| + s, cs := grantServer(t) |
| + now := time.Now() |
| + putEdgesAddrs(t, cs, "annie", []string{"10.0.1.5", "fd00:5::/64"}) |
| + setBoxSnapshot(t, cs, "annie", now) |
| + putEdgesAddrs(t, cs, "olive", []string{"10.0.5.20"}) |
| + setBoxSnapshot(t, cs, "olive", now.Add(-24*time.Hour)) |
| + if !status.BoxStale(status.Box{GeneratedAt: now.Add(-24 * time.Hour)}, now, status.SnapshotPeriod) { |
| + t.Fatal("olive's snapshot is not stale; the fixture tests nothing") |
| + } |
| + |
| + for _, c := range []struct { |
| + add []string |
| + want string |
| + }{ |
| + {[]string{"10.0.5.20"}, "10.0.5.20 is an address of box olive. An egress allow cannot open a box of this fleet."}, |
| + {[]string{"10.0.9.9", "10.0.1.5"}, "10.0.1.5 is an address of box annie. An egress allow cannot open a box of this fleet."}, |
| + {[]string{"fd00:5::20"}, "fd00:5::20 is an address of box annie. An egress allow cannot open a box of this fleet."}, |
| + } { |
| + nc, ref := s.egressGrantCheck(context.Background(), addrs(t, c.add...)) |
| + if ref == nil || ref.Status != http.StatusConflict || ref.Msg != c.want { |
| + t.Errorf("grant %v = %v %+v, want 409 %q", c.add, nc, ref, c.want) |
| + } |
| + } |
| + nc, ref := s.egressGrantCheck(context.Background(), addrs(t, "10.0.5.21")) |
| + if ref != nil || nc == nil || len(nc) != 0 { |
| + t.Fatalf("an address no box lists = %#v %+v, want [] and no refusal", nc, ref) |
| + } |
| +} |
| + |
| +// Any listed file that cannot be read refuses the grant (503, naming the |
| +// file's box), and so does a failed listing: the check does not pass what |
| +// it could not see. |
| +func TestEgressGrantRefusesWhileAFileIsUnreadable(t *testing.T) { |
| + t.Parallel() |
| + s, cs := grantServer(t) |
| + putEdgesAddrs(t, cs, "annie", []string{"10.0.1.5"}) |
| + putFixtureObject(t, cs, edgesfile.Key("betty"), []byte(`{"v":1,"box":"betty",`)) |
| + nc, ref := s.egressGrantCheck(context.Background(), addrs(t, "10.0.5.20")) |
| + if want := "Box betty's address list could not be read. Try again in a minute."; ref == nil || |
| + ref.Status != http.StatusServiceUnavailable || ref.Msg != want { |
| + t.Fatalf("an unreadable file = %v %+v, want 503 %q", nc, ref, want) |
| + } |
| + |
| + cs.fail(true) |
| + nc, ref = s.egressGrantCheck(context.Background(), addrs(t, "10.0.5.20")) |
| + if want := "The fleet's address lists could not be read. Try again in a minute."; ref == nil || |
| + ref.Status != http.StatusServiceUnavailable || ref.Msg != want { |
| + t.Fatalf("a failed listing = %v %+v, want 503 %q", nc, ref, want) |
| + } |
| +} |
| + |
| +// A box whose file has no addrs (an older box, or null) has addresses the |
| +// check cannot see: the grant is refused until it is upgraded. |
| +func TestEgressGrantRefusesAnOlderBox(t *testing.T) { |
| + t.Parallel() |
| + s, cs := grantServer(t) |
| + putEdgesAddrs(t, cs, "annie", []string{"10.0.1.5"}) |
| + putEdgesAddrs(t, cs, "betty", nil) |
| + nc, ref := s.egressGrantCheck(context.Background(), addrs(t, "10.0.5.20")) |
| + if want := "Box betty does not publish its addresses yet. Upgrade it before granting an egress allow."; ref == nil || |
| + ref.Status != http.StatusConflict || ref.Msg != want { |
| + t.Fatalf("an older box = %v %+v, want 409 %q", nc, ref, want) |
| + } |
| +} |
| + |
| +// Boxes that publish no edges file at all (bucketless fleet boxes) cannot |
| +// be checked: the grant goes through and names them. A stale box is not |
| +// named, nor is a box with a file (with or without addresses of its own). |
| +func TestEgressGrantNamesBoxesNotChecked(t *testing.T) { |
| + t.Parallel() |
| + fx := newConsoleFixture(t) |
| + now := time.Now() |
| + putEdgesAddrs(t, fx.rel, "annie", []string{"10.0.1.5"}) |
| + setBoxSnapshot(t, fx.rel, "annie", now) |
| + putEdgesAddrs(t, fx.rel, "dora", []string{}) |
| + setBoxSnapshot(t, fx.rel, "dora", now) |
| + setBoxSnapshot(t, fx.rel, "bert", now) // no edges file: not checked |
| + setBoxSnapshot(t, fx.rel, "cleo", now.Add(-24*time.Hour)) // stale, no file: not named |
| + reg, err := registry.Open(fx.regPath) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := reg.CreateFleet(registry.Fleet{Name: "fx", Account: fx.sqid, Bucket: "fx-data"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + reg.Close() |
| + setBoxSnapshot(t, release.NewPrefixStore(fx.rel, "fleets/fx/"), "bx-1", now) // a fleet box: no edges file |
| + |
| + nc, ref := fx.srv.egressGrantCheck(context.Background(), addrs(t, "10.0.5.20")) |
| + if ref != nil { |
| + t.Fatalf("refused: %+v", ref) |
| + } |
| + if want := []string{"bert", "bx-1"}; !slices.Equal(nc, want) { |
| + t.Fatalf("not checked = %v, want %v", nc, want) |
| + } |
| +} |
| + |
| +// Only the addresses a grant adds are checked (Review Focus 4). An allowed |
| +// address a box has since taken does not block an unrelated addition, and |
| +// a revoke or a no-op re-send reads nothing at all, so a store outage never |
| +// holds one up (ruling C5). A second grant over an unchanged edges prefix |
| +// costs one listing and no Gets of edges files (the request budget). |
| +func TestEgressGrantChecksOnlyAddedAddresses(t *testing.T) { |
| + t.Parallel() |
| + s, cs := grantServer(t) |
| + putEdgesAddrs(t, cs, "annie", []string{"10.0.5.20"}) |
| + putEdgesAddrs(t, cs, "betty", []string{"10.0.1.6"}) |
| + cs.requests() // the fixture's own writes |
| + ctx := context.Background() |
| + |
| + nc, st, msg := s.checkEgressGrant(ctx, nil, "shop", []string{"10.0.5.20"}, []string{"10.0.5.20", "10.0.5.21"}) |
| + if st != 0 || nc == nil || len(nc) != 0 { |
| + t.Fatalf("adding 10.0.5.21 beside a held 10.0.5.20 = %v %d %q, want [] 0", nc, st, msg) |
| + } |
| + if got := cs.edgesGets(); got != 2 { |
| + t.Fatalf("the first grant read %d edges files, want 2", got) |
| + } |
| + cs.requests() |
| + if _, st, msg := s.checkEgressGrant(ctx, nil, "shop", nil, []string{"10.0.5.22"}); st != 0 { |
| + t.Fatalf("a second grant = %d %q", st, msg) |
| + } |
| + if got := cs.edgesGets(); got != 0 { |
| + t.Fatalf("a second grant over unchanged edges files read %d of them again", got) |
| + } |
| + if cs.listETags[edgesfile.Prefix] != 1 || cs.lists[edgesfile.Prefix] != 0 { |
| + t.Fatalf("a second grant listed edges %d times with ETags and %d without, want 1 and 0", |
| + cs.listETags[edgesfile.Prefix], cs.lists[edgesfile.Prefix]) |
| + } |
| + if _, st, _ := s.checkEgressGrant(ctx, nil, "shop", []string{"10.0.5.20"}, []string{"10.0.5.20", "10.0.1.6"}); st != http.StatusConflict { |
| + t.Fatalf("adding betty's address = %d, want 409", st) |
| + } |
| + |
| + cs.fail(true) |
| + cs.requests() |
| + for _, c := range []struct{ old, next []string }{ |
| + {[]string{"10.0.5.20", "10.0.5.21"}, []string{"10.0.5.21"}}, // a revoke |
| + {[]string{"10.0.5.20"}, []string{}}, // revoke everything |
| + {[]string{"10.0.5.20"}, []string{"10.0.5.20"}}, // the same list again |
| + {nil, []string{}}, |
| + } { |
| + nc, st, msg := s.checkEgressGrant(ctx, nil, "shop", c.old, c.next) |
| + if st != 0 || nc == nil || len(nc) != 0 { |
| + t.Errorf("%v -> %v with the store down = %v %d %q, want [] 0", c.old, c.next, nc, st, msg) |
| + } |
| + } |
| + if l, le, g := cs.requests(); l+le+g != 0 { |
| + t.Fatalf("a revoke made %d lists, %d ETag lists and %d gets, want none", l, le, g) |
| + } |
| +} |
| + |
| +// The door's reply names the boxes it could not check, in the JSON a CLI |
| +// reads. |
| +func TestPolicyDoorReplyCarriesNotChecked(t *testing.T) { |
| + t.Parallel() |
| + fx := newPolicyFx(t, "code-policy-not-checked") |
| + now := time.Now() |
| + putEdgesAddrs(t, fx.rel, "annie", []string{"10.0.1.5"}) |
| + setBoxSnapshot(t, fx.rel, "annie", now) |
| + setBoxSnapshot(t, fx.rel, "bert", now) |
| + st, _, out := policyReq(t, fx.consoleFixture, http.MethodPut, "shop", fx.owner, `{"egress_allow":["10.0.5.20"]}`, policyCreate) |
| + if st != http.StatusOK || !strings.Contains(out, `"not_checked":["bert"]`) { |
| + t.Fatalf("PUT = %d %s, want 200 with not_checked [bert]", st, out) |
| + } |
| +} |
| + |
| +// Through the door, with control/edges/ and the status snapshots down: a |
| +// revoke and a size-only change go through and read neither, and only an |
| +// addition is refused (ruling C5). |
| +func TestPolicyDoorRevokeGoesThroughAnEdgesOutage(t *testing.T) { |
| + t.Parallel() |
| + fx := newPolicyFx(t, "code-policy-revoke-outage") |
| + first := fx.mustPut(t, "shop", `{"egress_allow":["10.0.5.20","10.0.5.21"]}`, policyCreate) |
| + cs := newEdgesCountingStore(fx.srv.cfg.Releases) |
| + fx.srv.cfg.Releases = cs |
| + cs.fail(true) |
| + |
| + revoked := fx.mustPut(t, "shop", `{"egress_allow":["10.0.5.21"]}`, policyIfMatch(first.ETag)) |
| + if !slices.Equal(revoked.Policy.EgressAllow, []string{"10.0.5.21"}) || revoked.NotChecked == nil || len(revoked.NotChecked) != 0 { |
| + t.Fatalf("revoke reply %+v", revoked) |
| + } |
| + sized := fx.mustPut(t, "shop", `{"default_size":"large"}`, policyIfMatch(revoked.ETag)) |
| + if l, le, g := cs.requests(); l+le+g != 0 { |
| + t.Fatalf("a revoke and a size change made %d lists, %d ETag lists and %d gets of edges or status, want none", l, le, g) |
| + } |
| + if st, _, out := fx.put(t, "shop", `{"egress_allow":["10.0.5.21","10.0.5.22"]}`, policyIfMatch(sized.ETag)); st != http.StatusServiceUnavailable { |
| + t.Fatalf("an addition with edges down = %d %s, want 503", st, out) |
| + } |
| +} |
| + |
| +// A refused check writes nothing: no audit event, no policy object, no row, |
| +// no serial bump. |
| +func TestPolicyDoorWritesNothingWhenTheCheckRefuses(t *testing.T) { |
| + t.Parallel() |
| + fx := newPolicyFx(t, "code-policy-check-refuses") |
| + putEdgesAddrs(t, fx.rel, "annie", []string{"10.0.5.20"}) |
| + before := fx.serial(t) |
| + st, _, out := fx.put(t, "shop", `{"egress_allow":["10.0.5.20"],"default_size":"large"}`, policyCreate) |
| + if want := "10.0.5.20 is an address of box annie. An egress allow cannot open a box of this fleet."; st != http.StatusConflict || errorOf(t, out) != want { |
| + t.Fatalf("PUT = %d %s, want 409 %q", st, out, want) |
| + } |
| + fx.noObject(t, "shop") |
| + if evs := fx.policyEvents(t, "shop"); len(evs) != 0 { |
| + t.Fatalf("a refused grant recorded events: %+v", evs) |
| + } |
| + if row := fx.row(t, "shop"); row.Revision != 0 { |
| + t.Fatalf("a refused grant recorded a row: %+v", row) |
| + } |
| + if got := fx.serial(t); got != before { |
| + t.Fatalf("serial %d, want %d", got, before) |
| + } |
| +} |