Skip to content

Commit

Permalink
Fix(git): add warning of the mismatch of git cred and url and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FogDong committed Sep 9, 2020
1 parent 49b6185 commit 8c01fe7
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 0 deletions.
29 changes: 29 additions & 0 deletions pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"

Expand All @@ -33,6 +34,11 @@ const (
sshMissingKnownHostsSSHCommand = "ssh -o StrictHostKeyChecking=accept-new"
)

var (
// sshURLRegexFormat matches the url of SSH git repository
sshURLRegexFormat = regexp.MustCompile(`.+@([\w\d\.]+)(:[\d]+){0,1}/*(.*)`)
)

func run(logger *zap.SugaredLogger, dir string, args ...string) (string, error) {
c := exec.Command("git", args...)
var output bytes.Buffer
Expand Down Expand Up @@ -69,6 +75,7 @@ func Fetch(logger *zap.SugaredLogger, spec FetchSpec) error {
if err := ensureHomeEnv(logger); err != nil {
return err
}
validateGitAuth(logger, spec.URL)

if spec.Path != "" {
if _, err := run(logger, "", "init", spec.Path); err != nil {
Expand Down Expand Up @@ -244,3 +251,25 @@ func userHasKnownHostsFile(logger *zap.SugaredLogger) (bool, error) {
f.Close()
return true, nil
}

func validateGitAuth(logger *zap.SugaredLogger, url string) {
homeenv := os.Getenv("HOME")
sshCred := true
if _, err := os.Stat(homeenv + "/.ssh"); os.IsNotExist(err) {
sshCred = false
}
urlSSHFormat := ValidateGitSSHURLFormat(url)
if sshCred && !urlSSHFormat {
logger.Warnf("SSH credentials have been provided but the URL(%q) is not a valid SSH URL", url)
} else if !sshCred && urlSSHFormat {
logger.Warnf("The given SSH URL(%q) does not match the credential", url)
}
}

// ValidateGitSSHURLFormat validates the given URL format is SSH or not
func ValidateGitSSHURLFormat(url string) bool {
if sshURLRegexFormat.MatchString(url) {
return true
}
return false
}
145 changes: 145 additions & 0 deletions pkg/git/git_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
Copyright 2020 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package git

import "testing"

func TestValidateGitSSHURLFormat(t *testing.T) {
tests := []struct {
url string
want bool
}{
{
url: "git@github.com:user/project.git",
want: true,
},
{
url: "https://github.com/user/project.git",
want: false,
},
{
url: "http://github.com/user/project.git",
want: false,
},
{
url: "git@127.0.0.1:user/project.git",
want: true,
},
{
url: "https://127.0.0.1/user/project.git",
want: false,
},
{
url: "http://127.0.0.1/user/project.git",
want: false,
},
{
url: "ssh://user@host.xz:port/path/to/repo.git/",
want: true,
},
{
url: "ssh://user@host.xz/path/to/repo.git/",
want: true,
},
{
url: "ssh://host.xz:port/path/to/repo.git/",
want: false,
},
{
url: "ssh://host.xz/path/to/repo.git/",
want: false,
},
{
url: "ssh://user@host.xz/~user/path/to/repo.git/",
want: true,
},
{
url: "ssh://host.xz/~user/path/to/repo.git/",
want: false,
},
{
url: "ssh://user@host.xz/~/path/to/repo.git",
want: true,
},
{
url: "ssh://host.xz/~/path/to/repo.git",
want: false,
},
{
url: "git://host.xz/path/to/repo.git/",
want: false,
},
{
url: "git://host.xz/~user/path/to/repo.git/",
want: false,
},
{
url: "http://host.xz/path/to/repo.git/",
want: false,
},
{
url: "https://host.xz/path/to/repo.git/",
want: false,
},
{
url: "https://127.0.0.1/user/project.git",
want: false,
},
{
url: "/path/to/repo.git/",
want: false,
},
{
url: "file://~/path/to/repo.git/",
want: false,
},
{
url: "user@host.xz:/path/to/repo.git/",
want: true,
},
{
url: "host.xz:/path/to/repo.git/",
want: false,
},
{
url: "ssh://host.xz/path/to/repo.git/",
want: false,
},
{
url: "user@host.xz:~user/path/to/repo.git/",
want: true,
},
{
url: "host.xz:~user/path/to/repo.git/",
want: false,
},
{
url: "user@host.xz:path/to/repo.git",
want: true,
},
{
url: "host.xz:path/to/repo.git",
want: false,
},
}

for _, tt := range tests {
got := ValidateGitSSHURLFormat(tt.url)
if got != tt.want {
t.Errorf("Validate URL(%v)'s SSH format got %v, want %v", tt.url, got, tt.want)
}
}
}

0 comments on commit 8c01fe7

Please sign in to comment.