Repository navigation
Support connect-go v2 - #32
emcfarlane wants to merge 3 commits into
Conversation
Signed-off-by: Edward McFarlane <emcfarlane@buf.build>
Signed-off-by: Edward McFarlane <emcfarlane@buf.build>
jferrl
left a comment
There was a problem hiding this comment.
I read this against our migration. NewServerInterceptor matches the interceptor we wrote, and CallInfo has everything our auth funcs read (procedure and headers).
I haven't run it yet. The comments below come from reading this branch next to connect v2.0.2. The one on NewMiddleware is what keeps #31 open for us.
| } | ||
| authInfo, err := auth(ctx, callInfo) | ||
| if err != nil { | ||
| return err |
There was a problem hiding this comment.
An auth func that still calls authn.Errorf compiles here and answers unknown (HTTP 500) with no message. The v1 *connect.Error isn't a v2 one, so scrubHandlerError in connecthttp treats it as an uncoded error.
The call is still rejected, but a client waiting for a 401 won't refresh its token. Could the interceptor convert a v1 error to v2 before returning it? The module imports both now.
| // is attached to the context and may be retrieved with [GetInfo]. | ||
| // | ||
| // For connect-go v1, use [NewMiddleware]. | ||
| func NewServerInterceptor(auth InterceptorAuthFunc) connect.ServerInterceptor { |
There was a problem hiding this comment.
Two things change for someone moving from Wrap(mux). I think the doc should say both.
- Only registered procedures are authenticated. An unknown procedure is answered
unimplementedwithout running the chain, unlessSetUnknownHandleris set. connecthttp also answers 405, 415 and a bad timeout or compression before the interceptor runs. With the middleware an unauthenticated caller got 401 for all of these. Now they can tell which methods exist. - It only protects what comes after it in
NewServer. An interceptor placed earlier runs unauthenticated. If that one reads the message, like validation, decode-before-auth is back.
| complex, but [pkg.go.dev][godoc] includes a complete example. | ||
| Cookie and basic authentication is similar. Mutual TLS is a bit more | ||
| complex, but [pkg.go.dev][godoc] includes a complete example. For connect-go | ||
| v1, wrap the HTTP handler with `authn.NewMiddleware` instead. |
There was a problem hiding this comment.
A v2 server still needs NewMiddleware for what the interceptor doesn't cover: plain HTTP routes on the same mux, or a 401 before routing.
It still writes errors with the v1 ErrorWriter, so an AuthFunc that returns a v2 error answers unknown (500). That was the first point in #31. Could Wrap use connecthttp.NewErrorWriter when it gets a v2 error?
| if authorization != "" { | ||
| info.RequestHeader().Set("Authorization", authorization) | ||
| } | ||
| return server.Call(t.Context(), procedure, info, nil) |
There was a problem hiding this comment.
This calls server.Call in process, so it doesn't cover the HTTP status, or that auth runs before the body is read. A case over httptest for the 401, one streaming RPC and one unknown procedure would pin that down.
The examples were also the only end-to-end tests of the v1 middleware with a real Connect handler. They are v2 only now.
Signed-off-by: Edward McFarlane <emcfarlane@buf.build>
This adds support for connect-go v2. Updates the API to return a
connect.ServerInterceptor, with anInterceptorAuthFuncto replaceAuthFuncfor the interceptor, and helper for getting theBearerTokenFromHeader.The module now depends on
connectrpc.com/connect/v2as well as v1. This aligns with the pre v1 libraries. Examples are updated to use the new v2 APIs.Closes #31