Fix nil pointer exception in config parser (#702)

In config reader if the input.PreSharedKey is nil then the GetConfig
throw nil pointer exception
This commit is contained in:
Zoltan Papp 2023-02-23 09:48:43 +01:00 committed by GitHub
parent c3ed08c249
commit d8429c5c34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 6 deletions

View File

@ -242,13 +242,12 @@ func GetConfig(input ConfigInput) (*Config, error) {
if _, err := os.Stat(input.ConfigPath); os.IsNotExist(err) {
log.Infof("generating new config %s", input.ConfigPath)
return createNewConfig(input)
} else {
// don't overwrite pre-shared key if we receive asterisks from UI
if *input.PreSharedKey == "**********" {
input.PreSharedKey = nil
}
return ReadConfig(input)
}
if isPreSharedKeyHidden(input.PreSharedKey) {
input.PreSharedKey = nil
}
return ReadConfig(input)
}
// generateKey generates a new Wireguard private key
@ -364,3 +363,11 @@ func isProviderConfigValid(config ProviderConfig) error {
}
return nil
}
// don't overwrite pre-shared key if we receive asterisks from UI
func isPreSharedKeyHidden(preSharedKey *string) bool {
if preSharedKey != nil && *preSharedKey == "**********" {
return true
}
return false
}

View File

@ -85,3 +85,40 @@ func TestGetConfig(t *testing.T) {
}
assert.Equal(t, readConf.(*Config).ManagementURL.String(), newManagementURL)
}
func TestHiddenPreSharedKey(t *testing.T) {
hidden := "**********"
samplePreSharedKey := "mysecretpresharedkey"
tests := []struct {
name string
preSharedKey *string
want string
}{
{"nil", nil, ""},
{"hidden", &hidden, ""},
{"filled", &samplePreSharedKey, samplePreSharedKey},
}
// generate default cfg
cfgFile := filepath.Join(t.TempDir(), "config.json")
_, _ = GetConfig(ConfigInput{
ConfigPath: cfgFile,
})
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg, err := GetConfig(ConfigInput{
ConfigPath: cfgFile,
PreSharedKey: tt.preSharedKey,
})
if err != nil {
t.Fatalf("failed to get cfg: %s", err)
}
if cfg.PreSharedKey != tt.want {
t.Fatalf("invalid preshared key: '%s', expected: '%s' ", cfg.PreSharedKey, tt.want)
}
})
}
}