1
1
package docker
2
2
3
3
import (
4
- "encoding/base64"
5
4
"fmt"
6
- "io/ioutil"
7
- "log"
8
5
"os"
9
6
"os/exec"
10
7
"path/filepath"
@@ -13,11 +10,6 @@ import (
13
10
"time"
14
11
)
15
12
16
- const (
17
- SSHAgentSockPath = "/tmp/drone-ssh-agent-sock"
18
- SSHPrivateKeyFromEnv = "SSH_KEY"
19
- )
20
-
21
13
type (
22
14
// Daemon defines Docker daemon parameters.
23
15
Daemon struct {
71
63
AddHost []string // Docker build add-host
72
64
Quiet bool // Docker build quiet
73
65
Platform string // Docker build platform
74
- SSHAgent []string // Docker build ssh
66
+ SSHAgentKey string // Docker build ssh agent key
67
+ SSHKeyPath string // Docker build ssh key path
75
68
}
76
69
77
70
// Plugin defines the Docker plugin parameters.
@@ -153,7 +146,7 @@ func (p Plugin) Exec() error {
153
146
os .MkdirAll (dockerHome , 0600 )
154
147
155
148
path := filepath .Join (dockerHome , "config.json" )
156
- err := ioutil .WriteFile (path , []byte (p .Login .Config ), 0600 )
149
+ err := os .WriteFile (path , []byte (p .Login .Config ), 0600 )
157
150
if err != nil {
158
151
return fmt .Errorf ("Error writing config.json: %s" , err )
159
152
}
@@ -189,10 +182,12 @@ func (p Plugin) Exec() error {
189
182
}
190
183
191
184
// setup for using ssh agent (https://docs.docker.com/develop/develop-images/build_enhancements/#using-ssh-to-access-private-data-in-builds)
192
-
193
- if len (p .Build .SSHAgent ) > 0 {
194
- fmt .Printf ("ssh agent set to \" %s\" \n " , p .Build .SSHAgent )
195
- cmds = append (cmds , commandSSHAgentForwardingSetup (p .Build )... )
185
+ if p .Build .SSHAgentKey != "" {
186
+ var sshErr error
187
+ p .Build .SSHKeyPath , sshErr = writeSSHPrivateKey (p .Build .SSHAgentKey )
188
+ if sshErr != nil {
189
+ return sshErr
190
+ }
196
191
}
197
192
198
193
cmds = append (cmds , commandBuild (p .Build )) // docker build
@@ -344,8 +339,8 @@ func commandBuild(build Build) *exec.Cmd {
344
339
if build .Platform != "" {
345
340
args = append (args , "--platform" , build .Platform )
346
341
}
347
- for _ , sshagent := range build . SSHAgent {
348
- args = append (args , "--ssh" , sshagent )
342
+ if build . SSHKeyPath != "" {
343
+ args = append (args , "--ssh" , build . SSHKeyPath )
349
344
}
350
345
351
346
if build .AutoLabel {
@@ -373,7 +368,7 @@ func commandBuild(build Build) *exec.Cmd {
373
368
}
374
369
375
370
// we need to enable buildkit, for secret support and ssh agent support
376
- if build .Secret != "" || len (build .SecretEnvs ) > 0 || len (build .SecretFiles ) > 0 || len ( build .SSHAgent ) > 0 {
371
+ if build .Secret != "" || len (build .SecretEnvs ) > 0 || len (build .SecretFiles ) > 0 || build .SSHAgentKey != "" {
377
372
os .Setenv ("DOCKER_BUILDKIT" , "1" )
378
373
}
379
374
return exec .Command (dockerExe , args ... )
@@ -526,38 +521,21 @@ func commandRmi(tag string) *exec.Cmd {
526
521
return exec .Command (dockerExe , "rmi" , tag )
527
522
}
528
523
529
- func commandSSHAgentForwardingSetup (build Build ) []* exec.Cmd {
530
- cmds := make ([]* exec.Cmd , 0 )
531
- if err := writeSSHPrivateKey (); err != nil {
532
- log .Fatalf ("unable to setup ssh agent forwarding: %s" , err )
533
- }
534
- os .Setenv ("SSH_AUTH_SOCK" , SSHAgentSockPath )
535
- cmds = append (cmds , exec .Command ("ssh-agent" , "-a" , SSHAgentSockPath ))
536
- cmds = append (cmds , exec .Command ("ssh-add" ))
537
- return cmds
538
- }
539
-
540
- func writeSSHPrivateKey () error {
541
- privateKeyBase64 := os .Getenv (SSHPrivateKeyFromEnv )
542
- if privateKeyBase64 == "" {
543
- return fmt .Errorf ("%s must be defined and contain the base64 encoded private key to use for ssh agent forwarding" , SSHPrivateKeyFromEnv )
544
- }
545
- var err error
546
- privateKey , err := base64 .StdEncoding .DecodeString (privateKeyBase64 )
547
- if err != nil {
548
- return fmt .Errorf ("unable to base64 decode private key" )
549
- }
524
+ func writeSSHPrivateKey (key string ) (path string , err error ) {
550
525
home , err := os .UserHomeDir ()
551
526
if err != nil {
552
- return fmt .Errorf ("unable to determine home directory: %s" , err )
527
+ return "" , fmt .Errorf ("unable to determine home directory: %s" , err )
553
528
}
554
529
if err := os .MkdirAll (filepath .Join (home , ".ssh" ), 0700 ); err != nil {
555
- return fmt .Errorf ("unable to create .ssh directory: %s" , err )
530
+ return "" , fmt .Errorf ("unable to create .ssh directory: %s" , err )
556
531
}
557
- if err := os .WriteFile (filepath .Join (home , ".ssh" , "id_rsa" ), privateKey , 0400 ); err != nil {
558
- return fmt .Errorf ("unable to write ssh key: %s" , err )
532
+ pathToKey := filepath .Join (home , ".ssh" , "id_rsa" )
533
+ if err := os .WriteFile (pathToKey , []byte (key ), 0400 ); err != nil {
534
+ return "" , fmt .Errorf ("unable to write ssh key %s: %s" , pathToKey , err )
559
535
}
560
- return nil
536
+ path = fmt .Sprintf ("default=%s" , pathToKey )
537
+
538
+ return path , nil
561
539
}
562
540
563
541
// trace writes each command to stdout with the command wrapped in an xml
0 commit comments