Skip to content

Commit c7c9ed5

Browse files
committed
Adds regex validation for git url ssh and base auth case with additional tests
Signed-off-by: Caroline Scherf <fcaroline@vmware.com>
1 parent dd3341f commit c7c9ed5

3 files changed

Lines changed: 80 additions & 35 deletions

File tree

pkg/secret/factory.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package secret
66
import (
77
"encoding/json"
88
"github.com/google/go-containerregistry/pkg/name"
9+
"os"
910
"regexp"
1011
"sort"
1112
"strings"
@@ -106,22 +107,29 @@ func (f *Factory) validate() error {
106107
}
107108

108109
if f.GitUser != "" {
109-
if !(strings.HasPrefix(f.GitUrl, "http://") || strings.HasPrefix(f.GitUrl, "https://")) {
110-
return errors.Errorf("must provide a valid git url for basic auth (ex. https://github.com)")
111-
}
110+
//if !(strings.HasPrefix(f.GitUrl, "http://") || strings.HasPrefix(f.GitUrl, "https://")) {
111+
112+
//}
112113
// regex checks if slash appears after a period, ex: www.github.com/repo
113-
if match, _ := regexp.Match(`\.\w+/`, []byte(f.GitUrl)); match {
114-
return errors.Errorf("git url should be a valid url without the repository path (ex. https://github.com)")
114+
if match, _ := regexp.Match(`^(?:https?://)(?:[a-zA-Z0-9\-\.]+(?::\d+)?)(?:\/(?:$|[^/]))?$`, []byte(f.GitUrl)); !match {
115+
return errors.Errorf("must provide a valid git url without the repository path for basic auth (ex. https://github.com)")
115116
}
117+
// return errors.Errorf("git url should be a valid url without the repository path (ex. https://github.com)")
118+
//}
116119
}
117120

118-
if f.GitSshKeyFile != "" && !strings.HasPrefix(f.GitUrl, "git@") {
121+
if f.GitSshKeyFile != "" && !validateGitSshAddress(f.GitUrl) {
119122
return errors.Errorf("must provide a valid git url for SSH (ex. git@github.com)")
120123
}
121124

122125
return nil
123126
}
124127

128+
func validateGitSshAddress(address string) bool {
129+
re := regexp.MustCompile(`^git@github\.com$`)
130+
return re.MatchString(address)
131+
}
132+
125133
func (f *Factory) getSecretKind() (secretKind, error) {
126134
if f.DockerhubId != "" {
127135
return dockerHubKind, nil

pkg/secret/factory_test.go

Lines changed: 66 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package secret_test
55

66
import (
7+
"fmt"
78
"testing"
89

910
"github.com/sclevine/spec"
@@ -99,23 +100,19 @@ func testSecretFactory(t *testing.T, when spec.G, it spec.S) {
99100

100101
when("sub params are mixed with git", func() {
101102
it("returns an error message", func() {
102-
it("returns an error message", func() {
103-
factory.GitUrl = "some-git"
104-
factory.RegistryUser = "some-reg-user"
105-
factory.GitUser = "some-git-user"
106-
_, _, err := factory.MakeSecret("test-name", "test-namespace")
107-
require.EqualError(t, err, "extraneous parameters: registry-user")
108-
})
103+
factory.GitUrl = "some-git"
104+
factory.RegistryUser = "some-reg-user"
105+
factory.GitUser = "some-git-user"
106+
_, _, err := factory.MakeSecret("test-name", "test-namespace")
107+
require.EqualError(t, err, "extraneous parameters: registry-user")
109108
})
110109
})
111110

112111
when("neither git basic auth nor git ssh are provided", func() {
113112
it("returns an error message", func() {
114-
it("returns an error message", func() {
115-
factory.GitUrl = "some-git"
116-
_, _, err := factory.MakeSecret("test-name", "test-namespace")
117-
require.EqualError(t, err, "missing parameter git-user or git-ssh-key")
118-
})
113+
factory.GitUrl = "some-git"
114+
_, _, err := factory.MakeSecret("test-name", "test-namespace")
115+
require.EqualError(t, err, "missing parameter git-user or git-ssh-key")
119116
})
120117
})
121118

@@ -130,29 +127,69 @@ func testSecretFactory(t *testing.T, when spec.G, it spec.S) {
130127
})
131128

132129
when("using git basic auth", func() {
133-
it("validates that the git url begins with http:// or https://", func() {
134-
factory.GitUrl = "some-git"
135-
factory.GitUser = "some-git-user"
136-
_, _, err := factory.MakeSecret("test-name", "test-namespace")
137-
require.EqualError(t, err, "must provide a valid git url for basic auth (ex. https://github.com)")
130+
it("validates that the git url is correct", func() {
131+
validGitUrls := []string{
132+
"https://github.com",
133+
"http://github.com",
134+
"https://github.enterprise:1234",
135+
"http://github.enterprise:1234",
136+
}
137+
for _, testUrl := range validGitUrls {
138+
factory.GitUrl = testUrl
139+
factory.GitUser = "some-git-user"
140+
s, _, err := factory.MakeSecret("test-name", "test-namespace")
141+
require.NotNilf(t, s, "factory.GitUrl = \"%s\" secret should not be nil", factory.GitUrl)
142+
require.NoError(t, err, fmt.Sprintf("factory.GitUrl = \"%s\" should not have errors", factory.GitUrl))
143+
}
138144
})
139-
})
140145

141-
when("using git basic auth", func() {
142-
it("validates that the git url does not contain additional repo info", func() {
143-
factory.GitUrl = "https://some-git.com/test"
144-
factory.GitUser = "some-git-user"
145-
_, _, err := factory.MakeSecret("test-name", "test-namespace")
146-
require.EqualError(t, err, "git url should be a valid url without the repository path (ex. https://github.com)")
146+
it("validates that the git url is correct", func() {
147+
invalidGitUrls := []string{
148+
"some-git",
149+
"https://some-git.com/test",
150+
}
151+
for _, testUrl := range invalidGitUrls {
152+
factory.GitUrl = testUrl
153+
factory.GitUser = "some-git-user"
154+
s, _, err := factory.MakeSecret("test-name", "test-namespace")
155+
require.Nilf(t, s, "factory.GitUrl = \"%s\" secret should be nil", factory.GitUrl)
156+
require.EqualError(t, err, "must provide a valid git url without the repository path for basic auth (ex. https://github.com)")
157+
}
147158
})
148159
})
149160

150161
when("using git ssh keys", func() {
151-
it("validates that the git url begins with git@", func() {
152-
factory.GitUrl = "some-git"
153-
factory.GitSshKeyFile = "some-ssh-key"
154-
_, _, err := factory.MakeSecret("test-name", "test-namespace")
155-
require.EqualError(t, err, "must provide a valid git url for SSH (ex. git@github.com)")
162+
it("creates a secret when a valid ssh git url is passed", func() {
163+
validGitSshUrls := []string{
164+
"git@github.com",
165+
}
166+
for _, testUrl := range validGitSshUrls {
167+
factory.GitUrl = testUrl
168+
factory.GitSshKeyFile = "./testdata/some-ssh-key.pem"
169+
s, _, err := factory.MakeSecret("test-name", "test-namespace")
170+
require.NotNilf(t, s, "factory.GitUrl = \"%s\" secret should not be nil", factory.GitUrl)
171+
require.NoError(t, err, fmt.Sprintf("factory.GitUrl = \"%s\" should not have errors", factory.GitUrl))
172+
}
173+
})
174+
175+
it("prints an error when the git url is not valid", func() {
176+
invalidGitSshUrls := []string{
177+
"some-git",
178+
"git@github.com:owner",
179+
"git@github.com:owner/repo",
180+
"git@github.com:443/user/repo.git",
181+
"git@github.com:user/repo.git",
182+
"git@github.com:buildpacks-community/kpack-cli.git",
183+
"ssh://git@ssh.github.com:443/YOUR-USERNAME/YOUR-REPOSITORY.git",
184+
}
185+
for _, testUrl := range invalidGitSshUrls {
186+
factory.GitUrl = testUrl
187+
factory.GitUrl = "some-git"
188+
factory.GitSshKeyFile = "./testdata/some-ssh-key.pem"
189+
s, _, err := factory.MakeSecret("test-name", "test-namespace")
190+
require.Nilf(t, s, "factory.GitUrl = \"%s\" secret should be nil", factory.GitUrl)
191+
require.EqualError(t, err, "must provide a valid git url for SSH (ex. git@github.com)")
192+
}
156193
})
157194
})
158195
}

pkg/secret/testdata/some-ssh-key.pem

Whitespace-only changes.

0 commit comments

Comments
 (0)