Skip to content

Commit

Permalink
Empty auth token env variables are equal to being unset
Browse files Browse the repository at this point in the history
  • Loading branch information
samcoe committed Nov 12, 2020
1 parent 414de33 commit 2eb40f8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 38 deletions.
4 changes: 2 additions & 2 deletions cmd/gh/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ func basicClient(currentVersion string) (*api.Client, error) {
}
opts = append(opts, api.AddHeader("User-Agent", fmt.Sprintf("GitHub CLI %s", currentVersion)))

token, _, found := config.AuthTokenFromEnv(ghinstance.Default())
if !found {
token, _ := config.AuthTokenFromEnv(ghinstance.Default())
if token == "" {
if c, err := config.ParseDefaultConfig(); err == nil {
token, _ = c.Get(ghinstance.Default(), "oauth_token")
}
Expand Down
24 changes: 11 additions & 13 deletions internal/config/from_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func (c *envConfig) Hosts() ([]string, error) {
hasDefault = true
}
}
_, _, found := AuthTokenFromEnv(ghinstance.Default())
if (err != nil || !hasDefault) && found {
token, _ := AuthTokenFromEnv(ghinstance.Default())
if (err != nil || !hasDefault) && token != "" {
hosts = append([]string{ghinstance.Default()}, hosts...)
return hosts, nil
}
Expand All @@ -45,7 +45,7 @@ func (c *envConfig) Get(hostname, key string) (string, error) {

func (c *envConfig) GetWithSource(hostname, key string) (string, string, error) {
if hostname != "" && key == "oauth_token" {
if token, env, found := AuthTokenFromEnv(hostname); found {
if token, env := AuthTokenFromEnv(hostname); token != "" {
return token, env, nil
}
}
Expand All @@ -55,28 +55,26 @@ func (c *envConfig) GetWithSource(hostname, key string) (string, string, error)

func (c *envConfig) CheckWriteable(hostname, key string) error {
if hostname != "" && key == "oauth_token" {
if _, env, found := AuthTokenFromEnv(hostname); found {
if token, env := AuthTokenFromEnv(hostname); token != "" {
return fmt.Errorf("read-only token in %s cannot be modified", env)
}
}

return c.Config.CheckWriteable(hostname, key)
}

func AuthTokenFromEnv(hostname string) (string, string, bool) {
func AuthTokenFromEnv(hostname string) (string, string) {
if ghinstance.IsEnterprise(hostname) {
if token, found := os.LookupEnv(GH_ENTERPRISE_TOKEN); found {
return token, GH_ENTERPRISE_TOKEN, found
if token := os.Getenv(GH_ENTERPRISE_TOKEN); token != "" {
return token, GH_ENTERPRISE_TOKEN
}

token, found := os.LookupEnv(GITHUB_ENTERPRISE_TOKEN)
return token, GITHUB_ENTERPRISE_TOKEN, found
return os.Getenv(GITHUB_ENTERPRISE_TOKEN), GITHUB_ENTERPRISE_TOKEN
}

if token, found := os.LookupEnv(GH_TOKEN); found {
return token, GH_TOKEN, found
if token := os.Getenv(GH_TOKEN); token != "" {
return token, GH_TOKEN
}

token, found := os.LookupEnv(GITHUB_TOKEN)
return token, GITHUB_TOKEN, found
return os.Getenv(GITHUB_TOKEN), GITHUB_TOKEN
}
27 changes: 4 additions & 23 deletions internal/config/from_env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,29 +260,10 @@ func TestInheritEnv(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.GITHUB_TOKEN != "" {
os.Setenv("GITHUB_TOKEN", tt.GITHUB_TOKEN)
} else {
os.Unsetenv("GITHUB_TOKEN")
}

if tt.GITHUB_ENTERPRISE_TOKEN != "" {
os.Setenv("GITHUB_ENTERPRISE_TOKEN", tt.GITHUB_ENTERPRISE_TOKEN)
} else {
os.Unsetenv("GITHUB_ENTERPRISE_TOKEN")
}

if tt.GH_TOKEN != "" {
os.Setenv("GH_TOKEN", tt.GH_TOKEN)
} else {
os.Unsetenv("GH_TOKEN")
}

if tt.GH_ENTERPRISE_TOKEN != "" {
os.Setenv("GH_ENTERPRISE_TOKEN", tt.GH_ENTERPRISE_TOKEN)
} else {
os.Unsetenv("GH_ENTERPRISE_TOKEN")
}
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)

baseCfg := NewFromString(tt.baseConfig)
cfg := InheritEnv(baseCfg)
Expand Down

0 comments on commit 2eb40f8

Please sign in to comment.