Skip to content

Commit ced9875

Browse files
committed
add only docker registry auths to docker config
1 parent e9b38c9 commit ced9875

File tree

4 files changed

+79
-30
lines changed

4 files changed

+79
-30
lines changed

cmd/drone-gar/main.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ type Config struct {
2727
WorkloadIdentity bool
2828
Username string
2929
AccessToken string
30-
BaseImageRegistry string // Docker registry to pull base image
31-
BaseImageUsername string // Docker registry username to pull base image
32-
BaseImagePassword string // Docker registry password to pull base image
3330
}
3431

3532
type staticTokenSource struct {
@@ -103,22 +100,18 @@ func main() {
103100
os.Setenv("DOCKER_USERNAME", config.Username)
104101
os.Setenv("DOCKER_PASSWORD", config.Password)
105102
}
106-
//data, err := ioutil.ReadFile("/.docker/config.json")
107-
fmt.Println(" Aishwarya config.json is 1.." )
108103

109104
os.Setenv("PLUGIN_REPO", config.Repo)
110105
os.Setenv("PLUGIN_REGISTRY", config.Registry)
111106

112107
// invoke the base docker plugin binary
113108
cmd := exec.Command(docker.GetDroneDockerExecCmd())
114-
fmt.Println(" Aishwarya config.json is 2.." )
115109
cmd.Stdout = os.Stdout
116110
cmd.Stderr = os.Stderr
117111
err := cmd.Run()
118112
if err != nil {
119113
logrus.Fatal(err)
120114
}
121-
fmt.Println(" Aishwarya config.json is 4.." )
122115
}
123116

124117
func getOauthToken(data []byte) (s string) {

docker.go

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package docker
22

33
import (
44
"fmt"
5-
"log"
65
"os"
76
"os/exec"
87
"path/filepath"
@@ -13,7 +12,6 @@ import (
1312
"github.com/drone-plugins/drone-docker/internal/docker"
1413
"github.com/drone-plugins/drone-plugin-lib/drone"
1514
"github.com/pkg/errors"
16-
1715
)
1816

1917
type (
@@ -172,12 +170,10 @@ func (p Plugin) Exec() error {
172170
}
173171
defer file.Close()
174172
}
175-
log.Printf("p.Login.Config .... %s", p.Login.Config)
176-
// add docker credentials to the existing config file, else create new
177-
if p.Login.Password != "" && p.BaseImagePassword != "" {
173+
// add base image docker credentials to the existing config file, else create new
174+
if p.BaseImagePassword != "" {
178175
json, err := setDockerAuth(p.Login.Username, p.Login.Password, p.Login.Registry,
179176
p.BaseImageUsername, p.BaseImagePassword, p.BaseImageRegistry)
180-
fmt.Println("json after set Auth: %s", json)
181177
if err != nil {
182178
return errors.Wrap(err, "Failed to set authentication in docker config")
183179
}
@@ -194,8 +190,6 @@ func (p Plugin) Exec() error {
194190
}
195191
}
196192
}
197-
fmt.Println("json after set Auth: %s", json)
198-
199193

200194
// login to the Docker registry
201195
if p.Login.Password != "" {
@@ -309,16 +303,19 @@ func (p Plugin) Exec() error {
309303

310304
// helper function to set the credentials
311305
func setDockerAuth(username, password, registry, baseImageUsername,
312-
baseImagePassword, baseImageRegistry string) ([]byte, error) {
313-
dockerConfig := docker.NewConfig()
314-
pushToRegistryCreds := docker.RegistryCredentials{
315-
Registry: registry,
316-
Username: username,
317-
Password: password,
306+
baseImagePassword, baseImageRegistry string) ([]byte, error) {
307+
var credentials []docker.RegistryCredentials
308+
// add only docker registry to the config
309+
if password != "" && strings.Contains(registry, "docker") {
310+
dockerConfig := docker.NewConfig()
311+
pushToRegistryCreds := docker.RegistryCredentials{
312+
Registry: registry,
313+
Username: username,
314+
Password: password,
315+
}
316+
// push registry auth
317+
credentials := append(credentials, pushToRegistryCreds)
318318
}
319-
// push registry auth
320-
//credentials := []docker.RegistryCredentials{pushToRegistryCreds}
321-
credentials := []docker.RegistryCredentials{}
322319

323320
if baseImageRegistry != "" {
324321
pullFromRegistryCreds := docker.RegistryCredentials{
@@ -567,7 +564,6 @@ func commandPush(build Build, tag string) *exec.Cmd {
567564

568565
// helper function to create the docker daemon command.
569566
func commandDaemon(daemon Daemon) *exec.Cmd {
570-
fmt.Println(" Aishwarya config.json is 5.." )
571567
args := []string{
572568
"--data-root", daemon.StoragePath,
573569
"--host=unix:///var/run/docker.sock",
@@ -649,8 +645,6 @@ func trace(cmd *exec.Cmd) {
649645
}
650646

651647
func GetDroneDockerExecCmd() string {
652-
fmt.Println(" Aishwarya config.json is 3.." )
653-
654648
if runtime.GOOS == "windows" {
655649
return "C:/bin/drone-docker.exe"
656650
}

internal/docker/config.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/json"
66
"fmt"
77
"io/ioutil"
8-
"log"
98
"os"
109

1110
"github.com/pkg/errors"
@@ -54,7 +53,7 @@ func (c *Config) SetCredHelper(registry, helper string) {
5453

5554
func (c *Config) CreateDockerConfigJson(credentials []RegistryCredentials) ([]byte, error) {
5655
for _, cred := range credentials {
57-
if cred.Registry != "" {
56+
if cred.Registry != "" && strings.Contains(cred.Registry, "docker") {
5857

5958
if cred.Username == "" {
6059
return nil, fmt.Errorf("Username must be specified for registry: %s", cred.Registry)
@@ -67,7 +66,6 @@ func (c *Config) CreateDockerConfigJson(credentials []RegistryCredentials) ([]by
6766
}
6867

6968
jsonBytes, err := json.Marshal(c)
70-
log.Printf("jsonBytes config : %s", jsonBytes)
7169
if err != nil {
7270
return nil, errors.Wrap(err, "failed to serialize docker config json")
7371
}

internal/docker/config_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package docker
2+
3+
import (
4+
"encoding/json"
5+
"io/ioutil"
6+
"os"
7+
"path/filepath"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
const (
14+
RegistryV1 string = "https://index.docker.io/v1/"
15+
RegistryV2 string = "https://index.docker.io/v2/"
16+
RegistryECRPublic string = "public.ecr.aws"
17+
)
18+
19+
func TestConfig(t *testing.T) {
20+
c := NewConfig()
21+
assert.NotNil(t, c.Auths)
22+
assert.NotNil(t, c.CredHelpers)
23+
24+
c.SetAuth(RegistryV1, "test", "password")
25+
expectedAuth := Auth{Auth: "dGVzdDpwYXNzd29yZA=="}
26+
assert.Equal(t, expectedAuth, c.Auths[RegistryV1])
27+
28+
c.SetCredHelper(RegistryECRPublic, "ecr-login")
29+
assert.Equal(t, "ecr-login", c.CredHelpers[RegistryECRPublic])
30+
31+
tempDir, err := ioutil.TempDir("", "docker-config-test")
32+
assert.NoError(t, err)
33+
defer os.RemoveAll(tempDir)
34+
35+
credentials := []RegistryCredentials{
36+
{
37+
Registry: "https://index.docker.io/v1/",
38+
Username: "user1",
39+
Password: "pass1",
40+
},
41+
{
42+
Registry: "gcr.io",
43+
Username: "user2",
44+
Password: "pass2",
45+
},
46+
}
47+
48+
jsonBytes, err := c.CreateDockerConfigJson(credentials)
49+
assert.NoError(t, err)
50+
51+
configPath := filepath.Join(tempDir, "config.json")
52+
err = ioutil.WriteFile(configPath, jsonBytes, 0644)
53+
assert.NoError(t, err)
54+
55+
data, err := ioutil.ReadFile(configPath)
56+
assert.NoError(t, err)
57+
58+
var configFromFile Config
59+
err = json.Unmarshal(data, &configFromFile)
60+
assert.NoError(t, err)
61+
62+
assert.Equal(t, c.Auths, configFromFile.Auths)
63+
assert.Equal(t, c.CredHelpers, configFromFile.CredHelpers)
64+
}

0 commit comments

Comments
 (0)