Skip to content

Commit facb2f6

Browse files
authored
chore: Fix GDCH tests referencing obsolete GDCH audience methods (#12771)
1 parent 72e5508 commit facb2f6

File tree

15 files changed

+146
-118
lines changed

15 files changed

+146
-118
lines changed

sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
import com.google.common.collect.ImmutableMap;
5858
import com.google.common.collect.Sets;
5959
import java.io.IOException;
60-
import java.net.URI;
6160
import java.util.Collections;
6261
import java.util.HashMap;
6362
import java.util.List;
@@ -355,14 +354,7 @@ static GdchCredentials getGdchCredentials(
355354
} else {
356355
throw new IllegalArgumentException("Could not infer GDCH api audience from settings");
357356
}
358-
359-
URI gdchAudienceUri;
360-
try {
361-
gdchAudienceUri = URI.create(audienceString);
362-
} catch (IllegalArgumentException ex) { // thrown when passing a malformed uri string
363-
throw new IllegalArgumentException("The GDC-H API audience string is not a valid URI", ex);
364-
}
365-
return ((GdchCredentials) credentials).createWithGdchAudience(gdchAudienceUri);
357+
return ((GdchCredentials) credentials).createWithGdchAudience(audienceString);
366358
}
367359

368360
/**

sdk-platform-java/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import static com.google.common.truth.Truth.assertThat;
3434
import static org.junit.jupiter.api.Assertions.assertEquals;
3535
import static org.junit.jupiter.api.Assertions.assertFalse;
36+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
3637
import static org.junit.jupiter.api.Assertions.assertNotNull;
3738
import static org.junit.jupiter.api.Assertions.assertNotSame;
3839
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -63,7 +64,6 @@
6364
import com.google.common.collect.ImmutableMap;
6465
import com.google.common.truth.Truth;
6566
import java.io.IOException;
66-
import java.net.URI;
6767
import java.util.Collections;
6868
import java.util.List;
6969
import java.util.Map;
@@ -890,16 +890,16 @@ void testExecutorSettings() throws Exception {
890890
assertThat(transportChannel.getExecutor()).isSameInstanceAs(executorProvider.getExecutor());
891891
}
892892

893-
private GdchCredentials getMockGdchCredentials() throws IOException {
893+
private GdchCredentials getMockGdchCredentials() {
894894
GdchCredentials creds = Mockito.mock(GdchCredentials.class);
895895

896896
// GdchCredentials builder is mocked to accept a well-formed uri
897897
GdchCredentials.Builder gdchCredsBuilder = Mockito.mock(GdchCredentials.Builder.class);
898-
Mockito.when(gdchCredsBuilder.setGdchAudience(Mockito.any(URI.class)))
898+
Mockito.when(gdchCredsBuilder.setGdchAudience(Mockito.anyString()))
899899
.thenReturn(gdchCredsBuilder);
900900
Mockito.when(gdchCredsBuilder.build()).thenReturn(creds);
901901
Mockito.when(creds.toBuilder()).thenReturn(gdchCredsBuilder);
902-
Mockito.when(creds.createWithGdchAudience(Mockito.any()))
902+
Mockito.when(creds.createWithGdchAudience(Mockito.anyString()))
903903
.thenAnswer((uri) -> getMockGdchCredentials());
904904
return creds;
905905
}
@@ -939,7 +939,7 @@ void testCreateClientContext_withGdchCredentialNoAudienceNoEndpoint() throws IOE
939939
assertThat(fromProvider).isInstanceOf(GdchCredentials.class);
940940
assertNotSame(fromContext, fromProvider);
941941
verify((GdchCredentials) fromProvider, times(1))
942-
.createWithGdchAudience(URI.create("test.googleapis.com:443"));
942+
.createWithGdchAudience("test.googleapis.com:443");
943943
}
944944

945945
@Test
@@ -994,8 +994,7 @@ void testCreateClientContext_withGdchCredentialWithoutAudienceWithEndpoint_corre
994994
assertThat(fromContext).isInstanceOf(GdchCredentials.class);
995995
assertThat(fromProvider).isInstanceOf(GdchCredentials.class);
996996
assertNotSame(fromContext, fromProvider);
997-
verify((GdchCredentials) fromProvider, times(1))
998-
.createWithGdchAudience(URI.create("test-endpoint"));
997+
verify((GdchCredentials) fromProvider, times(1)).createWithGdchAudience("test-endpoint");
999998
}
1000999

10011000
@Test
@@ -1021,14 +1020,13 @@ void testCreateClientContext_withGdchCredentialAndValidAudience() throws IOExcep
10211020
assertNotNull(fromContext);
10221021
// using an audience should have made the context to recreate the credentials
10231022
assertNotSame(fromContext, fromProvider);
1024-
verify((GdchCredentials) fromProvider, times(1))
1025-
.createWithGdchAudience(URI.create("valid-uri"));
1026-
verify((GdchCredentials) fromProvider, times(0))
1027-
.createWithGdchAudience(URI.create("test-endpoint"));
1023+
verify((GdchCredentials) fromProvider, times(1)).createWithGdchAudience("valid-uri");
1024+
verify((GdchCredentials) fromProvider, times(0)).createWithGdchAudience("test-endpoint");
10281025
}
10291026

10301027
@Test
1031-
void testCreateClientContext_withGdchCredentialAndInvalidAudience_throws() throws IOException {
1028+
void testCreateClientContext_withGdchCredentialAndInvalidAudience_doesNotThrow()
1029+
throws IOException {
10321030
TransportChannelProvider transportChannelProvider = getFakeTransportChannelProvider();
10331031
Credentials creds = getMockGdchCredentials();
10341032
CredentialsProvider provider = FixedCredentialsProvider.create(creds);
@@ -1044,17 +1042,14 @@ void testCreateClientContext_withGdchCredentialAndInvalidAudience_throws() throw
10441042
clientSettingsBuilder.setCredentialsProvider(provider);
10451043
clientSettingsBuilder.setTransportChannelProvider(transportChannelProvider);
10461044
final ClientSettings withGdchCredentialsAndMalformedApiAudience = clientSettingsBuilder.build();
1047-
// should throw
1048-
String exMessage =
1049-
assertThrows(
1050-
IllegalArgumentException.class,
1051-
() -> ClientContext.create(withGdchCredentialsAndMalformedApiAudience))
1052-
.getMessage();
1053-
assertThat(exMessage).contains("The GDC-H API audience string is not a valid URI");
1045+
1046+
// There is an invalid URI for the GDCH audience, but we do not validate the URI
1047+
// There are some use cases where there the GDCH audience is not a URI.
1048+
ClientContext clientContext = ClientContext.create(withGdchCredentialsAndMalformedApiAudience);
1049+
assertInstanceOf(GdchCredentials.class, clientContext.getCredentials());
10541050

10551051
Credentials fromProvider = provider.getCredentials();
1056-
verify((GdchCredentials) fromProvider, times(0))
1057-
.createWithGdchAudience(URI.create("test-endpoint"));
1052+
verify((GdchCredentials) fromProvider, times(0)).createWithGdchAudience("test-endpoint");
10581053
}
10591054

10601055
@Test

sdk-platform-java/java-showcase-3.21.0/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITGdch.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ void testCreateClient_withGdchCredentialAndNoAudience_defaultsToEndpointBasedAud
156156
NullPointerException expectedEx =
157157
assertThrows(NullPointerException.class, () -> initialCredentials.refresh());
158158
assertTrue(
159-
expectedEx.getMessage().contains("Audience are not configured for GDCH service account"));
159+
expectedEx
160+
.getMessage()
161+
.contains("Audience cannot be null or empty for GDCH service account credentials"));
160162

161163
// However, the credentials prepared in ClientContext should be able to refresh since the
162164
// audience would be
@@ -201,7 +203,7 @@ void testCreateClient_withGdchCredentialWithValidAudience_usesCredentialWithPass
201203
assertTrue(
202204
thrownByClientCreds
203205
.getMessage()
204-
.contains("Audience are not configured for GDCH service account"));
206+
.contains("Audience cannot be null or empty for GDCH service account credentials"));
205207

206208
// But the credentials prepared in ClientContext should be able to refresh since the audience
207209
// would be internally

sdk-platform-java/java-showcase-3.21.0/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/util/InterceptingMockTokenServerTransport.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818

1919
import com.google.api.client.http.LowLevelHttpRequest;
2020
import com.google.api.client.json.JsonFactory;
21+
import com.google.api.client.json.JsonParser;
2122
import com.google.api.client.json.gson.GsonFactory;
22-
import com.google.api.client.json.webtoken.JsonWebSignature;
2323
import com.google.api.client.testing.http.MockLowLevelHttpRequest;
24-
import com.google.auth.TestUtils;
2524
import com.google.auth.oauth2.MockTokenServerTransport;
2625
import java.io.IOException;
2726
import java.util.Map;
@@ -39,10 +38,8 @@ public LowLevelHttpRequest buildRequest(String method, String url) throws IOExce
3938

4039
public String getLastAudienceSent() throws IOException {
4140
String contentString = lastRequest.getContentAsString();
42-
Map<String, String> query = TestUtils.parseQuery(contentString);
43-
String assertion = query.get("assertion");
44-
JsonWebSignature signature = JsonWebSignature.parse(JSON_FACTORY, assertion);
45-
String foundTargetAudience = (String) signature.getPayload().get("api_audience");
46-
return foundTargetAudience;
41+
JsonParser jsonParser = JSON_FACTORY.createJsonParser(contentString);
42+
Map<String, Object> json = jsonParser.parseAndClose(Map.class);
43+
return (String) json.get("audience");
4744
}
4845
}

sdk-platform-java/java-showcase-3.21.0/gapic-showcase/src/test/resources/test_gdch_credential.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"format_version": "1",
44
"project": "project-id",
55
"private_key_id": "d84a4fefcf50791d4a90f2d7af17469d6282df9d",
6-
"private_key": "-----BEGIN PRIVATE KEY-----\nMIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALX0PQoe1igW12i\nkv1bN/r9lN749y2ijmbc/mFHPyS3hNTyOCjDvBbXYbDhQJzWVUikh4mvGBA07qTj79Xc3yBDfKP2IeyYQIFe0t0\nzkd7R9Zdn98Y2rIQC47aAbDfubtkU1U72t4zL11kHvoa0/RuFZjncvlr42X7be7lYh4p3NAgMBAAECgYASk5wDw\n4Az2ZkmeuN6Fk/y9H+Lcb2pskJIXjrL533vrDWGOC48LrsThMQPv8cxBky8HFSEklPpkfTF95tpD43iVwJRB/Gr\nCtGTw65IfJ4/tI09h6zGc4yqvIo1cHX/LQ+SxKLGyir/dQM925rGt/VojxY5ryJR7GLbCzxPnJm/oQJBANwOCO6\nD2hy1LQYJhXh7O+RLtA/tSnT1xyMQsGT+uUCMiKS2bSKx2wxo9k7h3OegNJIu1q6nZ6AbxDK8H3+d0dUCQQDTrP\nSXagBxzp8PecbaCHjzNRSQE2in81qYnrAFNB4o3DpHyMMY6s5ALLeHKscEWnqP8Ur6X4PvzZecCWU9BKAZAkAut\nLPknAuxSCsUOvUfS1i87ex77Ot+w6POp34pEX+UWb+u5iFn2cQacDTHLV1LtE80L8jVLSbrbrlH43H0DjU5AkEA\ngidhycxS86dxpEljnOMCw8CKoUBd5I880IUahEiUltk7OLJYS/Ts1wbn3kPOVX3wyJs8WBDtBkFrDHW2ezth2QJ\nADj3e1YhMVdjJW5jqwlD/VNddGjgzyunmiZg0uOXsHXbytYmsA545S8KRQFaJKFXYYFo2kOjqOiC1T2cAzMDjCQ\n==\n-----END PRIVATE KEY-----\n",
6+
"private_key": "-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEIK+G0KyJyJPDK/tyYsF0RyFW+X1GMsWbrlWn8TbLAI0doAoGCCqGSM49\nAwEHoUQDQgAEmGMJNcYyb9IfS4ngfvSf+c0sxOdcRfPNnZajry4bLgs++2VpQn8e\nl27zuFvF8jrM2/XyG5y9heE8YSjfLMy0Rw==\n-----END EC PRIVATE KEY-----\n",
77
"name": "service-identity-name",
88
"ca_cert_path": "fake-cert-path",
99
"token_uri": "https://service-identity.fake-domain/authenticate"

sdk-platform-java/java-showcase-3.25.8/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITGdch.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ void testCreateClient_withGdchCredentialAndNoAudience_defaultsToEndpointBasedAud
156156
NullPointerException expectedEx =
157157
assertThrows(NullPointerException.class, () -> initialCredentials.refresh());
158158
assertTrue(
159-
expectedEx.getMessage().contains("Audience are not configured for GDCH service account"));
159+
expectedEx
160+
.getMessage()
161+
.contains("Audience cannot be null or empty for GDCH service account credentials"));
160162

161163
// However, the credentials prepared in ClientContext should be able to refresh since the
162164
// audience would be
@@ -201,7 +203,7 @@ void testCreateClient_withGdchCredentialWithValidAudience_usesCredentialWithPass
201203
assertTrue(
202204
thrownByClientCreds
203205
.getMessage()
204-
.contains("Audience are not configured for GDCH service account"));
206+
.contains("Audience cannot be null or empty for GDCH service account credentials"));
205207

206208
// But the credentials prepared in ClientContext should be able to refresh since the audience
207209
// would be internally

sdk-platform-java/java-showcase-3.25.8/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/util/InterceptingMockTokenServerTransport.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.api.client.http.LowLevelHttpRequest;
2020
import com.google.api.client.json.JsonFactory;
21+
import com.google.api.client.json.JsonParser;
2122
import com.google.api.client.json.gson.GsonFactory;
2223
import com.google.api.client.json.webtoken.JsonWebSignature;
2324
import com.google.api.client.testing.http.MockLowLevelHttpRequest;
@@ -39,10 +40,8 @@ public LowLevelHttpRequest buildRequest(String method, String url) throws IOExce
3940

4041
public String getLastAudienceSent() throws IOException {
4142
String contentString = lastRequest.getContentAsString();
42-
Map<String, String> query = TestUtils.parseQuery(contentString);
43-
String assertion = query.get("assertion");
44-
JsonWebSignature signature = JsonWebSignature.parse(JSON_FACTORY, assertion);
45-
String foundTargetAudience = (String) signature.getPayload().get("api_audience");
46-
return foundTargetAudience;
43+
JsonParser jsonParser = JSON_FACTORY.createJsonParser(contentString);
44+
Map<String, Object> json = jsonParser.parseAndClose(Map.class);
45+
return (String) json.get("audience");
4746
}
4847
}

sdk-platform-java/java-showcase-3.25.8/gapic-showcase/src/test/resources/test_gdch_credential.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"format_version": "1",
44
"project": "project-id",
55
"private_key_id": "d84a4fefcf50791d4a90f2d7af17469d6282df9d",
6-
"private_key": "-----BEGIN PRIVATE KEY-----\nMIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALX0PQoe1igW12i\nkv1bN/r9lN749y2ijmbc/mFHPyS3hNTyOCjDvBbXYbDhQJzWVUikh4mvGBA07qTj79Xc3yBDfKP2IeyYQIFe0t0\nzkd7R9Zdn98Y2rIQC47aAbDfubtkU1U72t4zL11kHvoa0/RuFZjncvlr42X7be7lYh4p3NAgMBAAECgYASk5wDw\n4Az2ZkmeuN6Fk/y9H+Lcb2pskJIXjrL533vrDWGOC48LrsThMQPv8cxBky8HFSEklPpkfTF95tpD43iVwJRB/Gr\nCtGTw65IfJ4/tI09h6zGc4yqvIo1cHX/LQ+SxKLGyir/dQM925rGt/VojxY5ryJR7GLbCzxPnJm/oQJBANwOCO6\nD2hy1LQYJhXh7O+RLtA/tSnT1xyMQsGT+uUCMiKS2bSKx2wxo9k7h3OegNJIu1q6nZ6AbxDK8H3+d0dUCQQDTrP\nSXagBxzp8PecbaCHjzNRSQE2in81qYnrAFNB4o3DpHyMMY6s5ALLeHKscEWnqP8Ur6X4PvzZecCWU9BKAZAkAut\nLPknAuxSCsUOvUfS1i87ex77Ot+w6POp34pEX+UWb+u5iFn2cQacDTHLV1LtE80L8jVLSbrbrlH43H0DjU5AkEA\ngidhycxS86dxpEljnOMCw8CKoUBd5I880IUahEiUltk7OLJYS/Ts1wbn3kPOVX3wyJs8WBDtBkFrDHW2ezth2QJ\nADj3e1YhMVdjJW5jqwlD/VNddGjgzyunmiZg0uOXsHXbytYmsA545S8KRQFaJKFXYYFo2kOjqOiC1T2cAzMDjCQ\n==\n-----END PRIVATE KEY-----\n",
6+
"private_key": "-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEIK+G0KyJyJPDK/tyYsF0RyFW+X1GMsWbrlWn8TbLAI0doAoGCCqGSM49\nAwEHoUQDQgAEmGMJNcYyb9IfS4ngfvSf+c0sxOdcRfPNnZajry4bLgs++2VpQn8e\nl27zuFvF8jrM2/XyG5y9heE8YSjfLMy0Rw==\n-----END EC PRIVATE KEY-----\n",
77
"name": "service-identity-name",
88
"ca_cert_path": "fake-cert-path",
99
"token_uri": "https://service-identity.fake-domain/authenticate"

sdk-platform-java/java-showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCompositeTracer.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
import static com.google.common.truth.Truth.assertThat;
3434

3535
import com.google.api.gax.tracing.CompositeTracerFactory;
36-
import com.google.api.gax.tracing.OpenTelemetryMetricsFactory;
3736
import com.google.api.gax.tracing.ObservabilityAttributes;
37+
import com.google.api.gax.tracing.OpenTelemetryMetricsFactory;
3838
import com.google.api.gax.tracing.OpenTelemetryTracingFactory;
3939
import com.google.showcase.v1beta1.EchoClient;
4040
import com.google.showcase.v1beta1.EchoRequest;
@@ -92,11 +92,13 @@ void tearDown() {
9292
}
9393

9494
private CompositeTracerFactory createCompositeTracerFactory() {
95-
OpenTelemetryTracingFactory openTelemetryTracingFactory = new OpenTelemetryTracingFactory(openTelemetrySdk);
95+
OpenTelemetryTracingFactory openTelemetryTracingFactory =
96+
new OpenTelemetryTracingFactory(openTelemetrySdk);
9697
OpenTelemetryMetricsFactory metricsTracerFactory =
9798
new OpenTelemetryMetricsFactory(openTelemetrySdk);
9899

99-
return new CompositeTracerFactory(Arrays.asList(openTelemetryTracingFactory, metricsTracerFactory));
100+
return new CompositeTracerFactory(
101+
Arrays.asList(openTelemetryTracingFactory, metricsTracerFactory));
100102
}
101103

102104
@Test

sdk-platform-java/java-showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITGdch.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ void testCreateClient_withGdchCredentialAndNoAudience_defaultsToEndpointBasedAud
156156
NullPointerException expectedEx =
157157
assertThrows(NullPointerException.class, () -> initialCredentials.refresh());
158158
assertTrue(
159-
expectedEx.getMessage().contains("Audience are not configured for GDCH service account"));
159+
expectedEx
160+
.getMessage()
161+
.contains("Audience cannot be null or empty for GDCH service account credentials"));
160162

161163
// However, the credentials prepared in ClientContext should be able to refresh since the
162164
// audience would be
@@ -201,7 +203,7 @@ void testCreateClient_withGdchCredentialWithValidAudience_usesCredentialWithPass
201203
assertTrue(
202204
thrownByClientCreds
203205
.getMessage()
204-
.contains("Audience are not configured for GDCH service account"));
206+
.contains("Audience cannot be null or empty for GDCH service account credentials"));
205207

206208
// But the credentials prepared in ClientContext should be able to refresh since the audience
207209
// would be internally

0 commit comments

Comments
 (0)