Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support HTTPQueryParamMatch as CEL routeRuleConditions #981

Merged
merged 4 commits into from
Nov 7, 2024
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
15 changes: 12 additions & 3 deletions pkg/wasm/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,14 @@ func PredicatesFromHTTPRouteMatch(match gatewayapiv1.HTTPRouteMatch) []string {
predicates = append(predicates, predicateFromHeader(headerMatch))
}

// TODO(eguzki): query params. Investigate integration with wasm regarding Envoy params
// from https://www.envoyproxy.io/docs/envoy/latest/intro/arch_overview/advanced/attributes
// request.query -> string : The query portion of the URL in the format of “name1=value1&name2=value2”.
// query param, only consider the first in case of repetition, as per spec
queryParams := make(map[gatewayapiv1.HTTPHeaderName]bool)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dunno if that's idiomatic Go...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its fine, I think you would find most use queryParams := map[gatewaypiv1.HTTPHeaderName]bool{} but I don't think it is that important

for _, queryParamMatch := range match.QueryParams {
if !queryParams[queryParamMatch.Name] {
queryParams[queryParamMatch.Name] = true
predicates = append(predicates, predicateFromQueryParam(queryParamMatch))
}
}

return predicates
}
Expand Down Expand Up @@ -181,3 +186,7 @@ func predicateFromHeader(headerMatch gatewayapiv1.HTTPHeaderMatch) string {
// https://gateway-api.sigs.k8s.io/reference/spec/#gateway.networking.k8s.io/v1.HTTPHeaderMatch
return fmt.Sprintf("request.headers['%s'] == '%s'", headerMatch.Name, headerMatch.Value)
}

func predicateFromQueryParam(queryParam gatewayapiv1.HTTPQueryParamMatch) string {
return fmt.Sprintf("queryMap(request.query)['%s'] == '%s'", queryParam.Name, queryParam.Value)
}
51 changes: 51 additions & 0 deletions pkg/wasm/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import (
"errors"
"testing"

"gotest.tools/assert"

"github.com/google/go-cmp/cmp"
"google.golang.org/protobuf/types/known/structpb"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"
"sigs.k8s.io/yaml"
)

Expand Down Expand Up @@ -248,3 +251,51 @@ func TestConfigFromStruct(t *testing.T) {
})
}
}

func TestPredicatesFromHTTPRouteMatch(t *testing.T) {
queryParams := make([]gatewayapiv1.HTTPQueryParamMatch, 0)
queryParamMatch := gatewayapiv1.QueryParamMatchExact
queryParams = append(queryParams, gatewayapiv1.HTTPQueryParamMatch{
Type: &queryParamMatch,
Name: "foo",
Value: "bar",
})
queryParams = append(queryParams, gatewayapiv1.HTTPQueryParamMatch{
Type: &queryParamMatch,
Name: "foo",
Value: "baz",
}) // this param will be ignored, as `foo` was defined above to match `bar`
queryParams = append(queryParams, gatewayapiv1.HTTPQueryParamMatch{
Type: &queryParamMatch,
Name: "kua",
Value: "drant",
})

headerMatch := gatewayapiv1.HeaderMatchExact
header := gatewayapiv1.HTTPHeaderMatch{
Type: &headerMatch,
Name: "x-auth",
Value: "kuadrant",
}

method := gatewayapiv1.HTTPMethodTrace

pathMatch := gatewayapiv1.PathMatchPathPrefix
path := "/admin"
predicates := PredicatesFromHTTPRouteMatch(gatewayapiv1.HTTPRouteMatch{
Path: &gatewayapiv1.HTTPPathMatch{
Type: &pathMatch,
Value: &path,
},
Headers: []gatewayapiv1.HTTPHeaderMatch{header},
QueryParams: queryParams,
Method: &method,
})

assert.Equal(t, predicates[0], "request.method == 'TRACE'")
assert.Equal(t, predicates[1], "request.url_path.startsWith('/admin')")
assert.Equal(t, predicates[2], "request.headers['x-auth'] == 'kuadrant'")
assert.Equal(t, predicates[3], "queryMap(request.query)['foo'] == 'bar'")
assert.Equal(t, predicates[4], "queryMap(request.query)['kua'] == 'drant'")
assert.Equal(t, len(predicates), 5)
}
Loading