Skip to content

Commit

Permalink
Merge pull request #88 from ivanilves/issues-87
Browse files Browse the repository at this point in the history
Added handling for possibly corrupt configs
  • Loading branch information
vonrabbe authored Oct 31, 2017
2 parents d15b77b + 49152b5 commit 863a1a8
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
21 changes: 20 additions & 1 deletion docker/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"os"
"strings"
Expand Down Expand Up @@ -73,7 +74,25 @@ func Load(fileName string) (*Config, error) {
return nil, err
}

usernameAndPassword := strings.Split(string(b), ":")
authenticationToken := string(b)
usernameAndPassword := strings.Split(authenticationToken, ":")

if len(usernameAndPassword) != 2 {
if fileName != DefaultDockerJSON {
errStr := "Invalid auth for Docker registry: %s\nBase64-encoded string is wrong: %s (%s)\n"

return nil, errors.New(
fmt.Sprint(
errStr,
registry,
a.B64Auth,
authenticationToken,
),
)
}

continue
}

c.usernames[registry] = usernameAndPassword[0]
c.passwords[registry] = usernameAndPassword[1]
Expand Down
31 changes: 30 additions & 1 deletion docker/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
const configFile = "../../fixtures/docker/config.json"
const irrelevantConfigFile = "../../fixtures/docker/config.json.irrelevant"
const invalidConfigFile = "../../fixtures/docker/config.json.invalid"
const corruptConfigFile = "../../fixtures/docker/config.json.corrupt"

func TestLoad(t *testing.T) {
examples := map[string]string{
Expand Down Expand Up @@ -76,6 +77,34 @@ func TestLoadWithAbsentConfigFile(t *testing.T) {

_, err = Load("i/exist/only/in/your/magination")
if err != nil {
t.Fatalf("Expected NOT to fail while trying to load absent config file from a default path")
t.Fatalf(
"Expected NOT to fail while trying to load absent config file from a default path: %s",
err.Error(),
)
}
}

// Corrupt file = valid JSON file with badly encoded auth
func TestLoadWithCorruptConfigFile(t *testing.T) {
const corruptRegistry = "registry.valencia.io"

_, err := Load(corruptConfigFile)
if err == nil {
t.Fatalf("Expected to fail while loading corrupt config file")
}

DefaultDockerJSON = corruptConfigFile

c, err := Load(corruptConfigFile)
if err != nil {
t.Fatalf(
"Expected NOT to fail while loading corrupt config file from a default path: %s",
err.Error(),
)
}

_, _, defined := c.GetCredentials(corruptRegistry)
if defined {
t.Fatalf("Should NOT get credentials from a corrupt registry record: %s", corruptRegistry)
}
}
10 changes: 10 additions & 0 deletions fixtures/docker/config.json.corrupt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"auths": {
"registry.galicia.io": {
"auth": "ZnJhbmNvOmZyYW5jbw=="
},
"registry.valencia.io": {
"auth": "bWFjb3N1YXU="
}
}
}

0 comments on commit 863a1a8

Please sign in to comment.