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

xds: Fix invert functionality for header matcher #4902

Merged
merged 2 commits into from Oct 28, 2021

Conversation

zasweq
Copy link
Contributor

@zasweq zasweq commented Oct 27, 2021

Fixes #4896. This PR makes the header matchers present in grpc-go consistent with Envoy, as Envoy has a gatekeeper for each matcher that isn't a present matcher on whether the header is actually there, and if so returns false, regardless of the configured matchers comparator or the invert knob.

RELEASE NOTES: None

@zasweq zasweq requested a review from menghanl October 27, 2021 03:02
@zasweq zasweq added this to the 1.42 Release milestone Oct 27, 2021
}

func (hcm *HeaderContainsMatcher) String() string {
return fmt.Sprintf("headerContains:%v%v", hcm.key, hcm.contains)
}

/*
Copy link
Contributor

Choose a reason for hiding this comment

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

Delete this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -66,7 +67,7 @@ func (hem *HeaderExactMatcher) Match(md metadata.MD) bool {
if !ok {
return false
}
return v == hem.exact
return (v == hem.exact) != hem.invert
Copy link
Contributor

Choose a reason for hiding this comment

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

Haha nice!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copied from Envoy haha I didn't come up with this!!!!

func NewHeaderPresentMatcher(key string, present bool) *HeaderPresentMatcher {
return &HeaderPresentMatcher{key: key, present: present}
func NewHeaderPresentMatcher(key string, present bool, invert bool) *HeaderPresentMatcher {
return &HeaderPresentMatcher{key: key, present: present, invert: invert}
Copy link
Contributor

Choose a reason for hiding this comment

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

Invert present? Then you don't need the invert bool field.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

{
name: "no match exact",
m: NewHeaderExactMatcher(":method", "GET", true),
md: metadata.Pairs("th", "GET"),
Copy link
Contributor

Choose a reason for hiding this comment

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

This only covers presence check, not the value check.
To be a bit paranoid, I think we need to test

  • invert=true, key doesn't exist (returns false, except for PresentMatcher)
  • invert=true, key exists, value matches (returns false)
  • invert=true, key exists, value doesn't match (returns true)

And let's cover this for all the matchers.

You can add them to the each matcher test (add to TestHeaderExactMatcherMatch, instead of a new TestInvertWhenNotPresent

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added these three tests for all the matchers. Deleted the invert tests, since those are not part of each matchers test.

@menghanl menghanl assigned zasweq and unassigned menghanl Oct 27, 2021
Copy link
Contributor Author

@zasweq zasweq left a comment

Choose a reason for hiding this comment

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

Thanks for the comments :D!

}

func (hcm *HeaderContainsMatcher) String() string {
return fmt.Sprintf("headerContains:%v%v", hcm.key, hcm.contains)
}

/*
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -66,7 +67,7 @@ func (hem *HeaderExactMatcher) Match(md metadata.MD) bool {
if !ok {
return false
}
return v == hem.exact
return (v == hem.exact) != hem.invert
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copied from Envoy haha I didn't come up with this!!!!

func NewHeaderPresentMatcher(key string, present bool) *HeaderPresentMatcher {
return &HeaderPresentMatcher{key: key, present: present}
func NewHeaderPresentMatcher(key string, present bool, invert bool) *HeaderPresentMatcher {
return &HeaderPresentMatcher{key: key, present: present, invert: invert}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

{
name: "no match exact",
m: NewHeaderExactMatcher(":method", "GET", true),
md: metadata.Pairs("th", "GET"),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added these three tests for all the matchers. Deleted the invert tests, since those are not part of each matchers test.

@zasweq zasweq assigned menghanl and unassigned zasweq Oct 27, 2021
Copy link
Contributor

@menghanl menghanl left a comment

Choose a reason for hiding this comment

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

LGTM

@menghanl menghanl assigned zasweq and unassigned menghanl Oct 27, 2021
@dfawley dfawley changed the title xds: Fixed invert functionality for header matcher xds: Fix invert functionality for header matcher Oct 27, 2021
@zasweq zasweq merged commit d47437c into grpc:master Oct 28, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

HeaderMatcher.invert_match should invert the operation, not the result
2 participants