From 18c4e995d32b6a1fc7a6db3aaa2e623af2879fd5 Mon Sep 17 00:00:00 2001 From: Andrea Cervesato Date: Tue, 24 Mar 2020 19:20:52 +0100 Subject: [PATCH 1/3] Added AuthConfig to login to multiple registry --- cmd/drone-docker/main.go | 14 ++++++++++---- docker.go | 40 +++++++++++++++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/cmd/drone-docker/main.go b/cmd/drone-docker/main.go index 946d994..2cbb5ce 100644 --- a/cmd/drone-docker/main.go +++ b/cmd/drone-docker/main.go @@ -208,6 +208,11 @@ func main() { Usage: "docker email", EnvVar: "PLUGIN_EMAIL,DOCKER_EMAIL", }, + cli.StringFlag{ + Name: "docker.authconfig", + Usage: "docker json authconfig content", + EnvVar: "PLUGIN_AUTHCONFIG,DOCKER_AUTHCONFIG", + }, cli.BoolTFlag{ Name: "docker.purge", Usage: "docker should cleanup images", @@ -240,10 +245,11 @@ func run(c *cli.Context) error { Dryrun: c.Bool("dry-run"), Cleanup: c.BoolT("docker.purge"), Login: docker.Login{ - Registry: c.String("docker.registry"), - Username: c.String("docker.username"), - Password: c.String("docker.password"), - Email: c.String("docker.email"), + Registry: c.String("docker.registry"), + Username: c.String("docker.username"), + Password: c.String("docker.password"), + Email: c.String("docker.email"), + AuthConfig: c.String("docker.authconfig"), }, Build: docker.Build{ Remote: c.String("remote.url"), diff --git a/docker.go b/docker.go index be23616..6209650 100644 --- a/docker.go +++ b/docker.go @@ -28,10 +28,11 @@ type ( // Login defines Docker login parameters. Login struct { - Registry string // Docker registry address - Username string // Docker registry username - Password string // Docker registry password - Email string // Docker registry email + Registry string // Docker registry address + Username string // Docker registry username + Password string // Docker registry password + Email string // Docker registry email + AuthConfig string // Docker Auth Config } // Build defines Docker build parameters. @@ -83,6 +84,35 @@ func (p Plugin) Exec() error { time.Sleep(time.Second * 1) } + // Create Auth Config File + if p.Login.AuthConfig != "" { + fmt.Println("AuthConfig provided.") + err_mkdir := os.MkdirAll("/root/.docker", 0600) + if err_mkdir != nil { + fmt.Println("Error creating root's docker auth config directory: ") + fmt.Println(err_mkdir) + } + cfile, err_create := os.Create("/root/.docker/config.json") + if err_create != nil { + fmt.Println("Error creating root's docker auth config file: ") + fmt.Println(err_create) + cfile.Close() + } + err_chmod := os.Chmod("/root/.docker/config.json", 0600) + if err_chmod != nil { + fmt.Println("Error setting permissions on root's docker auth config file: ") + fmt.Println(err_chmod) + } + _, err_str := cfile.WriteString(p.Login.AuthConfig) + if err_str != nil { + fmt.Println("Error filling root's docker auth config file: ") + fmt.Println(err_str) + } else { + cfile.Close() + } + + } + // login to the Docker registry if p.Login.Password != "" { cmd := commandLogin(p.Login) @@ -93,7 +123,7 @@ func (p Plugin) Exec() error { } else { fmt.Println("Registry credentials not provided. Guest mode enabled.") } - + if p.Build.Squash && !p.Daemon.Experimental { fmt.Println("Squash build flag is only available when Docker deamon is started with experimental flag. Ignoring...") p.Build.Squash = false From 063f479004ee69e933ed05901adda1bb4cdb03e2 Mon Sep 17 00:00:00 2001 From: Andrea Cervesato Date: Tue, 24 Mar 2020 21:00:24 +0100 Subject: [PATCH 2/3] Using more generic configuration & respecting win --- cmd/drone-docker/main.go | 8 ++++---- daemon.go | 1 + daemon_win.go | 1 + docker.go | 38 ++++++++++++++------------------------ 4 files changed, 20 insertions(+), 28 deletions(-) diff --git a/cmd/drone-docker/main.go b/cmd/drone-docker/main.go index 2cbb5ce..e720fe7 100644 --- a/cmd/drone-docker/main.go +++ b/cmd/drone-docker/main.go @@ -209,9 +209,9 @@ func main() { EnvVar: "PLUGIN_EMAIL,DOCKER_EMAIL", }, cli.StringFlag{ - Name: "docker.authconfig", - Usage: "docker json authconfig content", - EnvVar: "PLUGIN_AUTHCONFIG,DOCKER_AUTHCONFIG", + Name: "docker.docker_config", + Usage: "docker json dockerconfig content", + EnvVar: "PLUGIN_DOCKER_CONFIG,DOCKER_CONFIG", }, cli.BoolTFlag{ Name: "docker.purge", @@ -249,7 +249,7 @@ func run(c *cli.Context) error { Username: c.String("docker.username"), Password: c.String("docker.password"), Email: c.String("docker.email"), - AuthConfig: c.String("docker.authconfig"), + DockerConfig: c.String("docker.docker_config"), }, Build: docker.Build{ Remote: c.String("remote.url"), diff --git a/daemon.go b/daemon.go index 5bf1119..09234ff 100644 --- a/daemon.go +++ b/daemon.go @@ -9,6 +9,7 @@ import ( const dockerExe = "/usr/local/bin/docker" const dockerdExe = "/usr/local/bin/dockerd" +const dockerrootconfdir = "/root/.docker/" func (p Plugin) startDaemon() { cmd := commandDaemon(p.Daemon) diff --git a/daemon_win.go b/daemon_win.go index 062cf5e..b435f5b 100644 --- a/daemon_win.go +++ b/daemon_win.go @@ -4,6 +4,7 @@ package docker const dockerExe = "C:\\bin\\docker.exe" const dockerdExe = "" +const dockerrootconfdir = "C:\\ProgramData\\docker\" func (p Plugin) startDaemon() { // this is a no-op on windows diff --git a/docker.go b/docker.go index 6209650..d472f10 100644 --- a/docker.go +++ b/docker.go @@ -6,6 +6,7 @@ import ( "os/exec" "strings" "time" + "io/ioutil" ) type ( @@ -32,7 +33,7 @@ type ( Username string // Docker registry username Password string // Docker registry password Email string // Docker registry email - AuthConfig string // Docker Auth Config + DockerConfig string // Docker Auth Config } // Build defines Docker build parameters. @@ -85,32 +86,19 @@ func (p Plugin) Exec() error { } // Create Auth Config File - if p.Login.AuthConfig != "" { - fmt.Println("AuthConfig provided.") - err_mkdir := os.MkdirAll("/root/.docker", 0600) + if p.Login.DockerConfig != "" { + fmt.Println("DockerConfig provided.") + err_mkdir := os.MkdirAll(dockerrootconfdir, 0600) if err_mkdir != nil { fmt.Println("Error creating root's docker auth config directory: ") fmt.Println(err_mkdir) } - cfile, err_create := os.Create("/root/.docker/config.json") - if err_create != nil { - fmt.Println("Error creating root's docker auth config file: ") - fmt.Println(err_create) - cfile.Close() + conffile := fmt.Sprintf("%s%s", dockerrootconfdir, "config.json") + err_mkconf := ioutil.WriteFile(conffile, []byte(p.Login.DockerConfig), 0600) + if err_mkconf != nil { + fmt.Println("Error creating root's docker auth configuration: ") + fmt.Println(err_mkconf) } - err_chmod := os.Chmod("/root/.docker/config.json", 0600) - if err_chmod != nil { - fmt.Println("Error setting permissions on root's docker auth config file: ") - fmt.Println(err_chmod) - } - _, err_str := cfile.WriteString(p.Login.AuthConfig) - if err_str != nil { - fmt.Println("Error filling root's docker auth config file: ") - fmt.Println(err_str) - } else { - cfile.Close() - } - } // login to the Docker registry @@ -120,8 +108,10 @@ func (p Plugin) Exec() error { if err != nil { return fmt.Errorf("Error authenticating: %s", err) } - } else { - fmt.Println("Registry credentials not provided. Guest mode enabled.") + } + + if p.Login.Password != "" && p.Login.DockerConfig != "" { + fmt.Println("Registry credentials or Docker config not provided. Guest mode enabled.") } if p.Build.Squash && !p.Daemon.Experimental { From 122443b3e68295a9a8786b9f72fe0921407c431e Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Tue, 24 Mar 2020 13:40:35 -0700 Subject: [PATCH 3/3] minor variable name change --- cmd/drone-docker/main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/drone-docker/main.go b/cmd/drone-docker/main.go index e720fe7..98fc000 100644 --- a/cmd/drone-docker/main.go +++ b/cmd/drone-docker/main.go @@ -209,9 +209,9 @@ func main() { EnvVar: "PLUGIN_EMAIL,DOCKER_EMAIL", }, cli.StringFlag{ - Name: "docker.docker_config", + Name: "docker.config", Usage: "docker json dockerconfig content", - EnvVar: "PLUGIN_DOCKER_CONFIG,DOCKER_CONFIG", + EnvVar: "PLUGIN_CONFIG", }, cli.BoolTFlag{ Name: "docker.purge", @@ -249,7 +249,7 @@ func run(c *cli.Context) error { Username: c.String("docker.username"), Password: c.String("docker.password"), Email: c.String("docker.email"), - DockerConfig: c.String("docker.docker_config"), + DockerConfig: c.String("docker.config"), }, Build: docker.Build{ Remote: c.String("remote.url"),