Repository navigation
[cherrypick][6.11] Validate token endpoints of user-supplied external_account credentials - #1639
Conversation
A credential configuration of type external_account tells the auth library where to read a subject token from (credential_source) and where to send it: the subject token is POSTed to token_url and the resulting access token to service_account_impersonation_url. When such a configuration is supplied through the service account JSON field of a plugin, these endpoints must be validated so that tokens are only sent to Google. Following https://cloud.google.com/docs/authentication/client-libraries#validate_other_credential_configurations GCPUtils.loadServiceAccountCredentials now requires, for external_account configurations, that - token_url is https://sts.googleapis.com/v1/token or one of the Google locational (sts.<location>.rep.googleapis.com) or mTLS (sts.mtls.googleapis.com) variants generated by 'gcloud iam workload-identity-pools create-cred-config' - service_account_impersonation_url, if present, is https://iamcredentials[.mtls].googleapis.com/v1/projects/-/serviceAccounts/<email>:generateAccessToken The configuration is parsed with the same parser that GoogleCredentials.fromStream uses, so validation and the library see exactly the same document and unparseable content fails with the library's own error. The external_account credential generated by CDAP for GKE Workload Identity and gcloud-generated configurations continue to load; service_account JSON and all other content are handled by the library exactly as before. (cherry picked from commit de27cae)
There was a problem hiding this comment.
Code Review
This pull request introduces validation for Google Cloud external account (workload identity federation) credential configurations in GCPUtils. It ensures that the token_url and service_account_impersonation_url point to trusted Google endpoints before the credentials are used, preventing potential token leakage to unauthorized endpoints. Comprehensive unit tests have been added in GCPUtilsTest to verify these validations. A review comment suggests adding null checks in the test teardown method to prevent masking initialization errors with a NullPointerException.
| public void tearDown() { | ||
| metadataServer.stop(0); | ||
| tokenExchangeServer.stop(0); | ||
| } |
There was a problem hiding this comment.
In tearDown(), calling stop(0) on metadataServer or tokenExchangeServer without a null check can result in a NullPointerException if the test setup (setUp()) fails before both servers are fully initialized. This NullPointerException in tearDown will mask the actual initialization exception, making test failures harder to debug. Consider adding null checks before stopping the servers.
public void tearDown() {
if (metadataServer != null) {
metadataServer.stop(0);
}
if (tokenExchangeServer != null) {
tokenExchangeServer.stop(0);
}
}
Cherry-pick of #1628 (commit
de27cae1acefe95902eb449d4287eba6395b0c77, merge commit14eab53585aefd829df36f860d58d80bf23be651) intorelease/0.24for CDAP 6.11.Issue
The GCP plugins accept a service account credential as inline JSON or a file path and load it with
GoogleCredentials.fromStream, which accepts any credential type the auth library supports, includingexternal_account(workload identity federation).An
external_accountconfiguration tells the auth library where to obtain a subject token (credential_source) and where to send it: the subject token is sent totoken_urland the resulting access token toservice_account_impersonation_url. When such a configuration comes from user input, these endpoints must be validated; otherwise the configuration could direct tokens to an arbitrary endpoint. See Google's guidance on validating credential configurations from external sources: https://cloud.google.com/docs/authentication/client-libraries#validate_other_credential_configurationsFix
This change adds that validation in
GCPUtils.loadServiceAccountCredentials, the single method all plugin credential paths (validation, connection test, deploy and runtime) go through. For configurations of typeexternal_account, both endpoints must be Google-owned:token_urlmust match^https://sts(\.[a-z0-9-]+\.rep)?(\.mtls)?\.googleapis\.com/v1/token$— i.e.https://sts.googleapis.com/v1/token, or the locational (sts.<location>.rep.googleapis.com) and mTLS (sts.mtls.googleapis.com) variants generated bygcloud iam workload-identity-pools create-cred-config --sts-location / --enable-mtls.service_account_impersonation_url, if present, must match^https://iamcredentials(\.mtls)?\.googleapis\.com/v1/projects/-/serviceAccounts/<email>:generateAccessToken$With both endpoints pinned to
googleapis.com, whatever the configuration reads can only be sent to Google. Non-conforming configurations are rejected with a validation error naming the field. Configurations of any other type are passed to the library unchanged.The JSON is parsed with the same parser
GoogleCredentials.fromStreamuses, so the validation sees exactly the document the library will act on (including lenient handling of trailing content).Tests
GCPUtilsTestcovers:service_accountJSON andexternal_accountconfigurations with the global, locational and mTLS endpoints load successfully.credential_source.url→ metadata server,token_url→ attacker) is rejected before any endpoint is contacted, verified with local stand-in HTTP servers.service_account_impersonation_url, host-suffix/userinfo/scheme/case/query/trailing-slash tricks, and trailing content after the JSON object are all rejected.external_accountcontent (including unparseable or null input) yields the same exception as the library alone.