Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions internal/xds/resolver/serviceconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,35 @@ type route struct {
maxStreamDuration time.Duration
retryConfig *xdsresource.RetryConfig
hashPolicies []*xdsresource.HashPolicy
autoHostRewrite bool
}

func (r route) String() string {
return fmt.Sprintf("%s -> { clusters: %v, maxStreamDuration: %v }", r.m.String(), r.clusters, r.maxStreamDuration)
}

// autoHostRewriteKey is the context key used to store the value of
// route's autoHostRewrite in the RPC context.
type autoHostRewriteKey struct{}

// getMatchedAutoHostRewrite retrieves the autoHostRewrite value from the provided context.
func getMatchedAutoHostRewrite(ctx context.Context) bool {
autohostRewrite, _ := ctx.Value(autoHostRewriteKey{}).(bool)
return autohostRewrite
}

// GetMatchedAutoHostRewriteForTesting returns the value of autoHostRewrite feild;
// to be used for testing only.
func GetMatchedAutoHostRewriteForTesting(ctx context.Context) bool {
return getMatchedAutoHostRewrite(ctx)
}

// SetMatchedAutoHostRewrite adds the autoHostRewrite value to the context for
// the xds_cluster_impl LB policy to pick.
func SetMatchedAutoHostRewrite(ctx context.Context, autohostRewrite bool) context.Context {
return context.WithValue(ctx, autoHostRewriteKey{}, autohostRewrite)
}

// stoppableConfigSelector extends the iresolver.ConfigSelector interface with a
// stop() method. This makes it possible to swap the current config selector
// with an erroring config selector when the LDS or RDS resource is not found on
Expand Down Expand Up @@ -197,6 +220,7 @@ func (cs *configSelector) SelectConfig(rpcInfo iresolver.RPCInfo) (*iresolver.RP

lbCtx := clustermanager.SetPickedCluster(rpcInfo.Context, cluster.name)
lbCtx = iringhash.SetXDSRequestHash(lbCtx, cs.generateHash(rpcInfo, rt.hashPolicies))
lbCtx = SetMatchedAutoHostRewrite(lbCtx, rt.autoHostRewrite)

config := &iresolver.RPCConfig{
// Communicate to the LB policy the chosen cluster and request hash, if Ring Hash LB policy.
Expand Down
1 change: 1 addition & 0 deletions internal/xds/resolver/xds_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ func (r *xdsResolver) newConfigSelector() (*configSelector, error) {

cs.routes[i].retryConfig = rt.RetryConfig
cs.routes[i].hashPolicies = rt.HashPolicies
cs.routes[i].autoHostRewrite = rt.AutoHostRewrite
}

// Account for this config selector's clusters. Do this after no further
Expand Down
123 changes: 123 additions & 0 deletions internal/xds/resolver/xds_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ import (
"google.golang.org/grpc/codes"
estats "google.golang.org/grpc/experimental/stats"
"google.golang.org/grpc/internal"
"google.golang.org/grpc/internal/envconfig"
iresolver "google.golang.org/grpc/internal/resolver"
iringhash "google.golang.org/grpc/internal/ringhash"
"google.golang.org/grpc/internal/testutils"
"google.golang.org/grpc/internal/testutils/xds/e2e"
"google.golang.org/grpc/internal/xds/balancer/clustermanager"
"google.golang.org/grpc/internal/xds/bootstrap"
serverFeature "google.golang.org/grpc/internal/xds/clients/xdsclient"
xdsResolver "google.golang.org/grpc/internal/xds/resolver"
rinternal "google.golang.org/grpc/internal/xds/resolver/internal"
"google.golang.org/grpc/internal/xds/xdsclient"
"google.golang.org/grpc/internal/xds/xdsclient/xdsresource/version"
Expand Down Expand Up @@ -1297,3 +1300,123 @@ func (s) TestConfigSelector_FailureCases(t *testing.T) {
func newDurationP(d time.Duration) *time.Duration {
return &d
}

// TestResolver_AutoHostRewrite verifies the propagation of the AutoHostRewrite
// field from the xDS RouteConfiguration to the internal MatchedRoute.
//
// Per gRFC A81, this feature should only be active if three conditions are met:
// 1. The environment variable (XDSAuthorityRewrite) is enabled.
// 2. The xDS server is marked as a "trusted_xds_server" in the bootstrap config.
// 3. The specific Route Configuration requests `auto_host_rewrite=true`.
func TestResolver_AutoHostRewrite(t *testing.T) {
for _, tt := range []struct {
name string
autoHostRewrite bool
envconfig bool
serverfeature serverFeature.ServerFeature
wantAutoHostRewrite bool
}{
{
name: "AutoHostRewrite enabled",
autoHostRewrite: true,
envconfig: true,
serverfeature: serverFeature.ServerFeatureTrustedXDSServer,
wantAutoHostRewrite: true,
},
{
name: "Disable_EnvVarOff",
autoHostRewrite: true,
envconfig: false,
serverfeature: serverFeature.ServerFeatureTrustedXDSServer,
wantAutoHostRewrite: false,
},
{
name: "Disable_UntrustedServer",
autoHostRewrite: true,
envconfig: true,
wantAutoHostRewrite: false,
},
{
name: "Route config with AutoHostRewrite disabled",
autoHostRewrite: false,
envconfig: true,
serverfeature: serverFeature.ServerFeatureTrustedXDSServer,
wantAutoHostRewrite: false,
},
} {
t.Run(tt.name, func(t *testing.T) {
testutils.SetEnvConfig(t, &envconfig.XDSAuthorityRewrite, tt.envconfig)

// Spin up an xDS management server for the test.
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()
nodeID := uuid.New().String()
mgmtServer, _, _, _ := setupManagementServerForTest(t, nodeID)

// Configure the management server with a good listener resource and a
// route configuration resource, as specified by the test case.
listeners := []*v3listenerpb.Listener{e2e.DefaultClientListener(defaultTestServiceName, defaultTestRouteConfigName)}
clusters := []*v3clusterpb.Cluster{e2e.DefaultCluster(defaultTestClusterName, defaultTestEndpointName, e2e.SecurityLevelNone)}
endpoints := []*v3endpointpb.ClusterLoadAssignment{e2e.DefaultEndpoint(defaultTestEndpointName, defaultTestHostname, defaultTestPort)}
routes := []*v3routepb.RouteConfiguration{{
Name: defaultTestRouteConfigName,
VirtualHosts: []*v3routepb.VirtualHost{{
Domains: []string{defaultTestServiceName},
Routes: []*v3routepb.Route{{
Match: &v3routepb.RouteMatch{PathSpecifier: &v3routepb.RouteMatch_Prefix{Prefix: "/"}},
Action: &v3routepb.Route_Route{Route: &v3routepb.RouteAction{
ClusterSpecifier: &v3routepb.RouteAction_WeightedClusters{WeightedClusters: &v3routepb.WeightedCluster{
Clusters: []*v3routepb.WeightedCluster_ClusterWeight{
{
Name: defaultTestClusterName,
Weight: &wrapperspb.UInt32Value{Value: 100},
},
},
}},
HostRewriteSpecifier: &v3routepb.RouteAction_AutoHostRewrite{
AutoHostRewrite: &wrapperspb.BoolValue{Value: tt.autoHostRewrite},
},
}},
}},
}},
}}
configureAllResourcesOnManagementServer(ctx, t, mgmtServer, nodeID, listeners, routes, clusters, endpoints)

trustedXdsServer := "[]"
if tt.serverfeature == serverFeature.ServerFeatureTrustedXDSServer {
trustedXdsServer = `["trusted_xds_server"]`
}

opts := bootstrap.ConfigOptionsForTesting{
Servers: []byte(fmt.Sprintf(`[{
"server_uri": %q,
"channel_creds": [{"type": "insecure"}],
"server_features": %s
}]`, mgmtServer.Address, trustedXdsServer)),
Node: []byte(fmt.Sprintf(`{"id": "%s"}`, nodeID)),
}

contents, err := bootstrap.NewContentsForTesting(opts)
if err != nil {
t.Fatalf("Failed to create bootstrap configuration: %v", err)
}

// Build the resolver and read the config selector out of it.
stateCh, _, _ := buildResolverForTarget(t, resolver.Target{URL: *testutils.MustParseURL("xds:///" + defaultTestServiceName)}, contents)
cs := verifyUpdateFromResolver(ctx, t, stateCh, "")

res, err := cs.SelectConfig(iresolver.RPCInfo{
Context: ctx,
Method: "/service/method",
})
if err != nil {
t.Fatalf("cs.SelectConfig(): %v", err)
}

gotAutoHostRewrite := xdsResolver.GetMatchedAutoHostRewriteForTesting(res.Context)
if gotAutoHostRewrite != tt.wantAutoHostRewrite {
t.Fatalf("Got autoHostRewrite: %v, want: %v", gotAutoHostRewrite, tt.wantAutoHostRewrite)
}
})
}
}