Skip to content

Commit 4f521cf

Browse files
committed
mTLS client-server test from bootstrap configuration
1 parent a1cbaad commit 4f521cf

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@
1818

1919
import com.google.common.collect.ImmutableList;
2020
import com.google.common.collect.ImmutableMap;
21+
import io.grpc.ChannelCredentials;
22+
import io.grpc.TlsChannelCredentials;
2123
import io.grpc.internal.BackoffPolicy;
2224
import io.grpc.internal.FakeClock;
2325
import io.grpc.internal.JsonParser;
2426
import io.grpc.xds.client.Bootstrapper.ServerInfo;
2527
import io.grpc.xds.internal.security.CommonTlsContextTestsUtil;
2628
import io.grpc.xds.internal.security.TlsContextManagerImpl;
29+
import java.io.File;
2730
import java.io.IOException;
2831
import java.util.ArrayList;
2932
import java.util.HashMap;
@@ -157,6 +160,42 @@ public static Bootstrapper.BootstrapInfo buildBootstrapInfo(
157160
.build();
158161
}
159162

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

0 commit comments

Comments
 (0)