From cbac660db6d9e3e4ae0a5e94a161446e6db8f5ea Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Thu, 28 Jun 2018 10:44:06 -0700 Subject: [PATCH 1/3] Add test for setting project id in service options from credentials --- .../com/google/cloud/ServiceOptionsTest.java | 53 +++++++++++++++++-- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/google-cloud-clients/google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java b/google-cloud-clients/google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java index 49190fc4c2ab..bd5c7534fb26 100644 --- a/google-cloud-clients/google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java +++ b/google-cloud-clients/google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java @@ -83,12 +83,51 @@ public class ServiceOptionsTest { + " \"client_id\": \"someclientid.apps.googleusercontent.com\",\n" + " \"type\": \"service_account\"\n" + "}"; - private static final InputStream JSON_KEY_STREAM = new ByteArrayInputStream(JSON_KEY.getBytes()); private static GoogleCredentials credentials; static { try { - credentials = GoogleCredentials.fromStream(JSON_KEY_STREAM); + InputStream keyStream = new ByteArrayInputStream(JSON_KEY.getBytes()); + credentials = GoogleCredentials.fromStream(keyStream); + } catch (IOException e) { + fail("Couldn't create fake JSON credentials."); + } + } + + private static final String JSON_KEY_PROJECT_ID = + "{\n" + + " \"private_key_id\": \"somekeyid\",\n" + + " \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggS" + + "kAgEAAoIBAQC+K2hSuFpAdrJI\\nnCgcDz2M7t7bjdlsadsasad+fvRSW6TjNQZ3p5LLQY1kSZRqBqylRkzteMOyHg" + + "aR\\n0Pmxh3ILCND5men43j3h4eDbrhQBuxfEMalkG92sL+PNQSETY2tnvXryOvmBRwa/\\nQP/9dJfIkIDJ9Fw9N4" + + "Bhhhp6mCcRpdQjV38H7JsyJ7lih/oNjECgYAt\\nknddadwkwewcVxHFhcZJO+XWf6ofLUXpRwiTZakGMn8EE1uVa2" + + "LgczOjwWHGi99MFjxSer5m9\\n1tCa3/KEGKiS/YL71JvjwX3mb+cewlkcmweBKZHM2JPTk0ZednFSpVZMtycjkbLa" + + "\\ndYOS8V85AgMBewECggEBAKksaldajfDZDV6nGqbFjMiizAKJolr/M3OQw16K6o3/\\n0S31xIe3sSlgW0+UbYlF" + + "4U8KifhManD1apVSC3csafaspP4RZUHFhtBywLO9pR5c\\nr6S5aLp+gPWFyIp1pfXbWGvc5VY/v9x7ya1VEa6rXvL" + + "sKupSeWAW4tMj3eo/64ge\\nsdaceaLYw52KeBYiT6+vpsnYrEkAHO1fF/LavbLLOFJmFTMxmsNaG0tuiJHgjshB\\" + + "n82DpMCbXG9YcCgI/DbzuIjsdj2JC1cascSP//3PmefWysucBQe7Jryb6NQtASmnv\\nCdDw/0jmZTEjpe4S1lxfHp" + + "lAhHFtdgYTvyYtaLZiVVkCgYEA8eVpof2rceecw/I6\\n5ng1q3Hl2usdWV/4mZMvR0fOemacLLfocX6IYxT1zA1FF" + + "JlbXSRsJMf/Qq39mOR2\\nSpW+hr4jCoHeRVYLgsbggtrevGmILAlNoqCMpGZ6vDmJpq6ECV9olliDvpPgWOP+\\nm" + + "YPDreFBGxWvQrADNbRt2dmGsrsCgYEAyUHqB2wvJHFqdmeBsaacewzV8x9WgmeX\\ngUIi9REwXlGDW0Mz50dxpxcK" + + "CAYn65+7TCnY5O/jmL0VRxU1J2mSWyWTo1C+17L0\\n3fUqjxL1pkefwecxwecvC+gFFYdJ4CQ/MHHXU81Lwl1iWdF" + + "Cd2UoGddYaOF+KNeM\\nHC7cmqra+JsCgYEAlUNywzq8nUg7282E+uICfCB0LfwejuymR93CtsFgb7cRd6ak\\nECR" + + "8FGfCpH8ruWJINllbQfcHVCX47ndLZwqv3oVFKh6pAS/vVI4dpOepP8++7y1u\\ncoOvtreXCX6XqfrWDtKIvv0vjl" + + "HBhhhp6mCcRpdQjV38H7JsyJ7lih/oNjECgYAt\\nkndj5uNl5SiuVxHFhcZJO+XWf6ofLUregtevZakGMn8EE1uVa" + + "2AY7eafmoU/nZPT\\n00YB0TBATdCbn/nBSuKDESkhSg9s2GEKQZG5hBmL5uCMfo09z3SfxZIhJdlerreP\\nJ7gSi" + + "dI12N+EZxYd4xIJh/HFDgp7RRO87f+WJkofMQKBgGTnClK1VMaCRbJZPriw\\nEfeFCoOX75MxKwXs6xgrw4W//AYG" + + "GUjDt83lD6AZP6tws7gJ2IwY/qP7+lyhjEqN\\nHtfPZRGFkGZsdaksdlaksd323423d+15/UvrlRSFPNj1tWQmNKk" + + "XyRDW4IG1Oa2p\\nrALStNBx5Y9t0/LQnFI4w3aG\\n-----END PRIVATE KEY-----\\n\",\n" + + " \"project_id\": \"someprojectid\",\n" + + " \"client_email\": \"someclientid@developer.gserviceaccount.com\",\n" + + " \"client_id\": \"someclientid.apps.googleusercontent.com\",\n" + + " \"type\": \"service_account\"\n" + + "}"; + private static GoogleCredentials credentialsWithProjectId; + + static { + try { + InputStream keyStream = new ByteArrayInputStream(JSON_KEY_PROJECT_ID.getBytes()); + credentialsWithProjectId = GoogleCredentials.fromStream(keyStream); } catch (IOException e) { fail("Couldn't create fake JSON credentials."); } @@ -266,6 +305,14 @@ public void testBuilderNullCredentials() { TestServiceOptions.newBuilder().setCredentials(null).build(); } + @Test + public void testBuilderServiceAccount_setsProjectId() { + TestServiceOptions options = TestServiceOptions.newBuilder() + .setCredentials(credentialsWithProjectId) + .build(); + assertEquals("someprojectid", options.getProjectId()); + } + @Test public void testGetProjectIdRequired() { assertTrue(OPTIONS.projectIdRequired()); @@ -342,7 +389,7 @@ public void testResponseHeaderDoesNotContainMetaDataFlavor() throws Exception { HttpResponse httpResponse = createHttpResponseWithHeader(headers); assertThat(ServiceOptions.headerContainsMetadataFlavor(httpResponse)).isFalse(); } - + private HttpResponse createHttpResponseWithHeader(final Multimap headers) throws Exception { HttpTransport mockHttpTransport = new MockHttpTransport() { @Override From c55217e41fd76b44d6cf78f10a0004f8096321e2 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Thu, 28 Jun 2018 11:02:27 -0700 Subject: [PATCH 2/3] Set projectId from credentials if available --- .../src/main/java/com/google/cloud/ServiceOptions.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/google-cloud-clients/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java b/google-cloud-clients/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java index 950c6fd8e485..52177f824460 100644 --- a/google-cloud-clients/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java +++ b/google-cloud-clients/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java @@ -43,6 +43,7 @@ import com.google.api.gax.rpc.NoHeaderProvider; import com.google.auth.Credentials; import com.google.auth.oauth2.GoogleCredentials; +import com.google.auth.oauth2.ServiceAccountCredentials; import com.google.cloud.spi.ServiceRpcFactory; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; @@ -204,6 +205,10 @@ public B setHost(String host) { */ public B setCredentials(Credentials credentials) { this.credentials = checkNotNull(credentials); + // set project id if available + if (credentials instanceof ServiceAccountCredentials) { + this.projectId = ((ServiceAccountCredentials) credentials).getProjectId(); + } return self(); } From 5d786cd5f1c13a999a05d1edfa1e325108b58631 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Thu, 28 Jun 2018 15:51:13 -0700 Subject: [PATCH 3/3] Only set the projectId if not explicitly set --- .../java/com/google/cloud/ServiceOptions.java | 3 ++- .../com/google/cloud/ServiceOptionsTest.java | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/google-cloud-clients/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java b/google-cloud-clients/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java index 52177f824460..99f23a83825a 100644 --- a/google-cloud-clients/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java +++ b/google-cloud-clients/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java @@ -206,7 +206,8 @@ public B setHost(String host) { public B setCredentials(Credentials credentials) { this.credentials = checkNotNull(credentials); // set project id if available - if (credentials instanceof ServiceAccountCredentials) { + if (this.projectId == null && + credentials instanceof ServiceAccountCredentials) { this.projectId = ((ServiceAccountCredentials) credentials).getProjectId(); } return self(); diff --git a/google-cloud-clients/google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java b/google-cloud-clients/google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java index bd5c7534fb26..ee957a625f3c 100644 --- a/google-cloud-clients/google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java +++ b/google-cloud-clients/google-cloud-core/src/test/java/com/google/cloud/ServiceOptionsTest.java @@ -313,6 +313,24 @@ public void testBuilderServiceAccount_setsProjectId() { assertEquals("someprojectid", options.getProjectId()); } + @Test + public void testBuilderServiceAccount_explicitSetProjectIdBefore() { + TestServiceOptions options = TestServiceOptions.newBuilder() + .setProjectId("override-project-id") + .setCredentials(credentialsWithProjectId) + .build(); + assertEquals("override-project-id", options.getProjectId()); + } + + @Test + public void testBuilderServiceAccount_explicitSetProjectIdAfter() { + TestServiceOptions options = TestServiceOptions.newBuilder() + .setCredentials(credentialsWithProjectId) + .setProjectId("override-project-id") + .build(); + assertEquals("override-project-id", options.getProjectId()); + } + @Test public void testGetProjectIdRequired() { assertTrue(OPTIONS.projectIdRequired());