Skip to content

Commit 525cea9

Browse files
authored
x509 cert fix (#15691)
1 parent a557220 commit 525cea9

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

mmv1/third_party/terraform/transport/config.go.tmpl

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,9 +1451,18 @@ func (c *Config) GetCredentials(clientScopes []string, initialCredentialsOnly bo
14511451
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)
14521452
}
14531453
} else {
1454-
creds, err = transport.Creds(context.Background(), option.WithScopes(clientScopes...))
1455-
if err != nil {
1456-
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)
1454+
if AreADCCredentialsX509() {
1455+
log.Printf("[INFO] Authenticating using EnableNewAuthLibrary")
1456+
creds, err = transport.Creds(context.Background(), option.WithScopes(clientScopes...), internaloption.EnableNewAuthLibrary())
1457+
if err != nil {
1458+
//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.
1459+
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)
1460+
}
1461+
} else {
1462+
creds, err = transport.Creds(context.Background(), option.WithScopes(clientScopes...))
1463+
if err != nil {
1464+
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)
1465+
}
14571466
}
14581467
}
14591468
}
@@ -1481,6 +1490,36 @@ func (c *Config) GetCredentials(clientScopes []string, initialCredentialsOnly bo
14811490
return *creds, nil
14821491
}
14831492

1493+
// parse application default credentials to determine if they are X.509 certs
1494+
func AreADCCredentialsX509() bool {
1495+
adcCreds := MultiEnvSearch([]string{
1496+
"GOOGLE_APPLICATION_CREDENTIALS",
1497+
})
1498+
if adcCreds != "" {
1499+
contents, _, err := verify.PathOrContents(adcCreds)
1500+
if err != nil {
1501+
return false
1502+
}
1503+
1504+
var content map[string]any
1505+
if err := json.Unmarshal([]byte(contents), &content); err != nil {
1506+
return false
1507+
}
1508+
if content["credential_source"] != nil {
1509+
if content["credential_source"].(map[string]any)["certificate"] != nil {
1510+
log.Printf("[INFO] Application Default Credentials identified as using X.509 certificates")
1511+
return true
1512+
} else {
1513+
return false
1514+
}
1515+
} else {
1516+
//ADC file does not contain x509 attribute
1517+
return false
1518+
}
1519+
}
1520+
return false
1521+
}
1522+
14841523
// Remove the `/{{"{{"}}version{{"}}"}}/` from a base path if present.
14851524
func RemoveBasePathVersion(url string) string {
14861525
re := regexp.MustCompile(`(?P<base>http[s]://.*)(?P<version>/[^/]+?/$)`)

0 commit comments

Comments
 (0)