Skip to content

Commit

Permalink
Convert lib/web to use slog (#50407)
Browse files Browse the repository at this point in the history
  • Loading branch information
rosstimothy authored Dec 19, 2024
1 parent 3e3daec commit 6b80477
Show file tree
Hide file tree
Showing 28 changed files with 459 additions and 428 deletions.
4 changes: 2 additions & 2 deletions integration/kube_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1768,7 +1768,7 @@ func testKubeExecWeb(t *testing.T, suite *KubeSuite) {

ws := openWebsocketAndReadSession(t, endpoint, req)

wsStream := terminal.NewWStream(context.Background(), ws, suite.log, nil)
wsStream := terminal.NewWStream(context.Background(), ws, utils.NewSlogLoggerForTests(), nil)

// Check for the expected string in the output.
findTextInReader(t, wsStream, testNamespace, time.Second*2)
Expand All @@ -1789,7 +1789,7 @@ func testKubeExecWeb(t *testing.T, suite *KubeSuite) {

ws := openWebsocketAndReadSession(t, endpoint, req)

wsStream := terminal.NewWStream(context.Background(), ws, suite.log, nil)
wsStream := terminal.NewWStream(context.Background(), ws, utils.NewSlogLoggerForTests(), nil)

// Read first prompt from the server.
readData := make([]byte, 255)
Expand Down
6 changes: 3 additions & 3 deletions lib/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4723,7 +4723,7 @@ func (process *TeleportProcess) initProxyEndpoint(conn *Connector) error {
},
},
Handler: webHandler,
Log: process.log.WithField(teleport.ComponentKey, teleport.Component(teleport.ComponentReverseTunnelServer, process.id)),
Log: process.logger.With(teleport.ComponentKey, teleport.Component(teleport.ComponentReverseTunnelServer, process.id)),
})
if err != nil {
return trace.Wrap(err)
Expand Down Expand Up @@ -5517,7 +5517,7 @@ func (process *TeleportProcess) initMinimalReverseTunnel(listeners *proxyListene
return nil
})

log := process.log.WithField(teleport.ComponentKey, teleport.Component(teleport.ComponentReverseTunnelServer, process.id))
log := process.logger.With(teleport.ComponentKey, teleport.Component(teleport.ComponentReverseTunnelServer, process.id))

minimalWebServer, err := web.NewServer(web.ServerConfig{
Server: &http.Server{
Expand All @@ -5526,7 +5526,7 @@ func (process *TeleportProcess) initMinimalReverseTunnel(listeners *proxyListene
ReadHeaderTimeout: defaults.ReadHeadersTimeout,
WriteTimeout: apidefaults.DefaultIOTimeout,
IdleTimeout: apidefaults.DefaultIdleTimeout,
ErrorLog: utils.NewStdlogger(log.Error, teleport.ComponentReverseTunnelServer),
ErrorLog: slog.NewLogLogger(log.Handler(), slog.LevelError),
},
Handler: minimalWebHandler,
Log: log,
Expand Down
9 changes: 5 additions & 4 deletions lib/web/addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ package web

import (
"bufio"
"context"
"log/slog"
"net"
"net/http"
"net/netip"
"strings"

"github.com/gravitational/trace"
"github.com/sirupsen/logrus"

"github.com/gravitational/teleport/lib/authz"
"github.com/gravitational/teleport/lib/utils"
Expand Down Expand Up @@ -56,7 +57,7 @@ func NewXForwardedForMiddleware(next http.Handler) http.Handler {
// Serve with updated client source address.
default:
next.ServeHTTP(
responseWriterWithClientSrcAddr(w, clientSrcAddr),
responseWriterWithClientSrcAddr(r.Context(), w, clientSrcAddr),
requestWithClientSrcAddr(r, clientSrcAddr),
)
}
Expand Down Expand Up @@ -113,11 +114,11 @@ func requestWithClientSrcAddr(r *http.Request, clientSrcAddr net.Addr) *http.Req
return r
}

func responseWriterWithClientSrcAddr(w http.ResponseWriter, clientSrcAddr net.Addr) http.ResponseWriter {
func responseWriterWithClientSrcAddr(ctx context.Context, w http.ResponseWriter, clientSrcAddr net.Addr) http.ResponseWriter {
// Returns the original ResponseWriter if not a http.Hijacker.
_, ok := w.(http.Hijacker)
if !ok {
logrus.Debug("Provided ResponseWriter is not a hijacker.")
slog.DebugContext(ctx, "Provided ResponseWriter is not a hijacker")
return w
}

Expand Down
Loading

0 comments on commit 6b80477

Please sign in to comment.