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

✨ new feat: selector middleware #511

Merged
merged 1 commit into from Sep 7, 2022

Conversation

aimuz
Copy link
Contributor

@aimuz aimuz commented Sep 2, 2022

It allows to set check rules to whitelist or blacklist middleware such as Auth
Requests that match the rules will go to the next level of middleware

Copy link
Collaborator

@johanbrandhorst johanbrandhorst left a comment

Choose a reason for hiding this comment

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

This looks cool, but I think it's missing some things:

  1. Where are the tests? We should include some tests of this new functionality.
  2. Could you include an selector_example_test.go file with an example of how to use this? It will automatically be shown as part of the documentation. See https://go.dev/blog/examples.

@aimuz
Copy link
Contributor Author

aimuz commented Sep 3, 2022

Yes, that's how it should be. I'll take care of it.

@codecov-commenter
Copy link

codecov-commenter commented Sep 3, 2022

Codecov Report

Base: 84.01% // Head: 58.74% // Decreases project coverage by -25.26% ⚠️

Coverage data is based on head (eda6e54) compared to base (6e2c2ac).
Patch coverage: 50.91% of modified lines in pull request are covered.

❗ Current head eda6e54 differs from pull request most recent head f7fa9eb. Consider uploading reports for the commit f7fa9eb to get more accurate results

Additional details and impacted files
@@             Coverage Diff             @@
##               v2     #511       +/-   ##
===========================================
- Coverage   84.01%   58.74%   -25.27%     
===========================================
  Files          30       31        +1     
  Lines         932     1583      +651     
===========================================
+ Hits          783      930      +147     
- Misses        110      590      +480     
- Partials       39       63       +24     
Impacted Files Coverage Δ
chain.go 0.00% <ø> (-90.91%) ⬇️
interceptors/auth/auth.go 100.00% <ø> (ø)
interceptors/ratelimit/ratelimit.go 60.00% <0.00%> (-40.00%) ⬇️
interceptors/recovery/options.go 78.57% <ø> (ø)
metadata/single_key.go 60.00% <ø> (ø)
testing/testpb/interceptor_suite.go 0.00% <0.00%> (ø)
testing/testpb/test.manual_validator.pb.go 0.00% <0.00%> (ø)
util/backoffutils/backoff.go 60.00% <ø> (ø)
wrappers.go 66.66% <ø> (-33.34%) ⬇️
interceptors/reporter.go 45.45% <20.00%> (-17.05%) ⬇️
... and 28 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Collaborator

@johanbrandhorst johanbrandhorst 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 tests, just a few suggestions

`selector` a generic server-side selector middleware for gRPC.

# Server Side Selector Middleware
It allows to set check rules to whitelist or blacklist middleware such as Auth
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for the updates, do you mind changing these phrases to allowlist and blocklist consistently through this PR?

Suggested change
It allows to set check rules to whitelist or blacklist middleware such as Auth
It allows to set check rules to allowlist or blocklist middleware such as Auth


type Option func(o *options)

// WithMatch ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this comment needs improving

Suggested change
// WithMatch ...
// WithMatch configures what condition should trigger the selector to run the interceptor.
// If used multiple times, the last one specified will be used.

return optCopy
}

// AlwaysMatch is the default implementation, always selected
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// AlwaysMatch is the default implementation, always selected
// AlwaysMatch can be used to always run the interceptor. This is the default behavior.

return true
}

// UnaryServerInterceptor returns a new unary server interceptors that performs per-request selector.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// UnaryServerInterceptor returns a new unary server interceptors that performs per-request selector.
// UnaryServerInterceptor returns a new unary server interceptor that will decide whether to call the interceptor on the behavior of the MatchFunc.

}

// UnaryServerInterceptor returns a new unary server interceptors that performs per-request selector.
func UnaryServerInterceptor(interceptors grpc.UnaryServerInterceptor, opts ...Option) grpc.UnaryServerInterceptor {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm leaning towards removing MatchFunc as an option and making it a required parameter. This solves two problems:

  1. If you accidentally use the selector without a match func, you gain no utility from it.
  2. If you accidentally specify two match func options, you might be confused about which to use.

That simplifies things generally, no need for the AlwaysMatch function, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I readjusted it, can you help me in seeing what is wrong?


# Server Side Selector Middleware
It allows to set check rules to whitelist or blacklist middleware such as Auth
Requests that match the rules will go to the next level of middleware
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
Requests that match the rules will go to the next level of middleware
interceptors to toggle behavior on or off based on the request path.

}

func loginSkip(ctx context.Context, fullMethod string) bool {
return fullMethod != "login"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Lets use a realistic looking path, like you are doing in the tests

Suggested change
return fullMethod != "login"
return fullMethod != "/auth.v1.AuthService/Login"

Comment on lines 11 to 29
// Allow After the method is matched, the interceptor is run
func Allow(methods []string) MatchFunc {
return func(ctx context.Context, fullMethod string) bool {
for _, s := range methods {
if s == fullMethod {
return true
}
}
return false
}
}

// Block the interceptor will not run after the method matches
func Block(methods []string) MatchFunc {
allow := Allow(methods)
return func(ctx context.Context, fullMethod string) bool {
return !allow(ctx, fullMethod)
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think we need these, to be honest. Users can easily implement these themselves.

"testing"
)

var whiteList = []string{"/auth.v1beta1.AuthService/Login"}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
var whiteList = []string{"/auth.v1beta1.AuthService/Login"}
var allowList = []string{"/auth.v1beta1.AuthService/Login"}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have adjusted

@@ -1,18 +1,16 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.25.0
// protoc-gen-go v1.28.0
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you revert these changes, I don't think these should need to be changed?

@johanbrandhorst
Copy link
Collaborator

Looks like there is a formatting issue, can you run make lint?

@aimuz aimuz force-pushed the v2-selector branch 2 times, most recently from 9add81a to 948425e Compare September 7, 2022 05:07
Signed-off-by: aimuz <mr.imuz@gmail.com>
@aimuz
Copy link
Contributor Author

aimuz commented Sep 7, 2022

done

@johanbrandhorst johanbrandhorst merged commit 001ba37 into grpc-ecosystem:v2 Sep 7, 2022
@johanbrandhorst
Copy link
Collaborator

Thank you for your contribution!

@aimuz aimuz deleted the v2-selector branch September 7, 2022 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants