Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent duplicate entries in .docker/config.json #129

Merged
merged 2 commits into from
Sep 16, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 5 additions & 16 deletions cmd/build-init/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func main() {
}

remoteImageFactory := &registry.ImageFactory{
KeychainFactory: defaultKeychainFactory{},
KeychainFactory: keychainFactory{builderCreds},
}

filePermissionSetup := &cnb.FilePermissionSetup{
Expand All @@ -76,11 +76,12 @@ func main() {
}
}

type defaultKeychainFactory struct {
type keychainFactory struct {
keychain authn.Keychain
}

func (defaultKeychainFactory) KeychainForImageRef(registry.ImageRef) authn.Keychain {
return authn.DefaultKeychain
func (k keychainFactory) KeychainForImageRef(registry.ImageRef) authn.Keychain {
return k.keychain
}

type realOs struct {
Expand All @@ -89,15 +90,3 @@ type realOs struct {
func (realOs) Chown(volume string, uid, gid int) error {
return os.Chown(volume, uid, gid)
}

func fileExists(file string, logger *log.Logger) bool {
_, err := os.Stat(file)
if err != nil {
if os.IsNotExist(err) {
return false
}
logger.Fatal(err.Error())
}

return true
}
44 changes: 38 additions & 6 deletions pkg/dockercreds/docker_creds.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package dockercreds

import (
"encoding/json"
"github.com/pkg/errors"
"io/ioutil"
"net/url"
"strings"

"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
"github.com/pkg/errors"
)

type DockerCreds map[string]entry
Expand Down Expand Up @@ -36,7 +38,11 @@ func (c DockerCreds) AppendToDockerConfig(path string) error {
return err
}

appendedCreds := c.append(existingCreds)
appendedCreds, err := existingCreds.append(c)
if err != nil {
return err
}

configJson := dockerConfigJson{
Auths: appendedCreds,
}
Expand All @@ -47,15 +53,41 @@ func (c DockerCreds) AppendToDockerConfig(path string) error {
return ioutil.WriteFile(path, configJsonBytes, 0600)
}

func (c DockerCreds) append(a DockerCreds) DockerCreds {
func (c DockerCreds) append(a DockerCreds) (DockerCreds, error) {
if c == nil {
return a
return a, nil
} else if a == nil {
return c, nil
}

for k, v := range a {
c[k] = v
if contains, err := c.contains(k); err != nil {
return nil, err
} else if !contains {
c[k] = v
}
}

return c, nil
}

func (c DockerCreds) contains(reg string) (bool, error) {
if !strings.HasPrefix(reg, "http://") && !strings.HasPrefix(reg, "https://") {
reg = "//" + reg
}

u, err := url.Parse(reg)
if err != nil {
return false, err
}
return c

for existingRegistry := range c {
if (RegistryMatcher{}.Match(u.Host, existingRegistry)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we update this method to new require a receiver and just be a public method in the pkg?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed 🙂

return true, nil
}
}

return false, nil
}

type entry struct {
Expand Down
28 changes: 28 additions & 0 deletions pkg/dockercreds/docker_creds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,34 @@ func testDockerCreds(t *testing.T, when spec.G, it spec.S) {

assert.JSONEq(t, expectedConfigJsonContents, string(configJsonBytes))
})

it("does not overwrite registries if they already exist in a different format", func() {
expectedConfigJsonContents := `{
"auths": {
"https://gcr.io": {
"auth": "dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZHNpbGxpbmVzcwo=",
"username": "testdockerhub",
"password": "testdockerhubusername"
}
}
}`
err := ioutil.WriteFile(filepath.Join(testPullSecretsDir, "config.json"), []byte(expectedConfigJsonContents), os.ModePerm)
require.NoError(t, err)

credsToAppend := DockerCreds{
"gcr.io": entry{
Auth: "newCreds=",
},
}

err = credsToAppend.AppendToDockerConfig(filepath.Join(testPullSecretsDir, "config.json"))
require.NoError(t, err)

configJsonBytes, err := ioutil.ReadFile(filepath.Join(testPullSecretsDir, "config.json"))
require.NoError(t, err)

assert.JSONEq(t, expectedConfigJsonContents, string(configJsonBytes))
})
})

when("#Resolve", func() {
Expand Down
7 changes: 5 additions & 2 deletions pkg/dockercreds/parse_pull_secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func ParseDockerPullSecrets(path string) (DockerCreds, error) {
return nil, err
}

return dockerCfg.append(dockerJson), nil
return dockerCfg.append(dockerJson)
}

func parseDockerCfg(path string) (DockerCreds, error) {
Expand All @@ -44,7 +44,10 @@ func parseDockerCfg(path string) (DockerCreds, error) {
}

func parseDockerConfigJson(path string) (DockerCreds, error) {
var config dockerConfigJson
config := dockerConfigJson{
Auths: map[string]entry{},
}

configjsonExists, err := fileExists(path)
if err != nil {
return nil, err
Expand Down