Skip to content

Commit

Permalink
Trim white spaces when reading from secret files
Browse files Browse the repository at this point in the history
The secrets stored in files
GEOIPUPDATE_ACCOUNT_ID_FILE, GEOIPUPDATE_LICENSE_KEY_FILE
may contain white spaces.
  • Loading branch information
marselester committed Oct 18, 2023
1 parent 5761820 commit ce3439c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/geoipupdate/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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 {
Expand Down
35 changes: 35 additions & 0 deletions pkg/geoipupdate/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand Down

0 comments on commit ce3439c

Please sign in to comment.