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

Fix grpc recover and logging #155

Merged
merged 2 commits into from
Apr 8, 2022
Merged
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
21 changes: 17 additions & 4 deletions internal/services/api_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ import (
"github.com/freifunkMUC/wg-access-server/proto/proto"

"github.com/freifunkMUC/wg-embed/pkg/wgembed"
grpc_logrus "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
grpcMiddleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpcLogrus "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
grpcRecovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
"github.com/improbable-eng/grpc-web/go/grpcweb"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

type ApiServices struct {
Expand All @@ -27,9 +31,18 @@ func ApiRouter(deps *ApiServices) http.Handler {
// Native GRPC server
server := grpc.NewServer([]grpc.ServerOption{
grpc.MaxRecvMsgSize(int(1 * math.Pow(2, 20))), // 1MB
grpc.UnaryInterceptor(func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
return grpc_logrus.UnaryServerInterceptor(traces.Logger(ctx))(ctx, req, info, handler)
}),
grpc.UnaryInterceptor(grpcMiddleware.ChainUnaryServer(
func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
// wrapped in anonymous func to get ctx
return grpcLogrus.UnaryServerInterceptor(traces.Logger(ctx))(ctx, req, info, handler)
},
grpcRecovery.UnaryServerInterceptor(
grpcRecovery.WithRecoveryHandlerContext(func(ctx context.Context, p interface{}) (err error) {
// add trace id to error message so it's visible for the client
return status.Errorf(codes.Internal, "%v; trace = %s", p, traces.TraceID(ctx))
}),
),
)),
}...)

// Register GRPC services
Expand Down
4 changes: 2 additions & 2 deletions internal/services/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
)

func HealthEndpoint() http.Handler {
return http.HandlerFunc(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
fmt.Fprintf(w, "ok")
}))
})
}
3 changes: 1 addition & 2 deletions internal/services/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/http"
"runtime/debug"

"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"
"github.com/freifunkMUC/wg-access-server/internal/traces"
)

Expand All @@ -19,7 +18,7 @@ func RecoveryMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
ctxlogrus.Extract(r.Context()).
traces.Logger(r.Context()).
WithField("stack", string(debug.Stack())).
Error(err)
w.WriteHeader(500)
Expand Down
25 changes: 15 additions & 10 deletions pkg/authnz/authconfig/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
"strings"
"time"

"github.com/coreos/go-oidc"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/freifunkMUC/wg-access-server/pkg/authnz/authruntime"
"github.com/freifunkMUC/wg-access-server/pkg/authnz/authsession"
"github.com/freifunkMUC/wg-access-server/pkg/authnz/authutil"

"github.com/coreos/go-oidc"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/oauth2"
"gopkg.in/Knetic/govaluate.v2"
Expand Down Expand Up @@ -138,14 +139,18 @@ func (c *OIDCConfig) callbackHandler(runtime *authruntime.ProviderRuntime, oauth
}
}

identity := &authsession.Identity{
Provider: c.Name,
Subject: info.Subject,
Email: info.Email,
Claims: *claims,
}
if name, ok := oidcProfileData["name"].(string); ok {
identity.Name = name
}

err = runtime.SetSession(w, r, &authsession.AuthSession{
Identity: &authsession.Identity{
Provider: c.Name,
Subject: info.Subject,
Email: info.Email,
Name: oidcProfileData["name"].(string),
Claims: *claims,
},
Identity: identity,
})
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
Expand Down