From e21c5100fa02696073100a9a94684bd889ad4c6f Mon Sep 17 00:00:00 2001 From: Sam Coe Date: Tue, 1 Dec 2020 11:44:14 -0500 Subject: [PATCH] Properly check env auth tokens in CheckAuth --- internal/config/from_env.go | 7 ++++ internal/config/from_env_test.go | 57 ++++++++++++++++++++++++++++++++ pkg/cmdutil/auth_check.go | 4 +++ pkg/cmdutil/auth_check_test.go | 21 ++++++++++++ 4 files changed, 89 insertions(+) diff --git a/internal/config/from_env.go b/internal/config/from_env.go index da4ac1536..333dc879b 100644 --- a/internal/config/from_env.go +++ b/internal/config/from_env.go @@ -78,3 +78,10 @@ func AuthTokenFromEnv(hostname string) (string, string) { return os.Getenv(GITHUB_TOKEN), GITHUB_TOKEN } + +func AuthTokenProvidedFromEnv() bool { + return os.Getenv(GH_ENTERPRISE_TOKEN) != "" || + os.Getenv(GITHUB_ENTERPRISE_TOKEN) != "" || + os.Getenv(GH_TOKEN) != "" || + os.Getenv(GITHUB_TOKEN) != "" +} diff --git a/internal/config/from_env_test.go b/internal/config/from_env_test.go index baeb63194..c280b8a90 100644 --- a/internal/config/from_env_test.go +++ b/internal/config/from_env_test.go @@ -283,3 +283,60 @@ func TestInheritEnv(t *testing.T) { }) } } + +func TestAuthTokenProvidedFromEnv(t *testing.T) { + orig_GITHUB_TOKEN := os.Getenv("GITHUB_TOKEN") + orig_GITHUB_ENTERPRISE_TOKEN := os.Getenv("GITHUB_ENTERPRISE_TOKEN") + orig_GH_TOKEN := os.Getenv("GH_TOKEN") + orig_GH_ENTERPRISE_TOKEN := os.Getenv("GH_ENTERPRISE_TOKEN") + t.Cleanup(func() { + os.Setenv("GITHUB_TOKEN", orig_GITHUB_TOKEN) + os.Setenv("GITHUB_ENTERPRISE_TOKEN", orig_GITHUB_ENTERPRISE_TOKEN) + os.Setenv("GH_TOKEN", orig_GH_TOKEN) + os.Setenv("GH_ENTERPRISE_TOKEN", orig_GH_ENTERPRISE_TOKEN) + }) + + tests := []struct { + name string + GITHUB_TOKEN string + GITHUB_ENTERPRISE_TOKEN string + GH_TOKEN string + GH_ENTERPRISE_TOKEN string + provided bool + }{ + { + name: "no env tokens", + provided: false, + }, + { + name: "GH_TOKEN", + GH_TOKEN: "TOKEN", + provided: true, + }, + { + name: "GITHUB_TOKEN", + GITHUB_TOKEN: "TOKEN", + provided: true, + }, + { + name: "GH_ENTERPRISE_TOKEN", + GH_ENTERPRISE_TOKEN: "TOKEN", + provided: true, + }, + { + name: "GITHUB_ENTERPRISE_TOKEN", + GITHUB_ENTERPRISE_TOKEN: "TOKEN", + provided: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + os.Setenv("GITHUB_TOKEN", tt.GITHUB_TOKEN) + os.Setenv("GITHUB_ENTERPRISE_TOKEN", tt.GITHUB_ENTERPRISE_TOKEN) + os.Setenv("GH_TOKEN", tt.GH_TOKEN) + os.Setenv("GH_ENTERPRISE_TOKEN", tt.GH_ENTERPRISE_TOKEN) + assert.Equal(t, tt.provided, AuthTokenProvidedFromEnv()) + }) + } +} diff --git a/pkg/cmdutil/auth_check.go b/pkg/cmdutil/auth_check.go index 5d40d0143..10df9fade 100644 --- a/pkg/cmdutil/auth_check.go +++ b/pkg/cmdutil/auth_check.go @@ -17,6 +17,10 @@ func DisableAuthCheck(cmd *cobra.Command) { } func CheckAuth(cfg config.Config) bool { + if config.AuthTokenProvidedFromEnv() { + return true + } + hosts, err := cfg.Hosts() if err != nil { return false diff --git a/pkg/cmdutil/auth_check_test.go b/pkg/cmdutil/auth_check_test.go index 22b8ff5d4..2798750f0 100644 --- a/pkg/cmdutil/auth_check_test.go +++ b/pkg/cmdutil/auth_check_test.go @@ -1,6 +1,7 @@ package cmdutil import ( + "os" "testing" "github.com/cli/cli/internal/config" @@ -8,21 +9,34 @@ import ( ) func Test_CheckAuth(t *testing.T) { + orig_GITHUB_TOKEN := os.Getenv("GITHUB_TOKEN") + t.Cleanup(func() { + os.Setenv("GITHUB_TOKEN", orig_GITHUB_TOKEN) + }) + tests := []struct { name string cfg func(config.Config) + envToken bool expected bool }{ { name: "no hosts", cfg: func(c config.Config) {}, + envToken: false, expected: false, }, + {name: "no hosts, env auth token", + cfg: func(c config.Config) {}, + envToken: true, + expected: true, + }, { name: "host, no token", cfg: func(c config.Config) { _ = c.Set("github.com", "oauth_token", "") }, + envToken: false, expected: false, }, { @@ -30,12 +44,19 @@ func Test_CheckAuth(t *testing.T) { cfg: func(c config.Config) { _ = c.Set("github.com", "oauth_token", "a token") }, + envToken: false, expected: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + if tt.envToken { + os.Setenv("GITHUB_TOKEN", "TOKEN") + } else { + os.Setenv("GITHUB_TOKEN", "") + } + cfg := config.NewBlankConfig() tt.cfg(cfg) result := CheckAuth(cfg)