Skip to content

fix(auth): restore transportFactory upon deserialization in InternalAwsSecurityCredentialsSupplier - #14340

Merged
lqiu96 merged 1 commit into
mainfrom
fix-aws-credentials-deserialization-14050
Sep 10, 2026
Merged

fix(auth): restore transportFactory upon deserialization in InternalAwsSecurityCredentialsSupplier#14340
lqiu96 merged 1 commit into
mainfrom
fix-aws-credentials-deserialization-14050

Conversation

@lqiu96

@lqiu96 lqiu96 commented Sep 9, 2026

Copy link
Copy Markdown
Member

Fixes #14050
Part of #12580

In InternalAwsSecurityCredentialsSupplier, transportFactory is marked transient but no transportFactoryClassName was retained and no readObject was defined. Consequently, after Java deserialization (common in distributed frameworks like Apache Flink and Apache Spark), transportFactory is null. When deserialized AwsCredentials attempts to refresh its token, a NullPointerException is thrown when retrieving AWS security credentials or region from the metadata service.

This change:

  1. Records transportFactoryClassName during construction.
  2. Implements readObject to restore transportFactory using OAuth2Credentials.newInstance(transportFactoryClassName), with fallback to OAuth2Utils.HTTP_TRANSPORT_FACTORY for backwards compatibility with previously serialized streams.
  3. Adds reproduction and serialization unit tests verifying credentials and region retrieval after deserialization, including token refresh in AwsCredentials.

@lqiu96
lqiu96 requested a review from whowes September 9, 2026 21:34

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request implements serialization and deserialization support for the InternalAwsSecurityCredentialsSupplier class by storing and restoring the HttpTransportFactory class name. It also adds unit tests to verify that credentials and region retrieval, as well as token refreshing, work correctly after deserialization. The review feedback highlights a potential compatibility issue where serializing the internal class name of the default fallback transport factory could lead to ClassNotFoundException in future versions if that class is renamed or refactored. It is recommended to set transportFactoryClassName to null when the default factory is used.

@lqiu96
lqiu96 force-pushed the fix-aws-credentials-deserialization-14050 branch from 193536d to 5e04216 Compare September 9, 2026 22:09
@lqiu96
lqiu96 marked this pull request as ready for review September 9, 2026 22:21
@lqiu96
lqiu96 requested review from a team as code owners September 9, 2026 22:21
…wsSecurityCredentialsSupplier

Fixes #14050
Fixes #1538

In InternalAwsSecurityCredentialsSupplier, transportFactory is marked
transient but no transportFactoryClassName was retained and no readObject
was defined. Consequently, after Java deserialization (common in distributed
frameworks like Apache Flink and Apache Spark), transportFactory is null.
When deserialized AwsCredentials attempts to refresh its token, a
NullPointerException is thrown when retrieving AWS security credentials
or region from the metadata service.

This change:
1. Records transportFactoryClassName during construction.
2. Implements readObject to restore transportFactory using
   OAuth2Credentials.newInstance(transportFactoryClassName).
3. Adds reproduction and serialization unit tests verifying credentials
   and region retrieval after deserialization, including token refresh in
   AwsCredentials.
@lqiu96
lqiu96 force-pushed the fix-aws-credentials-deserialization-14050 branch from 5e04216 to 78c0f32 Compare September 9, 2026 22:26
@lqiu96
lqiu96 merged commit beea42f into main Sep 10, 2026
310 of 316 checks passed
@lqiu96
lqiu96 deleted the fix-aws-credentials-deserialization-14050 branch September 10, 2026 00:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[google-auth-library-java] external_account (AWS) Credentials NPE after Java deserialization: transient HttpTransportFactory not restored on refresh

2 participants