Skip to content

Commit

Permalink
Add logs around gRPC requests (#2159)
Browse files Browse the repository at this point in the history
  • Loading branch information
iskakaushik authored Oct 19, 2024
1 parent 95d160e commit e4f940e
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions flow/auth/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,35 @@ func AuthGrpcMiddleware(unauthenticatedMethods []string) ([]grpc.ServerOption, e
for _, method := range unauthenticatedMethods {
unauthenticatedMethodsMap[method] = struct{}{}
}

return []grpc.ServerOption{
grpc.ChainUnaryInterceptor(func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
slog.Info("Received gRPC request", slog.String("method", info.FullMethod))

if _, unauthorized := unauthenticatedMethodsMap[info.FullMethod]; !unauthorized {
var authHeader string
authHeaders := metadata.ValueFromIncomingContext(ctx, "Authorization")
if len(authHeaders) == 1 {
authHeader = authHeaders[0]
} else if len(authHeaders) > 1 {
slog.Warn("Multiple Authorization headers supplied, request rejected", slog.String("method", info.FullMethod))
return nil, status.Errorf(codes.Unauthenticated, "multiple Authorization headers supplied, request rejected")
}
_, err := validateRequestToken(authHeader, cfg.OauthJwtCustomClaims, ip...)
if err != nil {
slog.Debug("failed to validate request token", slog.Any("error", err))
slog.Debug("Failed to validate request token", slog.String("method", info.FullMethod), slog.Any("error", err))
return nil, status.Errorf(codes.Unauthenticated, "%s", err.Error())
}
}
return handler(ctx, req)

resp, err := handler(ctx, req)

if err != nil {
slog.Error("gRPC request failed", slog.String("method", info.FullMethod), slog.Any("error", err))
} else {
slog.Info("gRPC request completed successfully", slog.String("method", info.FullMethod))
}

return resp, err
}),
}, nil
}
Expand Down

0 comments on commit e4f940e

Please sign in to comment.