Skip to content

Commit 6806b23

Browse files
committed
Fix regexes
Signed-off-by: Caroline Scherf <fcaroline@vmware.com>
1 parent 6f45e38 commit 6806b23

2 files changed

Lines changed: 28 additions & 12 deletions

File tree

pkg/secret/factory.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ package secret
55

66
import (
77
"encoding/json"
8-
"github.com/google/go-containerregistry/pkg/name"
98
"os"
109
"regexp"
1110
"sort"
1211
"strings"
1312

13+
"github.com/google/go-containerregistry/pkg/name"
14+
1415
"github.com/google/go-containerregistry/pkg/authn"
1516
"github.com/pkg/errors"
1617
corev1 "k8s.io/api/core/v1"
@@ -24,6 +25,9 @@ const (
2425
GitAnnotation = "kpack.io/git"
2526
)
2627

28+
var gitHttpUrlRegex = regexp.MustCompile(`^(?:https?://)?(?:[a-zA-Z0-9\-\.])+\.(?:\w+)(:(?:\d){3,5})?$`)
29+
var gitSshRegex = regexp.MustCompile(`^((?:ssh://)?(?:[A-Za-z0-9])+@(?:[A-Za-z0-9+.-])+\.(?:\w+)(:(?:\d){3,5})?)$|^(?:[A-Za-z0-9][A-Za-z0-9+.-]+)\.(?:[A-Za-z0-9]+)$`)
30+
2731
type CredentialFetcher interface {
2832
FetchPassword(envVar, prompt string) (string, error)
2933
}
@@ -106,24 +110,17 @@ func (f *Factory) validate() error {
106110
}
107111
}
108112

109-
if f.GitUser != "" {
110-
if match, _ := regexp.Match(`^(?:https?://)(?:[a-zA-Z0-9\-\.]+(?::\d+)?)(?:\/(?:$|[^/]))?$`, []byte(f.GitUrl)); !match {
111-
return errors.Errorf("must provide a valid git url without the repository path for basic auth (ex. https://github.com)")
112-
}
113+
if f.GitUser != "" && !gitHttpUrlRegex.MatchString(f.GitUrl) {
114+
return errors.Errorf("must provide a valid git url without the repository path for basic auth (ex. https://github.com)")
113115
}
114116

115-
if f.GitSshKeyFile != "" && !validateGitSshAddress(f.GitUrl) {
117+
if f.GitSshKeyFile != "" && !gitSshRegex.MatchString(f.GitUrl) {
116118
return errors.Errorf("must provide a valid git url for SSH (ex. git@github.com)")
117119
}
118120

119121
return nil
120122
}
121123

122-
func validateGitSshAddress(address string) bool {
123-
re := regexp.MustCompile(`^git@github\.com$`)
124-
return re.MatchString(address)
125-
}
126-
127124
func (f *Factory) getSecretKind() (secretKind, error) {
128125
if f.DockerhubId != "" {
129126
return dockerHubKind, nil

pkg/secret/factory_test.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,11 @@ func testSecretFactory(t *testing.T, when spec.G, it spec.S) {
132132
"https://github.com",
133133
"http://github.com",
134134
"https://github.enterprise:1234",
135-
"http://github.enterprise:1234",
135+
"http://github.enterprise:134",
136+
"https://domain.com",
137+
"http://domain.com",
138+
"github.com",
139+
"bitbucket.org",
136140
}
137141
for _, testUrl := range validGitUrls {
138142
factory.GitUrl = testUrl
@@ -147,6 +151,9 @@ func testSecretFactory(t *testing.T, when spec.G, it spec.S) {
147151
invalidGitUrls := []string{
148152
"some-git",
149153
"https://some-git.com/test",
154+
"https://some-git.com/",
155+
"https://domain.com/stash/csm/blabla/project.git",
156+
"http://github.enterprise:13444456",
150157
}
151158
for _, testUrl := range invalidGitUrls {
152159
factory.GitUrl = testUrl
@@ -162,6 +169,11 @@ func testSecretFactory(t *testing.T, when spec.G, it spec.S) {
162169
it("creates a secret when a valid ssh git url is passed", func() {
163170
validGitSshUrls := []string{
164171
"git@github.com",
172+
"user@domain.com",
173+
"test@github.com",
174+
"domain.com",
175+
"ssh://git@github.com:123",
176+
"ssh://git@github.com",
165177
}
166178
for _, testUrl := range validGitSshUrls {
167179
factory.GitUrl = testUrl
@@ -181,6 +193,13 @@ func testSecretFactory(t *testing.T, when spec.G, it spec.S) {
181193
"git@github.com:user/repo.git",
182194
"git@github.com:buildpacks-community/kpack-cli.git",
183195
"ssh://git@ssh.github.com:443/YOUR-USERNAME/YOUR-REPOSITORY.git",
196+
"git@github.com/abc.git",
197+
"git@github.com:user/repo.git",
198+
"ssh://git@example.com/path/to/repo.git",
199+
"ssh://user@bitbucket.org/group/my-repo.git",
200+
"git@custom-git-server.local:myrepo.git",
201+
"git@gitlab.com:username/project.git",
202+
"git@github.com:44335678",
184203
}
185204
for _, testUrl := range invalidGitSshUrls {
186205
factory.GitUrl = testUrl

0 commit comments

Comments
 (0)