Skip to content

Commit

Permalink
replace env var literals with consts
Browse files Browse the repository at this point in the history
  • Loading branch information
quartzmo committed Apr 1, 2024
1 parent 836ae31 commit 317a23d
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 29 deletions.
2 changes: 1 addition & 1 deletion auth/credentials/detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ func TestDefaultCredentials_ExternalAccountAuthorizedUserKey(t *testing.T) {
}

func TestDefaultCredentials_Fails(t *testing.T) {
t.Setenv("GOOGLE_APPLICATION_CREDENTIALS", "nothingToSeeHere")
t.Setenv(credsfile.GoogleAppCredsEnvVar, "nothingToSeeHere")
t.Setenv("HOME", "nothingToSeeHere")
t.Setenv("APPDATA", "nothingToSeeHere")
allowOnGCECheck = false
Expand Down
12 changes: 6 additions & 6 deletions auth/credentials/downscope/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ import (
"cloud.google.com/go/auth"
"cloud.google.com/go/auth/credentials"
"cloud.google.com/go/auth/credentials/downscope"
"cloud.google.com/go/auth/internal/credsfile"
"cloud.google.com/go/auth/internal/testutil"
"cloud.google.com/go/auth/internal/testutil/testgcs"
)

const (
rootTokenScope = "https://www.googleapis.com/auth/cloud-platform"
envServiceAccountFile = "GOOGLE_APPLICATION_CREDENTIALS"
object1 = "cab-first-c45wknuy.txt"
object2 = "cab-second-c45wknuy.txt"
bucket = "dulcet-port-762"
rootTokenScope = "https://www.googleapis.com/auth/cloud-platform"
object1 = "cab-first-c45wknuy.txt"
object2 = "cab-second-c45wknuy.txt"
bucket = "dulcet-port-762"
)

func TestDownscopedToken(t *testing.T) {
testutil.IntegrationTestCheck(t)
creds, err := credentials.DetectDefault(&credentials.DetectOptions{
CredentialsFile: os.Getenv(envServiceAccountFile),
CredentialsFile: os.Getenv(credsfile.GoogleAppCredsEnvVar),
Scopes: []string{rootTokenScope},
})
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions auth/credentials/idtoken/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ import (

"cloud.google.com/go/auth/credentials/idtoken"
"cloud.google.com/go/auth/httptransport"
"cloud.google.com/go/auth/internal/credsfile"
"cloud.google.com/go/auth/internal/testutil"
)

const (
envCredentialFile = "GOOGLE_APPLICATION_CREDENTIALS"
aud = "http://example.com"
aud = "http://example.com"
)

func TestNewCredentials_CredentialsFile(t *testing.T) {
testutil.IntegrationTestCheck(t)
ctx := context.Background()
ts, err := idtoken.NewCredentials(&idtoken.Options{
Audience: "http://example.com",
CredentialsFile: os.Getenv(envCredentialFile),
CredentialsFile: os.Getenv(credsfile.GoogleAppCredsEnvVar),
})
if err != nil {
t.Fatalf("unable to create credentials: %v", err)
Expand All @@ -63,7 +63,7 @@ func TestNewCredentials_CredentialsFile(t *testing.T) {
func TestNewCredentials_CredentialsJSON(t *testing.T) {
testutil.IntegrationTestCheck(t)
ctx := context.Background()
b, err := os.ReadFile(os.Getenv(envCredentialFile))
b, err := os.ReadFile(os.Getenv(credsfile.GoogleAppCredsEnvVar))
if err != nil {
log.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions auth/credentials/impersonate/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ import (
"cloud.google.com/go/auth/credentials"
"cloud.google.com/go/auth/credentials/idtoken"
"cloud.google.com/go/auth/credentials/impersonate"
"cloud.google.com/go/auth/internal/credsfile"
"cloud.google.com/go/auth/internal/testutil"
"cloud.google.com/go/auth/internal/testutil/testgcs"
)

const (
envAppCreds = "GOOGLE_APPLICATION_CREDENTIALS"
envProjectID = "GCLOUD_TESTS_GOLANG_PROJECT_ID"
envReaderCreds = "GCLOUD_TESTS_IMPERSONATE_READER_KEY"
envReaderEmail = "GCLOUD_TESTS_IMPERSONATE_READER_EMAIL"
Expand All @@ -52,7 +52,7 @@ var (
func TestMain(m *testing.M) {
flag.Parse()
random = rand.New(rand.NewSource(time.Now().UnixNano()))
baseKeyFile = os.Getenv(envAppCreds)
baseKeyFile = os.Getenv(credsfile.GoogleAppCredsEnvVar)
projectID = os.Getenv(envProjectID)
readerKeyFile = os.Getenv(envReaderCreds)
readerEmail = os.Getenv(envReaderEmail)
Expand Down
2 changes: 1 addition & 1 deletion auth/grpctransport/grpctransport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func TestGrpcCredentialsProvider_GetClientUniverseDomain(t *testing.T) {
func TestNewClient_DetectedServiceAccount(t *testing.T) {
testQuota := "testquota"
wantHeader := "bar"
t.Setenv("GOOGLE_CLOUD_QUOTA_PROJECT", testQuota)
t.Setenv(internal.QuotaProjectEnvVar, testQuota)
l, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatal(err)
Expand Down
4 changes: 2 additions & 2 deletions auth/httptransport/httptransport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func TestOptions_ResolveDetectOptions(t *testing.T) {
func TestNewClient_DetectedServiceAccount(t *testing.T) {
testQuota := "testquota"
wantHeader := "bar"
t.Setenv("GOOGLE_CLOUD_QUOTA_PROJECT", testQuota)
t.Setenv(internal.QuotaProjectEnvVar, testQuota)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if got := r.Header.Get("Authorization"); got == "" {
t.Errorf(`got "", want an auth token`)
Expand Down Expand Up @@ -303,7 +303,7 @@ func TestNewClient_APIKey(t *testing.T) {
testQuota := "testquota"
apiKey := "thereisnospoon"
wantHeader := "bar"
t.Setenv("GOOGLE_CLOUD_QUOTA_PROJECT", testQuota)
t.Setenv(internal.QuotaProjectEnvVar, testQuota)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
got := r.URL.Query().Get("key")
if got != apiKey {
Expand Down
8 changes: 5 additions & 3 deletions auth/internal/credsfile/credsfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ import (
)

const (
envGoogApCreds = "GOOGLE_APPLICATION_CREDENTIALS"
userCredsFilename = "application_default_credentials.json"
// GoogleAppCredsEnvVar is the environment variable for setting the
// application default credentials.
GoogleAppCredsEnvVar = "GOOGLE_APPLICATION_CREDENTIALS"
userCredsFilename = "application_default_credentials.json"
)

// CredentialType represents different credential filetypes Google credentials
Expand Down Expand Up @@ -80,7 +82,7 @@ func GetFileNameFromEnv(override string) string {
if override != "" {
return override
}
return os.Getenv(envGoogApCreds)
return os.Getenv(GoogleAppCredsEnvVar)
}

// GetWellKnownFileName tries to locate the filepath for the user credential
Expand Down
6 changes: 4 additions & 2 deletions auth/internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ const (
// TokenTypeBearer is the auth header prefix for bearer tokens.
TokenTypeBearer = "Bearer"

quotaProjectEnvVar = "GOOGLE_CLOUD_QUOTA_PROJECT"
// QuotaProjectEnvVar is the environment variable for setting the quota
// project.
QuotaProjectEnvVar = "GOOGLE_CLOUD_QUOTA_PROJECT"
projectEnvVar = "GOOGLE_CLOUD_PROJECT"
maxBodySize = 1 << 20

Expand Down Expand Up @@ -82,7 +84,7 @@ func GetQuotaProject(b []byte, override string) string {
if override != "" {
return override
}
if env := os.Getenv(quotaProjectEnvVar); env != "" {
if env := os.Getenv(QuotaProjectEnvVar); env != "" {
return env
}
if b == nil {
Expand Down
16 changes: 8 additions & 8 deletions auth/internal/transport/cba_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,9 @@ func TestGetGRPCTransportConfigAndEndpoint(t *testing.T) {
httpGetMetadataMTLSConfig = tc.s2ARespFn
mtlsEndpointEnabledForS2A = tc.mtlsEnabledFn
if tc.opts.ClientCertProvider != nil {
t.Setenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "true")
t.Setenv(googleAPIUseCertSource, "true")
} else {
t.Setenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false")
t.Setenv(googleAPIUseCertSource, "false")
}
_, endpoint, _ := GetGRPCTransportCredsAndEndpoint(tc.opts)
if tc.want != endpoint {
Expand Down Expand Up @@ -432,9 +432,9 @@ func TestGetHTTPTransportConfig_S2a(t *testing.T) {
httpGetMetadataMTLSConfig = tc.s2aFn
mtlsEndpointEnabledForS2A = tc.mtlsEnabledFn
if tc.opts.ClientCertProvider != nil {
t.Setenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "true")
t.Setenv(googleAPIUseCertSource, "true")
} else {
t.Setenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false")
t.Setenv(googleAPIUseCertSource, "false")
}
_, dialFunc, err := GetHTTPTransportConfig(tc.opts)
if err != nil {
Expand Down Expand Up @@ -521,9 +521,9 @@ func TestGetTransportConfig_UniverseDomain(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.opts.ClientCertProvider != nil {
t.Setenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "true")
t.Setenv(googleAPIUseCertSource, "true")
} else {
t.Setenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false")
t.Setenv(googleAPIUseCertSource, "false")
}
config, err := getTransportConfig(tc.opts)
if err != nil {
Expand Down Expand Up @@ -627,9 +627,9 @@ func TestGetGRPCTransportCredsAndEndpoint_UniverseDomain(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.opts.ClientCertProvider != nil {
t.Setenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "true")
t.Setenv(googleAPIUseCertSource, "true")
} else {
t.Setenv("GOOGLE_API_USE_CLIENT_CERTIFICATE", "false")
t.Setenv(googleAPIUseCertSource, "false")
}
_, endpoint, err := GetGRPCTransportCredsAndEndpoint(tc.opts)
if err != nil {
Expand Down

0 comments on commit 317a23d

Please sign in to comment.