-
Notifications
You must be signed in to change notification settings - Fork 32
/
cloner.go
179 lines (156 loc) · 4.28 KB
/
cloner.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
package gogit
import (
"fmt"
"os"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/protocol/packp/capability"
"github.com/go-git/go-git/v5/plumbing/transport"
httpgit "github.com/go-git/go-git/v5/plumbing/transport/http"
gossh "github.com/go-git/go-git/v5/plumbing/transport/ssh"
"github.com/rancher/gitjob/cmd/gitcloner/cmd"
"github.com/sirupsen/logrus"
giturls "github.com/whilp/git-urls"
"golang.org/x/crypto/ssh"
)
const defaultBranch = "master"
var (
plainClone = git.PlainClone
readFile = os.ReadFile
)
type Cloner struct{}
type Options struct {
Repo string
Branch string
Auth transport.AuthMethod
InsecureSkipTLS bool
CABundle []byte
}
func NewCloner() *Cloner {
return &Cloner{}
}
func (c *Cloner) CloneRepo(opts *cmd.Options) error {
// Azure DevOps requires capabilities multi_ack / multi_ack_detailed,
// which are not fully implemented and by default are included in
// transport.UnsupportedCapabilities.
transport.UnsupportedCapabilities = []capability.Capability{
capability.ThinPack,
}
auth, err := createAuthFromOpts(opts)
if err != nil {
return err
}
caBundle, err := getCABundleFromFile(opts.CABundleFile)
if err != nil {
return err
}
if opts.Branch == "" && opts.Revision == "" {
opts.Branch = defaultBranch
return cloneBranch(opts, auth, caBundle)
}
if opts.Branch != "" {
if opts.Revision != "" {
logrus.Warn("Using branch for cloning the repo. Revision will be skipped.")
}
return cloneBranch(opts, auth, caBundle)
}
return cloneRevision(opts, auth, caBundle)
}
func cloneBranch(opts *cmd.Options, auth transport.AuthMethod, caBundle []byte) error {
_, err := plainClone(opts.Path, false, &git.CloneOptions{
URL: opts.Repo,
Auth: auth,
InsecureSkipTLS: opts.InsecureSkipTLS,
CABundle: caBundle,
SingleBranch: true,
ReferenceName: plumbing.ReferenceName(opts.Branch),
})
return err
}
func cloneRevision(opts *cmd.Options, auth transport.AuthMethod, caBundle []byte) error {
r, err := plainClone(opts.Path, false, &git.CloneOptions{
URL: opts.Repo,
Auth: auth,
InsecureSkipTLS: opts.InsecureSkipTLS,
CABundle: caBundle,
})
if err != nil {
return err
}
h, err := r.ResolveRevision(plumbing.Revision(opts.Revision))
if err != nil {
return err
}
w, err := r.Worktree()
if err != nil {
return err
}
return w.Checkout(&git.CheckoutOptions{
Hash: *h,
})
}
func getCABundleFromFile(path string) ([]byte, error) {
if path == "" {
return nil, nil
}
return readFile(path)
}
// createAuthFromOpts adds auth for cloning git repos based on the parameters provided in opts.
func createAuthFromOpts(opts *cmd.Options) (transport.AuthMethod, error) {
if opts.SSHPrivateKeyFile != "" {
privateKey, err := readFile(opts.SSHPrivateKeyFile)
if err != nil {
return nil, err
}
gitURL, err := giturls.Parse(opts.Repo)
if err != nil {
return nil, err
}
auth, err := gossh.NewPublicKeys(gitURL.User.Username(), privateKey, "")
if err != nil {
return nil, err
}
if opts.KnownHostsFile != "" {
knownHosts, err := readFile(opts.KnownHostsFile)
if err != nil {
return nil, err
}
knownHostsCallBack, err := createKnownHostsCallBack(knownHosts)
if err != nil {
return nil, err
}
auth.HostKeyCallback = knownHostsCallBack
} else {
//nolint G106: Use of ssh InsecureIgnoreHostKey should be audited
//this will run in an init-container, so there is no persistence
auth.HostKeyCallback = ssh.InsecureIgnoreHostKey()
}
return auth, nil
}
if opts.Username != "" && opts.PasswordFile != "" {
password, err := readFile(opts.PasswordFile)
if err != nil {
return nil, err
}
return &httpgit.BasicAuth{
Username: opts.Username,
Password: string(password),
}, nil
}
return nil, nil
}
func createKnownHostsCallBack(knownHosts []byte) (ssh.HostKeyCallback, error) {
f, err := os.CreateTemp("", "known_hosts")
if err != nil {
return nil, err
}
defer os.RemoveAll(f.Name())
defer f.Close()
if _, err := f.Write(knownHosts); err != nil {
return nil, err
}
if err := f.Close(); err != nil {
return nil, fmt.Errorf("closing knownHosts file %s: %w", f.Name(), err)
}
return gossh.NewKnownHostsCallback(f.Name())
}