Skip to content

Commit 45fa8a4

Browse files
Merge pull request #151 from cli/wm/treat-tenancy-as-non-enterprise
Treat tenancy as non enterprise
2 parents d88d88f + 50645d5 commit 45fa8a4

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

pkg/auth/auth.go

+25-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package auth
44

55
import (
6+
"fmt"
67
"os"
78
"os/exec"
89
"strconv"
@@ -148,8 +149,15 @@ func defaultHost(cfg *config.Config) (string, string) {
148149
return github, defaultSource
149150
}
150151

152+
// TenancyHost is the domain name of a tenancy GitHub instance.
153+
const tenancyHost = "ghe.com"
154+
151155
func isEnterprise(host string) bool {
152-
return host != github && host != localhost
156+
return host != github && host != localhost && !isTenancy(host)
157+
}
158+
159+
func isTenancy(host string) bool {
160+
return strings.HasSuffix(host, "."+tenancyHost)
153161
}
154162

155163
func normalizeHostname(host string) string {
@@ -160,5 +168,21 @@ func normalizeHostname(host string) string {
160168
if strings.HasSuffix(hostname, "."+localhost) {
161169
return localhost
162170
}
171+
// This has been copied over from the cli/cli NormalizeHostname function
172+
// to ensure compatible behaviour but we don't fully understand when or
173+
// why it would be useful here. We can't see what harm will come of
174+
// duplicating the logic.
175+
if before, found := cutSuffix(hostname, "."+tenancyHost); found {
176+
idx := strings.LastIndex(before, ".")
177+
return fmt.Sprintf("%s.%s", before[idx+1:], tenancyHost)
178+
}
163179
return hostname
164180
}
181+
182+
// Backport strings.CutSuffix from Go 1.20.
183+
func cutSuffix(s, suffix string) (string, bool) {
184+
if !strings.HasSuffix(s, suffix) {
185+
return s, false
186+
}
187+
return s[:len(s)-len(suffix)], true
188+
}

pkg/auth/auth_test.go

+45-2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,30 @@ func TestTokenForHost(t *testing.T) {
8484
wantToken: "yyyyyyyyyyyyyyyyyyyy",
8585
wantSource: "oauth_token",
8686
},
87+
{
88+
name: "token for tenant with GH_TOKEN, GITHUB_TOKEN, and config token",
89+
host: "tenant.ghe.com",
90+
ghToken: "GH_TOKEN",
91+
githubToken: "GITHUB_TOKEN",
92+
config: testHostsConfig(),
93+
wantToken: "GH_TOKEN",
94+
wantSource: "GH_TOKEN",
95+
},
96+
{
97+
name: "token for tenant with GITHUB_TOKEN, and config token",
98+
host: "tenant.ghe.com",
99+
githubToken: "GITHUB_TOKEN",
100+
config: testHostsConfig(),
101+
wantToken: "GITHUB_TOKEN",
102+
wantSource: "GITHUB_TOKEN",
103+
},
104+
{
105+
name: "token for tenant with config token",
106+
host: "tenant.ghe.com",
107+
config: testHostsConfig(),
108+
wantToken: "zzzzzzzzzzzzzzzzzzzz",
109+
wantSource: "oauth_token",
110+
},
87111
}
88112

89113
for _, tt := range tests {
@@ -171,7 +195,7 @@ func TestKnownHosts(t *testing.T) {
171195
{
172196
name: "includes authenticated hosts",
173197
config: testHostsConfig(),
174-
wantHosts: []string{"github.com", "enterprise.com"},
198+
wantHosts: []string{"github.com", "enterprise.com", "tenant.ghe.com"},
175199
},
176200
{
177201
name: "includes default host if environment auth token",
@@ -184,7 +208,7 @@ func TestKnownHosts(t *testing.T) {
184208
config: testHostsConfig(),
185209
ghHost: "test.com",
186210
ghToken: "TOKEN",
187-
wantHosts: []string{"test.com", "github.com", "enterprise.com"},
211+
wantHosts: []string{"test.com", "github.com", "enterprise.com", "tenant.ghe.com"},
188212
},
189213
}
190214

@@ -223,6 +247,11 @@ func TestIsEnterprise(t *testing.T) {
223247
host: "mygithub.com",
224248
wantOut: true,
225249
},
250+
{
251+
name: "tenant",
252+
host: "tenant.ghe.com",
253+
wantOut: false,
254+
},
226255
}
227256

228257
for _, tt := range tests {
@@ -259,6 +288,16 @@ func TestNormalizeHostname(t *testing.T) {
259288
host: "mygithub.com",
260289
wantHost: "mygithub.com",
261290
},
291+
{
292+
name: "bare tenant",
293+
host: "tenant.ghe.com",
294+
wantHost: "tenant.ghe.com",
295+
},
296+
{
297+
name: "subdomained tenant",
298+
host: "api.tenant.ghe.com",
299+
wantHost: "tenant.ghe.com",
300+
},
262301
}
263302

264303
for _, tt := range tests {
@@ -296,6 +335,10 @@ hosts:
296335
user: user2
297336
oauth_token: yyyyyyyyyyyyyyyyyyyy
298337
git_protocol: https
338+
tenant.ghe.com:
339+
user: user3
340+
oauth_token: zzzzzzzzzzzzzzzzzzzz
341+
git_protocol: https
299342
`
300343
return config.ReadFromString(data)
301344
}

0 commit comments

Comments
 (0)