From ce3439c8e21a76b1251fabd6430b3f7efea5ebb8 Mon Sep 17 00:00:00 2001 From: Marsel Mavletkulov Date: Wed, 18 Oct 2023 13:03:15 -0400 Subject: [PATCH] Trim white spaces when reading from secret files The secrets stored in files GEOIPUPDATE_ACCOUNT_ID_FILE, GEOIPUPDATE_LICENSE_KEY_FILE may contain white spaces. --- pkg/geoipupdate/config.go | 4 ++-- pkg/geoipupdate/config_test.go | 35 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/pkg/geoipupdate/config.go b/pkg/geoipupdate/config.go index 546ad8d2..ff0379d4 100644 --- a/pkg/geoipupdate/config.go +++ b/pkg/geoipupdate/config.go @@ -298,7 +298,7 @@ func setConfigFromEnv(config *Config) error { return fmt.Errorf("failed to open GEOIPUPDATE_ACCOUNT_ID_FILE: %w", err) } - config.AccountID, err = strconv.Atoi(string(accountID)) + config.AccountID, err = strconv.Atoi(strings.TrimSpace(string(accountID))) if err != nil { return fmt.Errorf("invalid account ID format") } @@ -328,7 +328,7 @@ func setConfigFromEnv(config *Config) error { return fmt.Errorf("failed to open GEOIPUPDATE_LICENSE_KEY_FILE: %w", err) } - config.LicenseKey = string(licenseKey) + config.LicenseKey = strings.TrimSpace(string(licenseKey)) } if value, ok := os.LookupEnv("GEOIPUPDATE_LOCK_FILE"); ok { diff --git a/pkg/geoipupdate/config_test.go b/pkg/geoipupdate/config_test.go index 7f3cea86..4544bed5 100644 --- a/pkg/geoipupdate/config_test.go +++ b/pkg/geoipupdate/config_test.go @@ -650,6 +650,41 @@ func TestSetConfigFromEnv(t *testing.T) { Verbose: true, }, }, + { + Description: "Clean up ACCOUNT_ID_FILE and LICENSE_KEY_FILE", + AccountIDFileContents: "\n\n2\t\n", + LicenseKeyFileContents: "\n000000000002\t\n\n", + Env: map[string]string{ + "GEOIPUPDATE_ACCOUNT_ID": "1", + "GEOIPUPDATE_ACCOUNT_ID_FILE": filepath.Join(t.TempDir(), "accountIDFile"), + "GEOIPUPDATE_DB_DIR": "/tmp/db", + "GEOIPUPDATE_EDITION_IDS": "GeoLite2-Country GeoLite2-City", + "GEOIPUPDATE_HOST": "updates.maxmind.com", + "GEOIPUPDATE_LICENSE_KEY": "000000000001", + "GEOIPUPDATE_LICENSE_KEY_FILE": filepath.Join(t.TempDir(), "licenseKeyFile"), + "GEOIPUPDATE_LOCK_FILE": "/tmp/lock", + "GEOIPUPDATE_PARALLELISM": "2", + "GEOIPUPDATE_PRESERVE_FILE_TIMES": "1", + "GEOIPUPDATE_PROXY": "127.0.0.1:8888", + "GEOIPUPDATE_PROXY_USER_PASSWORD": "username:password", + "GEOIPUPDATE_RETRY_FOR": "1m", + "GEOIPUPDATE_VERBOSE": "1", + }, + Expected: Config{ + AccountID: 2, + DatabaseDirectory: "/tmp/db", + EditionIDs: []string{"GeoLite2-Country", "GeoLite2-City"}, + LicenseKey: "000000000002", + LockFile: "/tmp/lock", + Parallelism: 2, + PreserveFileTimes: true, + proxyURL: "127.0.0.1:8888", + proxyUserInfo: "username:password", + RetryFor: time.Minute, + URL: "https://updates.maxmind.com", + Verbose: true, + }, + }, { Description: "Empty config", Env: map[string]string{},