forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sshkey.go
50 lines (41 loc) · 1.27 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
45
46
47
48
49
50
package scmauth
import (
"io/ioutil"
"path/filepath"
"github.com/golang/glog"
)
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
}
content := "#!/bin/sh\nssh -i " +
filepath.Join(baseDir, SSHPrivateKeyMethodName) +
" -o StrictHostKeyChecking=false \"$@\"\n"
glog.V(5).Infof("Adding Private SSH Auth:\n%s\n", content)
if _, err := script.WriteString(content); 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
}