Skip to content

Commit

Permalink
remove unused policy code
Browse files Browse the repository at this point in the history
  • Loading branch information
AmaliMatharaarachchi committed Mar 24, 2024
1 parent cc4c77d commit 99509f7
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 1,013 deletions.
6 changes: 4 additions & 2 deletions adapter/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/wso2/apk/adapter

go 1.20
go 1.21

toolchain go1.22.1

require (
github.com/envoyproxy/go-control-plane v0.12.0
Expand All @@ -16,7 +18,6 @@ require (
golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb
google.golang.org/grpc v1.62.0
google.golang.org/protobuf v1.32.0
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.29.2
k8s.io/apimachinery v0.29.2
k8s.io/client-go v0.29.2
Expand Down Expand Up @@ -82,6 +83,7 @@ require (
google.golang.org/genproto/googleapis/api v0.0.0-20240123012728-ef4313101c80 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.29.2 // indirect
k8s.io/component-base v0.29.2 // indirect
Expand Down
5 changes: 5 additions & 0 deletions adapter/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ github.com/envoyproxy/go-control-plane v0.12.0/go.mod h1:ZBTaoJ23lqITozF0M6G4/Ir
github.com/envoyproxy/protoc-gen-validate v1.0.4 h1:gVPz/FMfvh57HdSJQyvBtF00j8JU4zdyUgIUNhlgg0A=
github.com/envoyproxy/protoc-gen-validate v1.0.4/go.mod h1:qys6tmnRsYrQqIhm2bvKZH4Blx/1gTIZ2UKVY1M+Yew=
github.com/evanphx/json-patch v5.7.0+incompatible h1:vgGkfT/9f8zE6tvSCe74nfpAVDQ2tG6yudJd8LBksgI=
github.com/evanphx/json-patch v5.7.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/evanphx/json-patch/v5 v5.8.0 h1:lRj6N9Nci7MvzrXuX6HFzU8XjmhPiXPlsKEy1u0KQro=
github.com/evanphx/json-patch/v5 v5.8.0/go.mod h1:VNkHZ/282BpEyt/tObQO8s5CMPmYYq14uClGH4abBuQ=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
Expand Down Expand Up @@ -85,6 +86,7 @@ github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand Down Expand Up @@ -124,8 +126,10 @@ github.com/prometheus/common v0.45.0/go.mod h1:YJmSTw9BoKxJplESWWxlbyttQR4uaEcGy
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
github.com/shirou/gopsutil/v3 v3.24.2 h1:kcR0erMbLg5/3LcInpw0X/rrPSqq4CDPyI6A6ZRC18Y=
github.com/shirou/gopsutil/v3 v3.24.2/go.mod h1:tSg/594BcA+8UdQU2XcW803GWYgdtauFFPgJCJKZlVk=
github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM=
Expand Down Expand Up @@ -159,6 +163,7 @@ github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5t
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
Expand Down
4 changes: 0 additions & 4 deletions adapter/internal/oasparser/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ const (

// operational policy field names
const (
ActionHeaderAdd string = "SET_HEADER"
ActionHeaderRemove string = "REMOVE_HEADER"
ActionRewriteMethod string = "REWRITE_RESOURCE_METHOD"
ActionInterceptorService string = "CALL_INTERCEPTOR_SERVICE"
ActionRewritePath string = "REWRITE_RESOURCE_PATH"

Expand All @@ -82,7 +79,6 @@ const (

RewritePathResourcePath string = "resourcePath"
RewritePathType string = "rewritePathType"
InterceptorServiceURL string = "interceptorServiceURL"
InterceptorEndpoints string = "interceptorEndpoints"
InterceptorServiceIncludes string = "includes"
IncludeQueryParams string = "includeQueryParams"
Expand Down
179 changes: 12 additions & 167 deletions adapter/internal/oasparser/envoyconf/routes_with_clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -896,34 +896,10 @@ func createRoutes(params *routeCreateParams) (routes []*routev3.Route, err error
var responseHeadersToRemove []string
var pathRewriteConfig *envoy_type_matcherv3.RegexMatchAndSubstitute

hasMethodRewritePolicy := false
var newMethod string

// Policies - for request flow
for _, requestPolicy := range operation.GetPolicies().Request {
logger.LoggerOasparser.Debug("Adding request flow policies for ", resourcePath, operation.GetMethod())
switch requestPolicy.Action {

case constants.ActionHeaderAdd:
logger.LoggerOasparser.Debugf("Adding %s policy to request flow for %s %s",
constants.ActionHeaderAdd, resourcePath, operation.GetMethod())
requestHeaderToAdd, err := generateHeaderToAddRouteConfig(requestPolicy.Parameters)
if err != nil {
return nil, fmt.Errorf("error adding request policy %s to operation %s of resource %s."+
" %v", requestPolicy.Action, operation.GetMethod(), resourcePath, err)
}
requestHeadersToAdd = append(requestHeadersToAdd, requestHeaderToAdd)

case constants.ActionHeaderRemove:
logger.LoggerOasparser.Debugf("Adding %s policy to request flow for %s %s",
constants.ActionHeaderRemove, resourcePath, operation.GetMethod())
requestHeaderToRemove, err := generateHeaderToRemoveString(requestPolicy.Parameters)
if err != nil {
return nil, fmt.Errorf("error adding request policy %s to operation %s of resource %s."+
" %v", requestPolicy.Action, operation.GetMethod(), resourcePath, err)
}
requestHeadersToRemove = append(requestHeadersToRemove, requestHeaderToRemove)

case constants.ActionRewritePath:
logger.LoggerOasparser.Debugf("Adding %s policy to request flow for %s %s",
constants.ActionRewritePath, resourcePath, operation.GetMethod())
Expand All @@ -936,105 +912,23 @@ func createRoutes(params *routeCreateParams) (routes []*routev3.Route, err error
return nil, errors.New(errMsg)
}
pathRewriteConfig = regexRewrite

case constants.ActionRewriteMethod:
logger.LoggerOasparser.Debugf("Adding %s policy to request flow for %s %s",
constants.ActionRewriteMethod, resourcePath, operation.GetMethod())
hasMethodRewritePolicy, err = isMethodRewrite(resourcePath, operation.GetMethod(), requestPolicy.Parameters)
if err != nil {
return nil, err
}
if !hasMethodRewritePolicy {
continue
}
newMethod, err = getRewriteMethod(resourcePath, operation.GetMethod(), requestPolicy.Parameters)
if err != nil {
return nil, err
}
}
}

// Policies - for response flow
for _, responsePolicy := range operation.GetPolicies().Response {
logger.LoggerOasparser.Debug("Adding response flow policies for ", resourcePath, operation.GetMethod())
switch responsePolicy.Action {

case constants.ActionHeaderAdd:
logger.LoggerOasparser.Debugf("Adding %s policy to response flow for %s %s",
constants.ActionHeaderAdd, resourcePath, operation.GetMethod())
responseHeaderToAdd, err := generateHeaderToAddRouteConfig(responsePolicy.Parameters)
if err != nil {
return nil, fmt.Errorf("error adding response policy %s to operation %s of resource %s."+
" %v", responsePolicy.Action, operation.GetMethod(), resourcePath, err)
}
responseHeadersToAdd = append(responseHeadersToAdd, responseHeaderToAdd)

case constants.ActionHeaderRemove:
logger.LoggerOasparser.Debugf("Adding %s policy to response flow for %s %s",
constants.ActionHeaderRemove, resourcePath, operation.GetMethod())
responseHeaderToRemove, err := generateHeaderToRemoveString(responsePolicy.Parameters)
if err != nil {
return nil, fmt.Errorf("error adding response policy %s to operation %s of resource %s."+
" %v", responsePolicy.Action, operation.GetMethod(), resourcePath, err)
}
responseHeadersToRemove = append(responseHeadersToRemove, responseHeaderToRemove)
}
}

// TODO: (suksw) preserve header key case?
if hasMethodRewritePolicy {
logger.LoggerOasparser.Debugf("Creating two routes to support method rewrite for %s %s. New method: %s",
resourcePath, operation.GetMethod(), newMethod)
match1 := generateRouteMatch(routePath)
match1.Headers = generateHTTPMethodMatcher(operation.GetMethod(), clusterName)
match2 := generateRouteMatch(routePath)
match2.Headers = generateHTTPMethodMatcher(newMethod, clusterName)

//- external routes only accept requests if metadata "method-rewrite" is null
//- external routes adds the metadata "method-rewrite"
//- internal routes only accept requests if metadata "method-rewrite" matches
// metadataValue <old_method>_to_<new_method>
match1.DynamicMetadata = generateMetadataMatcherForExternalRoutes()
metadataValue := operation.GetMethod() + "_to_" + newMethod
match2.DynamicMetadata = generateMetadataMatcherForInternalRoutes(metadataValue)

action1 := generateRouteAction(apiType, routeConfig, rateLimitPolicyCriteria)
action2 := generateRouteAction(apiType, routeConfig, rateLimitPolicyCriteria)

// Create route1 for current method.
// Do not add policies to route config. Send via enforcer
route1 := generateRouteConfig(xWso2Basepath+operation.GetMethod(), match1, action1, nil, decorator, perRouteFilterConfigs,
nil, nil, nil, nil)

// Create route2 for new method.
// Add all policies to route config. Do not send via enforcer.
if pathRewriteConfig != nil {
action2.Route.RegexRewrite = pathRewriteConfig
} else {
action2.Route.RegexRewrite = generateRegexMatchAndSubstitute(routePath, resourcePath, pathMatchType)
}
configToSkipEnforcer := generateFilterConfigToSkipEnforcer()
route2 := generateRouteConfig(xWso2Basepath, match2, action2, nil, decorator, configToSkipEnforcer,
requestHeadersToAdd, requestHeadersToRemove, responseHeadersToAdd, responseHeadersToRemove)

routes = append(routes, route1)
routes = append(routes, route2)
logger.LoggerOasparser.Debug("Creating routes for resource with policies", resourcePath, operation.GetMethod())
// create route for current method. Add policies to route config. Send via enforcer
action := generateRouteAction(apiType, routeConfig, rateLimitPolicyCriteria)
match := generateRouteMatch(routePath)
match.Headers = generateHTTPMethodMatcher(operation.GetMethod(), clusterName)
match.DynamicMetadata = generateMetadataMatcherForExternalRoutes()
if pathRewriteConfig != nil {
action.Route.RegexRewrite = pathRewriteConfig
} else {
logger.LoggerOasparser.Debug("Creating routes for resource with policies", resourcePath, operation.GetMethod())
// create route for current method. Add policies to route config. Send via enforcer
action := generateRouteAction(apiType, routeConfig, rateLimitPolicyCriteria)
match := generateRouteMatch(routePath)
match.Headers = generateHTTPMethodMatcher(operation.GetMethod(), clusterName)
match.DynamicMetadata = generateMetadataMatcherForExternalRoutes()
if pathRewriteConfig != nil {
action.Route.RegexRewrite = pathRewriteConfig
} else {
action.Route.RegexRewrite = generateRegexMatchAndSubstitute(routePath, resourcePath, pathMatchType)
}
route := generateRouteConfig(xWso2Basepath, match, action, nil, decorator, perRouteFilterConfigs,
requestHeadersToAdd, requestHeadersToRemove, responseHeadersToAdd, responseHeadersToRemove)
routes = append(routes, route)
action.Route.RegexRewrite = generateRegexMatchAndSubstitute(routePath, resourcePath, pathMatchType)
}
route := generateRouteConfig(xWso2Basepath, match, action, nil, decorator, perRouteFilterConfigs,
requestHeadersToAdd, requestHeadersToRemove, responseHeadersToAdd, responseHeadersToRemove)
routes = append(routes, route)
}
} else {
logger.LoggerOasparser.Debugf("Creating routes for resource : %s that has no policies", resourcePath)
Expand Down Expand Up @@ -1410,55 +1304,6 @@ func generateSubstitutionString(resourcePath string, pathMatchType gwapiv1.PathM
return resourceRegex
}

func isMethodRewrite(resourcePath, method string, policyParams interface{}) (isMethodRewrite bool, err error) {
var paramsToRewriteMethod map[string]interface{}
var ok bool
if paramsToRewriteMethod, ok = policyParams.(map[string]interface{}); !ok {
return false, fmt.Errorf("error while processing policy parameter map for "+
"request policy %s to operation %s of resource %s. Map: %v",
constants.ActionRewriteMethod, method, resourcePath, policyParams)
}

currentMethod, exists := paramsToRewriteMethod[constants.CurrentMethod]
if !exists {
return true, nil
}
currentMethodString, _ := currentMethod.(string)

if currentMethodString == "<no value>" { // the package text/template return this for keys that does not exist
return true, nil
}

if currentMethodString != method {
return false, nil
}
return true, nil // currentMethodString == method
}

func getRewriteMethod(resourcePath, method string, policyParams interface{}) (rewriteMethod string, err error) {
var paramsToRewriteMethod map[string]interface{}
var ok bool
if paramsToRewriteMethod, ok = policyParams.(map[string]interface{}); !ok {
return "", fmt.Errorf("error while processing policy parameter map for "+
"request policy %s to operation %s of resource %s. Map: %v",
constants.ActionRewriteMethod, method, resourcePath, policyParams)
}

updatedMethod, exists := paramsToRewriteMethod[constants.UpdatedMethod]
if !exists {
return "", fmt.Errorf("error adding request policy %s to operation %s of resource %s."+
" Policy parameter updatedMethod not found",
constants.ActionRewriteMethod, method, resourcePath)
}
updatedMethodString, isString := updatedMethod.(string)
if !isString {
return "", fmt.Errorf("error adding request policy %s to operation %s of resource %s."+
" Policy parameter updatedMethod is in incorrect format", constants.ActionRewriteMethod,
method, resourcePath)
}
return updatedMethodString, nil
}

func getUpgradeConfig(apiType string) []*routev3.RouteAction_UpgradeConfig {
var upgradeConfig []*routev3.RouteAction_UpgradeConfig
if apiType == constants.WS {
Expand Down
67 changes: 0 additions & 67 deletions adapter/internal/oasparser/model/adapter_internal_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,73 +618,6 @@ func (adapterInternalAPI *AdapterInternalAPI) SetInfoHTTPRouteCR(httpRoute *gwap
'Resource' in resource level RateLimitPolicies`, filter.ExtensionRef.Name)
}
}
case gwapiv1.HTTPRouteFilterRequestHeaderModifier:
for _, header := range filter.RequestHeaderModifier.Add {
policyParameters := make(map[string]interface{})
policyParameters[constants.HeaderName] = string(header.Name)
policyParameters[constants.HeaderValue] = string(header.Value)

policies.Request = append(policies.Request, Policy{
PolicyName: string(gwapiv1.HTTPRouteFilterRequestHeaderModifier),
Action: constants.ActionHeaderAdd,
Parameters: policyParameters,
})
}
for _, header := range filter.RequestHeaderModifier.Remove {
policyParameters := make(map[string]interface{})
policyParameters[constants.HeaderName] = string(header)

policies.Request = append(policies.Request, Policy{
PolicyName: string(gwapiv1.HTTPRouteFilterRequestHeaderModifier),
Action: constants.ActionHeaderRemove,
Parameters: policyParameters,
})
}
for _, header := range filter.RequestHeaderModifier.Set {
policyParameters := make(map[string]interface{})
policyParameters[constants.HeaderName] = string(header.Name)
policyParameters[constants.HeaderValue] = string(header.Value)

policies.Request = append(policies.Request, Policy{
PolicyName: string(gwapiv1.HTTPRouteFilterRequestHeaderModifier),
Action: constants.ActionHeaderAdd,
Parameters: policyParameters,
})
}
case gwapiv1.HTTPRouteFilterResponseHeaderModifier:
for _, header := range filter.ResponseHeaderModifier.Add {
policyParameters := make(map[string]interface{})
policyParameters[constants.HeaderName] = string(header.Name)
policyParameters[constants.HeaderValue] = string(header.Value)

policies.Response = append(policies.Response, Policy{
PolicyName: string(gwapiv1.HTTPRouteFilterResponseHeaderModifier),
Action: constants.ActionHeaderAdd,
Parameters: policyParameters,
})
}
for _, header := range filter.ResponseHeaderModifier.Remove {
policyParameters := make(map[string]interface{})
policyParameters[constants.HeaderName] = string(header)

policies.Response = append(policies.Response, Policy{
PolicyName: string(gwapiv1.HTTPRouteFilterResponseHeaderModifier),
Action: constants.ActionHeaderRemove,
Parameters: policyParameters,
})
}
for _, header := range filter.ResponseHeaderModifier.Set {
policyParameters := make(map[string]interface{})
policyParameters[constants.HeaderName] = string(header.Name)
policyParameters[constants.HeaderValue] = string(header.Value)

policies.Response = append(policies.Response, Policy{
PolicyName: string(gwapiv1.HTTPRouteFilterResponseHeaderModifier),
Action: constants.ActionHeaderAdd,
Parameters: policyParameters,
})
}
}
}
resourceAPIPolicy = concatAPIPolicies(resourceAPIPolicy, nil)
resourceAuthScheme = concatAuthSchemes(resourceAuthScheme, nil)
Expand Down
Loading

0 comments on commit 99509f7

Please sign in to comment.