forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sshkey.go
44 lines (38 loc) · 1.16 KB
/
sshkey.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package scmauth
import (
"io/ioutil"
"path/filepath"
)
const SSHPrivateKeyMethodName = "ssh-privatekey"
// SSHPrivateKey implements SCMAuth interface for using SSH private keys.
type SSHPrivateKey struct{}
// Setup creates a wrapper script for SSH command to be able to use the provided
// SSH key while accessing private repository.
func (_ SSHPrivateKey) Setup(baseDir string, context SCMAuthContext) error {
script, err := ioutil.TempFile("", "gitssh")
if err != nil {
return err
}
defer script.Close()
if err := script.Chmod(0711); err != nil {
return err
}
if _, err := script.WriteString("#!/bin/sh\nssh -i " +
filepath.Join(baseDir, SSHPrivateKeyMethodName) +
" -o StrictHostKeyChecking=false \"$@\"\n"); err != nil {
return err
}
// set environment variable to tell git to use the SSH wrapper
if err := context.Set("GIT_SSH", script.Name()); err != nil {
return err
}
return nil
}
// Name returns the name of this auth method.
func (_ SSHPrivateKey) Name() string {
return SSHPrivateKeyMethodName
}
// Handles returns true if the file is an SSH private key
func (_ SSHPrivateKey) Handles(name string) bool {
return name == SSHPrivateKeyMethodName
}