console: egress ranges checked by containment, gated at v0.43.0 (#1357)
grantAdded returns prefixes. A grant adding a range is refused while any fresh heartbeat box of the deployment (platform and fleets) runs below release.EgressPrefixFloor (v0.43.0) or names no release, and 503 when the heartbeats cannot be read whole. Singles and revokes are never gated. An added entry overlapping a published box address is refused, naming the box and the address. The live report, its alert, GET's egress_conflicts (apps show "!") and status --layout work on entries, keyed by the stored form. The fleet-box warning names the boxes as fleet boxes and says each still refuses its own address; the --egress-allow help and docs/site cli.md describe ranges and the floor. The copy record carries the new IP1-50 and IP2-30 text pending the copy pass.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
8 files changed,
+369
−59
apps_policy.go+2 −2apps_policy_test.go+6 −5docs/site/cli.md+5 −5docs/superpowers/plans/2026-10-21-instance-policy-copy.json+2 −2internal/console/egressfleet.go+170 −42internal/console/egressfleet_test.go+161 −3internal/release/layoutintent.go+12 −0internal/release/layoutintent_test.go+11 −0
diff --git a/apps_policy.go b/apps_policy.go| index b39815bd9..46b9d50ae 100644 |
| --- a/apps_policy.go |
| +++ b/apps_policy.go |
| @@ -33,7 +33,7 @@ import ( |
| // Copy (approved by delegation, Luna pass 2026-10-26; deck copy-instance-policy-plan1.md). |
| const ( |
| - appsSetFlagEgress = `private addresses every instance of the app may reach, comma-separated, never a port. pass the whole list; "" clears it. system operators only` |
| + appsSetFlagEgress = `private addresses or ranges every instance of the app may reach, comma-separated, never a port. a range is at least /16 (IPv4) or /48 (IPv6). pass the whole list; "" clears it. system operators only` |
| appsSetFlagSize = "the size the app's instances run at: small, standard, large or xlarge. default uses the build's size. system operators only" |
| instancesSetFlagSize = "this instance's size: small, standard, large or xlarge. default follows the app's size. system operators only" |
| appsSetPolicyUsage = `Only system operators may pass --egress-allow or --size. A change reaches |
| @@ -45,7 +45,7 @@ the app's boxes within seconds on a box following the deployment's change index, |
| appsPolicyChanged = "the policy changed while this ran. run it again" |
| appsSetEgressDone = "egress allow for %s set to %s" |
| appsSetEgressCleared = "egress allow for %s cleared" |
| - appsSetEgressNotChecked = "not checked against these boxes, which publish no address list: %s" |
| + appsSetEgressNotChecked = "not checked against fleet boxes %s, which do not publish their addresses. each one still refuses an entry that holds its own address" |
| appsSetSizeDone = "default size for %s set to %s. instances with their own size are unchanged" |
| appsSetSizeCleared = "default size for %s set to the build's size. instances with their own size are unchanged" |
| instancesSetSizeDone = "%s size set to %s" |
diff --git a/apps_policy_test.go b/apps_policy_test.go| index 190a68d16..cb258815a 100644 |
| --- a/apps_policy_test.go |
| +++ b/apps_policy_test.go |
| @@ -99,7 +99,8 @@ func TestAppsSetPolicyRefusesClientSide(t *testing.T) { |
| want string |
| }{ |
| {[]string{"--egress-allow", "10.0.5.20:5432"}, "has a port"}, |
| - {[]string{"--egress-allow", "10.0.5.0/24"}, "is a range"}, |
| + {[]string{"--egress-allow", "10.0.64.5/20"}, "has host bits set. write it as 10.0.64.0/20"}, |
| + {[]string{"--egress-allow", "10.0.0.0/8"}, "is wider than /16"}, |
| {[]string{"--egress-allow", "8.8.8.8"}, "already reachable"}, |
| {[]string{"--egress-allow", "10.0.5.20,8.8.8.8"}, "already reachable"}, |
| } |
| @@ -287,12 +288,12 @@ func TestAppsShowPrintsThePolicy(t *testing.T) { |
| // egress_conflicts, marks nothing. |
| func TestAppsShowMarksConflicts(t *testing.T) { |
| out := captureStdout(t) |
| - c := &policyConsole{getBody: `{"policy":{"v":1,"egress_allow":["10.0.5.20","fd00:5::20"]},` + |
| - `"etag":"e1","not_checked":[],"egress_conflicts":["fd00:5::20","10.0.5.30"]}`} |
| + c := &policyConsole{getBody: `{"policy":{"v":1,"egress_allow":["10.0.5.20","10.0.64.0/20","fd00:5::20"]},` + |
| + `"etag":"e1","not_checked":[],"egress_conflicts":["fd00:5::20","10.0.5.30","10.0.64.0/20"]}`} |
| if err := appsShowRun(context.Background(), c.session(t), apiAcct{Seg: "bab"}, "shop"); err != nil { |
| t.Fatal(err) |
| } |
| - if want := "egress allow 10.0.5.20, !fd00:5::20\ndefault size the build's size\n"; out.String() != want { |
| + if want := "egress allow 10.0.5.20, !10.0.64.0/20, !fd00:5::20\ndefault size the build's size\n"; out.String() != want { |
| t.Fatalf("stdout = %q, want %q", out.String(), want) |
| } |
| @@ -323,7 +324,7 @@ func TestAppsSetPrintsNotChecked(t *testing.T) { |
| if out.String() != want { |
| t.Fatalf("stdout = %q, want %q", out.String(), want) |
| } |
| - if want := "not checked against these boxes, which publish no address list: box-c\n"; !strings.HasSuffix(out.String(), want) { |
| + if want := "not checked against fleet boxes box-c, which do not publish their addresses. each one still refuses an entry that holds its own address\n"; !strings.HasSuffix(out.String(), want) { |
| t.Fatalf("stdout = %q, want it to end %q", out.String(), want) |
| } |
diff --git a/docs/site/cli.md b/docs/site/cli.md| index 3923a88ca..bbfdeeddd 100644 |
| --- a/docs/site/cli.md |
| +++ b/docs/site/cli.md |
| @@ -253,21 +253,21 @@ If a box is lost or detached, the requested count stays the same. The platform u |
| System operators can also set an app's instance policy: which private addresses its instances may reach and the size each instance runs at. `carlos apps show` reads it back. |
| ```sh |
| -carlos apps set shop --egress-allow 10.0.5.20,fd00:5::20 |
| +carlos apps set shop --egress-allow 10.0.5.20,10.0.64.0/20 |
| carlos apps set shop --size large |
| carlos instances set shop-a.example.com --app shop --size xlarge |
| carlos apps show shop |
| ``` |
| -An instance cannot reach a private address unless its app allows it. `--egress-allow` takes the app's whole list, comma-separated, and replaces the list before it. To drop one address, pass the list without it; `--egress-allow ""` removes them all. Each entry is a single IP address, with no range, port or zone. Public addresses are refused because instances can reach them already, and so are loopback, link-local, multicast and reserved addresses, the cloud metadata service and the NAT64 prefix. An app may allow at most 8. |
| +An instance cannot reach a private address unless its app allows it. `--egress-allow` takes the app's whole list, comma-separated, and replaces the list before it. To drop one entry, pass the list without it; `--egress-allow ""` removes them all. Each entry is a single IP address or a private range such as `10.0.64.0/20` or `fd00:5::/64`, with no port or zone. A range is at least a /16 for IPv4 or a /48 for IPv6, and starts at its first address, so `10.0.64.5/20` is refused. Public addresses are refused because instances can reach them already, and so are loopback, link-local, multicast and reserved addresses, the cloud metadata service and the NAT64 prefix, and any range that holds one of them. An app may allow at most 8 entries, and a range counts as one. |
| -A change that adds an address is checked against the deployment's boxes. An address that belongs to one of them is refused, and so is any new address while a box does not publish its address list yet. Boxes that publish none at all are named after the change. An allow takes effect only on a box where every instance runs nested (see `carlos status --layout`). It does not take effect while host claims are on. If a box later takes an allowed address, the console sends an alert. |
| +A change that adds an entry is checked against the deployment's boxes. An entry that holds an address of one of them is refused, and so is any new entry while a box does not publish its address list yet. Fleet boxes that publish no address list are named after the change, and each of them still refuses an entry that holds its own address. A range can be added only while every box runs v0.43.0 or later. Single addresses work on any release. An allow takes effect only on a box where every instance runs nested (see `carlos status --layout`). It does not take effect while host claims are on. If a box later takes an allowed address, or an address in an allowed range, the console sends an alert. |
| The sizes are `small` (256 MiB of memory), `standard` (512 MiB), `large` (1 GiB) and `xlarge` (2 GiB), with CPU and IO to match. `carlos apps set --size` sets the app's size, and `--size default` goes back to the build's size. `carlos instances set --size` gives one instance a size of its own, and `--size default` makes it follow the app's size again. A size above `standard` is refused when it is more than a quarter of the memory on the smallest box the app can run on, or when one of those boxes reports no memory total. |
| After a size change, the command prints each box's memory committed to awake instances, out of its total. CPU and IO change at once. A larger size's memory applies when an instance next starts or is deployed. A smaller size applies only when the instance starts cold. Restart it to apply the new memory now. `carlos status --layout` shows the size each instance runs at. |
| -The console records a change, and the app's boxes apply it. A box that follows the deployment's change index does so within seconds; any other box within about 33 minutes. `carlos apps show` prints the egress allow, the app's size, and every instance with a size of its own. A `!` before an address means one of the deployment's boxes now holds it. |
| +The console records a change, and the app's boxes apply it. A box that follows the deployment's change index does so within seconds; any other box within about 33 minutes. `carlos apps show` prints the egress allow, the app's size, and every instance with a size of its own. A `!` before an entry means one of the deployment's boxes now holds that address, or an address in that range. |
| ### carlos ship |
| @@ -3310,7 +3310,7 @@ socket does not exist. Requests to that address then fail. Like |
| The REGION column names where each route or box runs, such as London (UK). |
| -The `SIZE` column is the size an awake nested instance runs at: `swap` while a deploy is swapping it, `?` when its box could not read it, and `-` for anything else. `standard>large` means it runs at standard and its policy asks for large; a note under the table says when that takes effect. The `EGRESS` column is the instance's egress allow as its box applies it. A `!` before an address means one of the deployment's boxes now holds it. A word after `!` means the allow is not in force: `failing`, `policy unreadable`, `policy unverifiable`, `policy expired`, `box not wholly nested`, `not yet supported under claims`, or `refused` when the box dropped an entry. `REASON` gives the detail after `egress:`. |
| +The `SIZE` column is the size an awake nested instance runs at: `swap` while a deploy is swapping it, `?` when its box could not read it, and `-` for anything else. `standard>large` means it runs at standard and its policy asks for large; a note under the table says when that takes effect. The `EGRESS` column is the instance's egress allow as its box applies it. A `!` before an entry means one of the deployment's boxes now holds that address, or an address in that range. A word after `!` means the allow is not in force: `failing`, `policy unreadable`, `policy unverifiable`, `policy expired`, `box not wholly nested`, `not yet supported under claims`, or `refused` when the box dropped an entry. `REASON` gives the detail after `egress:`. |
| A box that runs `nested` instances beside `flat`, `foreign` or `external` |
| ones gets a warning after the table. Each of those still shares the agent's |
diff --git a/docs/superpowers/plans/2026-10-21-instance-policy-copy.json b/docs/superpowers/plans/2026-10-21-instance-policy-copy.json| index e07e57792..460ea14aa 100644 |
| --- a/docs/superpowers/plans/2026-10-21-instance-policy-copy.json |
| +++ b/docs/superpowers/plans/2026-10-21-instance-policy-copy.json |
| @@ -57,7 +57,7 @@ |
| {"id": "IP1-35", "file": "apps_policy.go", "text": "the policy changed while this ran. run it again"}, |
| {"id": "IP1-36", "file": "apps_policy.go", "text": "egress allow for %s set to %s"}, |
| {"id": "IP1-37", "file": "apps_policy.go", "text": "egress allow for %s cleared"}, |
| - {"id": "IP2-30", "file": "apps_policy.go", "text": "not checked against these boxes, which publish no address list: %s"}, |
| + {"id": "IP2-30", "file": "apps_policy.go", "text": "not checked against fleet boxes %s, which do not publish their addresses. each one still refuses an entry that holds its own address"}, |
| {"id": "IP1-38", "file": "apps_policy.go", "text": "default size for %s set to %s. instances with their own size are unchanged"}, |
| {"id": "IP1-39", "file": "apps_policy.go", "text": "default size for %s set to the build's size. instances with their own size are unchanged"}, |
| {"id": "IP1-42", "file": "apps_policy.go", "text": "%s size set to %s"}, |
| @@ -65,7 +65,7 @@ |
| {"id": "IP3-10", "file": "apps_policy.go", "text": "%s: %d MiB committed to awake instances, of %d MiB. %d MiB free"}, |
| {"id": "IP3-11", "file": "apps_policy.go", "text": "the new memory size applies at the next start. a larger size also applies at the next deploy; a smaller size waits for a cold start. CPU and IO apply now"}, |
| {"id": "IP1-44", "file": "apps_policy.go", "text": "apps show works through a console, and this shell is in direct bucket mode (CARLOS_DEPLOYMENT_BUCKET or CARLOS_DEPLOYMENT_DIR is set). log in instead: carlos auth login --console <url>"}, |
| - {"id": "IP1-50", "file": "apps_policy.go", "text": "private addresses every instance of the app may reach, comma-separated, never a port. pass the whole list; \"\" clears it. system operators only"}, |
| + {"id": "IP1-50", "file": "apps_policy.go", "text": "private addresses or ranges every instance of the app may reach, comma-separated, never a port. a range is at least /16 (IPv4) or /48 (IPv6). pass the whole list; \"\" clears it. system operators only"}, |
| {"id": "IP1-51", "file": "apps_policy.go", "text": "the size the app's instances run at: small, standard, large or xlarge. default uses the build's size. system operators only"}, |
| {"id": "IP1-52", "file": "apps_policy.go", "text": "this instance's size: small, standard, large or xlarge. default follows the app's size. system operators only"}, |
| {"id": "IP1-53", "file": "apps_policy.go", "text": "Only system operators may pass --egress-allow or --size. A change reaches\nthe app's boxes within seconds on a box following the deployment's change index, or within about 33 minutes on another box."}, |
diff --git a/internal/console/egressfleet.go b/internal/console/egressfleet.go| index abfe747bb..0eaa07e15 100644 |
| --- a/internal/console/egressfleet.go |
| +++ b/internal/console/egressfleet.go |
| @@ -12,6 +12,11 @@ |
| // fleet boxes) cannot be checked; the grant goes through and the door's |
| // reply names them as not_checked. |
| // |
| +// An added entry may be a prefix (#1357): it is refused when any published |
| +// box address lies in it (decision 8), and it is added only while every |
| +// fresh box of the deployment runs release.EgressPrefixFloor or later |
| +// (decision 9). Single addresses are never held by the floor. |
| +// |
| // Only additions are checked (ruling C5): a revoke, a re-send of the same |
| // list or a size-only change reads nothing, so a store outage never holds |
| // one up. A grant reads the edges files through the console's AddrCache, |
| @@ -40,37 +45,57 @@ import ( |
| // Door copy (approved by delegation, Luna pass 2026-10-26; deck copy-instance-policy-plan2.md). |
| const ( |
| - egressFleetHeldMsg = "%s is an address of box %s. an egress allow cannot open a box of this fleet" |
| + egressFleetHeldMsg = "%s is an address of box %s. an egress allow cannot open a box of this fleet" |
| + // egressFleetRangeHeldMsg and egressRangeBelowFloorMsg are #1357's, |
| + // awaiting their copy pass. |
| + egressFleetRangeHeldMsg = "%s holds %s, an address of box %s. an egress allow cannot open a box of this fleet" |
| + // egressRangeBelowFloorMsg is filled with the first fresh box below |
| + // release.EgressPrefixFloor (by label), the release it runs |
| + // (boxReleaseWord) and the floor. |
| + egressRangeBelowFloorMsg = "box %s runs %s. allowing a range needs v%s or later on every box. single addresses are unaffected" |
| egressFleetUnreadableMsg = "box %s's address list could not be read. try again in a minute" |
| egressFleetOlderBoxMsg = "box %s does not publish its addresses yet. upgrade it before granting an egress allow" |
| egressFleetListMsg = "the fleet's address lists could not be read. try again in a minute" |
| egressFleetBoxesMsg = "the fleet's list of boxes could not be read. try again in a minute" |
| ) |
| -// egressGrantCheck checks the addresses a grant adds (new minus old) against |
| +// egressGrantCheck checks the entries a grant adds (new minus old) against |
| // every readable edges file, whatever its age. notChecked: labels of |
| // non-stale boxes in systemBoxes that publish no edges file. |
| // |
| -// The refusals, in order: an added address a file lists (409, naming the |
| -// box); a listed file that could not be read (503, naming it); a file with |
| -// no addrs (409, naming its box); a failed listing of control/edges/ or of |
| -// the boxes (503). With nothing added it reads nothing. |
| -func (s *Server) egressGrantCheck(ctx context.Context, added []netip.Addr) (notChecked []string, ref *doorRefusal) { |
| +// The refusals, in order: an added range while a fresh box runs below |
| +// release.EgressPrefixFloor (409, naming the box) or the heartbeats could |
| +// not be read whole (503); an added entry holding an address a file lists |
| +// (409, naming the box); a listed file that could not be read (503, naming |
| +// it); a file with no addrs (409, naming its box); a failed listing of |
| +// control/edges/ or of the boxes (503). With nothing added it reads |
| +// nothing. |
| +func (s *Server) egressGrantCheck(ctx context.Context, added []netip.Prefix) (notChecked []string, ref *doorRefusal) { |
| if len(added) == 0 { |
| return []string{}, nil |
| } |
| if s.cfg.Releases == nil { |
| return nil, &doorRefusal{Status: http.StatusServiceUnavailable, Msg: egressFleetListMsg} |
| } |
| + if slices.ContainsFunc(added, func(p netip.Prefix) bool { return !p.IsSingleIP() }) { |
| + if ref := s.egressPrefixFloorRefusal(ctx); ref != nil { |
| + return nil, ref |
| + } |
| + } |
| inv, err := edgesfile.ReadAddrsCached(ctx, s.cfg.Releases, &s.egressAddrs) |
| if err != nil { |
| s.policyLogf("console: egress grant check: list %s: %v", edgesfile.Prefix, err) |
| return nil, &doorRefusal{Status: http.StatusServiceUnavailable, Msg: egressFleetListMsg} |
| } |
| - for _, a := range added { |
| - if box, ok := inv.Holder(a); ok { |
| - return nil, &doorRefusal{Status: http.StatusConflict, Msg: fmt.Sprintf(egressFleetHeldMsg, a, box)} |
| + for _, p := range added { |
| + box, held, ok := egressHolder(inv, p) |
| + if !ok { |
| + continue |
| + } |
| + if p.IsSingleIP() { |
| + return nil, &doorRefusal{Status: http.StatusConflict, Msg: fmt.Sprintf(egressFleetHeldMsg, p.Addr(), box)} |
| } |
| + return nil, &doorRefusal{Status: http.StatusConflict, Msg: fmt.Sprintf(egressFleetRangeHeldMsg, p, held, box)} |
| } |
| if len(inv.Unreadable) > 0 { |
| s.policyLogf("console: egress grant check: unreadable edges files: %s", strings.Join(inv.Unreadable, ", ")) |
| @@ -103,31 +128,123 @@ func (s *Server) egressGrantCheck(ctx context.Context, added []netip.Addr) (notC |
| return slices.Compact(notChecked), nil |
| } |
| +// egressHolder names the first box (by label) with a published address that |
| +// overlaps entry, and that address as the box published it (a bare address, |
| +// or a prefix). For a single address this is edgesfile's Holder; for a |
| +// prefix it is containment, the box's address inside the range (decision |
| +// 8). An IPv4-mapped entry is matched as IPv4. |
| +func egressHolder(inv edgesfile.AddrInventory, entry netip.Prefix) (box, held string, ok bool) { |
| + if a := entry.Addr(); a.Is4In6() && entry.Bits() >= 96 { |
| + entry = netip.PrefixFrom(a.Unmap(), entry.Bits()-96) |
| + } |
| + entry = netip.PrefixFrom(entry.Addr().WithZone(""), entry.Bits()) |
| + boxes := slices.Sorted(maps.Keys(inv.Boxes)) |
| + for _, b := range boxes { |
| + for _, q := range inv.Boxes[b] { |
| + if entry.Overlaps(q) { |
| + return b, egressaddr.EntryString(q), true |
| + } |
| + } |
| + } |
| + return "", "", false |
| +} |
| + |
| +// egressPrefixFloorRefusal is the version floor on adding a range (decision |
| +// 9): nil when every fresh heartbeat box of the deployment (the platform's |
| +// and every fleet's, as an app can move to any box) runs |
| +// release.EgressPrefixFloor or later. A box below it would read the policy |
| +// as unverifiable and clear the app's whole allow. A deployment with no |
| +// fresh box admits, as teamFloorRefusal does. A release that is missing or |
| +// does not parse counts as below; heartbeats that cannot be read whole are |
| +// 503. |
| +func (s *Server) egressPrefixFloorRefusal(ctx context.Context) *doorRefusal { |
| + boxes, ok := s.egressFloorBoxes(ctx) |
| + if !ok { |
| + return &doorRefusal{Status: http.StatusServiceUnavailable, Msg: egressFleetBoxesMsg} |
| + } |
| + now := time.Now() |
| + var old *status.Box |
| + for i, b := range boxes { |
| + if status.BoxStale(b, now, status.SnapshotPeriod) || boxAtEgressPrefixFloor(b) { |
| + continue |
| + } |
| + if old == nil || b.Label < old.Label { |
| + old = &boxes[i] |
| + } |
| + } |
| + if old == nil { |
| + return nil |
| + } |
| + return &doorRefusal{Status: http.StatusConflict, Msg: fmt.Sprintf(egressRangeBelowFloorMsg, |
| + old.Label, boxReleaseWord(*old), release.EgressPrefixFloor.String())} |
| +} |
| + |
| +// boxAtEgressPrefixFloor is boxAtTeamFloor against release.EgressPrefixFloor. |
| +func boxAtEgressPrefixFloor(b status.Box) bool { |
| + if b.Metrics == nil { |
| + return false |
| + } |
| + v, ok := release.ParseReleaseVersion(b.Metrics.Release) |
| + return ok && v.Compare(release.EgressPrefixFloor) >= 0 |
| +} |
| + |
| +// egressFloorBoxes is every heartbeat of the deployment: the platform's and |
| +// each fleet's, less a fleet's detached boxes. ok is false when the fleets |
| +// or any heartbeat could not be read (systemBoxes, by contrast, reads past |
| +// an incomplete listing). |
| +func (s *Server) egressFloorBoxes(ctx context.Context) ([]status.Box, bool) { |
| + reg, fleets, err := s.systemFleets() |
| + if err != nil { |
| + s.policyLogf("console: egress prefix floor: fleets: %v", err) |
| + return nil, false |
| + } |
| + boxes, complete, err := s.loadBoxes(ctx, s.cfg.Releases, "") |
| + if err != nil || !complete { |
| + s.policyLogf("console: egress prefix floor: heartbeats: complete %t, %v", complete, err) |
| + return nil, false |
| + } |
| + boxes = slices.Clone(boxes) |
| + for _, f := range fleets { |
| + fb, complete, err := s.loadBoxes(ctx, s.cfg.Releases, "fleets/"+f.Name+"/") |
| + if err != nil || !complete { |
| + s.policyLogf("console: egress prefix floor: fleet %s heartbeats: complete %t, %v", f.Name, complete, err) |
| + return nil, false |
| + } |
| + detached, err := detachedFleetBoxes(ctx, reg, f.Name) |
| + if err != nil { |
| + s.policyLogf("console: egress prefix floor: fleet %s registry boxes: %v; counting every report", f.Name, err) |
| + } |
| + boxes = append(boxes, withoutDetached(fb, detached)...) |
| + } |
| + return boxes, true |
| +} |
| + |
| // edgesKeyBox is the box an edges file's key names: control/edges/<box>.json. |
| func edgesKeyBox(key string) string { |
| return strings.TrimSuffix(strings.TrimPrefix(key, edgesfile.Prefix), ".json") |
| } |
| -// grantAdded is next minus old, as addresses: what a grant opens that the |
| -// stored allow did not. next is the normalized list, so an entry that does |
| -// not parse is refused with egressaddr's reason; an old entry that does not |
| -// parse matches nothing. |
| -func grantAdded(old, next []string) ([]netip.Addr, error) { |
| - had := make(map[netip.Addr]bool, len(old)) |
| +// grantAdded is next minus old, as entries (a single address is its |
| +// full-length prefix): what a grant opens that the stored allow did not. A |
| +// range and an address inside it are separate entries. next is the |
| +// normalized list, so an entry that does not parse is refused with |
| +// egressaddr's reason; an old entry that does not parse matches nothing. |
| +func grantAdded(old, next []string) ([]netip.Prefix, error) { |
| + had := make(map[netip.Prefix]bool, len(old)) |
| for _, o := range old { |
| - if a, err := egressaddr.Parse(o); err == nil { |
| - had[a] = true |
| + if p, err := egressaddr.ParseEntry(o); err == nil { |
| + had[p] = true |
| } |
| } |
| - var added []netip.Addr |
| + var added []netip.Prefix |
| for _, n := range next { |
| - a, err := egressaddr.Parse(n) |
| + p, err := egressaddr.ParseEntry(n) |
| if err != nil { |
| return nil, err |
| } |
| - if !had[a] { |
| - had[a] = true |
| - added = append(added, a) |
| + if !had[p] { |
| + had[p] = true |
| + added = append(added, p) |
| } |
| } |
| return added, nil |
| @@ -166,12 +283,19 @@ var pageEgressConflict = alerting.Page |
| const ( |
| egressConflictSubject = "Egress allow %s is now a fleet box's address." |
| egressConflictBody = "App %s in account id %s may reach %s, and box %s now holds that address. The allow is still in force. Set the whole list again without that address by running carlos apps set %s --egress-allow, or move the address off the box." |
| + // The same alert for an allowed range (#1357, awaiting its copy pass). |
| + egressRangeConflictSubject = "Egress allow %s now holds a fleet box's address." |
| + egressRangeConflictBody = "App %s in account id %s may reach %s, and box %s now holds an address in that range. The allow is still in force. Set the whole list again without that range by running carlos apps set %s --egress-allow, or move the address off the box." |
| ) |
| -// egressConflictAlert is the report's alert. |
| -func egressConflictAlert(sqid, app string, addr netip.Addr, box string) (subject, body string) { |
| - return fmt.Sprintf(egressConflictSubject, addr), |
| - fmt.Sprintf(egressConflictBody, app, sqid, addr, box, app) |
| +// egressConflictAlert is the report's alert for an allowed entry. |
| +func egressConflictAlert(sqid, app string, entry netip.Prefix, box string) (subject, body string) { |
| + if !entry.IsSingleIP() { |
| + return fmt.Sprintf(egressRangeConflictSubject, entry), |
| + fmt.Sprintf(egressRangeConflictBody, app, sqid, entry, box, app) |
| + } |
| + return fmt.Sprintf(egressConflictSubject, entry.Addr()), |
| + fmt.Sprintf(egressConflictBody, app, sqid, entry.Addr(), box, app) |
| } |
| // egressReportState is the report's memory. Every field after mu is guarded |
| @@ -186,10 +310,10 @@ type egressReportState struct { |
| // genOK (the watch could vouch for it then). |
| gen uint64 |
| genOK bool |
| - // conflicts is the last pass's findings, address -> box; nil before |
| - // the first pass. |
| + // conflicts is the last pass's findings, allowed entry (its stored |
| + // form) -> box; nil before the first pass. |
| conflicts map[string]string |
| - // alerted is when each "<sqid>/<app> <addr>" alert was last sent. |
| + // alerted is when each "<sqid>/<app> <entry>" alert was last sent. |
| alerted map[string]time.Time |
| now func() time.Time // nil: time.Now |
| @@ -213,13 +337,17 @@ func (s *Server) egressEdgesGen() (uint64, bool) { |
| return w.Gen(egressEdgesScope) |
| } |
| -// egressAllowed is one address of one app's allow. |
| +// egressAllowed is one entry of one app's allow: a single address as its |
| +// full-length prefix, or a range. |
| type egressAllowed struct { |
| sqid, app string |
| - addr netip.Addr |
| + entry netip.Prefix |
| } |
| -// egressLiveAllows is every address of every live allow, read from the |
| +// key is the entry's stored form, the report's and the alert's key. |
| +func (a egressAllowed) key() string { return egressaddr.EntryString(a.entry) } |
| + |
| +// egressLiveAllows is every entry of every live allow, read from the |
| // console's own policy rows (AppPolicyRowsContext) and opened with the key |
| // they were signed with. unopened names the rows that do not open (or hold |
| // an entry that does not parse): their allows are not checked. |
| @@ -236,12 +364,12 @@ func (s *Server) egressLiveAllows(ctx context.Context) (allows []egressAllowed, |
| continue |
| } |
| for _, e := range p.EgressAllow { |
| - a, err := egressaddr.Parse(e) |
| + p, err := egressaddr.ParseEntry(e) |
| if err != nil { |
| unopened = append(unopened, r.Sqid+"/"+r.App) |
| continue |
| } |
| - allows = append(allows, egressAllowed{sqid: r.Sqid, app: r.App, addr: a}) |
| + allows = append(allows, egressAllowed{sqid: r.Sqid, app: r.App, entry: p}) |
| } |
| } |
| return allows, unopened, nil |
| @@ -375,12 +503,12 @@ func (s *Server) egressReportPass(ctx context.Context) error { |
| } |
| allowed := map[string]bool{} |
| for _, a := range allows { |
| - allowed[a.addr.String()] = true |
| - box, ok := inv.Holder(a.addr) |
| + allowed[a.key()] = true |
| + box, _, ok := egressHolder(inv, a.entry) |
| if !ok { |
| continue |
| } |
| - conflicts[a.addr.String()] = box |
| + conflicts[a.key()] = box |
| s.egressAlert(ctx, a, box, now) |
| } |
| st.mu.Lock() |
| @@ -429,7 +557,7 @@ func egressBoxReadInFull(inv edgesfile.AddrInventory, box string) bool { |
| // address within egressAlertEvery. The slot is taken before the send and |
| // given back if it fails, so the next pass tries again. |
| func (s *Server) egressAlert(ctx context.Context, a egressAllowed, box string, now time.Time) { |
| - key := a.sqid + "/" + a.app + " " + a.addr.String() |
| + key := a.sqid + "/" + a.app + " " + a.key() |
| st := &s.egressReport |
| st.mu.Lock() |
| if at, ok := st.alerted[key]; ok && now.Sub(at) < egressAlertEvery { |
| @@ -445,7 +573,7 @@ func (s *Server) egressAlert(ctx context.Context, a egressAllowed, box string, n |
| if page == nil { |
| page = pageEgressConflict |
| } |
| - subject, body := egressConflictAlert(a.sqid, a.app, a.addr, box) |
| + subject, body := egressConflictAlert(a.sqid, a.app, a.entry, box) |
| sctx, cancel := context.WithTimeout(ctx, egressAlertTimeout) |
| defer cancel() |
| if err := page(sctx, alerting.LevelWarning, subject, body); err != nil { |
| @@ -458,8 +586,8 @@ func (s *Server) egressAlert(ctx context.Context, a egressAllowed, box string, n |
| } |
| } |
| -// egressConflicts is the last pass's findings, allowed address -> the box |
| -// that holds it, or nil before the first pass. The map is a copy. |
| +// egressConflicts is the last pass's findings, allowed entry -> the box |
| +// that holds it (or, for a range, an address in it), or nil before the first pass. The map is a copy. |
| func (s *Server) egressConflicts() map[string]string { |
| st := &s.egressReport |
| st.mu.Lock() |
diff --git a/internal/console/egressfleet_test.go b/internal/console/egressfleet_test.go| index 8ada88f18..4e44fa723 100644 |
| --- a/internal/console/egressfleet_test.go |
| +++ b/internal/console/egressfleet_test.go |
| @@ -136,11 +136,13 @@ func grantServer(t *testing.T) (*Server, *edgesCountingStore) { |
| return &Server{cfg: Config{Releases: cs}, policyLog: func(string, ...any) {}}, cs |
| } |
| -func addrs(t *testing.T, ss ...string) []netip.Addr { |
| +// addrs is single addresses as the full-length prefixes grantAdded gives. |
| +func addrs(t *testing.T, ss ...string) []netip.Prefix { |
| t.Helper() |
| - out := make([]netip.Addr, 0, len(ss)) |
| + out := make([]netip.Prefix, 0, len(ss)) |
| for _, s := range ss { |
| - out = append(out, netip.MustParseAddr(s)) |
| + a := netip.MustParseAddr(s) |
| + out = append(out, netip.PrefixFrom(a, a.BitLen())) |
| } |
| return out |
| } |
| @@ -975,3 +977,159 @@ func TestEgressReportKeepsAConflictItCannotRead(t *testing.T) { |
| fx.mustPut(t, "shop", `{"egress_allow":["10.0.5.21"]}`, policyIfMatch(first.ETag)) |
| pass("the allow narrowed while betty is unreadable", map[string]string{}) |
| } |
| + |
| +func prefixes(t *testing.T, ss ...string) []netip.Prefix { |
| + t.Helper() |
| + out := make([]netip.Prefix, 0, len(ss)) |
| + for _, s := range ss { |
| + if !strings.Contains(s, "/") { |
| + a := netip.MustParseAddr(s) |
| + out = append(out, netip.PrefixFrom(a, a.BitLen())) |
| + continue |
| + } |
| + out = append(out, netip.MustParsePrefix(s)) |
| + } |
| + return out |
| +} |
| + |
| +// setBoxRelease writes a fresh heartbeat for label naming release rel ("" for |
| +// a heartbeat that names none). |
| +func setBoxRelease(t *testing.T, s release.Store, label, rel string, at time.Time) { |
| + t.Helper() |
| + if err := status.WriteBox(context.Background(), s, status.Box{Label: label, GeneratedAt: at, Metrics: &status.BoxMetrics{Release: rel}}); err != nil { |
| + t.Fatalf("snapshot %s: %v", label, err) |
| + } |
| +} |
| + |
| +// A range that holds a published box address is refused (409), naming the |
| +// box and the address; a range that holds none goes through. Decision 8. |
| +func TestEgressGrantRefusesARangeHoldingAFleetAddress(t *testing.T) { |
| + t.Parallel() |
| + s, cs := grantServer(t) |
| + now := time.Now() |
| + putEdgesAddrs(t, cs, "annie", []string{"10.0.64.7", "fd00:5::/64"}) |
| + setBoxRelease(t, cs, "annie", "v0.43.0", now) |
| + for _, c := range []struct { |
| + add string |
| + want string |
| + }{ |
| + {"10.0.64.0/20", "10.0.64.0/20 holds 10.0.64.7, an address of box annie. an egress allow cannot open a box of this fleet"}, |
| + {"fd00:5::/48", "fd00:5::/48 holds fd00:5::/64, an address of box annie. an egress allow cannot open a box of this fleet"}, |
| + } { |
| + nc, ref := s.egressGrantCheck(context.Background(), prefixes(t, c.add)) |
| + if ref == nil || ref.Status != http.StatusConflict || ref.Msg != c.want { |
| + t.Errorf("grant %s = %v %+v, want 409 %q", c.add, nc, ref, c.want) |
| + } |
| + } |
| + nc, ref := s.egressGrantCheck(context.Background(), prefixes(t, "10.0.80.0/20", "fd00:6::/48")) |
| + if ref != nil || nc == nil || len(nc) != 0 { |
| + t.Fatalf("ranges no box address is in = %#v %+v, want [] and no refusal", nc, ref) |
| + } |
| +} |
| + |
| +// Through the door: a range is added only while every fresh box runs |
| +// EgressPrefixFloor or later (decision 9). A single address is never held |
| +// up by the floor, nor is a revoke of a range. |
| +func TestPolicyDoorRangeNeedsTheFloorOnEveryBox(t *testing.T) { |
| + t.Parallel() |
| + fx := newPolicyFx(t, "code-policy-range-floor") |
| + now := time.Now() |
| + putEdgesAddrs(t, fx.rel, "annie", []string{"10.0.1.5"}) |
| + setBoxRelease(t, fx.rel, "annie", "v0.43.0", now) |
| + putEdgesAddrs(t, fx.rel, "betty", []string{"10.0.1.6"}) |
| + setBoxRelease(t, fx.rel, "betty", "v0.42.1", now) |
| + putEdgesAddrs(t, fx.rel, "olive", []string{"10.0.1.7"}) |
| + setBoxRelease(t, fx.rel, "olive", "v0.30.0", now.Add(-24*time.Hour)) // stale: not counted |
| + |
| + st, _, out := fx.put(t, "shop", `{"egress_allow":["10.0.64.0/20"]}`, policyCreate) |
| + if want := "box betty runs v0.42.1. allowing a range needs v0.43.0 or later on every box. single addresses are unaffected"; st != http.StatusConflict || errorOf(t, out) != want { |
| + t.Fatalf("a range with betty on v0.42.1 = %d %s, want 409 %q", st, out, want) |
| + } |
| + fx.noObject(t, "shop") |
| + |
| + single := fx.mustPut(t, "shop", `{"egress_allow":["10.0.64.5"]}`, policyCreate) |
| + |
| + setBoxRelease(t, fx.rel, "betty", "v0.43.0", now) |
| + ranged := fx.mustPut(t, "shop", `{"egress_allow":["10.0.64.0/20","10.0.64.5"]}`, policyIfMatch(single.ETag)) |
| + if want := []string{"10.0.64.0/20", "10.0.64.5"}; !slices.Equal(ranged.Policy.EgressAllow, want) { |
| + t.Fatalf("allow = %v, want %v", ranged.Policy.EgressAllow, want) |
| + } |
| + |
| + setBoxRelease(t, fx.rel, "betty", "", now) // names no release: below |
| + if st, _, out := fx.put(t, "shop", `{"egress_allow":["10.0.64.0/20","fd00:5::/64"]}`, policyIfMatch(ranged.ETag)); st != http.StatusConflict || |
| + errorOf(t, out) != "box betty runs no release. allowing a range needs v0.43.0 or later on every box. single addresses are unaffected" { |
| + t.Fatalf("a second range with betty naming no release = %d %s, want 409", st, out) |
| + } |
| + fx.mustPut(t, "shop", `{"egress_allow":["10.0.64.0/20"]}`, policyIfMatch(ranged.ETag)) |
| +} |
| + |
| +// A heartbeat read that fails refuses a range with 503: the floor is not |
| +// passed on what the console could not see. |
| +func TestEgressGrantRangeFloorUnreadable(t *testing.T) { |
| + t.Parallel() |
| + s, cs := grantServer(t) |
| + putEdgesAddrs(t, cs, "annie", []string{"10.0.1.5"}) |
| + setBoxRelease(t, cs, "annie", "v0.43.0", time.Now()) |
| + cs.fail(true) |
| + _, ref := s.egressGrantCheck(context.Background(), prefixes(t, "10.0.64.0/20")) |
| + if want := "the fleet's list of boxes could not be read. try again in a minute"; ref == nil || |
| + ref.Status != http.StatusServiceUnavailable || ref.Msg != want { |
| + t.Fatalf("a range with heartbeats unreadable = %+v, want 503 %q", ref, want) |
| + } |
| +} |
| + |
| +// grantAdded works on prefixes: an entry already allowed is not added, and |
| +// a range and an address inside it are separate entries. |
| +func TestGrantAddedPrefixes(t *testing.T) { |
| + t.Parallel() |
| + got, err := grantAdded([]string{"10.0.64.0/20", "10.0.5.20"}, []string{"10.0.64.0/20", "10.0.64.5", "fd00:5::/64"}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if want := prefixes(t, "10.0.64.5", "fd00:5::/64"); !slices.Equal(got, want) { |
| + t.Fatalf("added = %v, want %v", got, want) |
| + } |
| +} |
| + |
| +// The report checks a range by containment: a box that later takes an |
| +// address inside an allowed range is found, alerted on, and named by GET |
| +// (which `apps show` marks with "!"). |
| +func TestEgressReportFlagsARangeHoldingABoxAddress(t *testing.T) { |
| + t.Parallel() |
| + fx := newReportFx(t, "code-egress-report-range") |
| + setBoxRelease(t, fx.cs, "annie", "v0.43.0", time.Now()) |
| + putEdgesAddrs(t, fx.cs, "annie", []string{"10.0.1.5"}) |
| + fx.mustPut(t, "shop", `{"egress_allow":["10.0.64.0/20","10.0.5.21"]}`, policyCreate) |
| + if !fx.tick(t) { |
| + t.Fatal("the first tick with a live allow ran no pass") |
| + } |
| + if got := fx.srv.egressConflicts(); len(got) != 0 { |
| + t.Fatalf("conflicts before a box takes an address = %v", got) |
| + } |
| + putEdgesAddrs(t, fx.cs, "betty", []string{"10.0.64.9"}) |
| + if n := fx.ticks(t, reportBackstopTo); n != 1 { |
| + t.Fatalf("the backstop ran %d passes, want 1", n) |
| + } |
| + if got := fx.srv.egressConflicts(); !maps.Equal(got, map[string]string{"10.0.64.0/20": "betty"}) { |
| + t.Fatalf("conflicts = %v, want 10.0.64.0/20 on betty", got) |
| + } |
| + want := "Egress allow 10.0.64.0/20 now holds a fleet box's address.\n" + |
| + "App shop in account id " + fx.sqid + " may reach 10.0.64.0/20, and box betty now holds an address in that range. " + |
| + "The allow is still in force. Set the whole list again without that range by running carlos apps set shop --egress-allow, or move the address off the box." |
| + if got := fx.sent(); len(got) != 1 || got[0] != want { |
| + t.Fatalf("pages = %q, want one: %q", got, want) |
| + } |
| + if g := fx.get(t, "shop"); !slices.Equal(g.EgressConflicts, []string{"10.0.64.0/20"}) { |
| + t.Fatalf("GET egress_conflicts = %v, want [10.0.64.0/20]", g.EgressConflicts) |
| + } |
| +} |
| + |
| +// carlos status --layout marks a range the report found, in the form the box |
| +// reports it (the entry's stored form). |
| +func TestEgressViewMarksARange(t *testing.T) { |
| + t.Parallel() |
| + e := egressView(status.HostEgress{Allowed: []string{"10.0.5.20", "10.0.64.0/20"}}, true, map[string]string{"10.0.64.0/20": "betty"}) |
| + if !slices.Equal(e.Conflicts, []string{"10.0.64.0/20"}) { |
| + t.Fatalf("conflicts = %v, want [10.0.64.0/20]", e.Conflicts) |
| + } |
| +} |
diff --git a/internal/release/layoutintent.go b/internal/release/layoutintent.go| index baa1d3f71..a358e3e4c 100644 |
| --- a/internal/release/layoutintent.go |
| +++ b/internal/release/layoutintent.go |
| @@ -246,6 +246,18 @@ var RootfsLifecycleFloor = SemVer{Major: 0, Minor: 29, Patch: 0} |
| // TestTeamEnvironmentFloorIsPinned pins it. |
| var TeamEnvironmentFloor = SemVer{Major: 0, Minor: 30, Patch: 0} |
| +// EgressPrefixFloor is the first platform release whose boxes accept an |
| +// egress allow entry that is a prefix (#1357). An older box reads such a |
| +// policy as unverifiable and clears the app's whole allow and size override |
| +// on that box (fail closed, but a regression of the app's other entries). |
| +// So the console adds a prefix entry only while every fresh box of the |
| +// deployment runs at or above it (console/egressfleet.go); single |
| +// addresses, and revokes, are never gated. The latest tag at the time of |
| +// writing is v0.42.1, so this names the next minor: if the release ships |
| +// under another number, this moves with it (a floor set too high only |
| +// refuses prefixes for longer). TestEgressPrefixFloorIsPinned pins it. |
| +var EgressPrefixFloor = SemVer{Major: 0, Minor: 43, Patch: 0} |
| + |
| // RegionalDomainsFloor is the first platform release that reads a regional |
| // custom-domain claim (#447). A box below it skips the claim entirely: a |
| // regional claim is stamped schema 2, and every older reader refuses a |
diff --git a/internal/release/layoutintent_test.go b/internal/release/layoutintent_test.go| index dd43e2df2..888e0dcb7 100644 |
| --- a/internal/release/layoutintent_test.go |
| +++ b/internal/release/layoutintent_test.go |
| @@ -322,3 +322,14 @@ func TestFleetForwardFloorAllows(t *testing.T) { |
| } |
| } |
| } |
| + |
| +func TestEgressPrefixFloorIsPinned(t *testing.T) { |
| + if EgressPrefixFloor != (SemVer{Major: 0, Minor: 43, Patch: 0}) { |
| + t.Fatalf("EgressPrefixFloor = %+v, want v0.43.0 (the first release whose boxes accept egress prefixes)", EgressPrefixFloor) |
| + } |
| + for v, below := range map[string]bool{"v0.42.1": true, "v0.43.0": false, "v0.43.2": false} { |
| + if got := BelowFloor(v, EgressPrefixFloor); got != below { |
| + t.Errorf("BelowFloor(%q, EgressPrefixFloor) = %v, want %v", v, got, below) |
| + } |
| + } |
| +} |