Skip to content

Commit f202de1

Browse files
Merge pull request drone-plugins#449 from Aishwarya-Lad/CI-13422
Change docker login method, from config writing to login func
2 parents 531cc5c + 60929c7 commit f202de1

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

docker.go

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -158,34 +158,32 @@ func (p Plugin) Exec() error {
158158
os.MkdirAll(dockerHome, 0600)
159159

160160
path := filepath.Join(dockerHome, "config.json")
161-
file, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
161+
err := os.WriteFile(path, []byte(p.Login.Config), 0600)
162162
if err != nil {
163163
return fmt.Errorf("Error writing config.json: %s", err)
164164
}
165-
err = os.WriteFile(path, []byte(p.Login.Config), 0600)
166-
if err != nil {
167-
return fmt.Errorf("Error writing config.json: %s", err)
168-
}
169-
file.Close()
170165
}
171166

172-
// add base image docker credentials to the existing config file, else create new
167+
// instead of writing to config file directly, using docker's login func
168+
// is better to integrate with various credential helpers,
169+
// it also handles different registry specific logic in a better way,
170+
// as opposed to config write where different registries need to be addressed differently.
171+
// It handles any changes in the authentication process across different Docker versions.
172+
173173
if p.BaseImagePassword != "" {
174-
json, err := setDockerAuth(p.Login.Username, p.Login.Password, p.Login.Registry,
175-
p.BaseImageUsername, p.BaseImagePassword, p.BaseImageRegistry)
176-
if err != nil {
177-
return fmt.Errorf("Failed to set authentication in docker config %s", err)
178-
}
179-
os.MkdirAll(dockerHome, 0600)
180-
path := filepath.Join(dockerHome, "config.json")
181-
file, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
182-
if err != nil {
183-
return fmt.Errorf("Error opening config.json: %s", err)
184-
}
185-
defer file.Close()
186-
_, err = file.Write(json)
174+
var baseConnectorLogin Login
175+
baseConnectorLogin.Registry = p.BaseImageRegistry
176+
baseConnectorLogin.Username = p.BaseImageUsername
177+
baseConnectorLogin.Password = p.BaseImagePassword
178+
179+
cmd := commandLogin(baseConnectorLogin)
180+
181+
raw, err := cmd.CombinedOutput()
187182
if err != nil {
188-
return fmt.Errorf("Error writing config.json: %s", err)
183+
out := string(raw)
184+
out = strings.Replace(out, "WARNING! Using --password via the CLI is insecure. Use --password-stdin.", "", -1)
185+
fmt.Println(out)
186+
return fmt.Errorf("Error authenticating base connector: exit status 1")
189187
}
190188
}
191189

0 commit comments

Comments
 (0)