Skip to content

Commit

Permalink
auth: add method for merging two AuthConfigurations instances
Browse files Browse the repository at this point in the history
  • Loading branch information
fsouza committed Apr 9, 2020
1 parent 1873da6 commit f866d4d
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 1 deletion.
22 changes: 21 additions & 1 deletion auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,23 @@ func (c AuthConfigurations) isEmpty() bool {
return len(c.Configs) == 0
}

func (c AuthConfigurations) headerKey() string {
func (AuthConfigurations) headerKey() string {
return "X-Registry-Config"
}

// merge updates the configuration. If a key is defined in both maps, the one
// in c.Configs takes precedence.
func (c *AuthConfigurations) merge(other AuthConfigurations) {
for k, v := range other.Configs {
if c.Configs == nil {
c.Configs = make(map[string]AuthConfiguration)
}
if _, ok := c.Configs[k]; !ok {
c.Configs[k] = v
}
}
}

// AuthConfigurations119 is used to serialize a set of AuthConfigurations
// for Docker API >= 1.19.
type AuthConfigurations119 map[string]AuthConfiguration
Expand Down Expand Up @@ -119,6 +132,13 @@ func NewAuthConfigurationsFromDockerCfg() (*AuthConfigurations, error) {
var err error
for _, path := range pathsToTry {
auths, err = NewAuthConfigurationsFromFile(path)
if err != nil {
continue
}

if !auths.isEmpty() {
return auths, nil
}
if err == nil {
return auths, nil
}
Expand Down
93 changes: 93 additions & 0 deletions auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http"
"os"
"path"
"reflect"
"strings"
"testing"
)
Expand Down Expand Up @@ -237,3 +238,95 @@ func TestAuthCheck(t *testing.T) {
t.Fatal("expected failure from unauthorized auth")
}
}

func TestAuthConfigurationsMerge(t *testing.T) {
t.Parallel()
var tests = []struct {
name string
left AuthConfigurations
right AuthConfigurations
expected AuthConfigurations
}{
{
name: "empty configs",
expected: AuthConfigurations{},
},
{
name: "empty left config",
right: AuthConfigurations{
Configs: map[string]AuthConfiguration{
"docker.io": {Email: "[email protected]"},
},
},
expected: AuthConfigurations{
Configs: map[string]AuthConfiguration{
"docker.io": {Email: "[email protected]"},
},
},
},
{
name: "empty right config",
left: AuthConfigurations{
Configs: map[string]AuthConfiguration{
"docker.io": {Email: "[email protected]"},
},
},
expected: AuthConfigurations{
Configs: map[string]AuthConfiguration{
"docker.io": {Email: "[email protected]"},
},
},
},
{
name: "no conflicts",
left: AuthConfigurations{
Configs: map[string]AuthConfiguration{
"docker.io": {Email: "[email protected]"},
},
},
right: AuthConfigurations{
Configs: map[string]AuthConfiguration{
"us.gcr.io": {Email: "[email protected]"},
},
},
expected: AuthConfigurations{
Configs: map[string]AuthConfiguration{
"docker.io": {Email: "[email protected]"},
"us.gcr.io": {Email: "[email protected]"},
},
},
},
{
name: "no conflicts",
left: AuthConfigurations{
Configs: map[string]AuthConfiguration{
"docker.io": {Email: "[email protected]"},
"us.gcr.io": {Email: "[email protected]"},
},
},
right: AuthConfigurations{
Configs: map[string]AuthConfiguration{
"us.gcr.io": {Email: "[email protected]"},
},
},
expected: AuthConfigurations{
Configs: map[string]AuthConfiguration{
"docker.io": {Email: "[email protected]"},
"us.gcr.io": {Email: "[email protected]"},
},
},
},
}

for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
test.left.merge(test.right)

if !reflect.DeepEqual(test.left, test.expected) {
t.Errorf("wrong configuration map after merge\nwant %#v\ngot %#v", test.expected, test.left)
}
})
}
}

0 comments on commit f866d4d

Please sign in to comment.