Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions pkg/config/localconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
50 changes: 50 additions & 0 deletions pkg/config/localconfig_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
Loading