Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Prefix URL Rewrite #282

Merged
merged 3 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .changelog/282.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:feature
Support prefix replacement URLRewrite filter ([docs](https://gateway-api.sigs.k8s.io/references/spec/#gateway.networking.k8s.io/v1alpha2.HTTPPathModifier))
```
3 changes: 3 additions & 0 deletions dev/docs/supported-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ Supported features are marked with a grey checkbox
- [x] Remove headers
- [ ] Request mirroring
- [ ] Request redirecting
- [ ] URL Rewrite
- [x] Path Prefix Rewrite
- [ ] Full Path Rewrite
- [x] Extensions *not supported*
- [ ] Backend Refs
- [ ] Filters (see above)
Expand Down
27 changes: 22 additions & 5 deletions internal/adapters/consul/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
"sort"
"strconv"

"github.com/hashicorp/consul-api-gateway/internal/core"
"github.com/hashicorp/consul/api"

"github.com/hashicorp/consul-api-gateway/internal/core"
)

// httpRouteDiscoveryChain will convert a k8s HTTPRoute to a Consul service-router config entry and 0 or
Expand All @@ -23,6 +24,7 @@ func httpRouteDiscoveryChain(route core.HTTPRoute) (*api.ServiceRouterConfigEntr

for idx, rule := range route.Rules {
modifier := httpRouteFiltersToServiceRouteHeaderModifier(rule.Filters)
prefixRewrite := httpRouteFiltersToDestinationPrefixRewrite(rule.Filters)

var destination core.ResolvedService
if len(rule.Services) == 1 {
Expand Down Expand Up @@ -76,19 +78,22 @@ func httpRouteDiscoveryChain(route core.HTTPRoute) (*api.ServiceRouterConfigEntr
if len(rule.Matches) == 0 {
router.Routes = append(router.Routes, api.ServiceRoute{
Destination: &api.ServiceRouteDestination{
Service: destination.Service,
RequestHeaders: modifier,
Namespace: destination.ConsulNamespace,
PrefixRewrite: prefixRewrite,
RequestHeaders: modifier,
Service: destination.Service,
},
})
}

for _, match := range rule.Matches {
router.Routes = append(router.Routes, api.ServiceRoute{
Match: &api.ServiceRouteMatch{HTTP: httpRouteMatchToServiceRouteHTTPMatch(match)},
Destination: &api.ServiceRouteDestination{
Service: destination.Service,
RequestHeaders: modifier,
Namespace: destination.ConsulNamespace,
PrefixRewrite: prefixRewrite,
RequestHeaders: modifier,
Service: destination.Service,
},
})
}
Expand All @@ -97,6 +102,18 @@ func httpRouteDiscoveryChain(route core.HTTPRoute) (*api.ServiceRouterConfigEntr
return router, splitters
}

func httpRouteFiltersToDestinationPrefixRewrite(filters []core.HTTPFilter) string {
for _, filter := range filters {
switch filter.Type {
case core.HTTPURLRewriteFilterType:
if filter.URLRewrite.Type == core.ReplacePrefixMatchURLRewriteType {
return filter.URLRewrite.ReplacePrefixMatch
}
}
}
return ""
}

// httpRouteFiltersToServiceRouteHeaderModifier will consolidate a list of HTTP filters
// into a single set of header modifications for Consul to make as a request passes through.
func httpRouteFiltersToServiceRouteHeaderModifier(filters []core.HTTPFilter) *api.HTTPHeaderModifiers {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"Destination": {
"Service": "multiple-services-0",
"Namespace": "k8s",
"PrefixRewrite": "/",
"RequestHeaders": {
"Add": {
"x-add": "2",
Expand Down
7 changes: 7 additions & 0 deletions internal/adapters/consul/testdata/multiple-services.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@
"x-add-too": "2"
}
}
},
{
"Type": 2,
"URLRewrite": {
"Type": 0,
"ReplacePrefixMatch": "/"
}
}
],
"Services": [
Expand Down
144 changes: 144 additions & 0 deletions internal/commands/server/k8s_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,150 @@ func TestHTTPRouteFlattening(t *testing.T) {
testenv.Test(t, feature.Feature())
}

func TestHTTPRoutePathRewrite(t *testing.T) {
feature := features.New("http url path rewrite").
Assess("prefix rewrite", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
invalidService, err := e2e.DeployHTTPMeshService(ctx, cfg)
require.NoError(t, err)

validService, err := e2e.DeployHTTPMeshService(ctx, cfg)
require.NoError(t, err)

namespace := e2e.Namespace(ctx)
gatewayName := envconf.RandomName("gw", 16)
invalidRouteName := envconf.RandomName("route", 16)
validRouteName := envconf.RandomName("route", 16)

prefixMatch := gwv1alpha2.PathMatchPathPrefix

resources := cfg.Client().Resources(namespace)

_, gc := createGatewayClass(ctx, t, resources)
require.Eventually(t, gatewayClassStatusCheck(ctx, resources, gc.Name, namespace, conditionAccepted), checkTimeout, checkInterval, "gatewayclass not accepted in the allotted time")

checkPort := e2e.HTTPFlattenedPort(ctx)
httpsListener := createHTTPSListener(ctx, t, gwv1beta1.PortNumber(checkPort))
gw := createGateway(ctx, t, resources, gatewayName, namespace, gc, []gwv1beta1.Listener{httpsListener})
require.Eventually(t, gatewayStatusCheck(ctx, resources, gatewayName, namespace, conditionReady), checkTimeout, checkInterval, "no gateway found in the allotted time")

port := gwv1alpha2.PortNumber(invalidService.Spec.Ports[0].Port)
validPath := "/foo"
invalidPath := "/bar"
invalidPrefixMatch := "/v1/invalid"
validPrefixMatch := "/v1/api"
invalidRoute := &gwv1alpha2.HTTPRoute{
ObjectMeta: meta.ObjectMeta{
Name: invalidRouteName,
Namespace: namespace,
},
Spec: gwv1alpha2.HTTPRouteSpec{
CommonRouteSpec: gwv1alpha2.CommonRouteSpec{
ParentRefs: []gwv1alpha2.ParentReference{{
Name: gwv1alpha2.ObjectName(gatewayName),
}},
},
Hostnames: []gwv1alpha2.Hostname{"test.foo"},

Rules: []gwv1alpha2.HTTPRouteRule{{
Filters: []gwv1alpha2.HTTPRouteFilter{
{
Type: gwv1alpha2.HTTPRouteFilterURLRewrite,
URLRewrite: &gwv1alpha2.HTTPURLRewriteFilter{
Path: &gwv1alpha2.HTTPPathModifier{
Type: gwv1alpha2.PrefixMatchHTTPPathModifier,
ReplacePrefixMatch: &invalidPrefixMatch,
},
},
},
},
Matches: []gwv1alpha2.HTTPRouteMatch{{
Path: &gwv1alpha2.HTTPPathMatch{
Type: &prefixMatch,
Value: &invalidPath,
},
}},
BackendRefs: []gwv1alpha2.HTTPBackendRef{{
BackendRef: gwv1alpha2.BackendRef{
BackendObjectReference: gwv1alpha2.BackendObjectReference{
Name: gwv1alpha2.ObjectName(invalidService.Name),
Port: &port,
},
},
}},
}},
},
}
err = resources.Create(ctx, invalidRoute)
require.NoError(t, err)

port = gwv1alpha2.PortNumber(validService.Spec.Ports[0].Port)
validRoute := &gwv1alpha2.HTTPRoute{
ObjectMeta: meta.ObjectMeta{
Name: validRouteName,
Namespace: namespace,
},
Spec: gwv1alpha2.HTTPRouteSpec{
CommonRouteSpec: gwv1alpha2.CommonRouteSpec{
ParentRefs: []gwv1alpha2.ParentReference{{
Name: gwv1alpha2.ObjectName(gatewayName),
}},
},
Hostnames: []gwv1alpha2.Hostname{"test.foo"},

Rules: []gwv1alpha2.HTTPRouteRule{{
Filters: []gwv1alpha2.HTTPRouteFilter{
{
Type: gwv1alpha2.HTTPRouteFilterURLRewrite,
URLRewrite: &gwv1alpha2.HTTPURLRewriteFilter{
Path: &gwv1alpha2.HTTPPathModifier{
Type: gwv1alpha2.PrefixMatchHTTPPathModifier,
ReplacePrefixMatch: &validPrefixMatch,
},
},
},
},
Matches: []gwv1alpha2.HTTPRouteMatch{{
Path: &gwv1alpha2.HTTPPathMatch{
Type: &prefixMatch,
Value: &validPath,
},
}},
BackendRefs: []gwv1alpha2.HTTPBackendRef{{
BackendRef: gwv1alpha2.BackendRef{
BackendObjectReference: gwv1alpha2.BackendObjectReference{
Name: gwv1alpha2.ObjectName(validService.Name),
Port: &port,
},
},
}},
}},
},
}
err = resources.Create(ctx, validRoute)
require.NoError(t, err)

checkRoute(t, checkPort, invalidPath, httpResponse{
StatusCode: http.StatusOK,
Body: invalidService.Name,
}, map[string]string{
"Host": "test.foo",
}, "invalid not routable in allotted time")
checkRoute(t, checkPort, validPath, httpResponse{
StatusCode: http.StatusOK,
Body: validService.Name,
}, map[string]string{
"Host": "test.foo",
}, "valid service not routable in allotted time")

err = resources.Delete(ctx, gw)
require.NoError(t, err)

return ctx
})

testenv.Test(t, feature.Feature())
}

func TestHTTPMeshService(t *testing.T) {
feature := features.New("mesh service routing").
Assess("basic routing", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
Expand Down
19 changes: 16 additions & 3 deletions internal/core/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type HTTPFilterType int
const (
HTTPHeaderFilterType HTTPFilterType = iota
HTTPRedirectFilterType
HTTPURLRewriteFilterType
)

type HTTPHeaderFilter struct {
Expand All @@ -26,10 +27,22 @@ type HTTPRedirectFilter struct {
Status int
}

type URLRewriteType int

const (
ReplacePrefixMatchURLRewriteType = iota
)

type HTTPURLRewriteFilter struct {
Type URLRewriteType
ReplacePrefixMatch string
}

type HTTPFilter struct {
Type HTTPFilterType
Header HTTPHeaderFilter
Redirect HTTPRedirectFilter
Type HTTPFilterType
Header HTTPHeaderFilter
Redirect HTTPRedirectFilter
URLRewrite HTTPURLRewriteFilter
}

type HTTPMethod int
Expand Down
20 changes: 18 additions & 2 deletions internal/k8s/reconciler/http_route.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package reconciler

import (
"github.com/hashicorp/consul-api-gateway/internal/core"
"github.com/hashicorp/consul-api-gateway/internal/k8s/service"
"k8s.io/apimachinery/pkg/types"
gwv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"

"github.com/hashicorp/consul-api-gateway/internal/core"
"github.com/hashicorp/consul-api-gateway/internal/k8s/service"
)

func HTTPRouteID(namespacedName types.NamespacedName) string {
Expand Down Expand Up @@ -163,6 +164,21 @@ func convertHTTPRouteFilters(routeFilters []gwv1alpha2.HTTPRouteFilter) []core.H
Remove: filter.RequestHeaderModifier.Remove,
},
})
case gwv1alpha2.HTTPRouteFilterURLRewrite:
// We currently only support prefix match replacement
if filter.URLRewrite.Path == nil ||
filter.URLRewrite.Path.Type != gwv1alpha2.PrefixMatchHTTPPathModifier ||
filter.URLRewrite.Path.ReplacePrefixMatch == nil {
continue
}

filters = append(filters, core.HTTPFilter{
Type: core.HTTPURLRewriteFilterType,
URLRewrite: core.HTTPURLRewriteFilter{
Type: core.ReplacePrefixMatchURLRewriteType,
ReplacePrefixMatch: *filter.URLRewrite.Path.ReplacePrefixMatch,
},
})
case gwv1alpha2.HTTPRouteFilterRequestRedirect:
scheme := ""
if filter.RequestRedirect.Scheme != nil {
Expand Down
8 changes: 8 additions & 0 deletions internal/k8s/reconciler/http_route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ func TestConvertHTTPRoute(t *testing.T) {
"Hostname": "example.com",
"Port": 8443,
"Status": 302
},
"URLRewrite": {
"Type": 0,
"ReplacePrefixMatch": ""
}
},
{
Expand All @@ -167,6 +171,10 @@ func TestConvertHTTPRoute(t *testing.T) {
"Hostname": "",
"Port": 0,
"Status": 0
},
"URLRewrite": {
"Type": 0,
"ReplacePrefixMatch": ""
}
}
],
Expand Down