Skip to content

Commit

Permalink
Header base route support
Browse files Browse the repository at this point in the history
  • Loading branch information
Krishanx92 committed Aug 14, 2024
1 parent edcb9a6 commit 450ef5a
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 6 deletions.
18 changes: 18 additions & 0 deletions adapter/internal/oasparser/envoyconf/routes_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ func generateHTTPMethodMatcher(methodRegex string, sandClusterName string) []*ro
return headerMatcherArray
}

func generateHTTPMethodExactMatcher(headername, value string) *routev3.HeaderMatcher {
headerMatcher := generateHeaderExactMatch(headername, value)
return headerMatcher
}

func generateQueryParamMatcher(queryParamName, value string) []*routev3.QueryParameterMatcher {
queryParamMatcher := &routev3.QueryParameterMatcher{
Name: queryParamName,
Expand Down Expand Up @@ -269,6 +274,19 @@ func generateHeaderMatcher(headerName, valueRegex string) *routev3.HeaderMatcher
return headerMatcherArray
}

func generateHeaderExactMatch(headerName, value string) *routev3.HeaderMatcher {
if len(headerName) == 0 {
return nil
}
headerMatcherArray := &routev3.HeaderMatcher{
Name: headerName,
HeaderMatchSpecifier: &routev3.HeaderMatcher_ExactMatch{
ExactMatch: value,
},
}
return headerMatcherArray
}

func generateRegexMatchAndSubstitute(routePath, endpointResourcePath string,
pathMatchType gwapiv1.PathMatchType) *envoy_type_matcherv3.RegexMatchAndSubstitute {
substitutionString := generateSubstitutionString(endpointResourcePath, pathMatchType)
Expand Down
13 changes: 13 additions & 0 deletions adapter/internal/oasparser/envoyconf/routes_with_clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,9 @@ func createRoutes(params *routeCreateParams) (routes []*routev3.Route, err error
resourceMethods := resource.GetMethodList()
pathMatchType := resource.GetPathMatchType()

headerKey := resource.GetKey()
headerValue := resource.GetValue()

contextExtensions := make(map[string]string)
contextExtensions[pathContextExtension] = resourcePath
contextExtensions[vHostContextExtension] = vHost
Expand Down Expand Up @@ -1074,7 +1077,14 @@ func createRoutes(params *routeCreateParams) (routes []*routev3.Route, err error
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
match := generateRouteMatch(routePath)
logger.LoggerAPK.Info("Header key: ", headerKey)
logger.LoggerAPK.Info("Header value: ", headerValue)
match.Headers = generateHTTPMethodMatcher(operation.GetMethod(), clusterName)
if headerKey != "" && headerValue != "" {
match.Headers = append(match.Headers, generateHTTPMethodExactMatcher(string(headerKey), headerValue))
}
//match.Headers = append(match.Headers, generateHeaderMatcher(string(headerKey), headerValue))
logger.LoggerAPK.Info("Header keyx: ")
match.DynamicMetadata = generateMetadataMatcherForExternalRoutes()
if pathRewriteConfig != nil && requestRedirectAction == nil {
action.Route.RegexRewrite = pathRewriteConfig
Expand All @@ -1095,6 +1105,9 @@ func createRoutes(params *routeCreateParams) (routes []*routev3.Route, err error
}
match := generateRouteMatch(routePath)
match.Headers = generateHTTPMethodMatcher(methodRegex, clusterName)
if headerKey != "" && headerValue != "" {
match.Headers = append(match.Headers, generateHTTPMethodExactMatcher(string(headerKey), headerValue))
}
action := generateRouteAction(apiType, routeConfig, rateLimitPolicyCriteria, nil)
rewritePath := generateRoutePathForReWrite(basePath, resourcePath, pathMatchType)
action.Route.RegexRewrite = generateRegexMatchAndSubstitute(rewritePath, resourcePath, pathMatchType)
Expand Down
35 changes: 29 additions & 6 deletions adapter/internal/oasparser/model/adapter_internal_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,12 +764,32 @@ func (adapterInternalAPI *AdapterInternalAPI) SetInfoHTTPRouteCR(httpRoute *gwap
}
operations := getAllowedOperations(match.Method, policies, apiAuth,
parseRateLimitPolicyToInternal(resourceRatelimitPolicy), scopes, mirrorEndpointCluster)
resource := &Resource{path: resourcePath,
methods: operations,
pathMatchType: *match.Path.Type,
hasPolicies: true,
iD: uuid.New().String(),
hasRequestRedirectFilter: hasRequestRedirectPolicy,
var resource *Resource
if match.Headers != nil && len(match.Headers) > 0 {
for _, header := range match.Headers {
loggers.LoggerAPI.Info("Inside Header Match")
loggers.LoggerAPI.Info("Header Name: ", header.Name)
loggers.LoggerAPI.Info("Header Value: ", header.Value)
resource = &Resource{path: resourcePath,
methods: operations,
pathMatchType: *match.Path.Type,
headerMatchType: *header.Type,
key: header.Name,
value: header.Value,
hasPolicies: true,
iD: uuid.New().String(),
hasRequestRedirectFilter: hasRequestRedirectPolicy,
}
}
} else {
loggers.LoggerAPI.Info("Inside Not Header Match")
resource = &Resource{path: resourcePath,
methods: operations,
pathMatchType: *match.Path.Type,
hasPolicies: true,
iD: uuid.New().String(),
hasRequestRedirectFilter: hasRequestRedirectPolicy,
}
}

resource.endpoints = &EndpointCluster{
Expand Down Expand Up @@ -810,7 +830,10 @@ func (adapterInternalAPI *AdapterInternalAPI) SetInfoHTTPRouteCR(httpRoute *gwap
resource.endpoints.Config = endpointConfig
}
resource.endpointSecurity = utils.GetPtrSlice(securityConfig)
loggers.LoggerAPI.Info("Resource Key: ", resource.GetKey())
loggers.LoggerAPI.Info("Resource Value: ", resource.GetValue())
resources = append(resources, resource)

}
}

Expand Down
18 changes: 18 additions & 0 deletions adapter/internal/oasparser/model/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ type Resource struct {
path string
pathMatchType gwapiv1.PathMatchType
methods []*Operation
headerMatchType gwapiv1.HeaderMatchType
key gwapiv1.HTTPHeaderName
value string
iD string
endpoints *EndpointCluster
endpointSecurity []*EndpointSecurity
Expand All @@ -47,6 +50,21 @@ type Resource struct {
hasRequestRedirectFilter bool
}

// GetHeaderMatchType returns the header match type of the resource.
func (resource *Resource) GetHeaderMatchType() gwapiv1.HeaderMatchType {
return resource.headerMatchType
}

// GetKey returns the key of the resource.
func (resource *Resource) GetKey() gwapiv1.HTTPHeaderName {
return resource.key
}

// GetValue returns the value of the resource.
func (resource *Resource) GetValue() string {
return resource.value
}

// GetEndpointSecurity returns the endpoint security object of a given resource.
func (resource *Resource) GetEndpointSecurity() []*EndpointSecurity {
return resource.endpointSecurity
Expand Down

0 comments on commit 450ef5a

Please sign in to comment.