Auto Rewrite Host Headers for Terminating Gateways

Tries to partially address
https://github.com/hashicorp/consul/issues/8707
This commit is contained in:
Yong Wen Chua 2020-10-23 17:53:29 +08:00
parent 26d41f076b
commit e1820517aa
No known key found for this signature in database
GPG Key ID: 2D68945D154B53AD
1 changed files with 10 additions and 3 deletions

View File

@ -113,7 +113,7 @@ func (s *Server) routesFromSnapshotTerminatingGateway(_ connectionInfo, cfgSnap
if resolver.LoadBalancer != nil {
lb = resolver.LoadBalancer
}
route, err := makeNamedDefaultRouteWithLB(clusterName, lb)
route, err := makeNamedDefaultRouteWithLB(clusterName, lb, true)
if err != nil {
logger.Error("failed to make route", "cluster", clusterName, "error", err)
continue
@ -123,7 +123,7 @@ func (s *Server) routesFromSnapshotTerminatingGateway(_ connectionInfo, cfgSnap
// If there is a service-resolver for this service then also setup routes for each subset
for name := range resolver.Subsets {
clusterName = connect.ServiceSNI(svc.Name, name, svc.NamespaceOrDefault(), cfgSnap.Datacenter, cfgSnap.Roots.TrustDomain)
route, err := makeNamedDefaultRouteWithLB(clusterName, lb)
route, err := makeNamedDefaultRouteWithLB(clusterName, lb, true)
if err != nil {
logger.Error("failed to make route", "cluster", clusterName, "error", err)
continue
@ -135,13 +135,20 @@ func (s *Server) routesFromSnapshotTerminatingGateway(_ connectionInfo, cfgSnap
return resources, nil
}
func makeNamedDefaultRouteWithLB(clusterName string, lb *structs.LoadBalancer) (*envoy.RouteConfiguration, error) {
func makeNamedDefaultRouteWithLB(clusterName string, lb *structs.LoadBalancer, autoHostRewrite bool) (*envoy.RouteConfiguration, error) {
action := makeRouteActionFromName(clusterName)
if err := injectLBToRouteAction(lb, action.Route); err != nil {
return nil, fmt.Errorf("failed to apply load balancer configuration to route action: %v", err)
}
// Configure Envoy to rewrite Host header
if autoHostRewrite {
action.Route.HostRewriteSpecifier = &envoyroute.RouteAction_AutoHostRewrite{
AutoHostRewrite: makeBoolValue(true),
}
}
return &envoy.RouteConfiguration{
Name: clusterName,
VirtualHosts: []*envoyroute.VirtualHost{