Skip to content

Commit

Permalink
Merge pull request #152 from cli/andyfeller/export-auth-is-tenancy
Browse files Browse the repository at this point in the history
Allow reuse of host checks for extension authors
  • Loading branch information
andyfeller committed Apr 29, 2024
2 parents ecc5db7 + 8a3c02a commit 5840c44
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 6 deletions.
16 changes: 11 additions & 5 deletions pkg/auth/auth.go
Expand Up @@ -62,7 +62,7 @@ func TokenFromEnvOrConfig(host string) (string, string) {

func tokenForHost(cfg *config.Config, host string) (string, string) {
host = normalizeHostname(host)
if isEnterprise(host) {
if IsEnterprise(host) {
if token := os.Getenv(ghEnterpriseToken); token != "" {
return token, ghEnterpriseToken
}
Expand Down Expand Up @@ -152,12 +152,18 @@ func defaultHost(cfg *config.Config) (string, string) {
// TenancyHost is the domain name of a tenancy GitHub instance.
const tenancyHost = "ghe.com"

func isEnterprise(host string) bool {
return host != github && host != localhost && !isTenancy(host)
// IsEnterprise determines if a provided host is a GitHub Enterprise Server instance,
// rather than GitHub.com or a tenancy GitHub instance.
func IsEnterprise(host string) bool {
normalizedHost := normalizeHostname(host)
return normalizedHost != github && normalizedHost != localhost && !IsTenancy(normalizedHost)
}

func isTenancy(host string) bool {
return strings.HasSuffix(host, "."+tenancyHost)
// IsTenancy determines if a provided host is a tenancy GitHub instance,
// rather than GitHub.com or a GitHub Enterprise Server instance.
func IsTenancy(host string) bool {
normalizedHost := normalizeHostname(host)
return strings.HasSuffix(normalizedHost, "."+tenancyHost)
}

func normalizeHostname(host string) string {
Expand Down
68 changes: 67 additions & 1 deletion pkg/auth/auth_test.go
Expand Up @@ -237,11 +237,21 @@ func TestIsEnterprise(t *testing.T) {
host: "github.com",
wantOut: false,
},
{
name: "github API",
host: "api.github.com",
wantOut: false,
},
{
name: "localhost",
host: "github.localhost",
wantOut: false,
},
{
name: "localhost API",
host: "api.github.localhost",
wantOut: false,
},
{
name: "enterprise",
host: "mygithub.com",
Expand All @@ -252,11 +262,67 @@ func TestIsEnterprise(t *testing.T) {
host: "tenant.ghe.com",
wantOut: false,
},
{
name: "tenant API",
host: "api.tenant.ghe.com",
wantOut: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out := IsEnterprise(tt.host)
assert.Equal(t, tt.wantOut, out)
})
}
}

func TestIsTenancy(t *testing.T) {
tests := []struct {
name string
host string
wantOut bool
}{
{
name: "github",
host: "github.com",
wantOut: false,
},
{
name: "github API",
host: "api.github.com",
wantOut: false,
},
{
name: "localhost",
host: "github.localhost",
wantOut: false,
},
{
name: "localhost API",
host: "api.github.localhost",
wantOut: false,
},
{
name: "enterprise",
host: "mygithub.com",
wantOut: false,
},
{
name: "tenant",
host: "tenant.ghe.com",
wantOut: true,
},
{
name: "tenant API",
host: "api.tenant.ghe.com",
wantOut: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out := isEnterprise(tt.host)
out := IsTenancy(tt.host)
assert.Equal(t, tt.wantOut, out)
})
}
Expand Down

0 comments on commit 5840c44

Please sign in to comment.