Skip to content

Commit cc80f38

Browse files
authored
Merge pull request #46 from hashicorp/f-ssh-batch
Set SSH BatchMode=yes when cloning git over ssh
2 parents 8ff1c8c + 3a12002 commit cc80f38

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

get_git.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (g *GitGetter) Get(dst string, u *url.URL) error {
9999
}
100100

101101
// Lastly, download any/all submodules.
102-
return g.fetchSubmodules(dst)
102+
return g.fetchSubmodules(dst, sshKeyFile)
103103
}
104104

105105
// GetFile for Git doesn't support updating at this time. It will download
@@ -141,7 +141,7 @@ func (g *GitGetter) checkout(dst string, ref string) error {
141141

142142
func (g *GitGetter) clone(dst, sshKeyFile string, u *url.URL) error {
143143
cmd := exec.Command("git", "clone", u.String(), dst)
144-
addSSHKeyFile(cmd, sshKeyFile)
144+
setupGitEnv(cmd, sshKeyFile)
145145
return getRunCommand(cmd)
146146
}
147147

@@ -165,28 +165,32 @@ func (g *GitGetter) update(dst, sshKeyFile, ref string) error {
165165

166166
cmd = exec.Command("git", "pull", "--ff-only")
167167
cmd.Dir = dst
168-
addSSHKeyFile(cmd, sshKeyFile)
168+
setupGitEnv(cmd, sshKeyFile)
169169
return getRunCommand(cmd)
170170
}
171171

172172
// fetchSubmodules downloads any configured submodules recursively.
173-
func (g *GitGetter) fetchSubmodules(dst string) error {
173+
func (g *GitGetter) fetchSubmodules(dst, sshKeyFile string) error {
174174
cmd := exec.Command("git", "submodule", "update", "--init", "--recursive")
175175
cmd.Dir = dst
176+
setupGitEnv(cmd, sshKeyFile)
176177
return getRunCommand(cmd)
177178
}
178179

179-
// addSSHKeyFile sets up the given SSH private key file such that it will
180-
// be used by the "git" command during authentication. This is accomplished
181-
// using a special environment variable, which is set on the provided cmd.
182-
// If the sshKeyFile is empty, this is a noop.
183-
func addSSHKeyFile(cmd *exec.Cmd, sshKeyFile string) {
184-
if sshKeyFile == "" {
185-
return
186-
}
187-
cmd.Env = append(os.Environ(), "GIT_SSH_COMMAND=ssh "+
188-
"-o StrictHostKeyChecking=no "+
189-
"-i "+sshKeyFile)
180+
// setupGitEnv sets up the environment for the given command. This is used to
181+
// pass configuration data to git and ssh and enables advanced cloning methods.
182+
func setupGitEnv(cmd *exec.Cmd, sshKeyFile string) {
183+
var sshOpts []string
184+
185+
if sshKeyFile != "" {
186+
// We have an SSH key temp file configured, tell ssh about this.
187+
sshOpts = append(sshOpts, "-i", sshKeyFile)
188+
}
189+
190+
cmd.Env = append(os.Environ(),
191+
// Set the ssh command to use for clones.
192+
"GIT_SSH_COMMAND=ssh "+strings.Join(sshOpts, " "),
193+
)
190194
}
191195

192196
// checkGitVersion is used to check the version of git installed on the system

get_git_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,18 @@ func TestGitGetter_submodule(t *testing.T) {
294294
}
295295
}
296296

297+
func TestGitGetter_setupGitEnv_sshKey(t *testing.T) {
298+
cmd := exec.Command("/bin/sh", "-c", "echo -n $GIT_SSH_COMMAND")
299+
setupGitEnv(cmd, "/tmp/foo.pem")
300+
out, err := cmd.Output()
301+
if err != nil {
302+
t.Fatal(err)
303+
}
304+
if string(out) != "ssh -i /tmp/foo.pem" {
305+
t.Fatalf("unexpected GIT_SSH_COMMAND: %q", string(out))
306+
}
307+
}
308+
297309
// gitRepo is a helper struct which controls a single temp git repo.
298310
type gitRepo struct {
299311
t *testing.T

0 commit comments

Comments
 (0)