@@ -2,8 +2,17 @@ package git
2
2
3
3
import (
4
4
"fmt"
5
+ "regexp"
5
6
6
- "github.com/argoproj-labs/argocd-image-updater/ext/git"
7
+ "github.com/docplanner/helm-repo-updater/internal/app/log"
8
+ "github.com/go-git/go-git/v5/plumbing/transport"
9
+ "github.com/go-git/go-git/v5/plumbing/transport/http"
10
+ "github.com/go-git/go-git/v5/plumbing/transport/ssh"
11
+ )
12
+
13
+ var (
14
+ sshURLRegex = regexp .MustCompile ("^(ssh://)?([^/:]*?)@[^@]+$" )
15
+ httpsURLRegex = regexp .MustCompile ("^(https://).*" )
7
16
)
8
17
9
18
// Credentials is a git credential config
@@ -14,61 +23,107 @@ type Credentials struct {
14
23
SSHPrivKey string
15
24
}
16
25
17
- // NewGitCreds returns the credentials for the given repo url.
18
- func (c Credentials ) NewGitCreds (repoURL string ) (git.Creds , error ) {
19
- if isSshUrl (repoURL ) {
20
- return c .fromSsh (repoURL )
26
+ // NewGitCreds returns credentials for use with go-git library
27
+ func (c Credentials ) NewGitCreds (repoURL string , password string ) (transport.AuthMethod , error ) {
28
+ if isSSHURL (repoURL ) {
29
+ gitSSHCredentials , err := c .fromSSH (repoURL , password )
30
+ if err != nil {
31
+ return nil , err
32
+ }
33
+ return gitSSHCredentials , nil
21
34
}
22
35
23
- if isHttpsUrl (repoURL ) {
24
- return c .fromHttps (repoURL )
36
+ if isHTTPSURL (repoURL ) {
37
+ gitCreds , err := c .from (repoURL )
38
+ if err != nil {
39
+ return nil , err
40
+ }
41
+ return gitCreds , nil
25
42
}
26
43
27
44
return nil , unknownRepositoryType (repoURL )
28
45
}
29
46
30
- func isSshUrl (repoUrl string ) bool {
31
- ok , _ := git .IsSSHURL (repoUrl )
47
+ // isSSHURL returns true if supplied URL is SSH URL
48
+ func isSSHURL (url string ) bool {
49
+ matches := sshURLRegex .FindStringSubmatch (url )
50
+ return len (matches ) > 2
51
+ }
32
52
33
- return ok
53
+ // isHTTPSURL returns true if supplied URL is a valid HTTPS URL
54
+ func isHTTPSURL (url string ) bool {
55
+ return httpsURLRegex .MatchString (url )
34
56
}
35
57
36
- func isHttpsUrl (repoUrl string ) bool {
37
- return git .IsHTTPSURL (repoUrl )
58
+ // generateAuthForSSH generate the necessary public keys as auth for git repository using
59
+ // the provided privateKeyFile containing a valid SSH private key
60
+ func generateAuthForSSH (repoUrl string , userName string , privateKeyFile string , password string ) (ssh.AuthMethod , error ) {
61
+ publicKeys , err := ssh .NewPublicKeysFromFile ("git" , privateKeyFile , password )
62
+ if err != nil {
63
+ log .Warnf ("generate publickeys failed: %s\n " , err .Error ())
64
+ return nil , err
65
+ }
66
+ return publicKeys , err
38
67
}
39
68
40
- func (c Credentials ) fromSsh (repoUrl string ) (git.Creds , error ) {
69
+ // fromSSH generate a valid credentials using ssh key
70
+ func (c Credentials ) fromSSH (repoUrl string , password string ) (ssh.AuthMethod , error ) {
41
71
if c .allowsSshAuth () {
42
- return git .NewSSHCreds (c .SSHPrivKey , "" , true ), nil
72
+ sshPublicKeys , err := generateAuthForSSH (repoUrl , c .Username , c .SSHPrivKey , password )
73
+ if err != nil {
74
+ return nil , err
75
+ }
76
+ return sshPublicKeys , nil
43
77
}
44
78
45
79
return nil , sshPrivateKeyNotProvided (repoUrl )
46
80
}
47
81
48
- func (c Credentials ) fromHttps (repoURL string ) (git.Creds , error ) {
49
- if c .allowsHttpsAuth () {
50
- return git .NewHTTPSCreds (c .Username , c .Password , "" , "" , true , "" ), nil
82
+ // generatAuthFor generate a valid credentials for go-git library using
83
+ // username and password
84
+ func generatAuthFor (username string , password string ) * http.BasicAuth {
85
+ return & http.BasicAuth {
86
+ Username : username ,
87
+ Password : password ,
88
+ }
89
+ }
90
+
91
+ // from generate a valid credentials for go-git library using
92
+ // username and passowrd
93
+ func (c Credentials ) from (repoURL string ) (* http.BasicAuth , error ) {
94
+ if c .allowsAuth () {
95
+ return generatAuthFor (c .Username , c .Password ), nil
51
96
}
52
97
53
- return nil , httpsUserAndPasswordNotProvided (repoURL )
98
+ return nil , UserAndPasswordNotProvided (repoURL )
54
99
}
55
100
101
+ // allowSshAuth check if necessary attributes for generate an SSH
102
+ // credentials are provided
56
103
func (c Credentials ) allowsSshAuth () bool {
57
104
return c .SSHPrivKey != ""
58
105
}
59
106
60
- func (c Credentials ) allowsHttpsAuth () bool {
107
+ // allowsAuth check if necessary attributes for generate and
108
+ // credentials are provided
109
+ func (c Credentials ) allowsAuth () bool {
61
110
return c .Username != "" && c .Password != ""
62
111
}
63
112
113
+ // sshPrivateKeyNotProvided return an error used when sshPrivKey
114
+ // is not provided for generate and SSH credentials
64
115
func sshPrivateKeyNotProvided (repoUrl string ) error {
65
116
return fmt .Errorf ("sshPrivKey not provided for authenticatication to repository %s" , repoUrl )
66
117
}
67
118
68
- func httpsUserAndPasswordNotProvided (repoUrl string ) error {
119
+ // UserAndPasswordNotProvided return an error used when
120
+ // username or password are not provided for generate and credentials
121
+ func UserAndPasswordNotProvided (repoUrl string ) error {
69
122
return fmt .Errorf ("no value provided for username and password for authentication to repository %s" , repoUrl )
70
123
}
71
124
125
+ // unknownRepositoryType return an error used when
126
+ // the repository provided is not or SSH type
72
127
func unknownRepositoryType (repoUrl string ) error {
73
128
return fmt .Errorf ("unknown repository type for git repository URL %s" , repoUrl )
74
129
}
0 commit comments