Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 Logging a warning if readGitHubTokens finds several values which clash. #4483

Merged
merged 5 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions clients/githubrepo/roundtripper/tokens/accessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package tokens

import (
"log"
"os"
"strings"
)
Expand All @@ -33,13 +34,33 @@
Release(uint64)
}

var logDuplicateTokenWarning = func(firstName string, clashingName string) {
var stringBuilder strings.Builder
stringBuilder.WriteString("Warning: PATs stored in env variables ")
stringBuilder.WriteString(firstName)
stringBuilder.WriteString(" and ")
stringBuilder.WriteString(clashingName)
stringBuilder.WriteString(" differ. Scorecard will use the former.")
log.Println(stringBuilder.String())

Check warning on line 44 in clients/githubrepo/roundtripper/tokens/accessor.go

View check run for this annotation

Codecov / codecov/patch

clients/githubrepo/roundtripper/tokens/accessor.go#L37-L44

Added lines #L37 - L44 were not covered by tests
}

func readGitHubTokens() (string, bool) {
var firstName, firstToken string
for _, name := range githubAuthTokenEnvVars {
if token, exists := os.LookupEnv(name); exists && token != "" {
return token, exists
if firstName == "" {
firstName = name
firstToken = token
} else if token != firstToken {
logDuplicateTokenWarning(firstName, name)
}
}
}
return "", false
if firstName == "" {
return "", false
} else {
return firstToken, true
}
}

// MakeTokenAccessor is a factory function of TokenAccessor.
Expand Down
98 changes: 93 additions & 5 deletions clients/githubrepo/roundtripper/tokens/accessor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ package tokens
import (
"net/http/httptest"
"net/rpc"
"os"
"strings"
"testing"
)

//nolint:paralleltest // test uses t.Setenv
//nolint:paralleltest // test uses t.Setenv indirectly
func TestMakeTokenAccessor(t *testing.T) {
tests := []struct {
name string
Expand All @@ -40,10 +41,7 @@ func TestMakeTokenAccessor(t *testing.T) {
useServer: true,
},
}
// clear all env variables devs may have defined, or the test will fail locally
for _, envVar := range githubAuthTokenEnvVars {
t.Setenv(envVar, "")
}
unsetTokens(t)
t.Setenv(githubAuthServer, "")
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -91,3 +89,93 @@ func testServer(t *testing.T) {
t.Errorf("MakeTokenAccessor() = nil, want not nil")
}
}

func TestClashingTokensDisplayWarning(t *testing.T) {
unsetTokens(t)

someToken := "test_token"
otherToken := "clashing_token"
t.Setenv("GITHUB_AUTH_TOKEN", someToken)
t.Setenv("GITHUB_TOKEN", otherToken)

warningCalled := false
originalLogWarning := logDuplicateTokenWarning
logDuplicateTokenWarning = func(firstName string, clashingName string) {
warningCalled = true
}
defer func() { logDuplicateTokenWarning = originalLogWarning }()

token, exists := readGitHubTokens()

if token != someToken {
t.Errorf("Received wrong token")
}
if !exists {
t.Errorf("Token is expected to exist")
}
if !warningCalled {
t.Errorf("Expected logWarning to be called for clashing tokens, but it was not.")
}
}

func TestConsistentTokensDoNotDisplayWarning(t *testing.T) {
unsetTokens(t)

someToken := "test_token"
t.Setenv("GITHUB_AUTH_TOKEN", someToken)
t.Setenv("GITHUB_TOKEN", someToken)

warningCalled := false
originalLogWarning := logDuplicateTokenWarning
logDuplicateTokenWarning = func(firstName string, clashingName string) {
warningCalled = true
}
defer func() { logDuplicateTokenWarning = originalLogWarning }()

token, exists := readGitHubTokens()

if token != someToken {
t.Errorf("Received wrong token")
}
if !exists {
t.Errorf("Token is expected to exist")
}
if warningCalled {
t.Errorf("Expected logWarning to not have been called for consistent tokens, but it was.")
}
}

//nolint:paralleltest // test uses t.Setenv indirectly
func TestNoTokensDoNoDisplayWarning(t *testing.T) {
aunovis-heidrich marked this conversation as resolved.
Show resolved Hide resolved
unsetTokens(t)

warningCalled := false
originalLogWarning := logDuplicateTokenWarning
logDuplicateTokenWarning = func(firstName string, clashingName string) {
warningCalled = true
}
defer func() { logDuplicateTokenWarning = originalLogWarning }()

token, exists := readGitHubTokens()

if token != "" {
t.Errorf("Scorecard found a token somewhere")
}
if exists {
t.Errorf("Token is not expected to exist")
}
if warningCalled {
t.Errorf("Expected logWarning to not have been called for no set tokens, but it was not.")
}
}

// temporarily unset all of the github token env vars,
// as tests may otherwise fail depending on the local environment.
func unsetTokens(t *testing.T) {
t.Helper()
for _, name := range githubAuthTokenEnvVars {
// equivalent to t.Unsetenv (which does not exist)
t.Setenv(name, "")
os.Unsetenv(name)
}
}
Loading