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

decouples middleware modifications #1391

Merged
merged 7 commits into from
Jun 20, 2023
Merged

Conversation

vroldanbet
Copy link
Contributor

@vroldanbet vroldanbet commented Jun 9, 2023

until now middleware modifications composed both unary and streaming middlewares. This worked under the assumption that we wanted the same order in each type of API method.

SpiceDB relies on grpc-ecosystem/go-grpc-middleware to build chains of interceptors, which is not natively
supported by the go gRPC SDK. The order of execution is, assuming a slice of interceptors, from left to right.

Because of the design of gRPC Streaming APIs, composed of stream creation and actual streaming of messages, while the chain of interceptors will follow the expected middleware chain order, the actual interception of messages (RecvMsg and SendMsg) will happen in the opposite order.

Having middlewares depend on each other is a not good practice, but there are circumstances
where at the very least you need a middleware to be the first or the last in the chain, think
middlewares for auditing purposes should happen earlier, otherwise, it wouldn't catch requests that
fail earlier in the chain.

This can be addressed by reversing the order of the middleware to obtain the expected order, but this hasn't been possible because the MiddlewareModification API had both unary and streaming coupled.

This commit separates modifications to unary and streaming middlewares, generalized using Go generics.

ℹ️ The generated options rely on a modification to ecordell/optgen in order to support generics. See ecordell/optgen#9

@github-actions github-actions bot added area/CLI Affects the command line area/tooling Affects the dev or user toolchain (e.g. tests, ci, build tools) labels Jun 9, 2023
@vroldanbet vroldanbet force-pushed the decouple-middleware-modifications branch from c8a1c7a to 9c06e87 Compare June 9, 2023 12:05
@vroldanbet vroldanbet self-assigned this Jun 9, 2023
@vroldanbet vroldanbet force-pushed the decouple-middleware-modifications branch from 50b46d5 to efc36e3 Compare June 12, 2023 08:33
@vroldanbet vroldanbet marked this pull request as ready for review June 12, 2023 08:33
@vroldanbet vroldanbet requested a review from a team as a code owner June 12, 2023 08:33
@github-actions github-actions bot added the area/dependencies Affects dependencies label Jun 12, 2023
@vroldanbet vroldanbet force-pushed the decouple-middleware-modifications branch 2 times, most recently from cd93f12 to 783cbf5 Compare June 12, 2023 09:27
Copy link
Member

@josephschorr josephschorr left a comment

Choose a reason for hiding this comment

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

Do we have a test for the ordering?

pkg/cmd/server/middleware.go Outdated Show resolved Hide resolved
until now middleware modifications composed both unary and
streaming middlewares. This worked under the assumption
that we wanted the same order in each type of API method.

SpiceDB relies on `grpc-ecosystem/go-grpc-middleware` to build
[chains of interceptors](https://github.com/grpc-ecosystem/go-grpc-middleware/blob/bfde8086e68d6a129363b063ffd6518ab568467d/doc.go),
which is not natively
supported by the go gRPC SDK.
The order of execution is, assuming a slice of interceptors, from left to right.

Because of the design of gRPC Streaming APIs, composed of stream creation and actual
streaming of messages, while the chain of interceptors will follow the expected
middleware chain order, the actual interception of messages (`RecvMsg` and `SendMsg`)
will happen in the opposite order.

Having middlewares depend on each other is a not good practice, but there
are circumstances where at the very least you need a middleware to be the first
or the last in the chain, think middlewares for auditing purposes should happen
earlier, otherwise, it wouldn't catch requests that fail earlier in the chain.

This can be addressed by reversing the order of the middleware to obtain the
expected order, but this hasn't been possible because the `MiddlewareModification`
API had both unary and streaming coupled.

This commit separates modifications to unary and streaming middlewares, generalized using Go generics.

The generated options also rely on a modification to `ecordell/optgen` in order
to support generics. See ecordell/optgen#9
@vroldanbet vroldanbet force-pushed the decouple-middleware-modifications branch from 783cbf5 to 3983967 Compare June 12, 2023 17:28
@vroldanbet
Copy link
Contributor Author

Do we have a test for the ordering?

I did a whole thing asserting order in #1395

Copy link
Member

Choose a reason for hiding this comment

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

Should this file be in the PR?

Copy link
Contributor Author

@vroldanbet vroldanbet Jun 19, 2023

Choose a reason for hiding this comment

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

The file predated this PR, it just got updated, which makes me think it should have been updated in a previous PR but wasn't. We have some actions in CI to fail if the repository is dirty, but somehow ignored this file.

The file was introduced in 43678a7#diff-583a688fd5bc869dc63c64d19cb1dab06bfbde70115e13ad54f7e280ca57794eR1-R2. I'm not sure there is a reason to check it in git. If not, I can open a follow-up PR to address it.

@ecordell mind clarifying whether having it in git was intentional or not?☝🏻

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes it was intentional. Normally we wouldn't commit go.work like this, but it's just to make the tools find all of the right dependencies.

@vroldanbet vroldanbet added this pull request to the merge queue Jun 20, 2023
Copy link
Member

@josephschorr josephschorr left a comment

Choose a reason for hiding this comment

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

LGTM

Merged via the queue into main with commit ac5f23a Jun 20, 2023
@vroldanbet vroldanbet deleted the decouple-middleware-modifications branch June 20, 2023 17:35
@github-actions github-actions bot locked and limited conversation to collaborators Jun 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area/CLI Affects the command line area/dependencies Affects dependencies area/tooling Affects the dev or user toolchain (e.g. tests, ci, build tools)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants