Skip to content

Commit 788da41

Browse files
authored
Merge pull request Place1#155 from DasSkelett/fix/grpc-logging
2 parents 384771e + a66d984 commit 788da41

File tree

4 files changed

+35
-18
lines changed

4 files changed

+35
-18
lines changed

internal/services/api_router.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ import (
1212
"github.com/freifunkMUC/wg-access-server/proto/proto"
1313

1414
"github.com/freifunkMUC/wg-embed/pkg/wgembed"
15-
grpc_logrus "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
15+
grpcMiddleware "github.com/grpc-ecosystem/go-grpc-middleware"
16+
grpcLogrus "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
17+
grpcRecovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
1618
"github.com/improbable-eng/grpc-web/go/grpcweb"
1719
"google.golang.org/grpc"
20+
"google.golang.org/grpc/codes"
21+
"google.golang.org/grpc/status"
1822
)
1923

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

3548
// Register GRPC services

internal/services/health.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
)
77

88
func HealthEndpoint() http.Handler {
9-
return http.HandlerFunc(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
9+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1010
w.WriteHeader(200)
1111
fmt.Fprintf(w, "ok")
12-
}))
12+
})
1313
}

internal/services/middleware.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"net/http"
66
"runtime/debug"
77

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

@@ -19,7 +18,7 @@ func RecoveryMiddleware(next http.Handler) http.Handler {
1918
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2019
defer func() {
2120
if err := recover(); err != nil {
22-
ctxlogrus.Extract(r.Context()).
21+
traces.Logger(r.Context()).
2322
WithField("stack", string(debug.Stack())).
2423
Error(err)
2524
w.WriteHeader(500)

pkg/authnz/authconfig/oidc.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ import (
88
"strings"
99
"time"
1010

11-
"github.com/coreos/go-oidc"
12-
"github.com/gorilla/mux"
13-
"github.com/pkg/errors"
1411
"github.com/freifunkMUC/wg-access-server/pkg/authnz/authruntime"
1512
"github.com/freifunkMUC/wg-access-server/pkg/authnz/authsession"
1613
"github.com/freifunkMUC/wg-access-server/pkg/authnz/authutil"
14+
15+
"github.com/coreos/go-oidc"
16+
"github.com/gorilla/mux"
17+
"github.com/pkg/errors"
1718
"github.com/sirupsen/logrus"
1819
"golang.org/x/oauth2"
1920
"gopkg.in/Knetic/govaluate.v2"
@@ -138,14 +139,18 @@ func (c *OIDCConfig) callbackHandler(runtime *authruntime.ProviderRuntime, oauth
138139
}
139140
}
140141

142+
identity := &authsession.Identity{
143+
Provider: c.Name,
144+
Subject: info.Subject,
145+
Email: info.Email,
146+
Claims: *claims,
147+
}
148+
if name, ok := oidcProfileData["name"].(string); ok {
149+
identity.Name = name
150+
}
151+
141152
err = runtime.SetSession(w, r, &authsession.AuthSession{
142-
Identity: &authsession.Identity{
143-
Provider: c.Name,
144-
Subject: info.Subject,
145-
Email: info.Email,
146-
Name: oidcProfileData["name"].(string),
147-
Claims: *claims,
148-
},
153+
Identity: identity,
149154
})
150155
if err != nil {
151156
http.Error(w, err.Error(), http.StatusUnauthorized)

0 commit comments

Comments
 (0)