-
Notifications
You must be signed in to change notification settings - Fork 4
feat: add Body scope for request body matching in ApisixRoute #413
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
Closed
Closed
Changes from 5 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3f1f3f0
feat: add Body scope to ApisixRoute match expressions for request bod…
AlinsRan a653ef9
feat: add CEL validation and e2e tests for Body scope matching
AlinsRan 24f7e0c
chore: remove CEL XValidation, keep Enum marker for Scope field
AlinsRan 930df17
fix: address PR review comments
AlinsRan 64ffc78
feat: add CEL XValidation for ApisixRouteHTTPMatchExprSubject
AlinsRan 800c57c
fix: use size(self.name) > 0 in CEL rule to avoid YAML quote issues
AlinsRan a32cacf
Apply suggestions from code review
AlinsRan 92325fd
Potential fix for pull request finding
AlinsRan 271976c
feat: support Body scope in ApisixRoute HTTP match expressions
AlinsRan dd377dd
chore: merge master and fix consumer test after algorithm CEL rule ad…
AlinsRan ce311b2
refactor: rename validateObject to Validate, restore missing consumer…
AlinsRan a171b69
feat(api): add HealthCheck types to BackendTrafficPolicySpec
AlinsRan f839117
chore: regenerate deepcopy for BackendTrafficPolicy health check types
AlinsRan 0c19a9d
feat: translate BackendTrafficPolicy health checks to APISIX upstream
AlinsRan a03d90d
chore: regenerate CRD manifests with BackendTrafficPolicy health chec…
AlinsRan 9eecf12
fix: use trim() in CEL rule to reject whitespace-only name for non-Pa…
AlinsRan 3cbb4ca
fix: replace trim() CEL rule with Pattern annotation to avoid cost bu…
AlinsRan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| package v2 | ||
|
|
||
| import ( | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/google/cel-go/cel" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "sigs.k8s.io/yaml" | ||
| ) | ||
|
AlinsRan marked this conversation as resolved.
Outdated
|
||
|
|
||
| func strPtr(s string) *string { return &s } | ||
|
|
||
| // celSubjectRule is the CEL expression used in the +kubebuilder:validation:XValidation | ||
| // marker on ApisixRouteHTTPMatchExprSubject. | ||
| const celSubjectRule = `self.scope == 'Path' || self.name != ''` | ||
|
|
||
| func evalCELSubjectRule(t *testing.T, scope, name string) bool { | ||
| t.Helper() | ||
| env, err := cel.NewEnv( | ||
| cel.Variable("self", cel.MapType(cel.StringType, cel.StringType)), | ||
| ) | ||
| require.NoError(t, err) | ||
| ast, issues := env.Compile(celSubjectRule) | ||
| require.NoError(t, issues.Err()) | ||
| prg, err := env.Program(ast) | ||
| require.NoError(t, err) | ||
| out, _, err := prg.Eval(map[string]any{ | ||
| "self": map[string]any{"scope": scope, "name": name}, | ||
| }) | ||
| require.NoError(t, err) | ||
| return out.Value().(bool) | ||
| } | ||
|
|
||
| // TestCEL_SubjectRule_Logic verifies the CEL expression used in the XValidation marker. | ||
| func TestCEL_SubjectRule_Logic(t *testing.T) { | ||
| // Non-Path scopes with a non-empty name must pass. | ||
| for _, scope := range []string{ScopeHeader, ScopeQuery, ScopeCookie, ScopeVariable, ScopeBody} { | ||
| assert.True(t, evalCELSubjectRule(t, scope, "field"), "scope=%s with name should pass", scope) | ||
| } | ||
| // Path scope with empty name must pass (name is ignored for Path). | ||
| assert.True(t, evalCELSubjectRule(t, ScopePath, ""), "Path with empty name should pass") | ||
| // Non-Path scopes with empty name must fail. | ||
| for _, scope := range []string{ScopeHeader, ScopeQuery, ScopeCookie, ScopeVariable, ScopeBody} { | ||
| assert.False(t, evalCELSubjectRule(t, scope, ""), "scope=%s with empty name should fail", scope) | ||
| } | ||
| } | ||
|
|
||
| // TestCEL_SubjectRule_InCRD verifies the generated CRD YAML contains the XValidation rule | ||
| // with correct (ASCII) quote characters and not typographic quotes. | ||
| func TestCEL_SubjectRule_InCRD(t *testing.T) { | ||
| const crdPath = "../../config/crd/bases/apisix.apache.org_apisixroutes.yaml" | ||
| data, err := os.ReadFile(crdPath) | ||
| require.NoError(t, err, "CRD file should exist; run 'make manifests' if missing") | ||
|
|
||
| var crd map[string]any | ||
| require.NoError(t, yaml.Unmarshal(data, &crd)) | ||
|
|
||
| raw := string(data) | ||
| // The CEL rule must appear with ASCII single-quotes only. | ||
| assert.Contains(t, raw, `self.scope == 'Path' || self.name != ''`, | ||
| "CRD should contain the XValidation rule with ASCII quotes") | ||
| // Ensure no typographic/smart quotes crept in. | ||
| assert.NotContains(t, raw, "\u2018", "CRD must not contain left single quotation mark \u2018") | ||
| assert.NotContains(t, raw, "\u2019", "CRD must not contain right single quotation mark \u2019") | ||
| assert.NotContains(t, raw, "\u201c", "CRD must not contain left double quotation mark \u201c") | ||
| assert.NotContains(t, raw, "\u201d", "CRD must not contain right double quotation mark \u201d") | ||
| } | ||
|
|
||
| func TestToVars_ScopeBody_SimpleField(t *testing.T) { | ||
| exprs := ApisixRouteHTTPMatchExprs{ | ||
| { | ||
| Subject: ApisixRouteHTTPMatchExprSubject{ | ||
| Scope: ScopeBody, | ||
| Name: "action", | ||
| }, | ||
| Op: OpEqual, | ||
| Value: strPtr("login"), | ||
| }, | ||
| } | ||
|
|
||
| vars, err := exprs.ToVars() | ||
| require.NoError(t, err) | ||
| require.Len(t, vars, 1) | ||
|
|
||
| // vars[0] is []StringOrSlice: [subject, op, value] | ||
| // Should map to post_arg.action | ||
| assert.Equal(t, "post_arg.action", vars[0][0].StrVal) | ||
| assert.Equal(t, "==", vars[0][1].StrVal) | ||
| assert.Equal(t, "login", vars[0][2].StrVal) | ||
| } | ||
|
|
||
| func TestToVars_ScopeBody_NestedJSONPath(t *testing.T) { | ||
| exprs := ApisixRouteHTTPMatchExprs{ | ||
| { | ||
| Subject: ApisixRouteHTTPMatchExprSubject{ | ||
| Scope: ScopeBody, | ||
| Name: "model.version", | ||
| }, | ||
| Op: OpEqual, | ||
| Value: strPtr("gpt-4"), | ||
| }, | ||
| } | ||
|
|
||
| vars, err := exprs.ToVars() | ||
| require.NoError(t, err) | ||
| require.Len(t, vars, 1) | ||
|
|
||
| // Should map to post_arg.model.version (dot-notation passthrough) | ||
| assert.Equal(t, "post_arg.model.version", vars[0][0].StrVal) | ||
| } | ||
|
|
||
| func TestToVars_ScopeBody_EmptyName_ReturnsError(t *testing.T) { | ||
| exprs := ApisixRouteHTTPMatchExprs{ | ||
| { | ||
| Subject: ApisixRouteHTTPMatchExprSubject{ | ||
| Scope: ScopeBody, | ||
| Name: "", | ||
| }, | ||
| Op: OpEqual, | ||
| Value: strPtr("login"), | ||
| }, | ||
| } | ||
|
|
||
| _, err := exprs.ToVars() | ||
| assert.Error(t, err) | ||
| assert.Contains(t, err.Error(), "empty subject.name") | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.