Skip to content

Commit

Permalink
support agent forwarding
Browse files Browse the repository at this point in the history
missed error check

add debugging

fix empty val for SSHAgent

fix flag type

remove []

debug

base64 encode ssh key

fix

remove debug output

code cleanup

Update docker.go
  • Loading branch information
bkk-bcd authored and TP Honey committed Jan 17, 2023
1 parent 4d92e81 commit 84d8664
Showing 1 changed file with 53 additions and 4 deletions.
57 changes: 53 additions & 4 deletions docker.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package docker

import (
"encoding/base64"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
Expand All @@ -11,6 +13,11 @@ import (
"time"
)

const (
SSHAgentSockPath = "/tmp/drone-ssh-agent-sock"
SSHPrivateKeyFromEnv = "SSH_KEY"
)

type (
// Daemon defines Docker daemon parameters.
Daemon struct {
Expand Down Expand Up @@ -107,6 +114,7 @@ type (

// Exec executes the plugin step
func (p Plugin) Exec() error {

// start the Docker daemon server
if !p.Daemon.Disabled {
p.startDaemon()
Expand Down Expand Up @@ -180,6 +188,13 @@ func (p Plugin) Exec() error {
cmds = append(cmds, commandPull(img))
}

// setup for using ssh agent (https://docs.docker.com/develop/develop-images/build_enhancements/#using-ssh-to-access-private-data-in-builds)

if len(p.Build.SSHAgent) > 0 {
fmt.Printf("ssh agent set to \"%s\"\n", p.Build.SSHAgent)
cmds = append(cmds, commandSSHAgentForwardingSetup(p.Build)...)
}

cmds = append(cmds, commandBuild(p.Build)) // docker build

for _, tag := range p.Build.Tags {
Expand Down Expand Up @@ -329,8 +344,8 @@ func commandBuild(build Build) *exec.Cmd {
if build.Platform != "" {
args = append(args, "--platform", build.Platform)
}
if build.SSHAgent != "" {
args = append(args, "--ssh", build.SSHAgent)
for _, sshagent := range build.SSHAgent {
args = append(args, "--ssh", sshagent)
}

if build.AutoLabel {
Expand All @@ -357,8 +372,8 @@ func commandBuild(build Build) *exec.Cmd {
}
}

// we need to enable buildkit, for secret support
if build.Secret != "" || len(build.SecretEnvs) > 0 || len(build.SecretFiles) > 0 {
// we need to enable buildkit, for secret support and ssh agent support
if build.Secret != "" || len(build.SecretEnvs) > 0 || len(build.SecretFiles) > 0 || len(build.SSHAgent) > 0 {
os.Setenv("DOCKER_BUILDKIT", "1")
}
return exec.Command(dockerExe, args...)
Expand Down Expand Up @@ -511,6 +526,40 @@ func commandRmi(tag string) *exec.Cmd {
return exec.Command(dockerExe, "rmi", tag)
}

func commandSSHAgentForwardingSetup(build Build) []*exec.Cmd {
cmds := make([]*exec.Cmd, 0)
if err := writeSSHPrivateKey(); err != nil {
log.Fatalf("unable to setup ssh agent forwarding: %s", err)
}
os.Setenv("SSH_AUTH_SOCK", SSHAgentSockPath)
cmds = append(cmds, exec.Command("ssh-agent", "-a", SSHAgentSockPath))
cmds = append(cmds, exec.Command("ssh-add"))
return cmds
}

func writeSSHPrivateKey() error {
privateKeyBase64 := os.Getenv(SSHPrivateKeyFromEnv)
if privateKeyBase64 == "" {
return fmt.Errorf("%s must be defined and contain the base64 encoded private key to use for ssh agent forwarding", SSHPrivateKeyFromEnv)
}
var err error
privateKey, err := base64.StdEncoding.DecodeString(privateKeyBase64)
if err != nil {
return fmt.Errorf("unable to base64 decode private key")
}
home, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("unable to determine home directory: %s", err)
}
if err := os.MkdirAll(filepath.Join(home, ".ssh"), 0700); err != nil {
return fmt.Errorf("unable to create .ssh directory: %s", err)
}
if err := os.WriteFile(filepath.Join(home, ".ssh", "id_rsa"), privateKey, 0400); err != nil {
return fmt.Errorf("unable to write ssh key: %s", err)
}
return nil
}

// trace writes each command to stdout with the command wrapped in an xml
// tag so that it can be extracted and displayed in the logs.
func trace(cmd *exec.Cmd) {
Expand Down

0 comments on commit 84d8664

Please sign in to comment.