@@ -35,14 +35,17 @@ import (
3535 "google.golang.org/grpc/codes"
3636 estats "google.golang.org/grpc/experimental/stats"
3737 "google.golang.org/grpc/internal"
38+ "google.golang.org/grpc/internal/envconfig"
3839 iresolver "google.golang.org/grpc/internal/resolver"
3940 iringhash "google.golang.org/grpc/internal/ringhash"
4041 "google.golang.org/grpc/internal/testutils"
4142 "google.golang.org/grpc/internal/testutils/xds/e2e"
4243 "google.golang.org/grpc/internal/xds/balancer/clustermanager"
4344 "google.golang.org/grpc/internal/xds/bootstrap"
45+ serverFeature "google.golang.org/grpc/internal/xds/clients/xdsclient"
4446 rinternal "google.golang.org/grpc/internal/xds/resolver/internal"
4547 "google.golang.org/grpc/internal/xds/xdsclient"
48+ "google.golang.org/grpc/internal/xds/xdsclient/xdsresource"
4649 "google.golang.org/grpc/internal/xds/xdsclient/xdsresource/version"
4750 "google.golang.org/grpc/metadata"
4851 "google.golang.org/grpc/resolver"
@@ -1297,3 +1300,123 @@ func (s) TestConfigSelector_FailureCases(t *testing.T) {
12971300func newDurationP (d time.Duration ) * time.Duration {
12981301 return & d
12991302}
1303+
1304+ // TestResolver_AutoHostRewrite verifies the propagation of the AutoHostRewrite
1305+ // field from the xDS RouteConfiguration to the internal MatchedRoute.
1306+ //
1307+ // Per gRFC A81, this feature should only be active if three conditions are met:
1308+ // 1. The environment variable (XDSAuthorityRewrite) is enabled.
1309+ // 2. The xDS server is marked as a "trusted_xds_server" in the bootstrap config.
1310+ // 3. The specific Route Configuration requests `auto_host_rewrite=true`.
1311+ func TestResolver_AutoHostRewrite (t * testing.T ) {
1312+ for _ , tt := range []struct {
1313+ name string
1314+ autoHostRewrite bool
1315+ envconfig bool
1316+ serverfeature serverFeature.ServerFeature
1317+ wantAutoHostRewrite bool
1318+ }{
1319+ {
1320+ name : "AutoHostRewrite enabled" ,
1321+ autoHostRewrite : true ,
1322+ envconfig : true ,
1323+ serverfeature : serverFeature .ServerFeatureTrustedXDSServer ,
1324+ wantAutoHostRewrite : true ,
1325+ },
1326+ {
1327+ name : "Disable_EnvVarOff" ,
1328+ autoHostRewrite : true ,
1329+ envconfig : false ,
1330+ serverfeature : serverFeature .ServerFeatureTrustedXDSServer ,
1331+ wantAutoHostRewrite : false ,
1332+ },
1333+ {
1334+ name : "Disable_UntrustedServer" ,
1335+ autoHostRewrite : true ,
1336+ envconfig : true ,
1337+ wantAutoHostRewrite : false ,
1338+ },
1339+ {
1340+ name : "Route config with AutoHostRewrite disabled" ,
1341+ autoHostRewrite : false ,
1342+ envconfig : true ,
1343+ serverfeature : serverFeature .ServerFeatureTrustedXDSServer ,
1344+ wantAutoHostRewrite : false ,
1345+ },
1346+ } {
1347+ t .Run (tt .name , func (t * testing.T ) {
1348+ testutils .SetEnvConfig (t , & envconfig .XDSAuthorityRewrite , tt .envconfig )
1349+
1350+ // Spin up an xDS management server for the test.
1351+ ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
1352+ defer cancel ()
1353+ nodeID := uuid .New ().String ()
1354+ mgmtServer , _ , _ , _ := setupManagementServerForTest (t , nodeID )
1355+
1356+ // Configure the management server with a good listener resource and a
1357+ // route configuration resource, as specified by the test case.
1358+ listeners := []* v3listenerpb.Listener {e2e .DefaultClientListener (defaultTestServiceName , defaultTestRouteConfigName )}
1359+ clusters := []* v3clusterpb.Cluster {e2e .DefaultCluster (defaultTestClusterName , defaultTestEndpointName , e2e .SecurityLevelNone )}
1360+ endpoints := []* v3endpointpb.ClusterLoadAssignment {e2e .DefaultEndpoint (defaultTestEndpointName , defaultTestHostname , defaultTestPort )}
1361+ routes := []* v3routepb.RouteConfiguration {{
1362+ Name : defaultTestRouteConfigName ,
1363+ VirtualHosts : []* v3routepb.VirtualHost {{
1364+ Domains : []string {defaultTestServiceName },
1365+ Routes : []* v3routepb.Route {{
1366+ Match : & v3routepb.RouteMatch {PathSpecifier : & v3routepb.RouteMatch_Prefix {Prefix : "/" }},
1367+ Action : & v3routepb.Route_Route {Route : & v3routepb.RouteAction {
1368+ ClusterSpecifier : & v3routepb.RouteAction_WeightedClusters {WeightedClusters : & v3routepb.WeightedCluster {
1369+ Clusters : []* v3routepb.WeightedCluster_ClusterWeight {
1370+ {
1371+ Name : defaultTestClusterName ,
1372+ Weight : & wrapperspb.UInt32Value {Value : 100 },
1373+ },
1374+ },
1375+ }},
1376+ HostRewriteSpecifier : & v3routepb.RouteAction_AutoHostRewrite {
1377+ AutoHostRewrite : & wrapperspb.BoolValue {Value : tt .autoHostRewrite },
1378+ },
1379+ }},
1380+ }},
1381+ }},
1382+ }}
1383+ configureAllResourcesOnManagementServer (ctx , t , mgmtServer , nodeID , listeners , routes , clusters , endpoints )
1384+
1385+ trustedXdsServer := "[]"
1386+ if tt .serverfeature == serverFeature .ServerFeatureTrustedXDSServer {
1387+ trustedXdsServer = `["trusted_xds_server"]`
1388+ }
1389+
1390+ opts := bootstrap.ConfigOptionsForTesting {
1391+ Servers : []byte (fmt .Sprintf (`[{
1392+ "server_uri": %q,
1393+ "channel_creds": [{"type": "insecure"}],
1394+ "server_features": %s
1395+ }]` , mgmtServer .Address , trustedXdsServer )),
1396+ Node : []byte (fmt .Sprintf (`{"id": "%s"}` , nodeID )),
1397+ }
1398+
1399+ contents , err := bootstrap .NewContentsForTesting (opts )
1400+ if err != nil {
1401+ t .Fatalf ("Failed to create bootstrap configuration: %v" , err )
1402+ }
1403+
1404+ // Build the resolver and read the config selector out of it.
1405+ stateCh , _ , _ := buildResolverForTarget (t , resolver.Target {URL : * testutils .MustParseURL ("xds:///" + defaultTestServiceName )}, contents )
1406+ cs := verifyUpdateFromResolver (ctx , t , stateCh , "" )
1407+
1408+ res , err := cs .SelectConfig (iresolver.RPCInfo {
1409+ Context : ctx ,
1410+ Method : "/service/method" ,
1411+ })
1412+ if err != nil {
1413+ t .Fatalf ("cs.SelectConfig(): %v" , err )
1414+ }
1415+
1416+ gotRoute := xdsresource .GetMatchedRouteForTesting (res .Context )
1417+ if gotRoute .AutoHostRewrite != tt .wantAutoHostRewrite {
1418+ t .Fatalf ("Got autoHostRewrite: %v, want: %v" , gotRoute .AutoHostRewrite , tt .wantAutoHostRewrite )
1419+ }
1420+ })
1421+ }
1422+ }
0 commit comments