From 053053d56eee8e260ee6383e8e4063847665f10a Mon Sep 17 00:00:00 2001 From: Tiffany Kalin Date: Mon, 22 Nov 2021 13:24:44 -0700 Subject: [PATCH 1/4] mfw-1313: wip: fix getLicenseDetails --- services/licensemanager/LicenseManager.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/services/licensemanager/LicenseManager.go b/services/licensemanager/LicenseManager.go index 1aa932b0..522f7a77 100644 --- a/services/licensemanager/LicenseManager.go +++ b/services/licensemanager/LicenseManager.go @@ -169,6 +169,10 @@ func GetLicenseDetails() (LicenseInfo, error) { // Load file jsonLicense, err := ioutil.ReadFile(config.LicenseLocation) + if err != nil { + logger.Warn("Error opening license file: %s\n", err.Error()) + return retLicense, err + } // Unmarshal err = json.Unmarshal(jsonLicense, &retLicense) From d90067fffd0825c8bad49590a184be6462c7520a Mon Sep 17 00:00:00 2001 From: Tiffany Kalin Date: Mon, 22 Nov 2021 13:36:14 -0700 Subject: [PATCH 2/4] mfw-1313: check license file checks --- services/licensemanager/LicenseManager.go | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/services/licensemanager/LicenseManager.go b/services/licensemanager/LicenseManager.go index 522f7a77..36b27427 100644 --- a/services/licensemanager/LicenseManager.go +++ b/services/licensemanager/LicenseManager.go @@ -14,6 +14,10 @@ import ( "github.com/untangle/golang-shared/services/settings" ) +const ( + LicenseFileDoesNotExistStr string = "license_file_does_not_exist" +) + var config Config var services map[string]*Service @@ -168,6 +172,12 @@ func GetLicenseDetails() (LicenseInfo, error) { var retLicense LicenseInfo // Load file + licenseFileExists := licenseFileExists(config.LicenseLocation) + if !licenseFileExists { + logger.Warn("License file does not exist\n") + return retLicense, errors.New(LicenseFileDoesNotExistStr) + } + jsonLicense, err := ioutil.ReadFile(config.LicenseLocation) if err != nil { logger.Warn("Error opening license file: %s\n", err.Error()) @@ -185,6 +195,10 @@ func GetLicenseDetails() (LicenseInfo, error) { return retLicense, nil } +func GetLicenseFileDoesNotExistStr() string { + return LicenseFileDoesNotExistStr +} + // SetServices will disable any disabled services to un-enabled in settings func SetServices(enabledServices map[string]bool) error { var err error = nil @@ -264,6 +278,16 @@ func setServiceState(serviceName string, newAllowedState string, saveStates bool } +// licenseFileExists checks if a file exists and is not a directory before we +// try using it to prevent further errors. +func licenseFileExists(filename string) bool { + info, err := os.Stat(filename) + if os.IsNotExist(err) { + return false + } + return !info.IsDir() +} + // shutdownServices iterates servicesToShutdown and calls the shutdown hook on them, and also removes the license file // @param licenseFile string - the license file location // @param servicesToShutdown map[string]Service - the services we want to shutdown From 073d70f3a479fe13d54b6fcdb5159167e673797f Mon Sep 17 00:00:00 2001 From: Tiffany Kalin Date: Mon, 22 Nov 2021 13:43:13 -0700 Subject: [PATCH 3/4] mfw-1313: change const strings --- services/licensemanager/LicenseManager.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/licensemanager/LicenseManager.go b/services/licensemanager/LicenseManager.go index 36b27427..f6930003 100644 --- a/services/licensemanager/LicenseManager.go +++ b/services/licensemanager/LicenseManager.go @@ -15,7 +15,7 @@ import ( ) const ( - LicenseFileDoesNotExistStr string = "license_file_does_not_exist" + LicenseFileDoesNotExistStr string = "RELOAD_LICENSES" ) var config Config From d0e507d41b7ee660d15f6fadfa730c5af067bc7d Mon Sep 17 00:00:00 2001 From: Tiffany Kalin Date: Mon, 22 Nov 2021 13:45:38 -0700 Subject: [PATCH 4/4] mfw-1313: fix lint --- services/licensemanager/LicenseManager.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/services/licensemanager/LicenseManager.go b/services/licensemanager/LicenseManager.go index f6930003..b344f6b8 100644 --- a/services/licensemanager/LicenseManager.go +++ b/services/licensemanager/LicenseManager.go @@ -15,6 +15,7 @@ import ( ) const ( + // LicenseFileDoesNotExistStr is the string to check if licenses should be reloaded when status is returned LicenseFileDoesNotExistStr string = "RELOAD_LICENSES" ) @@ -195,6 +196,8 @@ func GetLicenseDetails() (LicenseInfo, error) { return retLicense, nil } +// GetLicenseFileDoesNotExistStr returns the error string for license file does not exist for comparison reasons +// @return string of the license file does not exist error func GetLicenseFileDoesNotExistStr() string { return LicenseFileDoesNotExistStr }