Don't allow joining more than one network when deprecated config format is used

This commit is contained in:
Brian Picciano 2024-12-17 13:40:31 +01:00
parent 3111d2ca74
commit 3f9863c39e
2 changed files with 43 additions and 4 deletions

View File

@ -8,14 +8,34 @@ import (
"isle/daemon/network" "isle/daemon/network"
) )
// getDeprecatedNetworkConfig will return the NetworkConfig configured in the
// deprecated configuration format, or nil if the deprecated format is not used.
//
// In the deprecated format it is not possible to explicitly know which network
// a NetworkConfig is intended for, so the Daemon can only allow a single
// network to be joined when using this format.
//
// DEPRECATED
func getDeprecatedNetworkConfig(
networkConfigs map[string]daecommon.NetworkConfig,
) *daecommon.NetworkConfig {
if len(networkConfigs) != 1 {
return nil
}
if c, ok := networkConfigs[daecommon.DeprecatedNetworkID]; ok {
return &c
}
return nil
}
func pickNetworkConfig( func pickNetworkConfig(
networkConfigs map[string]daecommon.NetworkConfig, networkConfigs map[string]daecommon.NetworkConfig,
creationParams bootstrap.CreationParams, creationParams bootstrap.CreationParams,
) *daecommon.NetworkConfig { ) *daecommon.NetworkConfig {
if len(networkConfigs) == 1 { // DEPRECATED if c := getDeprecatedNetworkConfig(networkConfigs); c != nil {
if c, ok := networkConfigs[daecommon.DeprecatedNetworkID]; ok { return c
return &c
}
} }
for searchStr, networkConfig := range networkConfigs { for searchStr, networkConfig := range networkConfigs {

View File

@ -71,6 +71,11 @@ func New(
return nil, fmt.Errorf("listing loadable networks: %w", err) return nil, fmt.Errorf("listing loadable networks: %w", err)
} }
if getDeprecatedNetworkConfig(daemonConfig.Networks) != nil &&
len(loadableNetworks) > 1 {
return nil, errors.New("Deprecated config format cannot be used when there are more than one loadable networks")
}
if err := validateConfig( if err := validateConfig(
ctx, networkLoader, daemonConfig, loadableNetworks, ctx, networkLoader, daemonConfig, loadableNetworks,
); err != nil { ); err != nil {
@ -131,6 +136,13 @@ func (d *Daemon) CreateNetwork(
d.l.Lock() d.l.Lock()
defer d.l.Unlock() defer d.l.Unlock()
if getDeprecatedNetworkConfig(d.daemonConfig.Networks) != nil &&
len(d.networks) > 0 {
return network.ErrInvalidConfig.WithData(
"Cannot use deprecated configuration file format while joined to more than one network",
)
}
var ( var (
networkConfig = pickNetworkConfig( networkConfig = pickNetworkConfig(
d.daemonConfig.Networks, creationParams, d.daemonConfig.Networks, creationParams,
@ -182,6 +194,13 @@ func (d *Daemon) JoinNetwork(
d.l.Lock() d.l.Lock()
defer d.l.Unlock() defer d.l.Unlock()
if getDeprecatedNetworkConfig(d.daemonConfig.Networks) != nil &&
len(d.networks) > 0 {
return network.ErrInvalidConfig.WithData(
"Cannot use deprecated configuration file format while joined to more than one network",
)
}
var ( var (
creationParams = newBootstrap.Bootstrap.NetworkCreationParams creationParams = newBootstrap.Bootstrap.NetworkCreationParams
networkID = creationParams.ID networkID = creationParams.ID