@@ -30,6 +30,7 @@ import (
3030
3131 "google.golang.org/grpc"
3232 "google.golang.org/grpc/codes"
33+ "google.golang.org/grpc/internal/envconfig"
3334 "google.golang.org/grpc/internal/grpctest"
3435 "google.golang.org/grpc/internal/stubserver"
3536 "google.golang.org/grpc/internal/testutils"
@@ -237,6 +238,7 @@ func (s) TestCaReloading(t *testing.T) {
237238// is performed and checked for failure, ensuring that gRPC is correctly using
238239// the changed-on-disk bundle map.
239240func (s ) Test_SPIFFE_Reloading (t * testing.T ) {
241+ testutils .SetEnvConfig (t , & envconfig .XDSSPIFFEEnabled , true )
240242 clientSPIFFEBundle , err := os .ReadFile (testdata .Path ("spiffe_end2end/client_spiffebundle.json" ))
241243 if err != nil {
242244 t .Fatalf ("Failed to read test SPIFFE bundle: %v" , err )
@@ -357,6 +359,7 @@ func (s) TestMTLS(t *testing.T) {
357359// chain that is compatible with the client's configured SPIFFE bundle map. An
358360// MTLS connection is attempted between the two and checked for success.
359361func (s ) Test_MTLS_SPIFFE (t * testing.T ) {
362+ testutils .SetEnvConfig (t , & envconfig .XDSSPIFFEEnabled , true )
360363 tests := []struct {
361364 name string
362365 serverOption grpc.ServerOption
@@ -372,7 +375,7 @@ func (s) Test_MTLS_SPIFFE(t *testing.T) {
372375 }
373376 for _ , tc := range tests {
374377 t .Run (tc .name , func (t * testing.T ) {
375- s := stubserver .StartTestService (t , nil , grpc . Creds ( testutils . CreateServerTLSCredentialsCompatibleWithSPIFFE ( t , tls . RequireAndVerifyClientCert )) )
378+ s := stubserver .StartTestService (t , nil , tc . serverOption )
376379 defer s .Stop ()
377380
378381 cfg := fmt .Sprintf (`{
@@ -403,7 +406,44 @@ func (s) Test_MTLS_SPIFFE(t *testing.T) {
403406 }
404407}
405408
409+ // Test_MTLS_SPIFFE_FlagDisabled configures a client and server. The server has
410+ // a certificate chain that is compatible with the client's configured SPIFFE
411+ // bundle map. However, the XDS flag that enabled SPIFFE usage is disabled. An
412+ // MTLS connection is attempted between the two and checked for failure.
413+ func (s ) Test_MTLS_SPIFFE_FlagDisabled (t * testing.T ) {
414+ testutils .SetEnvConfig (t , & envconfig .XDSSPIFFEEnabled , false )
415+ serverOption := grpc .Creds (testutils .CreateServerTLSCredentialsCompatibleWithSPIFFE (t , tls .RequireAndVerifyClientCert ))
416+ s := stubserver .StartTestService (t , nil , serverOption )
417+ defer s .Stop ()
418+
419+ cfg := fmt .Sprintf (`{
420+ "certificate_file": "%s",
421+ "private_key_file": "%s",
422+ "spiffe_trust_bundle_map_file": "%s"
423+ }` ,
424+ testdata .Path ("spiffe_end2end/client_spiffe.pem" ),
425+ testdata .Path ("spiffe_end2end/client.key" ),
426+ testdata .Path ("spiffe_end2end/client_spiffebundle.json" ))
427+ tlsBundle , stop , err := tlscreds .NewBundle ([]byte (cfg ))
428+ if err != nil {
429+ t .Fatalf ("Failed to create TLS bundle: %v" , err )
430+ }
431+ defer stop ()
432+ conn , err := grpc .NewClient (s .Address , grpc .WithCredentialsBundle (tlsBundle ), grpc .WithAuthority ("x.test.example.com" ))
433+ if err != nil {
434+ t .Fatalf ("Error dialing: %v" , err )
435+ }
436+ defer conn .Close ()
437+ client := testgrpc .NewTestServiceClient (conn )
438+ ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
439+ defer cancel ()
440+ if _ , err = client .EmptyCall (ctx , & testpb.Empty {}); err == nil {
441+ t .Errorf ("EmptyCall(): got success want failure" )
442+ }
443+ }
444+
406445func (s ) Test_MTLS_SPIFFE_Failure (t * testing.T ) {
446+ testutils .SetEnvConfig (t , & envconfig .XDSSPIFFEEnabled , true )
407447 tests := []struct {
408448 name string
409449 certFile string
0 commit comments