Skip to content

Commit

Permalink
Merge pull request #48 from cultureamp/translate-ssh
Browse files Browse the repository at this point in the history
fix: allow HTTPS credentials when SSH configured
  • Loading branch information
jamestelfer authored May 27, 2024
2 parents 1332f34 + c573bdc commit a6b4d66
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
15 changes: 15 additions & 0 deletions internal/vendor/vendor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/url"
"regexp"
"strconv"
"time"

Expand Down Expand Up @@ -60,6 +61,9 @@ func New(
return nil, fmt.Errorf("could not find repository for pipeline %s: %w", claims.PipelineSlug, err)
}

// allow HTTPS credentials if the pipeline is configured for an equivalent SSH URL
pipelineRepoURL = TranslateSSHToHTTPS(pipelineRepoURL)

if requestedRepoURL != "" && pipelineRepoURL != requestedRepoURL {
// git is asking for a different repo than we can handle: return nil
// to indicate that the handler should return a successful (but
Expand Down Expand Up @@ -89,3 +93,14 @@ func New(
}, nil
}
}

var sshUrl = regexp.MustCompile(`^git@github.com:([^/].+)$`)

func TranslateSSHToHTTPS(url string) string {
groups := sshUrl.FindStringSubmatch(url)
if groups == nil {
return url
}

return fmt.Sprintf("https://github.com/%s", groups[1])
}
66 changes: 66 additions & 0 deletions internal/vendor/vendor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,69 @@ func TestPipelineRepositoryToken_ExpiryUnix(t *testing.T) {
})
}
}

func TestTransformSSHToHTTPS(t *testing.T) {
testCases := []struct {
name string
url string
expected string
}{
{
name: "ssh, valid GitHub",
url: "git@github.com:organization/chinmina.git",
expected: "https://github.com/organization/chinmina.git",
},
{
name: "ssh, no user",
url: "github.com:organization/chinmina.git",
expected: "github.com:organization/chinmina.git",
},
{
name: "ssh, different host",
url: "git@githab.com:organization/chinmina.git",
expected: "git@githab.com:organization/chinmina.git",
},
{
name: "ssh, invalid path specifier",
url: "git@github.com/organization/chinmina.git",
expected: "git@github.com/organization/chinmina.git",
},
{
name: "ssh, zero length path",
url: "git@github.com:",
expected: "git@github.com:",
},
{
name: "ssh, no extension",
url: "git@github.com:organization/chinmina",
expected: "https://github.com/organization/chinmina",
},
{
name: "https, valid",
url: "https://github.com/organization/chinmina.git",
expected: "https://github.com/organization/chinmina.git",
},
{
name: "https, nonsense",
url: "https://github.com/organization/chinmina.git",
expected: "https://github.com/organization/chinmina.git",
},
{
name: "http, valid",
url: "http://github.com/organization/chinmina.git",
expected: "http://github.com/organization/chinmina.git",
},
{
name: "pure nonsense",
url: "molybdenum://mo",
expected: "molybdenum://mo",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := vendor.TranslateSSHToHTTPS(tc.url)
assert.Equal(t, tc.expected, actual)
})
}
}

0 comments on commit a6b4d66

Please sign in to comment.