awsjob: Task 9 review 1, proxies' table, active routes, guard witnesses
Each proxy address's subnet in the services VPC must use the services route table (association or main-table fallback); lookup failures are errors. Required peering routes, both directions, must be active. Tests now witness each guard on its own (duplicate route, services table identity, peering ID and count, owner and region, VM block, both halves of the controller's manifest check).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+335
−30
cmd/equipo-hosted-controller/config_test.go+1 −0docs/superpowers/plans/HOSTED-SLICE2-PROGRESS.md+51 −0internal/isolatedjob/awsjob/network.go+92 −20internal/isolatedjob/awsjob/network_test.go+191 −10
diff --git a/cmd/equipo-hosted-controller/config_test.go b/cmd/equipo-hosted-controller/config_test.go| index 39056ba6..a34ad9df 100644 |
| --- a/cmd/equipo-hosted-controller/config_test.go |
| +++ b/cmd/equipo-hosted-controller/config_test.go |
| @@ -542,6 +542,7 @@ func TestConfigPeeredNetwork(t *testing.T) { |
| "no egress": func(f *fileConfig) { f.Egress = []string{} }, |
| "a bootstrap listener missing": func(f *fileConfig) { f.Egress = f.Egress[:3] }, |
| "egress to another port": func(f *fileConfig) { f.Egress[0] = "10.42.2.10:9000" }, |
| + "an extra port to a proxy": func(f *fileConfig) { f.Egress = append(f.Egress, "10.42.1.10:9000") }, |
| "egress listed twice": func(f *fileConfig) { f.Egress = append(f.Egress, f.Egress[0]) }, |
| "egress that is not host:port": func(f *fileConfig) { f.Egress[0] = "10.42.2.10" }, |
| "egress to an unrouted address": func(f *fileConfig) { f.Egress[0] = "10.42.3.10:8444" }, |
diff --git a/docs/superpowers/plans/HOSTED-SLICE2-PROGRESS.md b/docs/superpowers/plans/HOSTED-SLICE2-PROGRESS.md| index 5ceacff1..d5bcbf04 100644 |
| --- a/docs/superpowers/plans/HOSTED-SLICE2-PROGRESS.md |
| +++ b/docs/superpowers/plans/HOSTED-SLICE2-PROGRESS.md |
| @@ -430,3 +430,54 @@ Mutation checks (each applied by script, the named test run with `CGO_ENABLED=0 |
| 11. Controller egress allowed without a peering (`if false && len(fc.Egress) > 0`). TestConfigRefusesMissingListenOrKeys: FAIL, `config_test.go:390: egress in slice 1a: config loaded, want a refusal`. Restored. |
| Also run: `CGO_ENABLED=0 go test ./internal/isolatedjob/... ./cmd/equipo-hosted-controller -count=1` ok; `go vet -tags hostedlive ./internal/isolatedjob/...` ok; `make budget build vet` ok (129 directories within budget); staticcheck v0.7.0 clean on both packages; gofmt and `git diff --check` clean. |
| + |
| +### Task 9 review round 1 (t9-r1.md: three Important findings fixed) |
| + |
| +1. The services table is now proved to be the one the proxies use. For each proxy address, the audit lists the services VPC's subnets (`DescribeSubnets`, filter `vpc-id`, paged), finds the one subnet whose IPv4 block holds the address, and resolves that subnet's effective table: its association, or else the services VPC's main table. This uses `effectiveTable`, now shared with the VM subnet. A table other than `ServicesRouteTableID`, or none at all, fails `RouteOutAbsent`. These are errors, giving the zero audit: a proxy in no subnet or in two, a subnet with no block or outside the services VPC (filtered out, so the proxy is in none), two associated tables, or a failed read. |
| +2. Required peering routes must be active. Each proxy route and the return route must have `State == RouteStateActive`; `blackhole`, an empty state or an unknown one fails `RouteOutAbsent`. `peerRoute` in the good fixtures sets `active` explicitly. |
| +3. Tests sharpened so each guard has its own witness. `a proxy route twice` now appends a duplicate and keeps both routes and all egress. New cases: `an ipv6 /32 proxy route, with egress`, `a proxy route with a gateway too`, `a local route over a second peering`, `services table twice`, `another table answered`, and controller `an extra port to a proxy`. `VM subnet without an IPv4 block` now leaves the services subnets intact, so the missing block is the only fault. New tests: TestAuditRefusesInactiveRoute and TestAuditProxySubnetsUseServicesTable. |
| + |
| +RED: the new tests, run against the round-0 `network.go` (`git show 8aa09e2e:internal/isolatedjob/awsjob/network.go`, swapped in and then restored), FAIL. TestAuditRefusesInactiveRoute fails in all six subtests (`network_test.go:919`/`:924`: `RouteOutAbsent:true`, `the audit passes`). TestAuditProxySubnetsUseServicesTable fails in `associated with another table`, `falls back to another main`, `no table at all` (`network_test.go:951`: `the audit passes`) and in the lookup-error subtests. |
| + |
| +GREEN: `CGO_ENABLED=0 go test ./internal/isolatedjob/... -run 'TestAudit' -count=1` ok. |
| + |
| +Mutation checks. Each was applied by script and the named test run with `CGO_ENABLED=0 go test <pkg> -run '^<Test>$' -count=1`; the file was then restored and checked by SHA-256. Every one FAILED on its assertion and was restored. |
| + |
| +12. Proxy route state unchecked (network.go, `proxyRoutesOnly`). TestAuditRefusesInactiveRoute: `proxy_route_blackhole`, `proxy_route_` (empty) and `proxy_route_something-new`, `network_test.go:921: ... RouteOutAbsent:true ...`, `the audit passes`. |
| +13. Return route state unchecked (`returnRouteOnly`). TestAuditRefusesInactiveRoute: `return_route_blackhole`, `return_route_` and `return_route_something-new`, `network_test.go:926`. |
| +14. Proxy subnet's table identity unchecked (`if table == nil {`). TestAuditProxySubnetsUseServicesTable: `associated_with_another_table`, `falls_back_to_another_main`, `network_test.go:953: ... the audit passes`. |
| +15. Proxy subnet with no table accepted (`table != nil && ...`). Same test: `no_table_at_all`, `network_test.go:953`. |
| +16. Main-table fallback removed in `effectiveTable` (`if false {`). Same test: `main-table_fallback`, `network_test.go:942: proxy B on the main table, the services table: ({... RouteOutAbsent:false ...}, <nil>), want a pass`. |
| +17. Proxy subnet lookup failure taken as a failed property (`pinned = false; continue` in place of the error). Same test: `proxy_in_no_subnet`, `proxy_in_two_subnets`, `network_test.go:977: ... <nil>), want the zero audit and an error`. |
| +18. Services subnet's VPC unchecked. Same test: `subnet_in_another_VPC`, `network_test.go:977`. |
| +19. Services table VPC unchecked. TestAuditRefusesMissingReturnRoute: `services_table_in_another_VPC`, `network_test.go:844: ... want the zero audit and an error`. |
| +20. Services table ID unchecked. Same test: `another_table_answered`, `network_test.go:844`. |
| +21. Services table count unchecked (`len(services) == 0`). Same test: `services_table_twice`, `network_test.go:844`. |
| +22. Peering ID unchecked. TestAuditRefusesInactivePeering: `another_peering_answered`, `network_test.go:753: ..., nil; want an error`. |
| +23. Peering count unchecked (`== 0`). Same test: `two_peerings_answered`, `network_test.go:776`. |
| +24. Peering owner unchecked (region kept). Same test: `requester_in_another_account`, `accepter_in_another_account`, `owner_unknown`, `network_test.go:753`. |
| +25. Peering region unchecked (owner kept). Same test: `accepter_in_another_region`, `network_test.go:753`. |
| +26. VM subnet block unchecked (`if false {`). In the first run it SURVIVED: the test's subnet stub also emptied the services subnets, so a proxy-lookup error hid it. After the test was fixed (above): TestAuditRefusesMissingReturnRoute `VM_subnet_without_an_IPv4_block`, `network_test.go:862: ({... RouteOutAbsent:false ...}, <nil>), want the zero audit and an error`. |
| +27. Second target over the peering allowed (`otherTarget` dropped from `overPeering`). TestAuditRefusesOtherPeeringRoute: `a_proxy_route_with_a_second_target`, `network_test.go:686: ... the audit passes`. |
| +28. Gateway over the peering allowed. Same test: `a_proxy_route_with_a_gateway_too`, `network_test.go:691`. |
| +29. Return route counted at least once (`found >= 1`). TestAuditRefusesMissingReturnRoute: `the_return_route_twice`, `network_test.go:806`. |
| +30. A local route may name a peering (`rt.VpcPeeringConnectionId == nil` dropped from `local`). TestAuditRefusesOtherPeeringRoute: `a_local_route_over_a_second_peering`, `network_test.go:675`. |
| +31. Duplicate proxy route accepted (runner.go, `if false {`). TestAuditPeeringConfig: `network_test.go:908: a proxy route twice: New = <nil>, want ErrInvalid`. |
| +32. IPv6 proxy route accepted (`Is4` dropped). Same test: `an ipv6 /32 proxy route, with egress: New = <nil>, want ErrInvalid`. |
| +33. Routed address with no egress accepted. Same test: `a routed address with no egress`. |
| +34. Peering fields without a peering accepted. Same test: `a services table without a peering`, `routes without a peering`, `a services VPC without a peering`, `proxy routes without a peering`. |
| +35. Peering without its fields accepted. Same test: `no services VPC`, `no services route table`. |
| +36. Services VPC equal to the VM VPC accepted. Same test: `services VPC the VM VPC`. |
| +37. Controller duplicate egress accepted (config.go). TestConfigPeeredNetwork: `config_test.go:553: egress listed twice: config loaded, want a refusal`. |
| +38. Controller egress outside the manifest accepted (the first loop of `proxyEgress`). In the first run it SURVIVED: `egress to another port` also drops a listener, so the second loop refused it. After the new case: `config_test.go:554: an extra port to a proxy: config loaded, want a refusal`. |
| +39. Controller manifest listener missing from egress accepted (the second loop). TestConfigPeeredNetwork: `a bootstrap listener missing: config loaded, want a refusal`. |
| + |
| +Not independently mutable (recorded, not run as guards): |
| +- The nil requester/accepter check: removing it dereferences nil and panics. |
| +- The IPv6-destination, prefix-list and `Masked` conditions in `overPeering`: subsumed, because `ParsePrefix` of an absent `DestinationCidrBlock` fails, and a non-canonical prefix is never equal to a configured `/32` or the VM block. |
| +- The services subnet-list error: with no subnets, every proxy is in none, which is itself an error. |
| +- The controller's egress and proxy-route parse errors: subsumed by awsjob's checks of invalid values. |
| + |
| +Peering DNS options (`AllowDnsResolutionFromRemoteVpc`) are deliberately not checked. The egress boundary is the VM VPC's DNS being off (ResolverDisabled), routes only to the proxies' `/32`s, and a security group allowing only the proxy listeners. Route state, and the proxies' use of the services table, guard return-path availability instead. |
| + |
| +Also run: `CGO_ENABLED=0 go test ./internal/isolatedjob/... ./cmd/equipo-hosted-controller -count=1` ok; `go vet -tags hostedlive ./internal/isolatedjob/...` ok; `make budget build vet` ok (129 directories within budget); staticcheck v0.7.0 clean; gofmt and `git diff --check` clean. |
diff --git a/internal/isolatedjob/awsjob/network.go b/internal/isolatedjob/awsjob/network.go| index c755446b..bed8c1ec 100644 |
| --- a/internal/isolatedjob/awsjob/network.go |
| +++ b/internal/isolatedjob/awsjob/network.go |
| @@ -30,9 +30,12 @@ import ( |
| // network (Config.PeeringID set, decision S2-4) the table may also hold, |
| // for each of Config.ProxyRoutes, exactly one route to it over the |
| // pinned peering, and must hold every one of them; the peering must be |
| -// active; and the services route table must route the VM subnet's IPv4 |
| -// block back over the same peering and nothing else over any peering. |
| -// Anything else, a missing route included, fails this property. |
| +// active; the services route table must route the VM subnet's IPv4 |
| +// block back over the same peering and nothing else over any peering; |
| +// each of these required routes must be active (not blackhole); and the |
| +// subnet of the services VPC holding each proxy address must use the |
| +// services route table (associated, or as the main table). Anything |
| +// else, a missing route included, fails this property. |
| // - EgressOnly: every host and port the security group's outbound rules |
| // allow. It equals Config.Egress when the group is right. |
| // |
| @@ -43,7 +46,8 @@ import ( |
| // network it also does so when the peering is not the one configured |
| // between the VM VPC and the services VPC with both sides in this account |
| // (the connector's) and region, the services route table is not one table |
| -// in the services VPC, or the VM subnet has no IPv4 block: what it cannot |
| +// in the services VPC, a proxy address is not in exactly one subnet of the |
| +// services VPC, or the VM subnet has no IPv4 block: what it cannot |
| // establish is never assumed. |
| func (r *Runner) AuditNetwork(ctx context.Context) (isolatedjob.NetworkAudit, error) { |
| a, err := r.audit(ctx) |
| @@ -165,29 +169,92 @@ func (r *Runner) subnetRouteTable(ctx context.Context) (ec2types.Subnet, *ec2typ |
| if len(subnets.Subnets) != 1 || aws.ToString(subnets.Subnets[0].VpcId) != r.cfg.VPCID { |
| return ec2types.Subnet{}, nil, fmt.Errorf("subnet %s is not one subnet in %s", r.cfg.VMSubnetID, r.cfg.VPCID) |
| } |
| - subnet := subnets.Subnets[0] |
| - tables, err := r.routeTables(ctx, filter("association.subnet-id", r.cfg.VMSubnetID)) |
| + table, err := r.effectiveTable(ctx, r.cfg.VPCID, r.cfg.VMSubnetID) |
| + return subnets.Subnets[0], table, err |
| +} |
| + |
| +// effectiveTable returns the route table subnet uses: the one associated |
| +// with it, or else vpc's main table. nil means none was found. |
| +func (r *Runner) effectiveTable(ctx context.Context, vpc, subnet string) (*ec2types.RouteTable, error) { |
| + tables, err := r.routeTables(ctx, filter("association.subnet-id", subnet)) |
| if err != nil { |
| - return subnet, nil, err |
| + return nil, err |
| } |
| if len(tables) == 0 { |
| - if tables, err = r.routeTables(ctx, filter("vpc-id", r.cfg.VPCID), filter("association.main", "true")); err != nil { |
| - return subnet, nil, err |
| + if tables, err = r.routeTables(ctx, filter("vpc-id", vpc), filter("association.main", "true")); err != nil { |
| + return nil, err |
| } |
| } |
| switch len(tables) { |
| case 0: |
| - return subnet, nil, nil |
| + return nil, nil |
| case 1: |
| - return subnet, &tables[0], nil |
| + return &tables[0], nil |
| + } |
| + return nil, fmt.Errorf("subnet %s matches %d route tables", subnet, len(tables)) |
| +} |
| + |
| +// proxyTablesPinned reports whether the subnet in the services VPC that |
| +// holds each proxy address uses the services route table, by association |
| +// or as the main table. It is an error when a proxy address is not in |
| +// exactly one subnet of the services VPC, or a read fails. |
| +func (r *Runner) proxyTablesPinned(ctx context.Context) (bool, error) { |
| + subnets, err := r.subnetsIn(ctx, r.cfg.ServicesVPCID) |
| + if err != nil { |
| + return false, err |
| + } |
| + pinned := true |
| + for _, p := range r.cfg.ProxyRoutes { |
| + var home []string |
| + for _, sn := range subnets { |
| + block, err := netip.ParsePrefix(aws.ToString(sn.CidrBlock)) |
| + if err == nil && aws.ToString(sn.VpcId) == r.cfg.ServicesVPCID && block.Contains(p.Addr()) { |
| + home = append(home, aws.ToString(sn.SubnetId)) |
| + } |
| + } |
| + if len(home) != 1 || home[0] == "" { |
| + return false, fmt.Errorf("proxy %s is in subnets %q of %s, want one", p.Addr(), home, r.cfg.ServicesVPCID) |
| + } |
| + table, err := r.effectiveTable(ctx, r.cfg.ServicesVPCID, home[0]) |
| + if err != nil { |
| + return false, err |
| + } |
| + if table == nil || aws.ToString(table.RouteTableId) != r.cfg.ServicesRouteTableID { |
| + pinned = false |
| + } |
| + } |
| + return pinned, nil |
| +} |
| + |
| +// subnetsIn lists every subnet of vpc. |
| +func (r *Runner) subnetsIn(ctx context.Context, vpc string) ([]ec2types.Subnet, error) { |
| + var subnets []ec2types.Subnet |
| + var token *string |
| + seen := map[string]bool{} |
| + for { |
| + out, err := r.api.DescribeSubnets(ctx, &ec2.DescribeSubnetsInput{Filters: []ec2types.Filter{filter("vpc-id", vpc)}, NextToken: token}) |
| + if err != nil { |
| + return nil, fmt.Errorf("describe subnets of %s: %w", vpc, err) |
| + } |
| + subnets = append(subnets, out.Subnets...) |
| + next := aws.ToString(out.NextToken) |
| + if next == "" { |
| + return subnets, nil |
| + } |
| + if seen[next] { |
| + return nil, fmt.Errorf("describe subnets repeats a page token") |
| + } |
| + seen[next] = true |
| + token = aws.String(next) |
| } |
| - return subnet, nil, fmt.Errorf("subnet %s matches %d route tables", r.cfg.VMSubnetID, len(tables)) |
| } |
| // peeredRoutes reports whether the peered network holds (see AuditNetwork, |
| // RouteOutAbsent): the VM table routes only locally and to each proxy over |
| -// the pinned peering, the peering is active, and the services table's only |
| -// route over a peering is the one back to the VM subnet over this one. |
| +// the pinned peering, the peering is active, the services table's only |
| +// route over a peering is the one back to the VM subnet over this one, |
| +// each of those required routes is active, and the proxies' subnets use |
| +// the services table. |
| func (r *Runner) peeredRoutes(ctx context.Context, subnet ec2types.Subnet, table *ec2types.RouteTable) (bool, error) { |
| vm, err := netip.ParsePrefix(aws.ToString(subnet.CidrBlock)) |
| if err != nil || !vm.Addr().Is4() { |
| @@ -205,7 +272,11 @@ func (r *Runner) peeredRoutes(ctx context.Context, subnet ec2types.Subnet, table |
| aws.ToString(services[0].VpcId) != r.cfg.ServicesVPCID { |
| return false, fmt.Errorf("route table %s is not one table in %s", r.cfg.ServicesRouteTableID, r.cfg.ServicesVPCID) |
| } |
| - return active && table != nil && proxyRoutesOnly(table.Routes, r.cfg.PeeringID, r.cfg.ProxyRoutes) && |
| + used, err := r.proxyTablesPinned(ctx) |
| + if err != nil { |
| + return false, err |
| + } |
| + return active && used && table != nil && proxyRoutesOnly(table.Routes, r.cfg.PeeringID, r.cfg.ProxyRoutes) && |
| returnRouteOnly(services[0].Routes, r.cfg.PeeringID, vm), nil |
| } |
| @@ -304,8 +375,8 @@ func overPeering(rt ec2types.Route, peering string) (dest netip.Prefix, ok bool) |
| } |
| // proxyRoutesOnly reports whether routes are local routes and, for each |
| -// proxy prefix, exactly one route to it over peering: no other route, and |
| -// none missing. |
| +// proxy prefix, exactly one active route to it over peering: no other |
| +// route, and none missing or in another state (blackhole, or one unknown). |
| func proxyRoutesOnly(routes []ec2types.Route, peering string, proxies []netip.Prefix) bool { |
| seen := map[netip.Prefix]int{} |
| for _, rt := range routes { |
| @@ -313,7 +384,7 @@ func proxyRoutesOnly(routes []ec2types.Route, peering string, proxies []netip.Pr |
| continue |
| } |
| dest, ok := overPeering(rt, peering) |
| - if !ok || !slices.Contains(proxies, dest) { |
| + if !ok || !slices.Contains(proxies, dest) || rt.State != ec2types.RouteStateActive { |
| return false |
| } |
| seen[dest]++ |
| @@ -327,7 +398,8 @@ func proxyRoutesOnly(routes []ec2types.Route, peering string, proxies []netip.Pr |
| } |
| // returnRouteOnly reports whether the services table's routes over any |
| -// peering are exactly one: to the VM subnet's block vm over peering. |
| +// peering are exactly one, active: to the VM subnet's block vm over |
| +// peering. |
| // Routes with no peering (local, the internet gateway) are the services |
| // VPC's own and not judged here. |
| func returnRouteOnly(routes []ec2types.Route, peering string, vm netip.Prefix) bool { |
| @@ -337,7 +409,7 @@ func returnRouteOnly(routes []ec2types.Route, peering string, vm netip.Prefix) b |
| continue |
| } |
| dest, ok := overPeering(rt, peering) |
| - if !ok || dest != vm { |
| + if !ok || dest != vm || rt.State != ec2types.RouteStateActive { |
| return false |
| } |
| found++ |
diff --git a/internal/isolatedjob/awsjob/network_test.go b/internal/isolatedjob/awsjob/network_test.go| index 529d636f..77a7e680 100644 |
| --- a/internal/isolatedjob/awsjob/network_test.go |
| +++ b/internal/isolatedjob/awsjob/network_test.go |
| @@ -484,15 +484,26 @@ func peeredConfig() Config { |
| } |
| func peerRoute(dest, peering string) ec2types.Route { |
| - return ec2types.Route{DestinationCidrBlock: aws.String(dest), VpcPeeringConnectionId: aws.String(peering)} |
| + return ec2types.Route{DestinationCidrBlock: aws.String(dest), VpcPeeringConnectionId: aws.String(peering), |
| + State: ec2types.RouteStateActive} |
| } |
| // peeredNet is the peered network as peeredConfig expects it; a test |
| // changes a field before building its stub. |
| type peeredNet struct { |
| - vmRoutes []ec2types.Route // the VM subnet's table |
| - svcRoutes []ec2types.Route // the services route table |
| - peering ec2types.VpcPeeringConnection |
| + vmRoutes []ec2types.Route // the VM subnet's table |
| + svcRoutes []ec2types.Route // the services route table |
| + peering ec2types.VpcPeeringConnection |
| + svcSubnets []ec2types.Subnet // the services VPC's subnets |
| + // assoc is each services subnet's associated table; a table other than |
| + // rtb-svc answers with svcRoutes too, so only its ID differs. mainSvc |
| + // is the services VPC's main table, "" for none. |
| + assoc map[string]string |
| + mainSvc string |
| +} |
| + |
| +func svcSubnet(id, cidr string) ec2types.Subnet { |
| + return ec2types.Subnet{SubnetId: aws.String(id), VpcId: aws.String("vpc-svc"), CidrBlock: aws.String(cidr)} |
| } |
| func goodPeeredNet() *peeredNet { |
| @@ -513,23 +524,42 @@ func goodPeeredNet() *peeredNet { |
| RequesterVpcInfo: &ec2types.VpcPeeringConnectionVpcInfo{VpcId: aws.String("vpc-1"), OwnerId: aws.String("123456789012"), Region: aws.String("eu-west-1")}, |
| AccepterVpcInfo: &ec2types.VpcPeeringConnectionVpcInfo{VpcId: aws.String("vpc-svc"), OwnerId: aws.String("123456789012"), Region: aws.String("eu-west-1")}, |
| }, |
| + svcSubnets: []ec2types.Subnet{svcSubnet("subnet-a", "10.42.1.0/24"), svcSubnet("subnet-b", "10.42.2.0/24"), |
| + svcSubnet("subnet-c", "10.42.3.0/24")}, |
| + assoc: map[string]string{"subnet-a": "rtb-svc", "subnet-b": "rtb-svc", "subnet-c": "rtb-other"}, |
| } |
| } |
| +// svcTable is the table id in the services VPC, with n's services routes. |
| +func (n *peeredNet) svcTable(id string) *ec2.DescribeRouteTablesOutput { |
| + return &ec2.DescribeRouteTablesOutput{RouteTables: []ec2types.RouteTable{{RouteTableId: aws.String(id), |
| + VpcId: aws.String("vpc-svc"), Routes: n.svcRoutes}}} |
| +} |
| + |
| func (n *peeredNet) stub() *stub { |
| return &stub{ |
| - subnets: func(*ec2.DescribeSubnetsInput) (*ec2.DescribeSubnetsOutput, error) { |
| + subnets: func(in *ec2.DescribeSubnetsInput) (*ec2.DescribeSubnetsOutput, error) { |
| + if filterValue(in.Filters, "vpc-id") == "vpc-svc" { |
| + return &ec2.DescribeSubnetsOutput{Subnets: n.svcSubnets}, nil |
| + } |
| + if !reflect.DeepEqual(in.SubnetIds, []string{"subnet-1"}) { |
| + return &ec2.DescribeSubnetsOutput{}, nil |
| + } |
| return &ec2.DescribeSubnetsOutput{Subnets: []ec2types.Subnet{{SubnetId: aws.String("subnet-1"), VpcId: aws.String("vpc-1"), |
| CidrBlock: aws.String("10.0.0.0/24")}}}, nil |
| }, |
| routeTables: func(in *ec2.DescribeRouteTablesInput) (*ec2.DescribeRouteTablesOutput, error) { |
| + subnet := filterValue(in.Filters, "association.subnet-id") |
| switch { |
| - case filterValue(in.Filters, "association.subnet-id") == "subnet-1": |
| + case subnet == "subnet-1": |
| return &ec2.DescribeRouteTablesOutput{RouteTables: []ec2types.RouteTable{{RouteTableId: aws.String("rtb-1"), |
| VpcId: aws.String("vpc-1"), Routes: n.vmRoutes}}}, nil |
| + case subnet != "" && n.assoc[subnet] != "": |
| + return n.svcTable(n.assoc[subnet]), nil |
| case filterValue(in.Filters, "route-table-id") == "rtb-svc": |
| - return &ec2.DescribeRouteTablesOutput{RouteTables: []ec2types.RouteTable{{RouteTableId: aws.String("rtb-svc"), |
| - VpcId: aws.String("vpc-svc"), Routes: n.svcRoutes}}}, nil |
| + return n.svcTable("rtb-svc"), nil |
| + case filterValue(in.Filters, "association.main") == "true" && filterValue(in.Filters, "vpc-id") == "vpc-svc" && n.mainSvc != "": |
| + return n.svcTable(n.mainSvc), nil |
| } |
| return &ec2.DescribeRouteTablesOutput{}, nil |
| }, |
| @@ -636,6 +666,8 @@ func TestAuditRefusesOtherPeeringRoute(t *testing.T) { |
| "a /32 over the peering with a second target": {DestinationCidrBlock: aws.String("10.42.3.10/32"), |
| VpcPeeringConnectionId: aws.String("pcx-1"), TransitGatewayId: aws.String("tgw-1")}, |
| "an internet gateway": {DestinationCidrBlock: aws.String("0.0.0.0/0"), GatewayId: aws.String("igw-1")}, |
| + "a local route over a second peering": {DestinationCidrBlock: aws.String("172.16.0.0/12"), GatewayId: aws.String("local"), |
| + VpcPeeringConnectionId: aws.String("pcx-2")}, |
| } { |
| t.Run(name, func(t *testing.T) { |
| n := goodPeeredNet() |
| @@ -653,6 +685,11 @@ func TestAuditRefusesOtherPeeringRoute(t *testing.T) { |
| n.vmRoutes[1].NatGatewayId = aws.String("nat-1") |
| wantRouteOut(t, n) |
| }) |
| + t.Run("a proxy route with a gateway too", func(t *testing.T) { |
| + n := goodPeeredNet() |
| + n.vmRoutes[1].GatewayId = aws.String("igw-1") |
| + wantRouteOut(t, n) |
| + }) |
| t.Run("a proxy route over another peering", func(t *testing.T) { |
| n := goodPeeredNet() |
| n.vmRoutes[1] = peerRoute("10.42.1.10/32", "pcx-2") |
| @@ -782,6 +819,13 @@ func TestAuditRefusesMissingReturnRoute(t *testing.T) { |
| "services table read fails": func(*ec2.DescribeRouteTablesInput) (*ec2.DescribeRouteTablesOutput, error) { |
| return nil, errors.New("denied") |
| }, |
| + "services table twice": func(*ec2.DescribeRouteTablesInput) (*ec2.DescribeRouteTablesOutput, error) { |
| + t := goodPeeredNet().svcTable("rtb-svc").RouteTables[0] |
| + return &ec2.DescribeRouteTablesOutput{RouteTables: []ec2types.RouteTable{t, t}}, nil |
| + }, |
| + "another table answered": func(*ec2.DescribeRouteTablesInput) (*ec2.DescribeRouteTablesOutput, error) { |
| + return goodPeeredNet().svcTable("rtb-other"), nil |
| + }, |
| } { |
| t.Run(name, func(t *testing.T) { |
| s := goodPeeredNet().stub() |
| @@ -803,7 +847,11 @@ func TestAuditRefusesMissingReturnRoute(t *testing.T) { |
| } |
| t.Run("VM subnet without an IPv4 block", func(t *testing.T) { |
| s := goodPeeredNet().stub() |
| - s.subnets = func(*ec2.DescribeSubnetsInput) (*ec2.DescribeSubnetsOutput, error) { |
| + services := s.subnets |
| + s.subnets = func(in *ec2.DescribeSubnetsInput) (*ec2.DescribeSubnetsOutput, error) { |
| + if filterValue(in.Filters, "vpc-id") != "" { |
| + return services(in) |
| + } |
| return &ec2.DescribeSubnetsOutput{Subnets: []ec2types.Subnet{{SubnetId: aws.String("subnet-1"), VpcId: aws.String("vpc-1")}}}, nil |
| } |
| r, err := New(peeredConfig(), s) |
| @@ -833,7 +881,11 @@ func TestAuditPeeringConfig(t *testing.T) { |
| "an ipv6 proxy route": func(c *Config) { c.ProxyRoutes[0] = netip.MustParsePrefix("2001:db8::1/128") }, |
| "a mapped proxy route": func(c *Config) { c.ProxyRoutes[0] = netip.MustParsePrefix("::ffff:10.42.1.10/128") }, |
| "an invalid proxy route": func(c *Config) { c.ProxyRoutes[0] = netip.Prefix{} }, |
| - "a proxy route twice": func(c *Config) { c.ProxyRoutes[1] = c.ProxyRoutes[0] }, |
| + "a proxy route twice": func(c *Config) { c.ProxyRoutes = append(c.ProxyRoutes, c.ProxyRoutes[0]) }, |
| + "an ipv6 /32 proxy route, with egress": func(c *Config) { |
| + c.ProxyRoutes = append(c.ProxyRoutes, netip.MustParsePrefix("2001:db8::/32")) |
| + c.Egress = append(c.Egress, netip.MustParseAddrPort("[2001:db8::]:8443")) |
| + }, |
| "egress to an unrouted address": func(c *Config) { |
| c.Egress = append(c.Egress, netip.MustParseAddrPort("10.42.3.10:8443")) |
| }, |
| @@ -861,3 +913,132 @@ func TestAuditPeeringConfig(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// TestAuditRefusesInactiveRoute: every required route over the peering, to |
| +// a proxy or back to the VM subnet, must be active; a blackhole route, or |
| +// one in a state the audit does not know, fails. |
| +func TestAuditRefusesInactiveRoute(t *testing.T) { |
| + for _, state := range []ec2types.RouteState{ec2types.RouteStateBlackhole, "", "something-new"} { |
| + t.Run("proxy route "+string(state), func(t *testing.T) { |
| + n := goodPeeredNet() |
| + n.vmRoutes[2].State = state |
| + wantRouteOut(t, n) |
| + }) |
| + t.Run("return route "+string(state), func(t *testing.T) { |
| + n := goodPeeredNet() |
| + n.svcRoutes[2].State = state |
| + wantRouteOut(t, n) |
| + }) |
| + } |
| +} |
| + |
| +// TestAuditProxySubnetsUseServicesTable: the subnet of the services VPC |
| +// that holds each proxy address must use the services route table, by |
| +// association or as the main table; otherwise the audited return route is |
| +// not the one the proxies' replies take. |
| +func TestAuditProxySubnetsUseServicesTable(t *testing.T) { |
| + t.Run("main-table fallback", func(t *testing.T) { |
| + n := goodPeeredNet() |
| + delete(n.assoc, "subnet-b") |
| + n.mainSvc = "rtb-svc" |
| + got, err := auditPeered(t, n) |
| + if err != nil || got.OK(peeredConfig().Egress) != nil { |
| + t.Errorf("proxy B on the main table, the services table: (%+v, %v), want a pass", got, err) |
| + } |
| + }) |
| + for name, change := range map[string]func(*peeredNet){ |
| + "associated with another table": func(n *peeredNet) { n.assoc["subnet-b"] = "rtb-other" }, |
| + "falls back to another main": func(n *peeredNet) { delete(n.assoc, "subnet-a"); n.mainSvc = "rtb-main" }, |
| + "no table at all": func(n *peeredNet) { delete(n.assoc, "subnet-b") }, |
| + } { |
| + t.Run(name, func(t *testing.T) { |
| + n := goodPeeredNet() |
| + change(n) |
| + wantRouteOut(t, n) |
| + }) |
| + } |
| + for name, change := range map[string]func(*peeredNet){ |
| + "proxy in no subnet": func(n *peeredNet) { n.svcSubnets = n.svcSubnets[:1] }, |
| + "proxy in two subnets": func(n *peeredNet) { |
| + n.svcSubnets = append(n.svcSubnets, svcSubnet("subnet-d", "10.42.2.0/25")) |
| + }, |
| + "subnet with no block": func(n *peeredNet) { n.svcSubnets[1].CidrBlock = nil }, |
| + "subnet in another VPC": func(n *peeredNet) { n.svcSubnets[1].VpcId = aws.String("vpc-1") }, |
| + "two tables associated": func(n *peeredNet) { n.assoc["subnet-b"] = "two" }, |
| + } { |
| + t.Run(name, func(t *testing.T) { |
| + n := goodPeeredNet() |
| + change(n) |
| + s := n.stub() |
| + tables := s.routeTables |
| + s.routeTables = func(in *ec2.DescribeRouteTablesInput) (*ec2.DescribeRouteTablesOutput, error) { |
| + if filterValue(in.Filters, "association.subnet-id") == "subnet-b" && n.assoc["subnet-b"] == "two" { |
| + two := n.svcTable("rtb-svc").RouteTables[0] |
| + return &ec2.DescribeRouteTablesOutput{RouteTables: []ec2types.RouteTable{two, two}}, nil |
| + } |
| + return tables(in) |
| + } |
| + wantErrorFrom(t, s) |
| + }) |
| + } |
| + for name, fail := range map[string]func(*stub){ |
| + "subnet list fails": func(s *stub) { |
| + subnets := s.subnets |
| + s.subnets = func(in *ec2.DescribeSubnetsInput) (*ec2.DescribeSubnetsOutput, error) { |
| + if filterValue(in.Filters, "vpc-id") == "vpc-svc" { |
| + return nil, errors.New("denied") |
| + } |
| + return subnets(in) |
| + } |
| + }, |
| + "association read fails": func(s *stub) { |
| + tables := s.routeTables |
| + s.routeTables = func(in *ec2.DescribeRouteTablesInput) (*ec2.DescribeRouteTablesOutput, error) { |
| + if filterValue(in.Filters, "association.subnet-id") == "subnet-b" { |
| + return nil, errors.New("denied") |
| + } |
| + return tables(in) |
| + } |
| + }, |
| + } { |
| + t.Run(name, func(t *testing.T) { |
| + s := goodPeeredNet().stub() |
| + fail(s) |
| + wantErrorFrom(t, s) |
| + }) |
| + } |
| + t.Run("subnets over two pages", func(t *testing.T) { |
| + n := goodPeeredNet() |
| + s := n.stub() |
| + vmSubnets := s.subnets |
| + s.subnets = func(in *ec2.DescribeSubnetsInput) (*ec2.DescribeSubnetsOutput, error) { |
| + if filterValue(in.Filters, "vpc-id") != "vpc-svc" { |
| + return vmSubnets(in) |
| + } |
| + if in.NextToken == nil { |
| + return &ec2.DescribeSubnetsOutput{Subnets: n.svcSubnets[:1], NextToken: aws.String("p2")}, nil |
| + } |
| + return &ec2.DescribeSubnetsOutput{Subnets: n.svcSubnets[1:]}, nil |
| + } |
| + r, err := New(peeredConfig(), s) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if got, err := r.AuditNetwork(context.Background()); err != nil || got.OK(peeredConfig().Egress) != nil { |
| + t.Errorf("AuditNetwork = (%+v, %v), want a pass", got, err) |
| + } |
| + }) |
| +} |
| + |
| +// wantErrorFrom checks that the audit through s, under peeredConfig, fails |
| +// to establish the network: an error and the zero audit. |
| +func wantErrorFrom(t *testing.T, s *stub) { |
| + t.Helper() |
| + r, err := New(peeredConfig(), s) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if got, err := r.AuditNetwork(context.Background()); err == nil || !reflect.DeepEqual(got, isolatedjob.NetworkAudit{}) { |
| + t.Errorf("AuditNetwork = (%+v, %v), want the zero audit and an error", got, err) |
| + } |
| +} |