From 3b0d3deae4cd2f70499dd554a81983f9bdb948d6 Mon Sep 17 00:00:00 2001 From: Marko Juraga Date: Fri, 20 Sep 2024 13:45:13 +0200 Subject: [PATCH] BUG/MAJOR: configuration: add nil checks when deprecating users --- configuration/configuration_deprecated.go | 64 ++++++++++++----------- configuration/configuration_storage.go | 14 ++--- 2 files changed, 41 insertions(+), 37 deletions(-) diff --git a/configuration/configuration_deprecated.go b/configuration/configuration_deprecated.go index 88d80634..c0120245 100644 --- a/configuration/configuration_deprecated.go +++ b/configuration/configuration_deprecated.go @@ -73,40 +73,42 @@ func (c *Configuration) migrateUsers() ([]string, error) { clusterModeStoragePath := path.Join(c.HAProxy.DataplaneStorageDir, storage.ClusterModeDataFileName) usersToMigrate := make([]storagetype.User, 0) - for _, singleModeUser := range dapiCfgStorage.Dataplaneapi.Users { - found := false - // Only migrate cluster users - if !singleModeUser.IsClusterUser() { - continue - } + if dapiCfgStorage.Dataplaneapi != nil { + for _, singleModeUser := range dapiCfgStorage.Dataplaneapi.Users { + found := false + // Only migrate cluster users + if !singleModeUser.IsClusterUser() { + continue + } - var muser storagetype.User - for _, muser = range dapiStorageUsers { - if muser.Name == singleModeUser.Name { - found = true - break + var muser storagetype.User + for _, muser = range dapiStorageUsers { + if muser.Name == singleModeUser.Name { + found = true + break + } } - } - // Already migrated - if found { - msg := fmt.Sprintf("[CFG DEPRECATED] [SKIP] [User] [%s]: already migrated. Old location [%s] New location [%s]. Use only new location", - singleModeUser.Name, - c.HAProxy.DataplaneConfig, - clusterModeStoragePath) - // Logging is not done here as at startup, the logger is not yet initialized - // so it's done later on - deprecationInfoMsg = append(deprecationInfoMsg, msg) - } else { - // If not already migrated, then migrate it - msg := fmt.Sprintf("[CFG DEPRECATED] [MIGRATE] [User] [%s]: migrating. Old location [%s] New location [%s]. Use only new location", - singleModeUser.Name, - c.HAProxy.DataplaneConfig, - clusterModeStoragePath) - // Logging is not done here as at startup, the logger is not yet initialized - // so it's done later on - deprecationInfoMsg = append(deprecationInfoMsg, msg) - usersToMigrate = append(usersToMigrate, singleModeUser) + // Already migrated + if found { + msg := fmt.Sprintf("[CFG DEPRECATED] [SKIP] [User] [%s]: already migrated. Old location [%s] New location [%s]. Use only new location", + singleModeUser.Name, + c.HAProxy.DataplaneConfig, + clusterModeStoragePath) + // Logging is not done here as at startup, the logger is not yet initialized + // so it's done later on + deprecationInfoMsg = append(deprecationInfoMsg, msg) + } else { + // If not already migrated, then migrate it + msg := fmt.Sprintf("[CFG DEPRECATED] [MIGRATE] [User] [%s]: migrating. Old location [%s] New location [%s]. Use only new location", + singleModeUser.Name, + c.HAProxy.DataplaneConfig, + clusterModeStoragePath) + // Logging is not done here as at startup, the logger is not yet initialized + // so it's done later on + deprecationInfoMsg = append(deprecationInfoMsg, msg) + usersToMigrate = append(usersToMigrate, singleModeUser) + } } } if err := c.clusterModeStorage.AddUsersAndStore(usersToMigrate); err != nil { diff --git a/configuration/configuration_storage.go b/configuration/configuration_storage.go index b7437a46..4c97ea3b 100644 --- a/configuration/configuration_storage.go +++ b/configuration/configuration_storage.go @@ -382,14 +382,16 @@ func copyConfigurationToStorage(cfg *Configuration) { func (cfgStorage *StorageDataplaneAPIConfiguration) emptyDeprecatedSections() { cfgStorage.DeprecatedBootstrapKey = nil // Remove Cluster Users from dapi configuration file - for i := 0; i < len(cfgStorage.Dataplaneapi.Users); { - if cfgStorage.Dataplaneapi.Users[i].IsClusterUser() { - if len(cfgStorage.Dataplaneapi.Users) > i { - cfgStorage.Dataplaneapi.Users = append(cfgStorage.Dataplaneapi.Users[:i], cfgStorage.Dataplaneapi.Users[i+1:]...) - continue + if cfgStorage.Dataplaneapi != nil { + for i := 0; i < len(cfgStorage.Dataplaneapi.Users); { + if cfgStorage.Dataplaneapi.Users[i].IsClusterUser() { + if len(cfgStorage.Dataplaneapi.Users) > i { + cfgStorage.Dataplaneapi.Users = append(cfgStorage.Dataplaneapi.Users[:i], cfgStorage.Dataplaneapi.Users[i+1:]...) + continue + } } + i++ } - i++ } // Remove Cluster cfgStorage.DeprecatedCluster = nil