diff --git a/microcloud/cmd/microcloud/main_init.go b/microcloud/cmd/microcloud/main_init.go index fe1c23395..3740fedad 100644 --- a/microcloud/cmd/microcloud/main_init.go +++ b/microcloud/cmd/microcloud/main_init.go @@ -141,6 +141,11 @@ func (c *cmdInit) RunInteractive(cmd *cobra.Command, args []string) error { return err } + err = validateSystems(s, systems) + if err != nil { + return err + } + err = setupCluster(s, systems) if err != nil { return err @@ -414,6 +419,68 @@ func AddPeers(sh *service.Handler, systems map[string]InitSystem) error { return nil } +func validateSystems(s *service.Handler, systems map[string]InitSystem) (err error) { + curSystem, bootstrap := systems[s.Name] + if !bootstrap { + return nil + } + + // Assume that the UPLINK network on each system is the same, so grab just + // the gateways from the current node's UPLINK to verify against the other + // systems' management net addrs + hasIP4Gateway, hasIP6Gateway := false, false + var ip4Gateway, ip6Gateway string + var ip4GatewayNet, ip6GatewayNet *net.IPNet + + for _, network := range curSystem.Networks { + if network.Type == "physical" && network.Name == "UPLINK" { + ip4Gateway, hasIP4Gateway = network.Config["ipv4.gateway"] + ip6Gateway, hasIP6Gateway = network.Config["ipv6.gateway"] + ovnRanges, hasOVNRanges := network.Config["ipv4.ovn.ranges"] + + if hasIP4Gateway { + _, ip4GatewayNet, err = net.ParseCIDR(ip4Gateway) + if err != nil { + return fmt.Errorf("Invalid ipv4.gateway %q: %w", ip4Gateway, err) + } + } + + if hasIP6Gateway { + _, ip6GatewayNet, err = net.ParseCIDR(ip6Gateway) + if err != nil { + return fmt.Errorf("Invalid ipv6.gateway %q: %w", ip6Gateway, err) + } + } + + if hasIP4Gateway && hasOVNRanges { + _, err = shared.ParseIPRanges(ovnRanges, ip4GatewayNet) + if err != nil { + return fmt.Errorf("Invalid ipv4.ovn.ranges %q: %w", ovnRanges, err) + } + } + + break + } + } + + for systemName, system := range systems { + systemAddr := net.ParseIP(system.ServerInfo.Address) + if systemAddr == nil { + return fmt.Errorf("Invalid address %q for system %q", system.ServerInfo.Address, systemName) + } + + if hasIP4Gateway && ip4GatewayNet.Contains(systemAddr) { + return fmt.Errorf("UPLINK ipv4.gateway must not include system address %q", systemAddr) + } + + if hasIP6Gateway && ip6GatewayNet.Contains(systemAddr) { + return fmt.Errorf("UPLINK ipv6.gateway must not include system address %q", systemAddr) + } + } + + return nil +} + // setupCluster Bootstraps the cluster if necessary, adds all peers to the cluster, and completes any post cluster // configuration. func setupCluster(s *service.Handler, systems map[string]InitSystem) error { diff --git a/microcloud/cmd/microcloud/main_init_preseed.go b/microcloud/cmd/microcloud/main_init_preseed.go index 8397f2b6c..ab90a40e7 100644 --- a/microcloud/cmd/microcloud/main_init_preseed.go +++ b/microcloud/cmd/microcloud/main_init_preseed.go @@ -159,6 +159,11 @@ func (c *CmdControl) RunPreseed(cmd *cobra.Command, init bool) error { } } + err = validateSystems(s, systems) + if err != nil { + return err + } + return setupCluster(s, systems) } diff --git a/microcloud/cmd/microcloud/main_init_test.go b/microcloud/cmd/microcloud/main_init_test.go new file mode 100644 index 000000000..6f8c3b954 --- /dev/null +++ b/microcloud/cmd/microcloud/main_init_test.go @@ -0,0 +1,174 @@ +package main + +import ( + "testing" + + lxdAPI "github.com/canonical/lxd/shared/api" + + "github.com/canonical/microcloud/microcloud/mdns" + "github.com/canonical/microcloud/microcloud/service" +) + +func newSystemWithNetworks(address string, networks []lxdAPI.NetworksPost) InitSystem { + return InitSystem{ + ServerInfo: mdns.ServerInfo{ + Name: "testSystem", + Address: address, + }, + Networks: networks, + } +} + +func newSystemWithUplinkNetConfig(address string, config map[string]string) InitSystem { + return newSystemWithNetworks(address, []lxdAPI.NetworksPost{{ + Name: "UPLINK", + Type: "physical", + NetworkPut: lxdAPI.NetworkPut{ + Config: config, + }, + }}) +} + +func newTestHandler(addr string, t *testing.T) *service.Handler { + handler, err := service.NewHandler("testSystem", addr, "/tmp/microcloud_test_hander", true, true) + if err != nil { + t.Fatalf("Failed to create test service handler: %s", err) + } + + return handler +} + +func newTestSystemsMap(systems ...InitSystem) map[string]InitSystem { + systemsMap := map[string]InitSystem{} + + for _, system := range systems { + systemsMap[system.ServerInfo.Name] = system + } + + return systemsMap +} + +func ensureValidateSystemsPasses(handler *service.Handler, testSystems map[string]InitSystem, t *testing.T) { + for testName, system := range testSystems { + systems := newTestSystemsMap(system) + + err := validateSystems(handler, systems) + if err != nil { + t.Fatalf("Valid system %q failed validate: %s", testName, err) + } + } +} + +func ensureValidateSystemsFails(handler *service.Handler, testSystems map[string]InitSystem, t *testing.T) { + for testName, system := range testSystems { + systems := newTestSystemsMap(system) + + err := validateSystems(handler, systems) + if err == nil { + t.Fatalf("Invalid system %q passed validation", testName) + } + } +} + +func TestValidateSystemsIP6(t *testing.T) { + address := "fc00:feed:beef::bed1" + handler := newTestHandler(address, t) + + validSystems := map[string]InitSystem{ + "64Net": newSystemWithUplinkNetConfig(address, map[string]string{ + "ipv6.gateway": "fc00:bad:feed::1/64", + }), + } + + ensureValidateSystemsPasses(handler, validSystems, t) + + invalidSystems := map[string]InitSystem{ + "uplinkInsideManagement6Net": newSystemWithUplinkNetConfig(address, map[string]string{ + "ipv6.gateway": "fc00:feed:beef::1/64", + }), + } + + ensureValidateSystemsFails(handler, invalidSystems, t) +} + +func TestValidateSystemsIP4(t *testing.T) { + address := "192.168.1.27" + handler := newTestHandler(address, t) + + // Each entry in these maps is validated individually + validSystems := map[string]InitSystem{ + "plainGateway": newSystemWithUplinkNetConfig(address, map[string]string{ + "ipv4.gateway": "10.234.0.1/16", + }), + "16Net": newSystemWithUplinkNetConfig(address, map[string]string{ + "ipv4.gateway": "10.42.0.1/16", + "ipv4.ovn.ranges": "10.42.1.1-10.42.5.255", + }), + "24Net": newSystemWithUplinkNetConfig(address, map[string]string{ + "ipv4.gateway": "190.168.4.1/24", + "ipv4.ovn.ranges": "190.168.4.50-190.168.4.60", + }), + } + + ensureValidateSystemsPasses(handler, validSystems, t) + + invalidSystems := map[string]InitSystem{ + "backwardsRange": newSystemWithUplinkNetConfig(address, map[string]string{ + "ipv4.gateway": "10.42.0.1/16", + "ipv4.ovn.ranges": "10.42.5.255-10.42.1.1", + }), + "rangesOutsideGateway": newSystemWithUplinkNetConfig(address, map[string]string{ + "ipv4.gateway": "10.1.1.0/24", + "ipv4.ovn.ranges": "10.2.2.50-10.2.2.100", + }), + "uplinkInsideManagementNet": newSystemWithUplinkNetConfig(address, map[string]string{ + "ipv4.gateway": "192.168.1.1/24", + "ipv4.ovn.ranges": "192.168.1.50-192.168.1.200", + }), + "uplinkInsideManagementNetNoRange": newSystemWithUplinkNetConfig(address, map[string]string{ + "ipv4.gateway": "192.168.1.1/16", + }), + } + + ensureValidateSystemsFails(handler, invalidSystems, t) +} + +func TestValidateSystemsMultiSystem(t *testing.T) { + localAddr := "10.23.1.72" + handler := newTestHandler(localAddr, t) + + uplinkConfig := map[string]string{ + "ipv4.gateway": "10.100.1.1/16", + } + + sys1 := newSystemWithUplinkNetConfig(localAddr, uplinkConfig) + + sys2 := newSystemWithUplinkNetConfig("10.100.20.20", uplinkConfig) + sys2.ServerInfo.Name = "sys2" + + systems := newTestSystemsMap(sys1, sys2) + + err := validateSystems(handler, systems) + if err == nil { + t.Fatalf("sys1 and sys2 with conflicting uplink and management networks passed validation") + } + + localAddr = "fc00:feed:beef::f00d" + handler = newTestHandler(localAddr, t) + + uplinkConfig = map[string]string{ + "ipv6.gateway": "fc00:bad:feed::1/64", + } + + sys3 := newSystemWithUplinkNetConfig(localAddr, uplinkConfig) + + sys4 := newSystemWithUplinkNetConfig("fc00:bad:feed::f00d", uplinkConfig) + sys4.ServerInfo.Name = "sys4" + + systems = newTestSystemsMap(sys3, sys4) + + err = validateSystems(handler, systems) + if err == nil { + t.Fatalf("sys3 and sys4 with conflicting uplink and default networks passed validation") + } +}