diff --git a/pkg/config/localconfig.go b/pkg/config/localconfig.go index 1e5eeda..9b7727f 100644 --- a/pkg/config/localconfig.go +++ b/pkg/config/localconfig.go @@ -56,9 +56,32 @@ type Instance struct { } type Auth struct { - Server string - ClientId string - ClientSecret string + Server string `yaml:"server"` + ClientId string `yaml:"clientId"` + ClientSecret string `yaml:"clientSecret"` +} + +func (a *Auth) UnmarshalYAML(unmarshal func(interface{}) error) error { + var raw struct { + Server string `yaml:"server"` + ClientId string `yaml:"clientId"` + ClientSecret string `yaml:"clientSecret"` + LegacyClientId string `yaml:"clientid"` + LegacyClientSecret string `yaml:"clientsecret"` + } + if err := unmarshal(&raw); err != nil { + return err + } + if raw.ClientId == "" { + raw.ClientId = raw.LegacyClientId + } + if raw.ClientSecret == "" { + raw.ClientSecret = raw.LegacyClientSecret + } + a.Server = raw.Server + a.ClientId = raw.ClientId + a.ClientSecret = raw.ClientSecret + return nil } type WatchConfig struct { diff --git a/pkg/config/localconfig_test.go b/pkg/config/localconfig_test.go new file mode 100644 index 0000000..24ee6ec --- /dev/null +++ b/pkg/config/localconfig_test.go @@ -0,0 +1,50 @@ +package config + +import ( + "testing" + + "github.com/stretchr/testify/require" + "gopkg.in/yaml.v2" +) + +func TestAuthYAMLTags(t *testing.T) { + cfg := LocalConfig{ + Auths: []Auth{ + { + Server: "local", + ClientId: "microcks-serviceaccount", + ClientSecret: "secret", + }, + }, + } + + data, err := yaml.Marshal(cfg) + require.NoError(t, err) + require.Contains(t, string(data), "server: local") + require.Contains(t, string(data), "clientId: microcks-serviceaccount") + require.Contains(t, string(data), "clientSecret: secret") + + var parsed LocalConfig + err = yaml.Unmarshal(data, &parsed) + require.NoError(t, err) + require.Equal(t, cfg.Auths, parsed.Auths) +} + +func TestAuthYAMLAcceptsLegacyKeys(t *testing.T) { + data := []byte(`auths: +- server: local + clientid: microcks-serviceaccount + clientsecret: secret +`) + + var parsed LocalConfig + err := yaml.Unmarshal(data, &parsed) + require.NoError(t, err) + require.Equal(t, []Auth{ + { + Server: "local", + ClientId: "microcks-serviceaccount", + ClientSecret: "secret", + }, + }, parsed.Auths) +}