Check that two different networks aren't trying to use the same nebula port

This commit is contained in:
Brian Picciano 2024-09-12 08:59:23 +02:00
parent df4eae8a5c
commit 6c036d1183

View File

@ -5,6 +5,7 @@ import (
"io" "io"
"isle/bootstrap" "isle/bootstrap"
"isle/toolkit" "isle/toolkit"
"net"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
@ -148,6 +149,35 @@ type Config struct {
Networks map[string]NetworkConfig `yaml:"networks"` Networks map[string]NetworkConfig `yaml:"networks"`
} }
// Validate asserts that the Config has no internal inconsistencies which would
// render it unusable.
func (c Config) Validate() error {
nebulaPorts := map[string]string{}
for id, network := range c.Networks {
if network.VPN.PublicAddr == "" {
continue
}
_, port, err := net.SplitHostPort(network.VPN.PublicAddr)
if err != nil {
return fmt.Errorf(
"invalid vpn.public_addr %q: %w", network.VPN.PublicAddr, err,
)
} else if otherID, ok := nebulaPorts[port]; ok {
return fmt.Errorf(
"two networks with the same vpn.public_addr: %q and %q",
id,
otherID,
)
}
nebulaPorts[port] = id
}
return nil
}
// CopyDefaultConfig copies the daemon config file embedded in the AppDir into // CopyDefaultConfig copies the daemon config file embedded in the AppDir into
// the given io.Writer. // the given io.Writer.
func CopyDefaultConfig(into io.Writer, appDirPath string) error { func CopyDefaultConfig(into io.Writer, appDirPath string) error {
@ -182,15 +212,16 @@ func LoadConfig(userConfigPath string) (Config, error) {
} }
{ // DEPRECATED { // DEPRECATED
var config NetworkConfig var networkConfig NetworkConfig
_ = yaml.Unmarshal(userConfigB, &config) _ = yaml.Unmarshal(userConfigB, &networkConfig)
if !toolkit.IsZero(config) { if !toolkit.IsZero(networkConfig) {
config.fillDefaults() networkConfig.fillDefaults()
return Config{ config := Config{
Networks: map[string]NetworkConfig{ Networks: map[string]NetworkConfig{
DeprecatedNetworkID: config, DeprecatedNetworkID: networkConfig,
}, },
}, nil }
return config, config.Validate()
} }
} }
@ -205,7 +236,7 @@ func LoadConfig(userConfigPath string) (Config, error) {
config.Networks[id] = network config.Networks[id] = network
} }
return config, nil return config, config.Validate()
} }
// BootstrapGarageHostForAlloc returns the bootstrap.GarageHostInstance which // BootstrapGarageHostForAlloc returns the bootstrap.GarageHostInstance which