Skip to content

Commit

Permalink
Add support for request redirect filter
Browse files Browse the repository at this point in the history
  • Loading branch information
sgayangi committed Jun 17, 2024
1 parent c48ad64 commit c924cec
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 28 deletions.
7 changes: 7 additions & 0 deletions adapter/internal/oasparser/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ const (
ActionRewriteMethod string = "REWRITE_RESOURCE_METHOD"
ActionInterceptorService string = "CALL_INTERCEPTOR_SERVICE"
ActionRewritePath string = "REWRITE_RESOURCE_PATH"
ActionRedirectRequest string = "REDIRECT_REQUEST"
ActionMirrorRequest string = "MIRROR_REQUEST"

PolicyRequestInterceptor string = "PolicyRequestInterceptor"
PolicyResponseInterceptor string = "PolicyResponseInterceptor"
Expand All @@ -90,6 +92,11 @@ const (
HeaderValue string = "headerValue"
CurrentMethod string = "currentMethod"
UpdatedMethod string = "updatedMethod"
RedirectScheme string = "scheme"
RedirectHostname string = "hostname"
RedirectPath string = "path"
RedirectPort string = "port"
RedirectStatusCode string = "statusCode"
)

// API Type Constants
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestCreateRoute(t *testing.T) {

resourceWithGet := model.CreateMinimalDummyResourceForTests("/xWso2BasePath/resourcePath",
[]*model.Operation{model.NewOperationWithPolicies("GET", policies)},
"resource_operation_id", []model.Endpoint{endpoint}, true)
"resource_operation_id", []model.Endpoint{endpoint}, true, false)
clusterName := "resource_operation_id"
hostRewriteSpecifier := &routev3.RouteAction_AutoHostRewrite{
AutoHostRewrite: &wrapperspb.BoolValue{
Expand Down Expand Up @@ -143,7 +143,7 @@ func TestCreateRouteClusterSpecifier(t *testing.T) {
RawURL: "http://abc.com",
}
resourceWithGet := model.CreateMinimalDummyResourceForTests("/resourcePath", []*model.Operation{model.NewOperation("GET", nil, nil)},
"resource_operation_id", []model.Endpoint{endpoint}, false)
"resource_operation_id", []model.Endpoint{endpoint}, false, false)

route, err := createRoutes(generateRouteCreateParamsForUnitTests(title, apiType, vHost, xWso2BasePath, version, endpointBasePath,
&resourceWithGet, clusterName, nil, false))
Expand Down Expand Up @@ -174,7 +174,7 @@ func TestCreateRouteExtAuthzContext(t *testing.T) {
RawURL: "http://abc.com",
}
resourceWithGet := model.CreateMinimalDummyResourceForTests("/resourcePath", []*model.Operation{model.NewOperation("GET", nil, nil)},
"resource_operation_id", []model.Endpoint{endpoint}, false)
"resource_operation_id", []model.Endpoint{endpoint}, false, false)

route, err := createRoutes(generateRouteCreateParamsForUnitTests(title, apiType, vHost, xWso2BasePath, version,
endpointBasePath, &resourceWithGet, clusterName, nil, false))
Expand Down Expand Up @@ -571,7 +571,7 @@ func TestGetCorsPolicy(t *testing.T) {
assert.Empty(t, corsPolicy3.GetAllowCredentials(), "Allow Credential property should not be assigned.")

resourceWithGet := model.CreateMinimalDummyResourceForTests("/resourcePath", []*model.Operation{model.NewOperation("GET", nil, nil)},
"resource_operation_id", []model.Endpoint{endpoint}, false)
"resource_operation_id", []model.Endpoint{endpoint}, false, false)

// Route without CORS configuration
routeWithoutCors, err := createRoutes(generateRouteCreateParamsForUnitTests("test", "HTTP", "localhost", "/test", "1.0.0", "/test",
Expand Down
8 changes: 4 additions & 4 deletions adapter/internal/oasparser/envoyconf/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,13 @@ func testCreateRoutesForUnitTests(t *testing.T) []*routev3.Route {
operationPost := model.NewOperation("POST", nil, nil)
operationPut := model.NewOperation("PUT", nil, nil)
resourceWithGet := model.CreateMinimalDummyResourceForTests("/resourcePath", []*model.Operation{operationGet},
"resource_operation_id", []model.Endpoint{endpoint}, false)
"resource_operation_id", []model.Endpoint{endpoint}, false, false)
resourceWithPost := model.CreateMinimalDummyResourceForTests("/resourcePath", []*model.Operation{operationPost},
"resource_operation_id", []model.Endpoint{endpoint}, false)
"resource_operation_id", []model.Endpoint{endpoint}, false, false)
resourceWithPut := model.CreateMinimalDummyResourceForTests("/resourcePath", []*model.Operation{operationPut},
"resource_operation_id", []model.Endpoint{endpoint}, false)
"resource_operation_id", []model.Endpoint{endpoint}, false, false)
resourceWithMultipleOperations := model.CreateMinimalDummyResourceForTests("/resourcePath", []*model.Operation{operationGet, operationPut},
"resource_operation_id", []model.Endpoint{endpoint}, false)
"resource_operation_id", []model.Endpoint{endpoint}, false, false)

route1, err := createRoutes(generateRouteCreateParamsForUnitTests("test", "HTTP", "localhost", "/test", "1.0.0", "/test",
&resourceWithGet, "test-cluster", corsConfigModel3, false))
Expand Down
7 changes: 5 additions & 2 deletions adapter/internal/oasparser/envoyconf/routes_with_clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func CreateRoutesWithClusters(adapterInternalAPI *model.AdapterInternalAPI, inte
},
}
gqlop := model.NewOperationWithPolicies("POST", policies)
resource := model.CreateMinimalResource(adapterInternalAPI.GetXWso2Basepath(), []*model.Operation{gqlop}, "", adapterInternalAPI.Endpoints, true, gwapiv1.PathMatchExact)
resource := model.CreateMinimalResource(adapterInternalAPI.GetXWso2Basepath(), []*model.Operation{gqlop}, "", adapterInternalAPI.Endpoints, true, false, gwapiv1.PathMatchExact)
routesP, err := createRoutes(genRouteCreateParams(adapterInternalAPI, &resource, vHost, basePath, clusterName, nil,
nil, organizationID, false, false))
if err != nil {
Expand All @@ -201,7 +201,10 @@ func CreateRoutesWithClusters(adapterInternalAPI *model.AdapterInternalAPI, inte
var clusterName string
resourcePath := resource.GetPath()
endpoint := resource.GetEndpoints()
basePath := strings.TrimSuffix(endpoint.Endpoints[0].Basepath, "/")
basePath := ""
if len(endpoint.Endpoints) > 0 {
basePath = strings.TrimSuffix(endpoint.Endpoints[0].Basepath, "/")
}
existingClusterName := getExistingClusterName(*endpoint, processedEndpoints)

if existingClusterName == "" {
Expand Down
36 changes: 19 additions & 17 deletions adapter/internal/oasparser/model/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ import (
// These values are populated from extensions/properties
// mentioned under pathItem.
type Resource struct {
path string
pathMatchType gwapiv1.PathMatchType
methods []*Operation
iD string
endpoints *EndpointCluster
endpointSecurity []*EndpointSecurity
vendorExtensions map[string]interface{}
hasPolicies bool
path string
pathMatchType gwapiv1.PathMatchType
methods []*Operation
iD string
endpoints *EndpointCluster
endpointSecurity []*EndpointSecurity
vendorExtensions map[string]interface{}
hasPolicies bool
hasRequestRedirectFilter bool
}

// GetEndpointSecurity returns the endpoint security object of a given resource.
Expand Down Expand Up @@ -107,21 +108,22 @@ func (resource *Resource) HasPolicies() bool {

// CreateMinimalDummyResourceForTests create a resource object with minimal required set of values
// which could be used for unit tests.
func CreateMinimalDummyResourceForTests(path string, methods []*Operation, id string, urls []Endpoint, hasPolicies bool) Resource {
func CreateMinimalDummyResourceForTests(path string, methods []*Operation, id string, urls []Endpoint, hasPolicies bool, hasRequestRedirectPolicy bool) Resource {

endpoints := generateEndpointCluster(urls, constants.LoadBalance)
return CreateMinimalResource(path, methods, id, endpoints, hasPolicies, gwapiv1.PathMatchPathPrefix)
return CreateMinimalResource(path, methods, id, endpoints, hasPolicies, hasRequestRedirectPolicy, gwapiv1.PathMatchPathPrefix)
}

// CreateMinimalResource create a resource object with minimal required set of values
func CreateMinimalResource(path string, methods []*Operation, id string, endpoints *EndpointCluster, hasPolicies bool, pathMatchType gwapiv1.PathMatchType) Resource {
func CreateMinimalResource(path string, methods []*Operation, id string, endpoints *EndpointCluster, hasPolicies bool, hasRequestRedirectPolicy bool, pathMatchType gwapiv1.PathMatchType) Resource {
return Resource{
path: path,
methods: methods,
iD: id,
endpoints: endpoints,
pathMatchType: pathMatchType,
hasPolicies: hasPolicies,
path: path,
methods: methods,
iD: id,
endpoints: endpoints,
pathMatchType: pathMatchType,
hasPolicies: hasPolicies,
hasRequestRedirectFilter: hasRequestRedirectPolicy,
}
}

Expand Down
2 changes: 1 addition & 1 deletion adapter/internal/oasparser/model/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func getResources() []*Resource {
resources := make([]*Resource, len(paths))
for index := range paths {
res := CreateMinimalDummyResourceForTests(paths[index], make([]*Operation, 0), "",
make([]Endpoint, 0), false)
make([]Endpoint, 0), false, false)
resources[index] = &res
}
return resources
Expand Down

0 comments on commit c924cec

Please sign in to comment.