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"
)
// 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(
networkConfigs map[string]daecommon.NetworkConfig,
creationParams bootstrap.CreationParams,
) *daecommon.NetworkConfig {
if len(networkConfigs) == 1 { // DEPRECATED
if c, ok := networkConfigs[daecommon.DeprecatedNetworkID]; ok {
return &c
}
if c := getDeprecatedNetworkConfig(networkConfigs); c != nil {
return c
}
for searchStr, networkConfig := range networkConfigs {

View File

@ -71,6 +71,11 @@ func New(
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(
ctx, networkLoader, daemonConfig, loadableNetworks,
); err != nil {
@ -131,6 +136,13 @@ func (d *Daemon) CreateNetwork(
d.l.Lock()
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 (
networkConfig = pickNetworkConfig(
d.daemonConfig.Networks, creationParams,
@ -182,6 +194,13 @@ func (d *Daemon) JoinNetwork(
d.l.Lock()
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 (
creationParams = newBootstrap.Bootstrap.NetworkCreationParams
networkID = creationParams.ID