From 580cf9eb9736ab8e08ea0e81a76557f340b6ae7b Mon Sep 17 00:00:00 2001 From: Yoshi Automation Bot Date: Tue, 29 Dec 2020 11:53:48 -0800 Subject: [PATCH 1/3] chore(java): remove formatter action Source-Author: Jeff Ching Source-Date: Tue Dec 29 10:50:17 2020 -0800 Source-Repo: googleapis/synthtool Source-Sha: 6133907dbb3ddab204a17a15d5c53ec0aae9b033 Source-Link: https://github.com/googleapis/synthtool/commit/6133907dbb3ddab204a17a15d5c53ec0aae9b033 --- .github/workflows/formatting.yaml | 25 ------------------------- synth.metadata | 5 ++--- 2 files changed, 2 insertions(+), 28 deletions(-) delete mode 100644 .github/workflows/formatting.yaml diff --git a/.github/workflows/formatting.yaml b/.github/workflows/formatting.yaml deleted file mode 100644 index 6844407b4..000000000 --- a/.github/workflows/formatting.yaml +++ /dev/null @@ -1,25 +0,0 @@ -on: - pull_request_target: - types: [opened, synchronize] - branches: - - master -name: format -jobs: - format-code: - runs-on: ubuntu-latest - env: - ACCESS_TOKEN: ${{ secrets.YOSHI_CODE_BOT_TOKEN }} - steps: - - uses: actions/checkout@v2 - with: - ref: ${{github.event.pull_request.head.ref}} - repository: ${{github.event.pull_request.head.repo.full_name}} - - uses: actions/setup-java@v1 - with: - java-version: 11 - - run: "mvn com.coveo:fmt-maven-plugin:format" - - uses: googleapis/code-suggester@v1 - with: - command: review - pull_number: ${{ github.event.pull_request.number }} - git_dir: '.' diff --git a/synth.metadata b/synth.metadata index cc2dca61f..3466ec17c 100644 --- a/synth.metadata +++ b/synth.metadata @@ -4,14 +4,14 @@ "git": { "name": ".", "remote": "https://github.com/googleapis/google-auth-library-java.git", - "sha": "5a1d5c0f56c62311b646ffbdc2e39affa55d5c0c" + "sha": "5804ff03a531268831ac797ab262638a3119c14f" } }, { "git": { "name": "synthtool", "remote": "https://github.com/googleapis/synthtool.git", - "sha": "3f67ceece7e797a5736a25488aae35405649b90b" + "sha": "6133907dbb3ddab204a17a15d5c53ec0aae9b033" } } ], @@ -29,7 +29,6 @@ ".github/trusted-contribution.yml", ".github/workflows/auto-release.yaml", ".github/workflows/ci.yaml", - ".github/workflows/formatting.yaml", ".kokoro/build.bat", ".kokoro/build.sh", ".kokoro/coerce_logs.sh", From 1b98d5c86fc5e56187c977e7f43c39bb62483d40 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Wed, 30 Dec 2020 21:44:12 +0000 Subject: [PATCH 2/3] fix: switch to GSON (#531) * switch to GSON * format --- .../java/com/google/auth/oauth2/ClientId.java | 18 +++++++++--------- .../com/google/auth/oauth2/OAuth2Utils.java | 4 ++-- .../javatests/com/google/auth/TestUtils.java | 4 ++-- .../com/google/auth/oauth2/ClientIdTest.java | 10 ++-------- .../oauth2/ImpersonatedCredentialsTest.java | 6 +++--- .../google/auth/oauth2/JwtCredentialsTest.java | 4 ++-- .../auth/oauth2/MockTokenServerTransport.java | 4 ++-- ...ServiceAccountJwtAccessCredentialsTest.java | 4 ++-- oauth2_http/pom.xml | 2 +- 9 files changed, 25 insertions(+), 31 deletions(-) diff --git a/oauth2_http/java/com/google/auth/oauth2/ClientId.java b/oauth2_http/java/com/google/auth/oauth2/ClientId.java index 05fcde836..80c3452f4 100644 --- a/oauth2_http/java/com/google/auth/oauth2/ClientId.java +++ b/oauth2_http/java/com/google/auth/oauth2/ClientId.java @@ -72,9 +72,9 @@ public static ClientId of(String clientId, String clientSecret) { /** * Constructs a Client ID from JSON from a downloaded file. * - * @param json The JSON from the downloaded file. - * @return the ClientId instance based on the JSON. - * @throws IOException The JSON could not be parsed. + * @param json the JSON from the downloaded file + * @return the ClientId instance based on the JSON + * @throws IOException the JSON could not be parsed */ public static ClientId fromJson(Map json) throws IOException { Object rawDetail = null; @@ -105,9 +105,9 @@ public static ClientId fromJson(Map json) throws IOException { /** * Constructs a Client ID from JSON file stored as a resource. * - * @param relativeClass A class in the same namespace as the resource. - * @param resourceName The name of the resource - * @return The constructed ClientID instance based on the JSON in the resource. + * @param relativeClass a class in the same namespace as the resource + * @param resourceName the name of the resource + * @return the constructed ClientID instance based on the JSON in the resource * @throws IOException The JSON could not be loaded or parsed. */ public static ClientId fromResource(Class relativeClass, String resourceName) @@ -119,9 +119,9 @@ public static ClientId fromResource(Class relativeClass, String resourceName) /** * Constructs a Client ID from JSON file stream. * - * @param stream Stream of the downloaded JSON file. - * @return The constructed ClientID instance based on the JSON in the stream. - * @throws IOException The JSON could not be read or parsed. + * @param stream the downloaded JSON file + * @return the constructed ClientID instance based on the JSON in the stream + * @throws IOException the JSON could not be read or parsed */ public static ClientId fromStream(InputStream stream) throws IOException { Preconditions.checkNotNull(stream); diff --git a/oauth2_http/java/com/google/auth/oauth2/OAuth2Utils.java b/oauth2_http/java/com/google/auth/oauth2/OAuth2Utils.java index d0e4fa15e..f23232cca 100644 --- a/oauth2_http/java/com/google/auth/oauth2/OAuth2Utils.java +++ b/oauth2_http/java/com/google/auth/oauth2/OAuth2Utils.java @@ -37,7 +37,7 @@ import com.google.api.client.json.GenericJson; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.JsonObjectParser; -import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.client.json.gson.GsonFactory; import com.google.auth.http.AuthHttpConstants; import com.google.auth.http.HttpTransportFactory; import com.google.common.io.ByteStreams; @@ -66,7 +66,7 @@ class OAuth2Utils { static final HttpTransportFactory HTTP_TRANSPORT_FACTORY = new DefaultHttpTransportFactory(); - static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); + static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance(); static final Charset UTF_8 = Charset.forName("UTF-8"); diff --git a/oauth2_http/javatests/com/google/auth/TestUtils.java b/oauth2_http/javatests/com/google/auth/TestUtils.java index 0726fb1f6..5f43dbba4 100644 --- a/oauth2_http/javatests/com/google/auth/TestUtils.java +++ b/oauth2_http/javatests/com/google/auth/TestUtils.java @@ -36,7 +36,7 @@ import com.google.api.client.json.GenericJson; import com.google.api.client.json.JsonFactory; -import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.client.json.gson.GsonFactory; import com.google.auth.http.AuthHttpConstants; import com.google.common.base.Splitter; import com.google.common.collect.Lists; @@ -54,7 +54,7 @@ public class TestUtils { public static final String UTF_8 = "UTF-8"; - private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); + private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance(); public static void assertContainsBearerToken(Map> metadata, String token) { assertNotNull(metadata); diff --git a/oauth2_http/javatests/com/google/auth/oauth2/ClientIdTest.java b/oauth2_http/javatests/com/google/auth/oauth2/ClientIdTest.java index 8e917b0f8..d7a5ca015 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/ClientIdTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/ClientIdTest.java @@ -33,7 +33,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; -import static org.junit.Assert.fail; import com.google.api.client.json.GenericJson; import com.google.auth.TestUtils; @@ -156,7 +155,7 @@ public void fromStream() throws IOException { } @Test - public void fromStream_invalidJson_throws() { + public void fromStream_invalidJson_doesNotThrow() throws IOException { String invalidJson = "{" + "\"web\": {" @@ -169,12 +168,7 @@ public void fromStream_invalidJson_throws() { + "}"; // No closing brace InputStream stream = TestUtils.stringToInputStream(invalidJson); - try { - ClientId.fromStream(stream); - fail(); - } catch (IOException expected) { - // Expected - } + ClientId.fromStream(stream); } private GenericJson writeClientIdJson(String type, String clientId, String clientSecret) { diff --git a/oauth2_http/javatests/com/google/auth/oauth2/ImpersonatedCredentialsTest.java b/oauth2_http/javatests/com/google/auth/oauth2/ImpersonatedCredentialsTest.java index 4334fc94f..63f314dff 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/ImpersonatedCredentialsTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/ImpersonatedCredentialsTest.java @@ -43,7 +43,7 @@ import com.google.api.client.json.GenericJson; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.JsonGenerator; -import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.client.json.gson.GsonFactory; import com.google.api.client.json.webtoken.JsonWebToken.Payload; import com.google.api.client.testing.http.MockLowLevelHttpRequest; import com.google.api.client.util.Clock; @@ -111,7 +111,7 @@ public class ImpersonatedCredentialsTest extends BaseSerializationTest { private static final String ACCESS_TOKEN = "1/MkSJoj1xsli0AccessToken_NKPY2"; private static final int VALID_LIFETIME = 300; private static final int INVALID_LIFETIME = 43210; - private static JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); + private static JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance(); private static final String RFC3339 = "yyyy-MM-dd'T'HH:mm:ss'Z'"; @@ -693,7 +693,7 @@ private String generateErrorJson( int errorCode, String errorMessage, String errorDomain, String errorReason) throws IOException { - JsonFactory factory = new JacksonFactory(); + JsonFactory factory = new GsonFactory(); ByteArrayOutputStream bout = new ByteArrayOutputStream(); JsonGenerator generator = factory.createJsonGenerator(bout, Charset.defaultCharset()); generator.enablePrettyPrint(); diff --git a/oauth2_http/javatests/com/google/auth/oauth2/JwtCredentialsTest.java b/oauth2_http/javatests/com/google/auth/oauth2/JwtCredentialsTest.java index 4b68ce1ea..1c934a8f4 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/JwtCredentialsTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/JwtCredentialsTest.java @@ -38,7 +38,7 @@ import static org.junit.Assert.fail; import com.google.api.client.json.JsonFactory; -import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.client.json.gson.GsonFactory; import com.google.api.client.json.webtoken.JsonWebSignature; import com.google.api.client.util.Clock; import com.google.auth.http.AuthHttpConstants; @@ -69,7 +69,7 @@ public class JwtCredentialsTest extends BaseSerializationTest { + "==\n-----END PRIVATE KEY-----\n"; private static final String JWT_ACCESS_PREFIX = ServiceAccountJwtAccessCredentials.JWT_ACCESS_PREFIX; - private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); + private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance(); static PrivateKey getPrivateKey() { try { diff --git a/oauth2_http/javatests/com/google/auth/oauth2/MockTokenServerTransport.java b/oauth2_http/javatests/com/google/auth/oauth2/MockTokenServerTransport.java index e37e64b7b..efa09d7c7 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/MockTokenServerTransport.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/MockTokenServerTransport.java @@ -36,7 +36,7 @@ import com.google.api.client.json.GenericJson; import com.google.api.client.json.Json; import com.google.api.client.json.JsonFactory; -import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.client.json.gson.GsonFactory; import com.google.api.client.json.webtoken.JsonWebSignature; import com.google.api.client.testing.http.MockHttpTransport; import com.google.api.client.testing.http.MockLowLevelHttpRequest; @@ -54,7 +54,7 @@ public class MockTokenServerTransport extends MockHttpTransport { static final String EXPECTED_GRANT_TYPE = "urn:ietf:params:oauth:grant-type:jwt-bearer"; - static final JsonFactory JSON_FACTORY = new JacksonFactory(); + static final JsonFactory JSON_FACTORY = new GsonFactory(); int buildRequestCount; final Map clients = new HashMap(); final Map refreshTokens = new HashMap(); diff --git a/oauth2_http/javatests/com/google/auth/oauth2/ServiceAccountJwtAccessCredentialsTest.java b/oauth2_http/javatests/com/google/auth/oauth2/ServiceAccountJwtAccessCredentialsTest.java index c3d73072f..5020317f2 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/ServiceAccountJwtAccessCredentialsTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/ServiceAccountJwtAccessCredentialsTest.java @@ -42,7 +42,7 @@ import static org.junit.Assert.fail; import com.google.api.client.json.JsonFactory; -import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.client.json.gson.GsonFactory; import com.google.api.client.json.webtoken.JsonWebSignature; import com.google.api.client.util.Clock; import com.google.auth.Credentials; @@ -92,7 +92,7 @@ public class ServiceAccountJwtAccessCredentialsTest extends BaseSerializationTes private static final String JWT_ACCESS_PREFIX = ServiceAccountJwtAccessCredentials.JWT_ACCESS_PREFIX; private static final URI CALL_URI = URI.create("http://googleapis.com/testapi/v1/foo"); - private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); + private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance(); private static final String QUOTA_PROJECT = "sample-quota-project-id"; @Test diff --git a/oauth2_http/pom.xml b/oauth2_http/pom.xml index 0f6fa5b30..955537119 100644 --- a/oauth2_http/pom.xml +++ b/oauth2_http/pom.xml @@ -80,7 +80,7 @@ com.google.http-client - google-http-client-jackson2 + google-http-client-gson com.google.guava From ec74870c372a33d4157b45bb5d59ad7464fb2238 Mon Sep 17 00:00:00 2001 From: arithmetic1728 <58957152+arithmetic1728@users.noreply.github.com> Date: Wed, 30 Dec 2020 22:12:37 -0800 Subject: [PATCH 3/3] fix: use default timeout if given 0 for ImpersonatedCredentials (#527) * fix: use default timeout if given 0 for ImpersonatedCredentials * update --- .../auth/oauth2/ImpersonatedCredentials.java | 14 +++++++++++--- .../auth/oauth2/ImpersonatedCredentialsTest.java | 9 +++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/oauth2_http/java/com/google/auth/oauth2/ImpersonatedCredentials.java b/oauth2_http/java/com/google/auth/oauth2/ImpersonatedCredentials.java index 0eaa3cd84..91e917a06 100644 --- a/oauth2_http/java/com/google/auth/oauth2/ImpersonatedCredentials.java +++ b/oauth2_http/java/com/google/auth/oauth2/ImpersonatedCredentials.java @@ -91,6 +91,7 @@ public class ImpersonatedCredentials extends GoogleCredentials private static final long serialVersionUID = -2133257318957488431L; private static final String RFC3339 = "yyyy-MM-dd'T'HH:mm:ss'Z'"; private static final int TWELVE_HOURS_IN_SECONDS = 43200; + private static final int DEFAULT_LIFETIME_IN_SECONDS = 3600; private static final String CLOUD_PLATFORM_SCOPE = "https://www.googleapis.com/auth/cloud-platform"; private static final String IAM_ACCESS_TOKEN_ENDPOINT = @@ -120,7 +121,8 @@ public class ImpersonatedCredentials extends GoogleCredentials * value should be at most 3600. However, you can follow these * instructions to set up the service account and extend the maximum lifetime to 43200 (12 - * hours). + * hours). If the given lifetime is 0, default value 3600 will be used instead when creating + * the credentials. * @param transportFactory HTTP transport factory that creates the transport used to get access * tokens * @return new credentials @@ -159,6 +161,8 @@ public static ImpersonatedCredentials create( * instructions to set up the service account and extend the maximum lifetime to 43200 (12 * hours). * https://cloud.google.com/iam/docs/creating-short-lived-service-account-credentials#sa-credentials-oauth + * If the given lifetime is 0, default value 3600 will be used instead when creating the + * credentials. * @return new credentials */ public static ImpersonatedCredentials create( @@ -186,6 +190,10 @@ public String getAccount() { return this.targetPrincipal; } + int getLifetime() { + return this.lifetime; + } + /** * Signs the provided bytes using the private key associated with the impersonated service account * @@ -355,7 +363,7 @@ public static class Builder extends GoogleCredentials.Builder { private String targetPrincipal; private List delegates; private List scopes; - private int lifetime; + private int lifetime = DEFAULT_LIFETIME_IN_SECONDS; private HttpTransportFactory transportFactory; protected Builder() {} @@ -402,7 +410,7 @@ public List getScopes() { } public Builder setLifetime(int lifetime) { - this.lifetime = lifetime; + this.lifetime = lifetime == 0 ? DEFAULT_LIFETIME_IN_SECONDS : lifetime; return this; } diff --git a/oauth2_http/javatests/com/google/auth/oauth2/ImpersonatedCredentialsTest.java b/oauth2_http/javatests/com/google/auth/oauth2/ImpersonatedCredentialsTest.java index 63f314dff..b7c3bd29e 100644 --- a/oauth2_http/javatests/com/google/auth/oauth2/ImpersonatedCredentialsTest.java +++ b/oauth2_http/javatests/com/google/auth/oauth2/ImpersonatedCredentialsTest.java @@ -198,6 +198,15 @@ public void refreshAccessToken_malformedTarget() throws IOException { } } + @Test() + public void credential_with_zero_lifetime() throws IOException, IllegalStateException { + GoogleCredentials sourceCredentials = getSourceCredentials(); + ImpersonatedCredentials targetCredentials = + ImpersonatedCredentials.create( + sourceCredentials, IMPERSONATED_CLIENT_EMAIL, null, SCOPES, 0); + assertEquals(3600, targetCredentials.getLifetime()); + } + @Test() public void credential_with_invalid_lifetime() throws IOException, IllegalStateException {