-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
git.go
183 lines (171 loc) · 5.18 KB
/
git.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package git
import (
"errors"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/config"
"gopkg.in/src-d/go-git.v4/plumbing/transport"
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
ssh2 "gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
)
// GitArtifactDriver is the artifact driver for a git repo
type GitArtifactDriver struct {
Username string
Password string
SSHPrivateKey string
InsecureIgnoreHostKey bool
}
var sshURLRegex = regexp.MustCompile("^(ssh://)?([^/:]*?)@[^@]+$")
func GetUser(url string) string {
matches := sshURLRegex.FindStringSubmatch(url)
if len(matches) > 2 {
return matches[2]
}
// default to `git` user unless username is specified in SSH url
return "git"
}
func (g *GitArtifactDriver) auth(sshUser string) (func(), transport.AuthMethod, []string, error) {
if g.SSHPrivateKey != "" {
signer, err := ssh.ParsePrivateKey([]byte(g.SSHPrivateKey))
if err != nil {
return nil, nil, nil, err
}
privateKeyFile, err := ioutil.TempFile("", "id_rsa.")
if err != nil {
return nil, nil, nil, err
}
err = ioutil.WriteFile(privateKeyFile.Name(), []byte(g.SSHPrivateKey), 0600)
if err != nil {
return nil, nil, nil, err
}
auth := &ssh2.PublicKeys{User: sshUser, Signer: signer}
if g.InsecureIgnoreHostKey {
auth.HostKeyCallback = ssh.InsecureIgnoreHostKey()
}
args := []string{"ssh", "-i", privateKeyFile.Name()}
if g.InsecureIgnoreHostKey {
args = append(args, "-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null")
} else {
args = append(args, "-o", "StrictHostKeyChecking=yes", "-o")
}
env := []string{"GIT_SSH_COMMAND=" + strings.Join(args, " ")}
if g.InsecureIgnoreHostKey {
auth.HostKeyCallback = ssh.InsecureIgnoreHostKey()
env = append(env, "GIT_SSL_NO_VERIFY=true")
}
return func() { _ = os.Remove(privateKeyFile.Name()) },
auth,
env,
nil
}
if g.Username != "" || g.Password != "" {
filename := filepath.Join(os.TempDir(), "git-ask-pass.sh")
_, err := os.Stat(filename)
if os.IsNotExist(err) {
err := ioutil.WriteFile(filename, []byte(`#!/bin/sh
case "$1" in
Username*) echo "${GIT_USERNAME}" ;;
Password*) echo "${GIT_PASSWORD}" ;;
esac
`), 0755)
if err != nil {
return nil, nil, nil, err
}
}
return func() {},
&http.BasicAuth{Username: g.Username, Password: g.Password},
[]string{
"GIT_ASKPASS=" + filename,
"GIT_USERNAME=" + g.Username,
"GIT_PASSWORD=" + g.Password,
},
nil
}
return func() {}, nil, nil, nil
}
// Save is unsupported for git output artifacts
func (g *GitArtifactDriver) Save(string, *wfv1.Artifact) error {
return errors.New("git output artifacts unsupported")
}
func (g *GitArtifactDriver) Load(inputArtifact *wfv1.Artifact, path string) error {
sshUser := GetUser(path)
closer, auth, env, err := g.auth(sshUser)
if err != nil {
return err
}
defer closer()
repo, err := git.PlainClone(path, false, &git.CloneOptions{
URL: inputArtifact.Git.Repo,
RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
Auth: auth,
Depth: inputArtifact.Git.GetDepth(),
})
if err != nil {
return err
}
if inputArtifact.Git.Fetch != nil {
refSpecs := make([]config.RefSpec, len(inputArtifact.Git.Fetch))
for i, spec := range inputArtifact.Git.Fetch {
refSpecs[i] = config.RefSpec(spec)
}
fetchOptions := git.FetchOptions{
Auth: auth,
RefSpecs: refSpecs,
Depth: inputArtifact.Git.GetDepth(),
}
err = fetchOptions.Validate()
if err != nil {
return err
}
err = repo.Fetch(&fetchOptions)
if isAlreadyUpToDateErr(err) {
return err
}
}
if inputArtifact.Git.Revision != "" {
// We still rely on forking git for checkout, since go-git does not have a reliable
// way of resolving revisions (e.g. mybranch, HEAD^, v1.2.3)
rev := getRevisionForCheckout(inputArtifact.Git.Revision)
log.Info("Checking out revision ", rev)
cmd := exec.Command("git", "checkout", rev)
cmd.Dir = path
cmd.Env = env
output, err := cmd.Output()
if err != nil {
return g.error(err, cmd)
}
log.Infof("`%s` stdout:\n%s", cmd.Args, string(output))
submodulesCmd := exec.Command("git", "submodule", "update", "--init", "--recursive", "--force")
submodulesCmd.Dir = path
submodulesCmd.Env = env
submoduleOutput, err := submodulesCmd.Output()
if err != nil {
return g.error(err, cmd)
}
log.Infof("`%s` stdout:\n%s", cmd.Args, string(submoduleOutput))
}
return nil
}
// getRevisionForCheckout trims "refs/heads/" from the revision name (if present)
// so that `git checkout` will succeed.
func getRevisionForCheckout(revision string) string {
return strings.TrimPrefix(revision, "refs/heads/")
}
func isAlreadyUpToDateErr(err error) bool {
return err != nil && err.Error() != "already up-to-date"
}
func (g *GitArtifactDriver) error(err error, cmd *exec.Cmd) error {
if exErr, ok := err.(*exec.ExitError); ok {
log.Errorf("`%s` stderr:\n%s", cmd.Args, string(exErr.Stderr))
return errors.New(strings.Split(string(exErr.Stderr), "\n")[0])
}
return err
}