3737import com .google .common .util .concurrent .SettableFuture ;
3838import io .envoyproxy .envoy .config .core .v3 .SocketAddress .Protocol ;
3939import io .envoyproxy .envoy .extensions .transport_sockets .tls .v3 .CertificateValidationContext ;
40+ import io .envoyproxy .envoy .type .matcher .v3 .StringMatcher ;
4041import io .grpc .Attributes ;
4142import io .grpc .EquivalentAddressGroup ;
4243import io .grpc .Grpc ;
117118@ RunWith (Parameterized .class )
118119public class XdsSecurityClientServerTest {
119120
121+ // TODO: Change this is a specific domain after
122+ // https://github.com/grpc/grpc-java/issues/12326 is fixed
123+ private static final String SAN_TO_MATCH = "*.test.google.fr" ;
124+
120125 @ Parameter
121126 public Boolean enableSpiffe ;
122127 private Boolean originalEnableSpiffe ;
@@ -217,7 +222,7 @@ public void tlsClientServer_useSystemRootCerts_useCombinedValidationContext() th
217222
218223 UpstreamTlsContext upstreamTlsContext =
219224 setBootstrapInfoAndBuildUpstreamTlsContextForUsingSystemRootCerts (CLIENT_KEY_FILE ,
220- CLIENT_PEM_FILE , true );
225+ CLIENT_PEM_FILE , true , SAN_TO_MATCH );
221226
222227 SimpleServiceGrpc .SimpleServiceBlockingStub blockingStub =
223228 getBlockingStub (upstreamTlsContext , /* overrideAuthority= */ OVERRIDE_AUTHORITY );
@@ -244,7 +249,7 @@ public void tlsClientServer_useSystemRootCerts_validationContext() throws Except
244249
245250 UpstreamTlsContext upstreamTlsContext =
246251 setBootstrapInfoAndBuildUpstreamTlsContextForUsingSystemRootCerts (CLIENT_KEY_FILE ,
247- CLIENT_PEM_FILE , false );
252+ CLIENT_PEM_FILE , false , SAN_TO_MATCH );
248253
249254 SimpleServiceGrpc .SimpleServiceBlockingStub blockingStub =
250255 getBlockingStub (upstreamTlsContext , /* overrideAuthority= */ OVERRIDE_AUTHORITY );
@@ -255,6 +260,39 @@ public void tlsClientServer_useSystemRootCerts_validationContext() throws Except
255260 }
256261 }
257262
263+ /**
264+ * Use system root ca cert for TLS channel - no mTLS.
265+ * Subj Alt Names to match are specified in the validaton context.
266+ */
267+ @ Test
268+ public void tlsClientServer_useSystemRootCerts_failureToMatchSubjAltNames () throws Exception {
269+ Path trustStoreFilePath = getCacertFilePathForTestCa ();
270+ try {
271+ setTrustStoreSystemProperties (trustStoreFilePath .toAbsolutePath ().toString ());
272+ DownstreamTlsContext downstreamTlsContext =
273+ setBootstrapInfoAndBuildDownstreamTlsContext (SERVER_1_PEM_FILE , null , null , null , null ,
274+ null , false , false );
275+ buildServerWithTlsContext (downstreamTlsContext );
276+
277+ UpstreamTlsContext upstreamTlsContext =
278+ setBootstrapInfoAndBuildUpstreamTlsContextForUsingSystemRootCerts (CLIENT_KEY_FILE ,
279+ CLIENT_PEM_FILE , true , "server1.test.google.in" );
280+
281+ SimpleServiceGrpc .SimpleServiceBlockingStub blockingStub =
282+ getBlockingStub (upstreamTlsContext , /* overrideAuthority= */ OVERRIDE_AUTHORITY );
283+ unaryRpc (/* requestMessage= */ "buddy" , blockingStub );
284+ fail ("Expected handshake failure exception" );
285+ } catch (StatusRuntimeException e ) {
286+ assertThat (e .getCause ()).isInstanceOf (SSLHandshakeException .class );
287+ assertThat (e .getCause ().getCause ()).isInstanceOf (CertificateException .class );
288+ assertThat (e .getCause ().getCause ().getMessage ()).isEqualTo (
289+ "Peer certificate SAN check failed" );
290+ } finally {
291+ Files .deleteIfExists (trustStoreFilePath );
292+ clearTrustStoreSystemProperties ();
293+ }
294+ }
295+
258296 /**
259297 * Use system root ca cert for TLS channel - mTLS.
260298 * Uses common_tls_context.combined_validation_context in upstream_tls_context.
@@ -266,12 +304,12 @@ public void tlsClientServer_useSystemRootCerts_requireClientAuth() throws Except
266304 setTrustStoreSystemProperties (trustStoreFilePath .toAbsolutePath ().toString ());
267305 DownstreamTlsContext downstreamTlsContext =
268306 setBootstrapInfoAndBuildDownstreamTlsContext (SERVER_1_PEM_FILE , null , null , null , null ,
269- null , false , false );
307+ null , false , true );
270308 buildServerWithTlsContext (downstreamTlsContext );
271309
272310 UpstreamTlsContext upstreamTlsContext =
273311 setBootstrapInfoAndBuildUpstreamTlsContextForUsingSystemRootCerts (CLIENT_KEY_FILE ,
274- CLIENT_PEM_FILE , true );
312+ CLIENT_PEM_FILE , true , SAN_TO_MATCH );
275313
276314 SimpleServiceGrpc .SimpleServiceBlockingStub blockingStub =
277315 getBlockingStub (upstreamTlsContext , /* overrideAuthority= */ OVERRIDE_AUTHORITY );
@@ -552,7 +590,7 @@ private UpstreamTlsContext setBootstrapInfoAndBuildUpstreamTlsContext(String cli
552590 private UpstreamTlsContext setBootstrapInfoAndBuildUpstreamTlsContextForUsingSystemRootCerts (
553591 String clientKeyFile ,
554592 String clientPemFile ,
555- boolean useCombinedValidationContext ) {
593+ boolean useCombinedValidationContext , String sanToMatch ) {
556594 bootstrapInfoForClient = CommonBootstrapperTestUtils
557595 .buildBootstrapInfo ("google_cloud_private_spiffe-client" , clientKeyFile , clientPemFile ,
558596 CA_PEM_FILE , null , null , null , null , null );
@@ -563,6 +601,9 @@ private UpstreamTlsContext setBootstrapInfoAndBuildUpstreamTlsContextForUsingSys
563601 CertificateValidationContext .newBuilder ()
564602 .setSystemRootCerts (
565603 CertificateValidationContext .SystemRootCerts .newBuilder ().build ())
604+ .addMatchSubjectAltNames (
605+ StringMatcher .newBuilder ()
606+ .setExact (sanToMatch ))
566607 .build ());
567608 }
568609 return CommonTlsContextTestsUtil .buildNewUpstreamTlsContextForCertProviderInstance (
0 commit comments