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"
"isle/bootstrap"
"isle/toolkit"
"net"
"os"
"path/filepath"
"strconv"
@ -148,6 +149,35 @@ type Config struct {
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
// the given io.Writer.
func CopyDefaultConfig(into io.Writer, appDirPath string) error {
@ -182,15 +212,16 @@ func LoadConfig(userConfigPath string) (Config, error) {
}
{ // DEPRECATED
var config NetworkConfig
_ = yaml.Unmarshal(userConfigB, &config)
if !toolkit.IsZero(config) {
config.fillDefaults()
return Config{
var networkConfig NetworkConfig
_ = yaml.Unmarshal(userConfigB, &networkConfig)
if !toolkit.IsZero(networkConfig) {
networkConfig.fillDefaults()
config := Config{
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
}
return config, nil
return config, config.Validate()
}
// BootstrapGarageHostForAlloc returns the bootstrap.GarageHostInstance which