/
transport.go
157 lines (135 loc) · 3.83 KB
/
transport.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
/*
Copyright 2020 The Flux authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gogit
import (
"fmt"
"github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
gossh "golang.org/x/crypto/ssh"
"github.com/fluxcd/pkg/git"
"github.com/fluxcd/pkg/ssh/knownhosts"
)
// transportAuth constructs the transport.AuthMethod for the git.Transport of
// the given git.AuthOptions. It returns the result, or an error.
func transportAuth(opts *git.AuthOptions, fallbackToDefaultKnownHosts bool) (transport.AuthMethod, error) {
if opts == nil {
return nil, nil
}
switch opts.Transport {
case git.HTTPS, git.HTTP:
// Some providers (i.e. GitLab) will reject empty credentials for
// public repositories.
if opts.Username != "" || opts.Password != "" {
return &http.BasicAuth{
Username: opts.Username,
Password: opts.Password,
}, nil
} else if opts.BearerToken != "" {
return &http.TokenAuth{
Token: opts.BearerToken,
}, nil
}
return nil, nil
case git.SSH:
// if the custom auth options don't provide a private key and known_hosts, we try
// to use the default known_hosts of the machine.
if len(opts.Identity)+len(opts.KnownHosts) == 0 && fallbackToDefaultKnownHosts {
authMethod, err := ssh.DefaultAuthBuilder(opts.Username)
if err != nil {
return nil, err
}
pkCallback, ok := authMethod.(*ssh.PublicKeysCallback)
if ok {
return &DefaultAuth{
pkCallack: pkCallback,
}, nil
}
return nil, nil
}
pk, err := ssh.NewPublicKeys(opts.Username, opts.Identity, opts.Password)
if err != nil {
return nil, err
}
var callback gossh.HostKeyCallback
if len(opts.KnownHosts) > 0 {
callback, err = knownhosts.New(opts.KnownHosts)
if err != nil {
return nil, err
}
}
customPK := &CustomPublicKeys{
pk: pk,
callback: callback,
}
return customPK, nil
case "":
return nil, fmt.Errorf("no transport type set")
default:
return nil, fmt.Errorf("unknown transport '%s'", opts.Transport)
}
}
// caBundle returns the CA bundle from the given git.AuthOptions.
func caBundle(opts *git.AuthOptions) []byte {
if opts == nil {
return nil
}
return opts.CAFile
}
// CustomPublicKeys is a wrapper around ssh.PublicKeys to help us
// customize the ssh config. It implements ssh.AuthMethod.
type CustomPublicKeys struct {
pk *ssh.PublicKeys
callback gossh.HostKeyCallback
}
func (a *CustomPublicKeys) Name() string {
return a.pk.Name()
}
func (a *CustomPublicKeys) String() string {
return a.pk.String()
}
func (a *CustomPublicKeys) ClientConfig() (*gossh.ClientConfig, error) {
if a.callback != nil {
a.pk.HostKeyCallback = a.callback
}
config, err := a.pk.ClientConfig()
if err != nil {
return nil, err
}
if len(git.KexAlgos) > 0 {
config.Config.KeyExchanges = git.KexAlgos
}
if len(git.HostKeyAlgos) > 0 {
config.HostKeyAlgorithms = git.HostKeyAlgos
}
return config, nil
}
type DefaultAuth struct {
pkCallack *ssh.PublicKeysCallback
}
func (a *DefaultAuth) Name() string {
return a.pkCallack.Name()
}
func (a *DefaultAuth) String() string {
return a.pkCallack.String()
}
func (a *DefaultAuth) ClientConfig() (*gossh.ClientConfig, error) {
config, err := a.pkCallack.ClientConfig()
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}
return config, nil
}