Skip to content

Commit 6e079f2

Browse files
committed
mTLS client-server test from bootstrap configuration
1 parent 3a23825 commit 6e079f2

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

xds/src/test/java/io/grpc/xds/XdsSecurityClientServerTest.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@
5151
import io.grpc.Status;
5252
import io.grpc.StatusOr;
5353
import io.grpc.StatusRuntimeException;
54+
import io.grpc.TlsServerCredentials;
5455
import io.grpc.stub.StreamObserver;
5556
import io.grpc.testing.GrpcCleanupRule;
57+
import io.grpc.testing.TlsTesting;
5658
import io.grpc.testing.protobuf.SimpleRequest;
5759
import io.grpc.testing.protobuf.SimpleResponse;
5860
import io.grpc.testing.protobuf.SimpleServiceGrpc;
@@ -513,6 +515,36 @@ public void mtlsClientServer_changeServerContext_expectException()
513515
}
514516
}
515517

518+
@Test
519+
public void mtlsClientServer_withClientAuthentication_withTlsChannelCredsFromBootstrap()
520+
throws Exception {
521+
final String mtlsCertProviderInstanceName = "mtls_channel_creds_identity_certs";
522+
523+
UpstreamTlsContext upstreamTlsContext =
524+
setBootstrapInfoWithMTlsChannelCredsAndBuildUpstreamTlsContext(
525+
mtlsCertProviderInstanceName, CLIENT_KEY_FILE, CLIENT_PEM_FILE, CA_PEM_FILE);
526+
527+
DownstreamTlsContext downstreamTlsContext =
528+
setBootstrapInfoWithMTlsChannelCredsAndBuildDownstreamTlsContext(
529+
mtlsCertProviderInstanceName, SERVER_1_KEY_FILE, SERVER_1_PEM_FILE, CA_PEM_FILE);
530+
531+
ServerCredentials serverCreds = TlsServerCredentials.newBuilder()
532+
.keyManager(TlsTesting.loadCert(SERVER_1_PEM_FILE), TlsTesting.loadCert(SERVER_1_KEY_FILE))
533+
.trustManager(TlsTesting.loadCert(CA_PEM_FILE))
534+
.clientAuth(TlsServerCredentials.ClientAuth.REQUIRE)
535+
.build();
536+
537+
buildServer(
538+
XdsServerBuilder.forPort(0, serverCreds)
539+
.xdsClientPoolFactory(fakePoolFactory)
540+
.addService(new SimpleServiceImpl()),
541+
downstreamTlsContext);
542+
543+
SimpleServiceGrpc.SimpleServiceBlockingStub blockingStub =
544+
getBlockingStub(upstreamTlsContext, OVERRIDE_AUTHORITY);
545+
assertThat(unaryRpc("buddy", blockingStub)).isEqualTo("Hello buddy");
546+
}
547+
516548
private void performMtlsTestAndGetListenerWatcher(
517549
UpstreamTlsContext upstreamTlsContext, String certInstanceName2,
518550
String privateKey2, String cert2, String trustCa2)
@@ -573,6 +605,22 @@ private UpstreamTlsContext setBootstrapInfoAndBuildUpstreamTlsContextForUsingSys
573605
.build());
574606
}
575607

608+
private UpstreamTlsContext setBootstrapInfoWithMTlsChannelCredsAndBuildUpstreamTlsContext(
609+
String instanceName, String clientKeyFile, String clientPemFile, String caCertFile) {
610+
bootstrapInfoForClient = CommonBootstrapperTestUtils
611+
.buildBootstrapInfoForMTlsChannelCredentialServerInfo(
612+
instanceName, clientKeyFile, clientPemFile, caCertFile);
613+
return CommonTlsContextTestsUtil.buildUpstreamTlsContext(instanceName, true);
614+
}
615+
616+
private DownstreamTlsContext setBootstrapInfoWithMTlsChannelCredsAndBuildDownstreamTlsContext(
617+
String instanceName, String serverKeyFile, String serverPemFile, String caCertFile) {
618+
bootstrapInfoForServer = CommonBootstrapperTestUtils
619+
.buildBootstrapInfoForMTlsChannelCredentialServerInfo(
620+
instanceName, serverKeyFile, serverPemFile, caCertFile);
621+
return CommonTlsContextTestsUtil.buildDownstreamTlsContext(instanceName, true, true);
622+
}
623+
576624
private void buildServerWithTlsContext(DownstreamTlsContext downstreamTlsContext)
577625
throws Exception {
578626
buildServerWithTlsContext(downstreamTlsContext, InsecureServerCredentials.create());

xds/src/test/java/io/grpc/xds/client/CommonBootstrapperTestUtils.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
import com.google.common.collect.ImmutableMap;
2121
import io.grpc.ChannelCredentials;
2222
import io.grpc.InsecureChannelCredentials;
23+
import io.grpc.TlsChannelCredentials;
2324
import io.grpc.internal.BackoffPolicy;
2425
import io.grpc.internal.FakeClock;
2526
import io.grpc.internal.JsonParser;
2627
import io.grpc.xds.client.Bootstrapper.ServerInfo;
2728
import io.grpc.xds.internal.security.CommonTlsContextTestsUtil;
2829
import io.grpc.xds.internal.security.TlsContextManagerImpl;
30+
import java.io.File;
2931
import java.io.IOException;
3032
import java.util.ArrayList;
3133
import java.util.HashMap;
@@ -160,6 +162,42 @@ public static Bootstrapper.BootstrapInfo buildBootstrapInfo(
160162
.build();
161163
}
162164

165+
public static Bootstrapper.BootstrapInfo buildBootstrapInfoForMTlsChannelCredentialServerInfo(
166+
String instanceName, String privateKey, String cert, String trustCa) {
167+
try {
168+
privateKey = CommonTlsContextTestsUtil.getTempFileNameForResourcesFile(privateKey);
169+
cert = CommonTlsContextTestsUtil.getTempFileNameForResourcesFile(cert);
170+
trustCa = CommonTlsContextTestsUtil.getTempFileNameForResourcesFile(trustCa);
171+
} catch (IOException ioe) {
172+
throw new RuntimeException(ioe);
173+
}
174+
175+
HashMap<String, String> config = new HashMap<>();
176+
config.put("certificate_file", cert);
177+
config.put("private_key_file", privateKey);
178+
config.put("ca_certificate_file", trustCa);
179+
180+
ChannelCredentials creds;
181+
try {
182+
creds = TlsChannelCredentials.newBuilder()
183+
.customCertificatesConfig(config)
184+
.keyManager(new File(cert), new File(privateKey))
185+
.trustManager(new File(trustCa))
186+
.build();
187+
} catch (IOException ioe) {
188+
throw new RuntimeException(ioe);
189+
}
190+
191+
// config for tls channel credentials and for certificate provider are the same
192+
return Bootstrapper.BootstrapInfo.builder()
193+
.servers(ImmutableList.<ServerInfo>of(ServerInfo.create(SERVER_URI, creds)))
194+
.node(EnvoyProtoData.Node.newBuilder().build())
195+
.certProviders(ImmutableMap.of(
196+
instanceName,
197+
Bootstrapper.CertificateProviderInfo.create("file_watcher", config)))
198+
.build();
199+
}
200+
163201
public static boolean setEnableXdsFallback(boolean target) {
164202
boolean oldValue = BootstrapperImpl.enableXdsFallback;
165203
BootstrapperImpl.enableXdsFallback = target;

0 commit comments

Comments
 (0)