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

default dynamic wasm rule data to empty string #232

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions controllers/ratelimitpolicy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ var _ = Describe("RateLimitPolicy controller", func() {
{
Selector: &wasm.SelectorSpec{
Selector: kuadrantv1beta2.ContextSelector("auth.identity.username"),
Default: common.Ptr(""),
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/rlptools/wasm_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func dataFromLimt(limitIdentifier string, limit *kuadrantv1beta2.Limit) (data []
data = append(data, wasm.DataItem{Static: &wasm.StaticSpec{Key: limitIdentifier, Value: "1"}})

for _, counter := range limit.Counters {
data = append(data, wasm.DataItem{Selector: &wasm.SelectorSpec{Selector: counter}})
data = append(data, wasm.DataItem{Selector: &wasm.SelectorSpec{Selector: counter, Default: common.Ptr("")}})
Copy link
Contributor

@eguzki eguzki Aug 22, 2023

Choose a reason for hiding this comment

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

What if I set as counter request.headers.My-Custom-Header-01 ? All the requests not having that header will contribute to a single (limitador's internal hit) counter

Copy link
Contributor Author

@guicassolato guicassolato Aug 22, 2023

Choose a reason for hiding this comment

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

Requests that do not have that header are treated as requests that have that header with an empty value, therefore sharing the same counter.

If what you want is to trigger or not trigger a limit based on the presence or absence of a header, use a condition instead; not a counter qualifier.

Copy link
Contributor

@eguzki eguzki Aug 23, 2023

Choose a reason for hiding this comment

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

Indeed, what you say is correct, but I do not fully agree on this UX: if you set a counter based on a header, add a condition on the header existence.

For me, adding a counter implicitly tells I only want to rate limit traffic that is qualified by the counter.

Let's see what @didierofrivia and @alexsnaps have to say about this

Let's say we have this RLP

apiVersion: kuadrant.io/v2beta1
kind: RateLimitPolicy
metadata: {}
spec:
  targetRef:
    group: gateway.networking.k8s.io
    kind: HTTPRoute
    name: <HTTPROUTE-NAME>
  limits:
    some-name:
      routeSelectors:           
        matches:                
        - path:                    
            type: PathPrefix
            value: "/toys"
      rates:
      - limit: 50
        duration: 1
        unit: minute
      counters:
      - auth.identity.username

What would you expect from this RLP? rate limiting traffic authenticated (hence, the auth.identify.username exists in the metadata) is expected. So far so good. What about traffic not authenticated (hence, the auth.identify.username does not exist in the metadata)?? With this PR approved, the traffic which is not authenticated will be rate limited because the username es "". Without the changes of this PR, if the request is not authenticated, rate limiting will not be triggered.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@eguzki, coincidently, this was exactly the example that made us1 want to default to an empty string in the wasm-shim (what Kuadrant/wasm-shim#39 wants to revert now).

Footnotes

  1. Discussed offline with @alexsnaps and @didierofrivia on 2023-07-27, and implemented by @alexsnaps on 2023-07-28.

Copy link
Contributor

Choose a reason for hiding this comment

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

Kuadrant/wasm-shim#39 does not revert entirely that commit. Only reverts the behavior when the descriptor is not found in the module generating RLS descriptors. For the conditions, Kuadrant/wasm-shim#39 keeps the changes to allow operators to be meaningful.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Kuadrant/wasm-shim#39 does not revert entirely that commit.

I understand that. But, most importantly, I also understand that setting the default value to "", so some RL is enforced, is one approach. In itself, it is nor better, nor worse than setting no default at all. If most people prefers the contrary of what's proposed here, I'm good with that too obviously.

}

return data
Expand Down
2 changes: 2 additions & 0 deletions pkg/rlptools/wasm_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
gatewayapiv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"

kuadrantv1beta2 "github.com/kuadrant/kuadrant-operator/api/v1beta2"
"github.com/kuadrant/kuadrant-operator/pkg/common"
"github.com/kuadrant/kuadrant-operator/pkg/rlptools/wasm"
)

Expand Down Expand Up @@ -318,6 +319,7 @@ func TestWasmRules(t *testing.T) {
{
Selector: &wasm.SelectorSpec{
Selector: "auth.identity.username",
Default: common.Ptr(""),
},
},
},
Expand Down