Skip to content

Commit e21c510

Browse files
committed
Properly check env auth tokens in CheckAuth
1 parent 34d549e commit e21c510

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

internal/config/from_env.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,10 @@ func AuthTokenFromEnv(hostname string) (string, string) {
7878

7979
return os.Getenv(GITHUB_TOKEN), GITHUB_TOKEN
8080
}
81+
82+
func AuthTokenProvidedFromEnv() bool {
83+
return os.Getenv(GH_ENTERPRISE_TOKEN) != "" ||
84+
os.Getenv(GITHUB_ENTERPRISE_TOKEN) != "" ||
85+
os.Getenv(GH_TOKEN) != "" ||
86+
os.Getenv(GITHUB_TOKEN) != ""
87+
}

internal/config/from_env_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,60 @@ func TestInheritEnv(t *testing.T) {
283283
})
284284
}
285285
}
286+
287+
func TestAuthTokenProvidedFromEnv(t *testing.T) {
288+
orig_GITHUB_TOKEN := os.Getenv("GITHUB_TOKEN")
289+
orig_GITHUB_ENTERPRISE_TOKEN := os.Getenv("GITHUB_ENTERPRISE_TOKEN")
290+
orig_GH_TOKEN := os.Getenv("GH_TOKEN")
291+
orig_GH_ENTERPRISE_TOKEN := os.Getenv("GH_ENTERPRISE_TOKEN")
292+
t.Cleanup(func() {
293+
os.Setenv("GITHUB_TOKEN", orig_GITHUB_TOKEN)
294+
os.Setenv("GITHUB_ENTERPRISE_TOKEN", orig_GITHUB_ENTERPRISE_TOKEN)
295+
os.Setenv("GH_TOKEN", orig_GH_TOKEN)
296+
os.Setenv("GH_ENTERPRISE_TOKEN", orig_GH_ENTERPRISE_TOKEN)
297+
})
298+
299+
tests := []struct {
300+
name string
301+
GITHUB_TOKEN string
302+
GITHUB_ENTERPRISE_TOKEN string
303+
GH_TOKEN string
304+
GH_ENTERPRISE_TOKEN string
305+
provided bool
306+
}{
307+
{
308+
name: "no env tokens",
309+
provided: false,
310+
},
311+
{
312+
name: "GH_TOKEN",
313+
GH_TOKEN: "TOKEN",
314+
provided: true,
315+
},
316+
{
317+
name: "GITHUB_TOKEN",
318+
GITHUB_TOKEN: "TOKEN",
319+
provided: true,
320+
},
321+
{
322+
name: "GH_ENTERPRISE_TOKEN",
323+
GH_ENTERPRISE_TOKEN: "TOKEN",
324+
provided: true,
325+
},
326+
{
327+
name: "GITHUB_ENTERPRISE_TOKEN",
328+
GITHUB_ENTERPRISE_TOKEN: "TOKEN",
329+
provided: true,
330+
},
331+
}
332+
333+
for _, tt := range tests {
334+
t.Run(tt.name, func(t *testing.T) {
335+
os.Setenv("GITHUB_TOKEN", tt.GITHUB_TOKEN)
336+
os.Setenv("GITHUB_ENTERPRISE_TOKEN", tt.GITHUB_ENTERPRISE_TOKEN)
337+
os.Setenv("GH_TOKEN", tt.GH_TOKEN)
338+
os.Setenv("GH_ENTERPRISE_TOKEN", tt.GH_ENTERPRISE_TOKEN)
339+
assert.Equal(t, tt.provided, AuthTokenProvidedFromEnv())
340+
})
341+
}
342+
}

pkg/cmdutil/auth_check.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ func DisableAuthCheck(cmd *cobra.Command) {
1717
}
1818

1919
func CheckAuth(cfg config.Config) bool {
20+
if config.AuthTokenProvidedFromEnv() {
21+
return true
22+
}
23+
2024
hosts, err := cfg.Hosts()
2125
if err != nil {
2226
return false

pkg/cmdutil/auth_check_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,62 @@
11
package cmdutil
22

33
import (
4+
"os"
45
"testing"
56

67
"github.com/cli/cli/internal/config"
78
"github.com/stretchr/testify/assert"
89
)
910

1011
func Test_CheckAuth(t *testing.T) {
12+
orig_GITHUB_TOKEN := os.Getenv("GITHUB_TOKEN")
13+
t.Cleanup(func() {
14+
os.Setenv("GITHUB_TOKEN", orig_GITHUB_TOKEN)
15+
})
16+
1117
tests := []struct {
1218
name string
1319
cfg func(config.Config)
20+
envToken bool
1421
expected bool
1522
}{
1623
{
1724
name: "no hosts",
1825
cfg: func(c config.Config) {},
26+
envToken: false,
1927
expected: false,
2028
},
29+
{name: "no hosts, env auth token",
30+
cfg: func(c config.Config) {},
31+
envToken: true,
32+
expected: true,
33+
},
2134
{
2235
name: "host, no token",
2336
cfg: func(c config.Config) {
2437
_ = c.Set("github.com", "oauth_token", "")
2538
},
39+
envToken: false,
2640
expected: false,
2741
},
2842
{
2943
name: "host, token",
3044
cfg: func(c config.Config) {
3145
_ = c.Set("github.com", "oauth_token", "a token")
3246
},
47+
envToken: false,
3348
expected: true,
3449
},
3550
}
3651

3752
for _, tt := range tests {
3853
t.Run(tt.name, func(t *testing.T) {
54+
if tt.envToken {
55+
os.Setenv("GITHUB_TOKEN", "TOKEN")
56+
} else {
57+
os.Setenv("GITHUB_TOKEN", "")
58+
}
59+
3960
cfg := config.NewBlankConfig()
4061
tt.cfg(cfg)
4162
result := CheckAuth(cfg)

0 commit comments

Comments
 (0)