-
Notifications
You must be signed in to change notification settings - Fork 43
/
source.go
204 lines (169 loc) · 4.55 KB
/
source.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package mirror
import (
"context"
"fmt"
"io/fs"
"os"
"strings"
"github.com/direktiv/direktiv/pkg/refactor/datastore"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
gitssh "github.com/go-git/go-git/v5/plumbing/transport/ssh"
"golang.org/x/crypto/ssh"
)
type Source interface {
FS() fs.FS
Free() error
Notes() map[string]string
}
type DirectorySource struct {
fs fs.FS
}
var _ Source = &DirectorySource{}
func NewDirectorySource(dir string) *DirectorySource {
return &DirectorySource{
fs: os.DirFS(dir),
}
}
func (src *DirectorySource) FS() fs.FS {
return src.fs
}
func (src *DirectorySource) Free() error {
return nil
}
func (src *DirectorySource) Notes() map[string]string {
return make(map[string]string)
}
type GitSourceConfig struct {
URL string
GitRef string
InsecureSkipTLS bool
TempDir string
}
type gitSource struct {
*DirectorySource
path string
conf GitSourceConfig
notes map[string]string
}
var _ Source = &gitSource{}
func basicCloneOpts(conf GitSourceConfig) *git.CloneOptions {
return &git.CloneOptions{
InsecureSkipTLS: conf.InsecureSkipTLS,
URL: conf.URL,
Progress: nil,
ReferenceName: plumbing.NewBranchReferenceName(conf.GitRef),
}
}
func clone(conf GitSourceConfig, cloneOpts *git.CloneOptions) (Source, error) {
path, err := os.MkdirTemp(conf.TempDir, "direktiv_clone_*")
if err != nil {
return nil, err
}
_, err = git.PlainClone(path, false, cloneOpts)
if err != nil {
return nil, err
}
return &gitSource{
conf: conf,
path: path,
DirectorySource: NewDirectorySource(path),
notes: map[string]string{
"ref": conf.GitRef,
"url": conf.URL,
},
}, nil
}
func (src *gitSource) FS() fs.FS {
return src.fs
}
func (src *gitSource) Free() error {
err := os.RemoveAll(src.path)
if err != nil {
return err
}
return nil
}
func (src *gitSource) Notes() map[string]string {
return src.notes
}
func NewGitSourceNoAuth(conf GitSourceConfig) (Source, error) {
return clone(conf, basicCloneOpts(conf))
}
type GitSourceTokenAuthConf struct {
Token string
}
func newGitSourceToken(conf GitSourceConfig, auth GitSourceTokenAuthConf) (Source, error) {
cloneOpts := basicCloneOpts(conf)
prefix := "https://"
cloneOpts.URL = fmt.Sprintf("%s%s@", prefix, auth.Token) + strings.TrimPrefix(conf.URL, prefix)
return clone(conf, cloneOpts)
}
type GitSourceSSHAuthConf struct {
PublicKey string
PrivateKey string
PrivateKeyPassphrase string
}
func NewGitSourceSSH(conf GitSourceConfig, auth GitSourceSSHAuthConf) (Source, error) {
cloneOpts := basicCloneOpts(conf)
publicKeys, err := gitssh.NewPublicKeys("git", []byte(auth.PrivateKey), auth.PrivateKeyPassphrase)
if err != nil {
return nil, err
}
publicKeys.HostKeyCallbackHelper = gitssh.HostKeyCallbackHelper{
//nolint:gosec
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
cloneOpts.Auth = publicKeys
return clone(conf, cloneOpts)
}
func GetSource(_ context.Context, cfg *datastore.MirrorConfig) (Source, error) {
insecureSkipTLS := cfg.Insecure
tempDir := ""
if cfg.URL == "" {
return nil, fmt.Errorf("URL is missing in the configuration")
}
if cfg.GitRef == "" {
return nil, fmt.Errorf("GitRef is missing in the configuration")
}
if cfg.PrivateKeyPassphrase == "" && cfg.PublicKey == "" && cfg.PrivateKey == "" {
return NewGitSourceNoAuth(GitSourceConfig{
URL: cfg.URL,
GitRef: cfg.GitRef,
InsecureSkipTLS: insecureSkipTLS,
TempDir: tempDir,
})
}
if strings.HasPrefix(cfg.URL, "http") {
if cfg.AuthToken == "" {
cfg.AuthToken = cfg.PrivateKeyPassphrase
}
if cfg.PrivateKeyPassphrase == "" {
cfg.PrivateKeyPassphrase = cfg.AuthToken
}
if cfg.PrivateKeyPassphrase == "" {
return nil, fmt.Errorf("PrivateKeyPassphrase field has to be filled with the auth-token. This is required for token-based source")
}
return newGitSourceToken(GitSourceConfig{
URL: cfg.URL,
GitRef: cfg.GitRef,
InsecureSkipTLS: cfg.Insecure,
TempDir: tempDir,
}, GitSourceTokenAuthConf{
Token: cfg.AuthToken,
})
}
if cfg.PrivateKey != "" || cfg.PublicKey != "" {
return NewGitSourceSSH(GitSourceConfig{
URL: cfg.URL,
GitRef: cfg.GitRef,
InsecureSkipTLS: insecureSkipTLS,
TempDir: tempDir,
}, GitSourceSSHAuthConf{
PrivateKey: cfg.PrivateKey,
PublicKey: cfg.PublicKey,
PrivateKeyPassphrase: cfg.PrivateKeyPassphrase,
})
}
return nil, fmt.Errorf("could not detect the git auth mode to use")
}