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

Support multiple secrets of type dockercfg and dockerconfigjson #3659

Merged
Merged
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
33 changes: 25 additions & 8 deletions pkg/credentials/dockercreds/creds.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ import (
const annotationPrefix = "tekton.dev/docker-"

var config basicDocker
var dockerConfig string
var dockerCfg string
var dockerConfig arrayArg
var dockerCfg arrayArg

// AddFlags adds CLI flags that dockercreds supports to a given flag.FlagSet.
func AddFlags(flagSet *flag.FlagSet) {
Expand All @@ -44,9 +44,11 @@ func AddFlags(flagSet *flag.FlagSet) {

func flags(fs *flag.FlagSet) {
config = basicDocker{make(map[string]entry)}
dockerConfig = arrayArg{[]string{}}
dockerCfg = arrayArg{[]string{}}
fs.Var(&config, "basic-docker", "List of secret=url pairs.")
fs.StringVar(&dockerConfig, "docker-config", "", "Docker config.json secret file.")
fs.StringVar(&dockerCfg, "docker-cfg", "", "Docker .dockercfg secret file.")
fs.Var(&dockerConfig, "docker-config", "Docker config.json secret file.")
fs.Var(&dockerCfg, "docker-cfg", "Docker .dockercfg secret file.")
}

// As the flag is read, this status is populated.
Expand Down Expand Up @@ -87,6 +89,19 @@ func (dc *basicDocker) Set(value string) error {
return nil
}

type arrayArg struct {
Values []string
}

func (aa *arrayArg) Set(value string) error {
aa.Values = append(aa.Values, value)
return nil
}

func (aa *arrayArg) String() string {
return strings.Join(aa.Values, ",")
}

type configFile struct {
Auth map[string]entry `json:"auths"`
}
Expand Down Expand Up @@ -158,17 +173,19 @@ func (*basicDockerBuilder) Write(directory string) error {
basicDocker := filepath.Join(dockerDir, "config.json")
cf := configFile{Auth: config.Entries}
auth := map[string]entry{}
if dockerCfg != "" {
dockerConfigAuthMap, err := authsFromDockerCfg(dockerCfg)

for _, secretName := range dockerCfg.Values {
dockerConfigAuthMap, err := authsFromDockerCfg(secretName)
if err != nil {
return err
}
for k, v := range dockerConfigAuthMap {
auth[k] = v
}
}
if dockerConfig != "" {
dockerConfigAuthMap, err := authsFromDockerConfig(dockerConfig)

for _, secretName := range dockerConfig.Values {
dockerConfigAuthMap, err := authsFromDockerConfig(secretName)
if err != nil {
return err
}
Expand Down
20 changes: 19 additions & 1 deletion pkg/credentials/dockercreds/creds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,14 @@ func TestMultipleFlagHandling(t *testing.T) {
t.Fatalf("ioutil.WriteFile(username) = %v", err)
}

blubbDir := credentials.VolumeName("blubb")
if err := os.MkdirAll(blubbDir, os.ModePerm); err != nil {
t.Fatalf("os.MkdirAll(%s) = %v", blubbDir, err)
}
if err := ioutil.WriteFile(filepath.Join(blubbDir, corev1.DockerConfigJsonKey), []byte(`{"auths":{"us.icr.io":{"auth":"fooisblubb"}}}`), 0777); err != nil {
t.Fatalf("ioutil.WriteFile(username) = %v", err)
}

bazDir := credentials.VolumeName("baz")
if err := os.MkdirAll(bazDir, os.ModePerm); err != nil {
t.Fatalf("os.MkdirAll(%s) = %v", bazDir, err)
Expand All @@ -252,12 +260,22 @@ func TestMultipleFlagHandling(t *testing.T) {
t.Fatalf("ioutil.WriteFile(username) = %v", err)
}

blaDir := credentials.VolumeName("bla")
if err := os.MkdirAll(blaDir, os.ModePerm); err != nil {
t.Fatalf("os.MkdirAll(%s) = %v", blaDir, err)
}
if err := ioutil.WriteFile(filepath.Join(blaDir, corev1.DockerConfigKey), []byte(`{"de.icr.io":{"auth":"fooisbla"}}`), 0777); err != nil {
t.Fatalf("ioutil.WriteFile(username) = %v", err)
}

fs := flag.NewFlagSet("test", flag.ContinueOnError)
AddFlags(fs)
err := fs.Parse([]string{
"-basic-docker=foo=https://us.gcr.io",
"-docker-config=bar",
"-docker-config=blubb",
"-docker-cfg=baz",
"-docker-cfg=bla",
})
if err != nil {
t.Fatalf("flag.CommandLine.Parse() = %v", err)
Expand All @@ -274,7 +292,7 @@ func TestMultipleFlagHandling(t *testing.T) {
}

// Note: "auth" is base64(username + ":" + password)
expected := `{"auths":{"https://index.docker.io/v1":{"auth":"fooisbar"},"https://my.registry/v1":{"auth":"fooisbaz"},"https://us.gcr.io":{"username":"bar","password":"baz","auth":"YmFyOmJheg==","email":"not@val.id"}}}`
expected := `{"auths":{"de.icr.io":{"auth":"fooisbla"},"https://index.docker.io/v1":{"auth":"fooisbar"},"https://my.registry/v1":{"auth":"fooisbaz"},"https://us.gcr.io":{"username":"bar","password":"baz","auth":"YmFyOmJheg==","email":"not@val.id"},"us.icr.io":{"auth":"fooisblubb"}}}`
if string(b) != expected {
t.Errorf("got: %v, wanted: %v", string(b), expected)
}
Expand Down