This repository has been archived by the owner on Aug 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
transport.go
73 lines (64 loc) · 2.13 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
package gitdir
import (
"errors"
"github.com/fluxcd/go-git-providers/gitprovider"
"github.com/fluxcd/toolkit/pkg/ssh/knownhosts"
"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"
)
// AuthMethod specifies the authentication method and related credentials for connecting
// to a Git repository.
type AuthMethod interface {
// This AuthMethod is a superset of the go-git AuthMethod
transport.AuthMethod
// TransportType defines what transport type should be used with this method
TransportType() gitprovider.TransportType
}
// NewSSHAuthMethod creates a new AuthMethod for the Git SSH protocol, using a given
// identity and known_hosts file.
//
// identityFile is the bytes of e.g. ~/.ssh/id_rsa, given that ~/.ssh/id_rsa.pub is
// registered with and trusted by the Git provider.
//
// knownHostsFile should be the file content of the known_hosts file to use for remote (e.g. GitHub)
// public key verification.
// If you want to use the default git CLI behavior, populate this byte slice with contents from
// ioutil.ReadFile("~/.ssh/known_hosts").
func NewSSHAuthMethod(identityFile, knownHostsFile []byte) (AuthMethod, error) {
if len(identityFile) == 0 || len(knownHostsFile) == 0 {
return nil, errors.New("invalid identityFile, knownHostsFile options")
}
pk, err := ssh.NewPublicKeys("git", identityFile, "")
if err != nil {
return nil, err
}
callback, err := knownhosts.New(knownHostsFile)
if err != nil {
return nil, err
}
pk.HostKeyCallback = callback
return &authMethod{
AuthMethod: pk,
t: gitprovider.TransportTypeGit,
}, nil
}
func NewHTTPSAuthMethod(username, password string) (AuthMethod, error) {
if len(username) == 0 || len(password) == 0 {
return nil, errors.New("invalid username, password options")
}
return &authMethod{
AuthMethod: &http.BasicAuth{
Username: username,
Password: password,
},
t: gitprovider.TransportTypeHTTPS,
}, nil
}
type authMethod struct {
transport.AuthMethod
t gitprovider.TransportType
}
func (a *authMethod) TransportType() gitprovider.TransportType {
return a.t
}