forked from git-lfs/git-lfs
/
ssh.go
54 lines (44 loc) · 1.09 KB
/
ssh.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
51
52
53
54
package lfs
import (
"encoding/json"
"github.com/rubyist/tracerx"
"net/http"
"os/exec"
)
type sshAuthResponse struct {
Message string `json:"-"`
Header map[string]string `json:"header"`
ExpiresAt string `json:"expires_at"`
}
func mergeSshHeader(header http.Header, endpoint Endpoint, operation, oid string) error {
if len(endpoint.SshUserAndHost) == 0 {
return nil
}
res, err := sshAuthenticate(endpoint, operation, oid)
if err != nil {
return err
}
if res.Header != nil {
for key, value := range res.Header {
header.Set(key, value)
}
}
return nil
}
func sshAuthenticate(endpoint Endpoint, operation, oid string) (sshAuthResponse, error) {
tracerx.Printf("ssh: %s git-lfs-authenticate %s %s %s",
endpoint.SshUserAndHost, endpoint.SshPath, operation, oid)
cmd := exec.Command("ssh", endpoint.SshUserAndHost,
"git-lfs-authenticate",
endpoint.SshPath,
operation, oid,
)
out, err := cmd.CombinedOutput()
res := sshAuthResponse{}
if err != nil {
res.Message = string(out)
} else {
err = json.Unmarshal(out, &res)
}
return res, err
}