From e3a2e9cbdd767c4664d895f98f69d8b742d645f0 Mon Sep 17 00:00:00 2001 From: Jin Date: Wed, 24 Jan 2024 10:41:24 -0800 Subject: [PATCH] fix: Issue #1347: ExternalAccountCredentials serialization is broken (#1358) * fix: Issue #1347: ExternalAccountCredentials serialization is broken * fix test * fix lint * Update oauth2_http/java/com/google/auth/oauth2/ExternalAccountCredentials.java Co-authored-by: Leo <39062083+lsirac@users.noreply.github.com> * address the removal of redaundant public * move the getter to test class --------- Co-authored-by: Leo <39062083+lsirac@users.noreply.github.com> --- .../auth/oauth2/ExternalAccountCredentials.java | 8 ++++++++ .../oauth2/ExternalAccountCredentialsTest.java | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/oauth2_http/java/com/google/auth/oauth2/ExternalAccountCredentials.java b/oauth2_http/java/com/google/auth/oauth2/ExternalAccountCredentials.java index 804df70df..6e6bb2e8b 100644 --- a/oauth2_http/java/com/google/auth/oauth2/ExternalAccountCredentials.java +++ b/oauth2_http/java/com/google/auth/oauth2/ExternalAccountCredentials.java @@ -42,6 +42,7 @@ import com.google.errorprone.annotations.CanIgnoreReturnValue; import java.io.IOException; import java.io.InputStream; +import java.io.ObjectInputStream; import java.math.BigDecimal; import java.net.URI; import java.nio.charset.StandardCharsets; @@ -555,6 +556,13 @@ public CredentialSource getCredentialSource() { return credentialSource; } + @SuppressWarnings("unused") + private void readObject(ObjectInputStream input) throws IOException, ClassNotFoundException { + // Properly deserialize the transient transportFactory. + input.defaultReadObject(); + transportFactory = newInstance(transportFactoryClassName); + } + @Nullable public String getServiceAccountImpersonationUrl() { return serviceAccountImpersonationUrl; diff --git a/oauth2_http/javatests/com/google/auth/oauth2/ExternalAccountCredentialsTest.java b/oauth2_http/javatests/com/google/auth/oauth2/ExternalAccountCredentialsTest.java index 372cb5380..2d2c88559 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/ExternalAccountCredentialsTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/ExternalAccountCredentialsTest.java @@ -1109,6 +1109,9 @@ public void serialize() throws IOException, ClassNotFoundException { testCredentials.getServiceAccountImpersonationOptions().getLifetime(), deserializedCredentials.getServiceAccountImpersonationOptions().getLifetime()); assertSame(deserializedCredentials.clock, Clock.SYSTEM); + assertEquals( + MockExternalAccountCredentialsTransportFactory.class, + deserializedCredentials.toBuilder().getHttpTransportFactory().getClass()); } @Test @@ -1299,6 +1302,11 @@ protected TestCredentialSource(Map credentialSourceMap) { } } + @Override + public Builder toBuilder() { + return new Builder(this); + } + public static Builder newBuilder() { return new Builder(); } @@ -1306,10 +1314,18 @@ public static Builder newBuilder() { static class Builder extends ExternalAccountCredentials.Builder { Builder() {} + Builder(TestExternalAccountCredentials credentials) { + super(credentials); + } + @Override public TestExternalAccountCredentials build() { return new TestExternalAccountCredentials(this); } + + public HttpTransportFactory getHttpTransportFactory() { + return transportFactory; + } } protected TestExternalAccountCredentials(ExternalAccountCredentials.Builder builder) {