From 0bcef1ad5f3794cf02c49fd3dbbd100ded6ebc1b Mon Sep 17 00:00:00 2001 From: Tim Ross Date: Fri, 6 Dec 2024 17:09:06 -0500 Subject: [PATCH] Convert lib/srv/transport to use slog --- lib/service/service.go | 2 +- lib/srv/transport/transportv1/transport.go | 11 ++++++----- lib/srv/transport/transportv1/transport_test.go | 8 ++++---- lib/web/apiserver_test.go | 2 +- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/service/service.go b/lib/service/service.go index 97fbd4a03e370..24c1d123e23ec 100644 --- a/lib/service/service.go +++ b/lib/service/service.go @@ -4932,7 +4932,7 @@ func (process *TeleportProcess) initProxyEndpoint(conn *Connector) error { transportService, err := transportv1.NewService(transportv1.ServerConfig{ FIPS: cfg.FIPS, - Logger: process.log.WithField(teleport.ComponentKey, "transport"), + Logger: process.logger.With(teleport.ComponentKey, "transport"), Dialer: proxyRouter, SignerFn: func(authzCtx *authz.Context, clusterName string) agentless.SignerCreator { return agentless.SignerFromAuthzContext(authzCtx, accessPoint, clusterName) diff --git a/lib/srv/transport/transportv1/transport.go b/lib/srv/transport/transportv1/transport.go index f019875186af7..c1ce020707ae6 100644 --- a/lib/srv/transport/transportv1/transport.go +++ b/lib/srv/transport/transportv1/transport.go @@ -22,12 +22,13 @@ import ( "context" "errors" "io" + "log/slog" "net" "net/netip" + "reflect" "sync" "github.com/gravitational/trace" - "github.com/sirupsen/logrus" "golang.org/x/crypto/ssh/agent" "google.golang.org/grpc/codes" "google.golang.org/grpc/credentials" @@ -63,7 +64,7 @@ type ServerConfig struct { // to run in FIPS mode. FIPS bool // Logger provides a mechanism to log output. - Logger logrus.FieldLogger + Logger *slog.Logger // Dialer is used to establish remote connections. Dialer Dialer // SignerFn is used to create an [ssh.Signer] for an authenticated connection. @@ -93,7 +94,7 @@ func (c *ServerConfig) CheckAndSetDefaults() error { } if c.Logger == nil { - c.Logger = utils.NewLogger().WithField(teleport.ComponentKey, "transport") + c.Logger = slog.With(teleport.ComponentKey, "transport") } if c.agentGetterFn == nil { @@ -247,7 +248,7 @@ func (s *Service) ProxySSH(stream transportv1pb.TransportService_ProxySSHServer) req, err := stream.Recv() if err != nil { if !utils.IsOKNetworkError(err) && !errors.Is(err, context.Canceled) && status.Code(err) != codes.Canceled { - s.cfg.Logger.Errorf("ssh stream terminated unexpectedly: %v", err) + s.cfg.Logger.ErrorContext(ctx, "ssh stream terminated unexpectedly", "error", err) } return @@ -262,7 +263,7 @@ func (s *Service) ProxySSH(stream transportv1pb.TransportService_ProxySSHServer) case *transportv1pb.ProxySSHRequest_Agent: agentStream.incomingC <- frame.Agent.Payload default: - s.cfg.Logger.Errorf("received unexpected ssh frame: %T", frame) + s.cfg.Logger.ErrorContext(ctx, "received unexpected ssh frame", "frame", reflect.TypeOf(frame)) continue } } diff --git a/lib/srv/transport/transportv1/transport_test.go b/lib/srv/transport/transportv1/transport_test.go index e45dc31194d47..4d3937816ef06 100644 --- a/lib/srv/transport/transportv1/transport_test.go +++ b/lib/srv/transport/transportv1/transport_test.go @@ -256,7 +256,7 @@ func TestService_GetClusterDetails(t *testing.T) { t.Parallel() srv := newServer(t, ServerConfig{ Dialer: fakeDialer{}, - Logger: utils.NewLoggerForTests(), + Logger: utils.NewSlogLoggerForTests(), FIPS: test.FIPS, SignerFn: fakeSigner, ConnectionMonitor: fakeMonitor{}, @@ -339,7 +339,7 @@ func TestService_ProxyCluster(t *testing.T) { cluster: conn, }, }, - Logger: utils.NewLoggerForTests(), + Logger: utils.NewSlogLoggerForTests(), SignerFn: fakeSigner, ConnectionMonitor: fakeMonitor{}, LocalAddr: utils.MustParseAddr("127.0.0.1:4242"), @@ -491,7 +491,7 @@ func TestService_ProxySSH_Errors(t *testing.T) { }, SignerFn: fakeSigner, ConnectionMonitor: fakeMonitor{}, - Logger: utils.NewLoggerForTests(), + Logger: utils.NewSlogLoggerForTests(), LocalAddr: utils.MustParseAddr("127.0.0.1:4242"), authzContextFn: func(info credentials.AuthInfo) (*authz.Context, error) { checker, err := test.checkerFn(info) @@ -554,7 +554,7 @@ func TestService_ProxySSH(t *testing.T) { srv := newServer(t, ServerConfig{ Dialer: sshSrv, SignerFn: fakeSigner, - Logger: utils.NewLoggerForTests(), + Logger: utils.NewSlogLoggerForTests(), LocalAddr: utils.MustParseAddr("127.0.0.1:4242"), ConnectionMonitor: fakeMonitor{}, agentGetterFn: func(rw io.ReadWriter) teleagent.Getter { diff --git a/lib/web/apiserver_test.go b/lib/web/apiserver_test.go index 3a701fe65a26a..5b78a5cd60c54 100644 --- a/lib/web/apiserver_test.go +++ b/lib/web/apiserver_test.go @@ -8308,7 +8308,7 @@ func createProxy(ctx context.Context, t *testing.T, proxyID string, node *regula transportService, err := transportv1.NewService(transportv1.ServerConfig{ FIPS: false, - Logger: utils.NewLoggerForTests(), + Logger: utils.NewSlogLoggerForTests(), Dialer: router, SignerFn: func(authzCtx *authz.Context, clusterName string) agentless.SignerCreator { return agentless.SignerFromAuthzContext(authzCtx, client, clusterName)