From 6e9fb7cda47d545c7a1e4e25ea9e1f4e6354290b Mon Sep 17 00:00:00 2001 From: Andy Feller Date: Fri, 8 Mar 2024 10:02:53 -0500 Subject: [PATCH 1/5] Allow reuse of host checks for extension authors Following on the heels of #151, this commit exports the internal logic used to determine if a host is GHES deployment or a tenant. Exporting these functions allow extension authors like `github/gh-copilot` to provide tailored experiences consistently with `gh`. --- pkg/auth/auth.go | 8 ++++---- pkg/auth/auth_test.go | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/pkg/auth/auth.go b/pkg/auth/auth.go index 88e02c2..72881f3 100644 --- a/pkg/auth/auth.go +++ b/pkg/auth/auth.go @@ -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 } @@ -152,11 +152,11 @@ 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) +func IsEnterprise(host string) bool { + return host != github && host != localhost && !IsTenancy(host) } -func isTenancy(host string) bool { +func IsTenancy(host string) bool { return strings.HasSuffix(host, "."+tenancyHost) } diff --git a/pkg/auth/auth_test.go b/pkg/auth/auth_test.go index a0bb59d..2ddc146 100644 --- a/pkg/auth/auth_test.go +++ b/pkg/auth/auth_test.go @@ -256,7 +256,43 @@ func TestIsEnterprise(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - out := isEnterprise(tt.host) + 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: "localhost", + host: "github.localhost", + wantOut: false, + }, + { + name: "enterprise", + host: "mygithub.com", + wantOut: false, + }, + { + name: "tenant", + host: "tenant.ghe.com", + wantOut: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + out := IsTenancy(tt.host) assert.Equal(t, tt.wantOut, out) }) } From 7b29c8ee05f19c023e752d74faa61d3443b1e33e Mon Sep 17 00:00:00 2001 From: Andy Feller Date: Mon, 18 Mar 2024 13:16:17 -0400 Subject: [PATCH 2/5] Normalize hostname checking for tenancy or enterprise This aligns the `cli/cli` behavior of IsTenancy and IsEnterprise with the counterparts here. --- pkg/auth/auth.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/auth/auth.go b/pkg/auth/auth.go index 72881f3..eb4236a 100644 --- a/pkg/auth/auth.go +++ b/pkg/auth/auth.go @@ -153,11 +153,13 @@ func defaultHost(cfg *config.Config) (string, string) { const tenancyHost = "ghe.com" func IsEnterprise(host string) bool { - return host != github && host != localhost && !IsTenancy(host) + normalizedHost := normalizeHostname(host) + return normalizedHost != github && normalizedHost != localhost && !IsTenancy(normalizedHost) } func IsTenancy(host string) bool { - return strings.HasSuffix(host, "."+tenancyHost) + normalizedHost := normalizeHostname(host) + return strings.HasSuffix(normalizedHost, "."+tenancyHost) } func normalizeHostname(host string) string { From 8d04841c2a216eee742f24307d9f74758e64bb93 Mon Sep 17 00:00:00 2001 From: Andy Feller Date: Wed, 10 Apr 2024 08:18:48 -0400 Subject: [PATCH 3/5] Document newly exported functions --- pkg/auth/auth.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/auth/auth.go b/pkg/auth/auth.go index eb4236a..6ab996f 100644 --- a/pkg/auth/auth.go +++ b/pkg/auth/auth.go @@ -152,11 +152,15 @@ func defaultHost(cfg *config.Config) (string, string) { // TenancyHost is the domain name of a tenancy GitHub instance. const tenancyHost = "ghe.com" +// 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) } +// 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) From 9afca849469bb1ae5523abe34f5814806b7d72ac Mon Sep 17 00:00:00 2001 From: Andy Feller Date: Mon, 29 Apr 2024 08:15:34 -0400 Subject: [PATCH 4/5] Expand tenancy test cases Including API cases for tenancy checks, ensuring that hosts are normalized. --- pkg/auth/auth_test.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pkg/auth/auth_test.go b/pkg/auth/auth_test.go index 2ddc146..ec1e821 100644 --- a/pkg/auth/auth_test.go +++ b/pkg/auth/auth_test.go @@ -273,11 +273,21 @@ func TestIsTenancy(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", @@ -288,6 +298,11 @@ func TestIsTenancy(t *testing.T) { host: "tenant.ghe.com", wantOut: true, }, + { + name: "tenant API", + host: "api.tenant.ghe.com", + wantOut: true, + }, } for _, tt := range tests { From 8a3c02a69314657d8b52cd69e510c0b771bff88b Mon Sep 17 00:00:00 2001 From: Andy Feller Date: Mon, 29 Apr 2024 08:17:17 -0400 Subject: [PATCH 5/5] Expand IsEnterprise test cases Adding API hosts to TestIsEnterprise to ensure that normalized hosts work as expected. --- pkg/auth/auth_test.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pkg/auth/auth_test.go b/pkg/auth/auth_test.go index ec1e821..0588a1c 100644 --- a/pkg/auth/auth_test.go +++ b/pkg/auth/auth_test.go @@ -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", @@ -252,6 +262,11 @@ 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 {