8000 bump auth dep and add some tests for createS2ASecuredChannelCredentials. · rmehta19/sdk-platform-java@8bf770d · GitHub
[go: up one dir, main page]

Skip to content

Commit 8bf770d

Browse files
committed
bump auth dep and add some tests for createS2ASecuredChannelCredentials.
1 parent c9a7edd commit 8bf770d

File tree

3 files changed

+76
-6
lines changed

3 files changed

+76
-6
lines changed

gapic-generator-java-pom-parent/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
consistent across modules in this repository -->
2828
<javax.annotation-api.version>1.3.2</javax.annotation-api.version>
2929
<grpc.version>1.68.1</grpc.version>
30-
<google.auth.version>1.29.1-SNAPSHOT</google.auth.version>
30+
<google.auth.version>1.30.0</google.auth.version>
3131
<google.http-client.version>1.45.0</google.http-client.version>
3232
<gson.version>2.11.0</gson.version>
3333
<guava.version>33.3.1-jre</guava.version>

gax-java/gax-grpc/src/main/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProvider.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
import com.google.auth.ApiKeyCredentials;
4747
import com.google.auth.Credentials;
4848
import com.google.auth.oauth2.ComputeEngineCredentials;
49-
import com.google.auth.oauth2.S2A;
49+
import com.google.auth.oauth2.SecureSessionAgent;
50+
import com.google.auth.oauth2.SecureSessionAgentConfig;
5051
import com.google.common.annotations.VisibleForTesting;
5152
import com.google.common.base.Preconditions;
5253
import com.google.common.base.Strings;
@@ -140,6 +141,7 @@ public final class InstantiatingGrpcChannelProvider implements TransportChannelP
140141
@Nullable private final Boolean allowNonDefaultServiceAccount;
141142
@VisibleForTesting final ImmutableMap<String, ?> directPathServiceConfig;
142143
@Nullable private final MtlsProvider mtlsProvider;
144+
@Nullable private final SecureSessionAgent s2aConfigProvider;
143145
@VisibleForTesting final Map<String, String> headersWithDuplicatesRemoved = new HashMap<>();
144146

145147
@Nullable
@@ -152,6 +154,7 @@ private InstantiatingGrpcChannelProvider(Builder builder) {
152154
this.endpoint = builder.endpoint;
153155
this.useS2A = builder.useS2A;
154156
this.mtlsProvider = builder.mtlsProvider;
157+
this.s2aConfigProvider = builder.s2aConfigProvider;
155158
this.envProvider = builder.envProvider;
156159
this.interceptorProvider = builder.interceptorProvider;
157160
this.maxInboundMessageSize = builder.maxInboundMessageSize;
@@ -492,11 +495,14 @@ ChannelCredentials createPlaintextToS2AChannelCredentials(String plaintextAddres
492495
* mtlsEndpoint.
493496
*/
494497
ChannelCredentials createS2ASecuredChannelCredentials() {
495-
S2A s2aUtils = S2A.newBuilder().build();
496-
String plaintextAddress = s2aUtils.getPlaintextS2AAddress();
497-
String mtlsAddress = s2aUtils.getMtlsS2AAddress();
498+
SecureSessionAgentConfig config = s2aConfigProvider.getConfig();
499+
String plaintextAddress = config.getPlaintextAddress();
500+
String mtlsAddress = config.getMtlsAddress();
498501
if (Strings.isNullOrEmpty(mtlsAddress)) {
499502
// Fallback to plaintext connection to S2A.
503+
LOG.log(
504+
Level.INFO,
505+
"Cannot establish an mTLS connection to S2A because autoconfig endpoint did not return a mtls address to reach S2A.");
500506
return createPlaintextToS2AChannelCredentials(plaintextAddress);
501507
}
502508
// Currently, MTLS to MDS is only available on GCE. See:
@@ -523,7 +529,7 @@ ChannelCredentials createS2ASecuredChannelCredentials() {
523529
// Fallback to plaintext-to-S2A connection if MTLS-MDS creds do not exist.
524530
LOG.log(
525531
Level.INFO,
526-
"Cannot establish an mTLS connection to S2A MTLS to MDS credentials do not exist on filesystem, falling back to plaintext connection to S2A");
532+
"Cannot establish an mTLS connection to S2A because MTLS to MDS credentials do not exist on filesystem, falling back to plaintext connection to S2A");
527533
return createPlaintextToS2AChannelCredentials(plaintextAddress);
528534
}
529535
}
@@ -739,6 +745,7 @@ public static final class Builder {
739745
private String endpoint;
740746
private boolean useS2A;
741747
private EnvironmentProvider envProvider;
748+
private SecureSessionAgent s2aConfigProvider = SecureSessionAgent.create();
742749
private MtlsProvider mtlsProvider = new MtlsProvider();
743750
@Nullable private GrpcInterceptorProvider interceptorProvider;
744751
@Nullable private Integer maxInboundMessageSize;
@@ -783,6 +790,7 @@ private Builder(InstantiatingGrpcChannelProvider provider) {
783790
this.allowNonDefaultServiceAccount = provider.allowNonDefaultServiceAccount;
784791
this.directPathServiceConfig = provider.directPathServiceConfig;
785792
this.mtlsProvider = provider.mtlsProvider;
793+
this.s2aConfigProvider = provider.s2aConfigProvider;
786794
}
787795

788796
/**
@@ -846,6 +854,12 @@ Builder setMtlsProvider(MtlsProvider mtlsProvider) {
846854
return this;
847855
}
848856

857+
@VisibleForTesting
858+
Builder setS2AConfigProvider(SecureSessionAgent s2aConfigProvider) {
859+
this.s2aConfigProvider = s2aConfigProvider;
860+
return this;
861+
}
862+
849863
/**
850864
* Sets the GrpcInterceptorProvider for this TransportChannelProvider.
851865
*

gax-java/gax-grpc/src/test/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProviderTest.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
import com.google.auth.http.AuthHttpConstants;
5252
import com.google.auth.oauth2.CloudShellCredentials;
5353
import com.google.auth.oauth2.ComputeEngineCredentials;
54+
import com.google.auth.oauth2.SecureSessionAgent;
55+
import com.google.auth.oauth2.SecureSessionAgentConfig;
5456
import com.google.common.collect.ImmutableList;
5557
import com.google.common.collect.ImmutableMap;
5658
import com.google.common.truth.Truth;
@@ -1042,6 +1044,60 @@ void createMtlsToS2AChannelCredentials_success() throws IOException {
10421044
TlsChannelCredentials.class);
10431045
}
10441046

1047+
@Test
1048+
void createS2ASecuredChannelCredentials_bothS2AAddressesNull_returnsNull() {
1049+
SecureSessionAgent s2aConfigProvider = Mockito.mock(SecureSessionAgent.class);
1050+
SecureSessionAgentConfig config = SecureSessionAgentConfig.createBuilder().build();
1051+
Mockito.when(s2aConfigProvider.getConfig()).thenReturn(config);
1052+
InstantiatingGrpcChannelProvider provider =
1053+
InstantiatingGrpcChannelProvider.newBuilder()
1054+
.setS2AConfigProvider(s2aConfigProvider)
1055+
.build();
1056+
assertThat(provider.createS2ASecuredChannelCredentials()).isNull();
1057+
}
1058+
1059+
@Test
1060+
void
1061+
createS2ASecuredChannelCredentials_mtlsS2AAddressNull_returnsPlaintextToS2AS2AChannelCredentials() {
1062+
SecureSessionAgent s2aConfigProvider = Mockito.mock(SecureSessionAgent.class);
1063+
SecureSessionAgentConfig config =
1064+
SecureSessionAgentConfig.createBuilder().setPlaintextAddress("localhost:8080").build();
1065+
Mockito.when(s2aConfigProvider.getConfig()).thenReturn(config);
1066+
FakeLogHandler logHandler = new FakeLogHandler();
1067+
InstantiatingGrpcChannelProvider.LOG.addHandler(logHandler);
1068+
InstantiatingGrpcChannelProvider provider =
1069+
InstantiatingGrpcChannelProvider.newBuilder()
1070+
.setS2AConfigProvider(s2aConfigProvider)
1071+
.build();
1072+
assertThat(provider.createS2ASecuredChannelCredentials()).isNotNull();
1073+
assertThat(logHandler.getAllMessages())
1074+
.contains(
1075+
"Cannot establish an mTLS connection to S2A because autoconfig endpoint did not return a mtls address to reach S2A.");
1076+
InstantiatingGrpcChannelProvider.LOG.removeHandler(logHandler);
1077+
}
1078+
1079+
@Test
1080+
void createS2ASecuredChannelCredentials_returnsPlaintextToS2AS2AChannelCredentials() {
1081+
SecureSessionAgent s2aConfigProvider = Mockito.mock(SecureSessionAgent.class);
1082+
SecureSessionAgentConfig config =
1083+
SecureSessionAgentConfig.createBuilder()
1084+
.setMtlsAddress("localhost:8080")
1085+
.setPlaintextAddress("localhost:8080")
1086+
.build();
1087+
Mockito.when(s2aConfigProvider.getConfig()).thenReturn(config);
1088+
FakeLogHandler logHandler = new FakeLogHandler();
1089+
InstantiatingGrpcChannelProvider.LOG.addHandler(logHandler);
1090+
InstantiatingGrpcChannelProvider provider =
1091+
InstantiatingGrpcChannelProvider.newBuilder()
1092+
.setS2AConfigProvider(s2aConfigProvider)
1093+
.build();
1094+
assertThat(provider.createS2ASecuredChannelCredentials()).isNotNull();
1095+
assertThat(logHandler.getAllMessages())
1096+
.contains(
1097+
"Cannot establish an mTLS connection to S2A because MTLS to MDS credentials do not exist on filesystem, falling back to plaintext connection to S2A");
1098+
InstantiatingGrpcChannelProvider.LOG.removeHandler(logHandler);
1099+
}
1100+
10451101
private static class FakeLogHandler extends Handler {
10461102

10471103
List<LogRecord> records = new ArrayList<>();

0 commit comments

Comments
 (0)
0