Skip to content

Commit e9b38c9

Browse files
committed
update docker config
1 parent 292ebe0 commit e9b38c9

File tree

4 files changed

+193
-9
lines changed

4 files changed

+193
-9
lines changed

cmd/drone-docker/main.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,21 @@ func main() {
222222
Usage: "docker password",
223223
EnvVar: "PLUGIN_PASSWORD,DOCKER_PASSWORD",
224224
},
225+
cli.StringFlag{
226+
Name: "docker.baseimageusername",
227+
Usage: "Docker username for base image registry",
228+
EnvVar: "PLUGIN_DOCKER_USERNAME,PLUGIN_BASE_IMAGE_USERNAME",
229+
},
230+
cli.StringFlag{
231+
Name: "docker.baseimagepassword",
232+
Usage: "Docker password for base image registry",
233+
EnvVar: "PLUGIN_DOCKER_PASSWORD,PLUGIN_BASE_IMAGE_PASSWORD",
234+
},
235+
cli.StringFlag{
236+
Name: "docker.baseimageregistry",
237+
Usage: "Docker registry for base image registry",
238+
EnvVar: "PLUGIN_DOCKER_REGISTRY,PLUGIN_BASE_IMAGE_REGISTRY",
239+
},
225240
cli.StringFlag{
226241
Name: "docker.email",
227242
Usage: "docker email",
@@ -367,6 +382,9 @@ func run(c *cli.Context) error {
367382
Experimental: c.Bool("daemon.experimental"),
368383
RegistryType: registryType,
369384
},
385+
BaseImageRegistry: c.String("docker.baseimageregistry"),
386+
BaseImageUsername: c.String("docker.baseimageusername"),
387+
BaseImagePassword: c.String("docker.baseimagepassword"),
370388
}
371389

372390
if c.Bool("tags.auto") {

cmd/drone-gar/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ 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
3033
}
3134

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

104109
os.Setenv("PLUGIN_REPO", config.Repo)
105110
os.Setenv("PLUGIN_REGISTRY", config.Registry)
106111

107112
// invoke the base docker plugin binary
108113
cmd := exec.Command(docker.GetDroneDockerExecCmd())
114+
fmt.Println(" Aishwarya config.json is 2.." )
109115
cmd.Stdout = os.Stdout
110116
cmd.Stderr = os.Stderr
111117
err := cmd.Run()
112118
if err != nil {
113119
logrus.Fatal(err)
114120
}
121+
fmt.Println(" Aishwarya config.json is 4.." )
115122
}
116123

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

docker.go

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package docker
22

33
import (
4-
"errors"
54
"fmt"
5+
"log"
66
"os"
77
"os/exec"
88
"path/filepath"
99
"runtime"
1010
"strings"
1111
"time"
1212

13+
"github.com/drone-plugins/drone-docker/internal/docker"
1314
"github.com/drone-plugins/drone-plugin-lib/drone"
15+
"github.com/pkg/errors"
16+
1417
)
1518

1619
type (
@@ -75,13 +78,16 @@ type (
7578

7679
// Plugin defines the Docker plugin parameters.
7780
Plugin struct {
78-
Login Login // Docker login configuration
79-
Build Build // Docker build configuration
80-
Daemon Daemon // Docker daemon configuration
81-
Dryrun bool // Docker push is skipped
82-
Cleanup bool // Docker purge is enabled
83-
CardPath string // Card path to write file to
84-
ArtifactFile string // Artifact path to write file to
81+
Login Login // Docker login configuration
82+
Build Build // Docker build configuration
83+
Daemon Daemon // Docker daemon configuration
84+
Dryrun bool // Docker push is skipped
85+
Cleanup bool // Docker purge is enabled
86+
CardPath string // Card path to write file to
87+
ArtifactFile string // Artifact path to write file to
88+
BaseImageRegistry string // Docker registry to pull base image
89+
BaseImageUsername string // Docker registry username to pull base image
90+
BaseImagePassword string // Docker registry password to pull base image
8591
}
8692

8793
Card []struct {
@@ -154,11 +160,42 @@ func (p Plugin) Exec() error {
154160
os.MkdirAll(dockerHome, 0600)
155161

156162
path := filepath.Join(dockerHome, "config.json")
157-
err := os.WriteFile(path, []byte(p.Login.Config), 0600)
163+
file, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
164+
if err != nil {
165+
return fmt.Errorf("Error writing config.json: %s", err)
166+
}
167+
_, err = file.Write([]byte(p.Login.Config))
168+
fmt.Println("Writing p.Login.Config: %s", p.Login.Config)
169+
158170
if err != nil {
159171
return fmt.Errorf("Error writing config.json: %s", err)
160172
}
173+
defer file.Close()
174+
}
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 != "" {
178+
json, err := setDockerAuth(p.Login.Username, p.Login.Password, p.Login.Registry,
179+
p.BaseImageUsername, p.BaseImagePassword, p.BaseImageRegistry)
180+
fmt.Println("json after set Auth: %s", json)
181+
if err != nil {
182+
return errors.Wrap(err, "Failed to set authentication in docker config")
183+
}
184+
if json != nil {
185+
path := filepath.Join(dockerHome, "config.json")
186+
file, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
187+
if err != nil {
188+
return fmt.Errorf("Error opening config.json: %s", err)
189+
}
190+
defer file.Close()
191+
_, err = file.Write(json)
192+
if err != nil {
193+
return fmt.Errorf("Error writing config.json: %s", err)
194+
}
195+
}
161196
}
197+
fmt.Println("json after set Auth: %s", json)
198+
162199

163200
// login to the Docker registry
164201
if p.Login.Password != "" {
@@ -270,6 +307,32 @@ func (p Plugin) Exec() error {
270307
return nil
271308
}
272309

310+
// helper function to set the credentials
311+
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,
318+
}
319+
// push registry auth
320+
//credentials := []docker.RegistryCredentials{pushToRegistryCreds}
321+
credentials := []docker.RegistryCredentials{}
322+
323+
if baseImageRegistry != "" {
324+
pullFromRegistryCreds := docker.RegistryCredentials{
325+
Registry: baseImageRegistry,
326+
Username: baseImageUsername,
327+
Password: baseImagePassword,
328+
}
329+
// base image registry auth
330+
credentials = append(credentials, pullFromRegistryCreds)
331+
}
332+
// Creates docker config for both the registries used for authentication
333+
return dockerConfig.CreateDockerConfigJson(credentials)
334+
}
335+
273336
// helper function to create the docker login command.
274337
func commandLogin(login Login) *exec.Cmd {
275338
if login.Email != "" {
@@ -504,6 +567,7 @@ func commandPush(build Build, tag string) *exec.Cmd {
504567

505568
// helper function to create the docker daemon command.
506569
func commandDaemon(daemon Daemon) *exec.Cmd {
570+
fmt.Println(" Aishwarya config.json is 5.." )
507571
args := []string{
508572
"--data-root", daemon.StoragePath,
509573
"--host=unix:///var/run/docker.sock",
@@ -585,6 +649,8 @@ func trace(cmd *exec.Cmd) {
585649
}
586650

587651
func GetDroneDockerExecCmd() string {
652+
fmt.Println(" Aishwarya config.json is 3.." )
653+
588654
if runtime.GOOS == "windows" {
589655
return "C:/bin/drone-docker.exe"
590656
}

internal/docker/config.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package docker
2+
3+
import (
4+
"encoding/base64"
5+
"encoding/json"
6+
"fmt"
7+
"io/ioutil"
8+
"log"
9+
"os"
10+
11+
"github.com/pkg/errors"
12+
)
13+
14+
const (
15+
v2HubRegistryURL string = "https://registry.hub.docker.com/v2/"
16+
v1RegistryURL string = "https://index.docker.io/v1/" // Default registry
17+
v2RegistryURL string = "https://index.docker.io/v2/" // v2 registry is not supported
18+
)
19+
20+
type (
21+
Auth struct {
22+
Auth string `json:"auth"`
23+
}
24+
25+
Config struct {
26+
Auths map[string]Auth `json:"auths"`
27+
CredHelpers map[string]string `json:"credHelpers,omitempty"`
28+
}
29+
)
30+
31+
type RegistryCredentials struct {
32+
Registry string
33+
Username string
34+
Password string
35+
}
36+
37+
func NewConfig() *Config {
38+
return &Config{
39+
Auths: make(map[string]Auth),
40+
CredHelpers: make(map[string]string),
41+
}
42+
}
43+
44+
func (c *Config) SetAuth(registry, username, password string) {
45+
authBytes := []byte(username + ":" + password)
46+
encodedString := base64.StdEncoding.EncodeToString(authBytes)
47+
log.Printf("auth : %s", encodedString)
48+
c.Auths[registry] = Auth{Auth: encodedString}
49+
}
50+
51+
func (c *Config) SetCredHelper(registry, helper string) {
52+
c.CredHelpers[registry] = helper
53+
}
54+
55+
func (c *Config) CreateDockerConfigJson(credentials []RegistryCredentials) ([]byte, error) {
56+
for _, cred := range credentials {
57+
if cred.Registry != "" {
58+
59+
if cred.Username == "" {
60+
return nil, fmt.Errorf("Username must be specified for registry: %s", cred.Registry)
61+
}
62+
if cred.Password == "" {
63+
return nil, fmt.Errorf("Password must be specified for registry: %s", cred.Registry)
64+
}
65+
c.SetAuth(cred.Registry, cred.Username, cred.Password)
66+
}
67+
}
68+
69+
jsonBytes, err := json.Marshal(c)
70+
log.Printf("jsonBytes config : %s", jsonBytes)
71+
if err != nil {
72+
return nil, errors.Wrap(err, "failed to serialize docker config json")
73+
}
74+
75+
return jsonBytes, nil
76+
}
77+
78+
func WriteDockerConfig(data []byte, path string) (string error) {
79+
err := os.MkdirAll(path, 0600)
80+
if err != nil {
81+
if !os.IsExist(err) {
82+
return errors.Wrap(err, fmt.Sprintf("failed to create %s directory", path))
83+
}
84+
}
85+
86+
filePath := path + "/config.json"
87+
log.Printf("Config data is %s", data)
88+
err = ioutil.WriteFile(filePath, data, 0644)
89+
if err != nil {
90+
return errors.Wrap(err, fmt.Sprintf("failed to create docker config file at %s", path))
91+
}
92+
return nil
93+
}

0 commit comments

Comments
 (0)