diff --git a/api/agent.yaml b/api/agent.yaml index 65c2603..1f0b7bb 100644 --- a/api/agent.yaml +++ b/api/agent.yaml @@ -234,7 +234,8 @@ paths: "400": description: > Missing required field; interface_ip missing on a bridge or public_ip subnet, or - supplied on a vxlan subnet (it is computed) + supplied on a vxlan subnet (it is computed); default_route true without gateway on a + vxlan or public_ip subnet content: application/json: schema: @@ -408,8 +409,9 @@ components: description: > A default route is always advertised via DHCP; this flag only chooses its next-hop. When false, the next-hop is the subnet interface_ip. When true, it is the `gateway` - field if supplied, otherwise the gateway read from the host routing table. - The route to the VPC CIDR always keeps interface_ip as its next-hop (except in bridge + field if supplied, otherwise the gateway read from the host routing table — this last + case is bridge mode only (400 in vxlan and public_ip modes, whose guests do not share + the L2 segment of the host gateway). The route to the VPC CIDR always keeps interface_ip as its next-hop (except in bridge mode, which has no VPC route), so VPC traffic never leaves through a public gateway. default: false gateway: diff --git a/internal/api/agent/subnet_test.go b/internal/api/agent/subnet_test.go index 705a293..b6b84a9 100644 --- a/internal/api/agent/subnet_test.go +++ b/internal/api/agent/subnet_test.go @@ -499,3 +499,42 @@ func TestGetSubnet_RelayIPOnlyForVxlan(t *testing.T) { t.Errorf("relay_ip inattendus dans la liste : %v", relays) } } + +func TestPostSubnet_DefaultRouteWithoutGatewayOnlyForBridge(t *testing.T) { + for _, req := range []SubnetCreateRequest{ + {Mode: "", CIDR: "10.0.0.0/24"}, + {Mode: "vxlan", VxlanID: 1101, CIDR: "10.0.0.0/24"}, + {Mode: "public_ip", InterfaceIP: "203.0.113.254", CIDR: "203.0.113.0/24"}, + } { + s, db := newTestServer(t) + kv.AddInDB(db, "vpc/vpc-1/state", "running") + req.Name, req.VPC, req.DefaultRoute = "sn-1", "vpc-1", true + w := postSubnetRequest(t, s, req) + if w.Code != http.StatusBadRequest { + t.Errorf("mode %q : attendu 400, obtenu %d : %s", req.Mode, w.Code, w.Body.String()) + } + if !strings.Contains(w.Body.String(), "default_route without gateway is only allowed for bridge subnets") { + t.Errorf("mode %q : message %s", req.Mode, w.Body.String()) + } + if _, err := kv.GetFromDB(db, "subnet/sn-1/state"); err == nil { + t.Errorf("mode %q : aucun subnet ne doit être créé", req.Mode) + } + } +} + +func TestPostSubnet_DefaultRouteAccepted(t *testing.T) { + for _, req := range []SubnetCreateRequest{ + {Mode: "bridge", InterfaceIP: "10.0.0.1", CIDR: "10.0.0.0/24"}, + {Mode: "bridge", InterfaceIP: "10.0.0.1", CIDR: "10.0.0.0/24", Gateway: "10.0.0.254"}, + {Mode: "", CIDR: "10.0.0.0/24", Gateway: "10.0.0.1"}, + {Mode: "vxlan", VxlanID: 1101, CIDR: "10.0.0.0/24", Gateway: "10.0.0.1"}, + {Mode: "public_ip", InterfaceIP: "203.0.113.254", CIDR: "203.0.113.0/24", Gateway: "203.0.113.1"}, + } { + s, db := newTestServer(t) + kv.AddInDB(db, "vpc/vpc-1/state", "running") + req.Name, req.VPC, req.DefaultRoute = "sn-1", "vpc-1", true + if w := postSubnetRequest(t, s, req); w.Code != http.StatusAccepted { + t.Errorf("mode %q gateway %q : attendu 202, obtenu %d : %s", req.Mode, req.Gateway, w.Code, w.Body.String()) + } + } +} diff --git a/internal/api/agent/subnets.go b/internal/api/agent/subnets.go index 5390d57..64f3ac7 100644 --- a/internal/api/agent/subnets.go +++ b/internal/api/agent/subnets.go @@ -78,6 +78,11 @@ func (s *Server) postSubnet(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(ErrorResponse{Error: "interface_ip is computed for vxlan subnets, do not set it"}) return } + if req.DefaultRoute && req.Gateway == "" && req.Mode != subnet.ModeBridge { + w.WriteHeader(http.StatusBadRequest) + json.NewEncoder(w).Encode(ErrorResponse{Error: "default_route without gateway is only allowed for bridge subnets: the host gateway is not on the subnet"}) + return + } cmd := dispatcher.CreateSubnetCommand{ Name: req.Name, VPC: req.VPC,