Skip to content

Commit b84ea44

Browse files
x509 cert fix (#15691) (#25144)
[upstream:525cea9d14cc8358f71a52c329f5fa79955de4b8] Signed-off-by: Modular Magician <magic-modules@google.com>
1 parent a43c6a5 commit b84ea44

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

.changelog/15691.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
provider: an issue preventing X.509 certificates from being used for authentication when supplied as Application Default Credentials as been resolved
3+
```

google/transport/config.go

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2587,9 +2587,18 @@ func (c *Config) GetCredentials(clientScopes []string, initialCredentialsOnly bo
25872587
return googleoauth.Credentials{}, fmt.Errorf("Attempted to load application default credentials since neither `credentials` nor `access_token` was set in the provider block. No credentials loaded. To use your gcloud credentials, run 'gcloud auth application-default login'. Original error: %w", err)
25882588
}
25892589
} else {
2590-
creds, err = transport.Creds(context.Background(), option.WithScopes(clientScopes...))
2591-
if err != nil {
2592-
return googleoauth.Credentials{}, fmt.Errorf("Attempted to load application default credentials since neither `credentials` nor `access_token` was set in the provider block. No credentials loaded. To use your gcloud credentials, run 'gcloud auth application-default login'. Original error: %w", err)
2590+
if AreADCCredentialsX509() {
2591+
log.Printf("[INFO] Authenticating using EnableNewAuthLibrary")
2592+
creds, err = transport.Creds(context.Background(), option.WithScopes(clientScopes...), internaloption.EnableNewAuthLibrary())
2593+
if err != nil {
2594+
//this call should be backwards compatible, but this initial implementation ahead of the EnableNewAuthLibrary being made default for all googleapi authentication calls is only intended for it to be called on X.509 requests.
2595+
return googleoauth.Credentials{}, fmt.Errorf("Attempted to load application default credentials since neither `credentials` nor `access_token` was set in the provider block. No credentials loaded. To use your gcloud credentials, run 'gcloud auth application-default login'. If you are recieving this error while not attempting to authenticate using X.509 certificates, please file an issue with the provider at https://github.com/hashicorp/terraform-provider-google/issues/new/choose. Original error: %w", err)
2596+
}
2597+
} else {
2598+
creds, err = transport.Creds(context.Background(), option.WithScopes(clientScopes...))
2599+
if err != nil {
2600+
return googleoauth.Credentials{}, fmt.Errorf("Attempted to load application default credentials since neither `credentials` nor `access_token` was set in the provider block. No credentials loaded. To use your gcloud credentials, run 'gcloud auth application-default login'. Original error: %w", err)
2601+
}
25932602
}
25942603
}
25952604
}
@@ -2617,6 +2626,36 @@ func (c *Config) GetCredentials(clientScopes []string, initialCredentialsOnly bo
26172626
return *creds, nil
26182627
}
26192628

2629+
// parse application default credentials to determine if they are X.509 certs
2630+
func AreADCCredentialsX509() bool {
2631+
adcCreds := MultiEnvSearch([]string{
2632+
"GOOGLE_APPLICATION_CREDENTIALS",
2633+
})
2634+
if adcCreds != "" {
2635+
contents, _, err := verify.PathOrContents(adcCreds)
2636+
if err != nil {
2637+
return false
2638+
}
2639+
2640+
var content map[string]any
2641+
if err := json.Unmarshal([]byte(contents), &content); err != nil {
2642+
return false
2643+
}
2644+
if content["credential_source"] != nil {
2645+
if content["credential_source"].(map[string]any)["certificate"] != nil {
2646+
log.Printf("[INFO] Application Default Credentials identified as using X.509 certificates")
2647+
return true
2648+
} else {
2649+
return false
2650+
}
2651+
} else {
2652+
//ADC file does not contain x509 attribute
2653+
return false
2654+
}
2655+
}
2656+
return false
2657+
}
2658+
26202659
// Remove the `/{{version}}/` from a base path if present.
26212660
func RemoveBasePathVersion(url string) string {
26222661
re := regexp.MustCompile(`(?P<base>http[s]://.*)(?P<version>/[^/]+?/$)`)

0 commit comments

Comments
 (0)