Skip to content

Commit

Permalink
Added Test_GitURL_skipsTLS unit test
Browse files Browse the repository at this point in the history
Signed-off-by: sethiyash <[email protected]>
  • Loading branch information
sethiyash committed Jan 12, 2024
1 parent 847ef9e commit 7f3c14b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
11 changes: 4 additions & 7 deletions pkg/fetch/vendir.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@ import (
kyaml "sigs.k8s.io/yaml"
)

// SourceType to extract host
type SourceType string

const (
// GitURL source type to extract host
GitURL SourceType = "gitURL"
GitURL = iota
// ImageRegistry source type to extract host
ImageRegistry SourceType = "image"
ImageRegistry

vendirEntireDirPath = "."
)
Expand Down Expand Up @@ -387,7 +384,7 @@ func (v *Vendir) configMapBytes(configMapRef vendirconf.DirectoryContentsLocalRe
}

// This function works on image refs and hostname extraction using isGitURL flag
func (v *Vendir) shouldSkipTLSVerify(url string, sourceType SourceType) bool {
func (v *Vendir) shouldSkipTLSVerify(url string, sourceType int) bool {
return v.opts.SkipTLSConfig.ShouldSkipTLSForAuthority(ExtractHost(url, sourceType))
}

Expand Down Expand Up @@ -440,7 +437,7 @@ func extractGitHostname(input string) string {
}

// ExtractHost return registry for Docker Image and Host for git url
func ExtractHost(input string, sourceType SourceType) string {
func ExtractHost(input string, sourceType int) string {
switch sourceType {
case GitURL:
return extractGitHostname(input)
Expand Down
46 changes: 43 additions & 3 deletions pkg/fetch/vendir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,50 @@ func Test_AddDir_skipsTLS(t *testing.T) {
}
}

func Test_GitURL_skipsTLS(t *testing.T) {
configMap := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "kapp-controller-config",
Namespace: "default",
},
Data: map[string]string{
"dangerousSkipTLSVerify": "github.com, gitlab.com, hostname.com",
},
}
k8scs := k8sfake.NewSimpleClientset(configMap)
config, err := kcconfig.NewConfig(k8scs)
assert.NoError(t, err)

vendir := fetch.NewVendir("default", k8scs,
fetch.VendirOpts{SkipTLSConfig: config}, exec.NewPlainCmdRunner())

type testCase struct {
URL string
shouldSkipTLS bool
}
testCases := []testCase{
{"https://github.com/bitnami/charts/", true},
{"https://github.com/bitnami/charts/", true},
{"ssh://[email protected]:/path/to/repo.git", true},
{"https://bitbucket.org/bitnami/charts/", false},
}
for i, tc := range testCases {
err = vendir.AddDir(v1alpha1.AppFetch{
Git: &v1alpha1.AppFetchGit{URL: tc.URL},
},
"dirpath/0")
assert.NoError(t, err)

vConf := vendir.Config()
assert.Equal(t, i+1, len(vConf.Directories), "Failed on iteration %d", i)
assert.Equal(t, tc.shouldSkipTLS, vConf.Directories[i].Contents[0].Git.DangerousSkipTLSVerify, "Failed with URL %s", tc.URL)
}
}

func TestExtractHost(t *testing.T) {
tests := []struct {
name string
sourceType fetch.SourceType
sourceType int
want string
}{
{
Expand Down Expand Up @@ -90,9 +130,9 @@ func TestExtractHost(t *testing.T) {
want: "github.com",
},
{
name: "http://github.com/bitnami/charts/",
name: "http://gitlab.com/bitnami/charts/",
sourceType: fetch.GitURL,
want: "github.com",
want: "gitlab.com",
},
{
name: "ssh://[email protected]:/path/to/repo.git",
Expand Down

0 comments on commit 7f3c14b

Please sign in to comment.