Skip to content

Commit

Permalink
Convert lib/proxy/peer to slog (#47649)
Browse files Browse the repository at this point in the history
* Convert lib/proxy/peer to slog

* .

* style compliance with RFD 0154
  • Loading branch information
espadolini authored Oct 17, 2024
1 parent 822c5e1 commit 3e75921
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 22 deletions.
23 changes: 11 additions & 12 deletions lib/proxy/peer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package peer
import (
"context"
"crypto/tls"
"log/slog"
"math/rand/v2"
"net"
"sync"
Expand All @@ -29,7 +30,6 @@ import (
"github.com/gravitational/trace"
"github.com/jonboulle/clockwork"
"github.com/quic-go/quic-go"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials"
Expand Down Expand Up @@ -72,7 +72,7 @@ type ClientConfig struct {
// TLSCipherSuites optionally contains a list of TLS ciphersuites to use.
TLSCipherSuites []uint16
// Log is the proxy client logger.
Log logrus.FieldLogger
Log *slog.Logger
// Clock is used to control connection monitoring ticker.
Clock clockwork.Clock
// GracefulShutdownTimout is used set the graceful shutdown
Expand Down Expand Up @@ -112,10 +112,10 @@ func noopConnShuffler() connShuffler {
// checkAndSetDefaults checks and sets default values
func (c *ClientConfig) checkAndSetDefaults() error {
if c.Log == nil {
c.Log = logrus.New()
c.Log = slog.Default()
}

c.Log = c.Log.WithField(
c.Log = c.Log.With(
teleport.ComponentKey,
teleport.Component(teleport.ComponentProxyPeer),
)
Expand Down Expand Up @@ -414,30 +414,29 @@ func (c *Client) sync() {
ResourceWatcherConfig: services.ResourceWatcherConfig{
Component: teleport.Component(teleport.ComponentProxyPeer),
Client: c.config.AccessPoint,
// TODO(tross): use the configured logger after updating peering to use slog
// Logger: c.config.Logger,
Logger: c.config.Log,
},
ProxyDiffer: func(old, new types.Server) bool {
return old.GetPeerAddr() != new.GetPeerAddr()
},
})
if err != nil {
c.config.Log.Errorf("Error initializing proxy peer watcher: %+v.", err)
c.config.Log.ErrorContext(c.ctx, "error initializing proxy peer watcher", "error", err)
return
}
defer proxyWatcher.Close()

for {
select {
case <-c.ctx.Done():
c.config.Log.Debug("Stopping peer proxy sync: context done.")
c.config.Log.DebugContext(c.ctx, "stopping peer proxy sync: context done")
return
case <-proxyWatcher.Done():
c.config.Log.Debug("Stopping peer proxy sync: proxy watcher done.")
c.config.Log.DebugContext(c.ctx, "stopping peer proxy sync: proxy watcher done")
return
case proxies := <-proxyWatcher.ProxiesC:
if err := c.updateConnections(proxies); err != nil {
c.config.Log.Errorf("Error syncing peer proxies: %+v.", err)
c.config.Log.ErrorContext(c.ctx, "error syncing peer proxies", "error", err)
}
}
}
Expand Down Expand Up @@ -488,7 +487,7 @@ func (c *Client) updateConnections(proxies []types.Server) error {
conn, err := c.connect(id, proxy.GetPeerAddr(), supportsQuic)
if err != nil {
c.metrics.reportTunnelError(errorProxyPeerTunnelDial)
c.config.Log.Debugf("Error dialing peer proxy %+v at %+v", id, proxy.GetPeerAddr())
c.config.Log.DebugContext(c.ctx, "error dialing peer proxy", "peer_id", id, "peer_addr", proxy.GetPeerAddr())
errs = append(errs, err)
continue
}
Expand Down Expand Up @@ -689,7 +688,7 @@ func (c *Client) getConnections(proxyIDs []string) ([]clientConn, bool, error) {
conn, err := c.connect(id, proxy.GetPeerAddr(), supportsQuic)
if err != nil {
c.metrics.reportTunnelError(errorProxyPeerTunnelDirectDial)
c.config.Log.Debugf("Error direct dialing peer proxy %+v at %+v", id, proxy.GetPeerAddr())
c.config.Log.DebugContext(c.ctx, "error direct dialing peer proxy", "peer_id", id, "peer_addr", proxy.GetPeerAddr())
errs = append(errs, err)
continue
}
Expand Down
13 changes: 7 additions & 6 deletions lib/proxy/peer/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ package peer

import (
"context"
"log/slog"
"net"

"github.com/gravitational/trace"
"github.com/sirupsen/logrus"
"google.golang.org/grpc/credentials"

"github.com/gravitational/teleport/api/types"
Expand All @@ -37,16 +37,16 @@ type clientCredentials struct {
credentials.TransportCredentials
peerID string
peerAddr string
logger logrus.FieldLogger
log *slog.Logger
}

// newClientCredentials creates new clientCredentials from the given [crendentials.TransportCredentials].
func newClientCredentials(peerID, peerAddr string, logger logrus.FieldLogger, creds credentials.TransportCredentials) *clientCredentials {
func newClientCredentials(peerID, peerAddr string, log *slog.Logger, creds credentials.TransportCredentials) *clientCredentials {
return &clientCredentials{
TransportCredentials: creds,
peerID: peerID,
peerAddr: peerAddr,
logger: logger,
log: log,
}
}

Expand All @@ -73,15 +73,16 @@ func (c *clientCredentials) ClientHandshake(ctx context.Context, laddr string, c
return nil, nil, trace.Wrap(err)
}

const duplicatePeerMsg = "Detected multiple Proxy Peers with the same public address %q when connecting to Proxy %q which can lead to inconsistent state and problems establishing sessions. For best results ensure that `peer_public_addr` is unique per proxy and not a load balancer."
if err := validatePeer(c.peerID, identity); err != nil {
c.logger.Errorf(duplicatePeerMsg, c.peerAddr, c.peerID)
c.log.ErrorContext(ctx, duplicatePeerMsg, "peer_addr", c.peerAddr, "peer_id", c.peerID)
return nil, nil, trace.Wrap(err)
}

return conn, authInfo, nil
}

const duplicatePeerMsg = "Detected multiple Proxy Peers with the same public address when connecting to a Proxy which can lead to inconsistent state and problems establishing sessions. For best results ensure that `peer_public_addr` is unique per proxy and not a load balancer."

// getIdentity returns a [tlsca.Identity] that is created from the certificate
// presented during the TLS handshake.
func getIdentity(authInfo credentials.AuthInfo) (*tlsca.Identity, error) {
Expand Down
2 changes: 1 addition & 1 deletion lib/proxy/peer/credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func TestClientCredentials(t *testing.T) {
},
}

creds := newClientCredentials(test.expectedPeerID, test.peerAddr, utils.NewLoggerForTests(), newTestCredentials(cert))
creds := newClientCredentials(test.expectedPeerID, test.peerAddr, utils.NewSlogLoggerForTests(), newTestCredentials(cert))

ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
Expand Down
4 changes: 2 additions & 2 deletions lib/proxy/peer/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (s *proxyService) DialNode(stream proto.ProxyService_DialNodeServer) error
"src", dial.Source.Addr,
"dst", dial.Destination.Addr,
)
log.DebugContext(stream.Context(), "Dial request from peer.")
log.DebugContext(stream.Context(), "dial request from peer")

_, clusterName, err := splitServerID(dial.NodeID)
if err != nil {
Expand Down Expand Up @@ -103,7 +103,7 @@ func (s *proxyService) DialNode(stream proto.ProxyService_DialNodeServer) error

err = utils.ProxyConn(stream.Context(), streamConn, nodeConn)
sent, received := streamConn.Stat()
log.DebugContext(stream.Context(), "Closing dial request from peer.", "sent", sent, "received", received)
log.DebugContext(stream.Context(), "closing dial request from peer", "sent", sent, "received", received)
return trace.Wrap(err)
}

Expand Down
2 changes: 1 addition & 1 deletion lib/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4347,7 +4347,7 @@ func (process *TeleportProcess) initProxyEndpoint(conn *Connector) error {
TLSCipherSuites: cfg.CipherSuites,
GetTLSCertificate: conn.ClientGetCertificate,
GetTLSRoots: conn.ClientGetPool,
Log: process.log,
Log: process.logger,
Clock: process.Clock,
ClusterName: clusterName,
QUICTransport: peerQUICTransport,
Expand Down

0 comments on commit 3e75921

Please sign in to comment.