Skip to content

Commit 03440f6

Browse files
authored
fix: avoid connection logging crashes in agent [2.26] (coder#20306)
# For release 2.26 - Ignore errors when reporting a connection from the server, just log them instead - Translate connection log IP `localhost` to `127.0.0.1` on both the server and the agent - Temporary fix: convert invalid IPs to `127.0.0.1` since the database forbids NULL Relates to coder#20194
1 parent 7afe6c8 commit 03440f6

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

agent/agent.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -790,11 +790,15 @@ func (a *agent) reportConnectionsLoop(ctx context.Context, aAPI proto.DRPCAgentC
790790
logger.Debug(ctx, "reporting connection")
791791
_, err := aAPI.ReportConnection(ctx, payload)
792792
if err != nil {
793-
return xerrors.Errorf("failed to report connection: %w", err)
793+
// Do not fail the loop if we fail to report a connection, just
794+
// log a warning.
795+
// Related to https://github.com/coder/coder/issues/20194
796+
logger.Warn(ctx, "failed to report connection to server", slog.Error(err))
797+
// no continue here, we still need to remove it from the slice
798+
} else {
799+
logger.Debug(ctx, "successfully reported connection")
794800
}
795801

796-
logger.Debug(ctx, "successfully reported connection")
797-
798802
// Remove the payload we sent.
799803
a.reportConnectionsMu.Lock()
800804
a.reportConnections[0] = nil // Release the pointer from the underlying array.
@@ -825,6 +829,13 @@ func (a *agent) reportConnection(id uuid.UUID, connectionType proto.Connection_T
825829
ip = host
826830
}
827831

832+
// If the IP is "localhost" (which it can be in some cases), set it to
833+
// 127.0.0.1 instead.
834+
// Related to https://github.com/coder/coder/issues/20194
835+
if ip == "localhost" {
836+
ip = "127.0.0.1"
837+
}
838+
828839
a.reportConnectionsMu.Lock()
829840
defer a.reportConnectionsMu.Unlock()
830841

coderd/agentapi/connectionlog.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package agentapi
33
import (
44
"context"
55
"database/sql"
6+
"net"
67
"sync/atomic"
78

89
"github.com/google/uuid"
10+
"github.com/sqlc-dev/pqtype"
911
"golang.org/x/xerrors"
1012
"google.golang.org/protobuf/types/known/emptypb"
1113

@@ -61,6 +63,27 @@ func (a *ConnLogAPI) ReportConnection(ctx context.Context, req *agentproto.Repor
6163
return nil, xerrors.Errorf("get workspace by agent id: %w", err)
6264
}
6365

66+
// Some older clients may incorrectly report "localhost" as the IP address.
67+
// Related to https://github.com/coder/coder/issues/20194
68+
logIPRaw := req.GetConnection().GetIp()
69+
if logIPRaw == "localhost" {
70+
logIPRaw = "127.0.0.1"
71+
}
72+
73+
// TEMPORARY FIX for https://github.com/coder/coder/issues/20194
74+
logIP := database.ParseIP(logIPRaw)
75+
if !logIP.Valid {
76+
// In older versions of Coder, NULL IPs are not permitted in the DB, so
77+
// use 127.0.0.1 instead.
78+
logIP = pqtype.Inet{
79+
IPNet: net.IPNet{
80+
IP: net.IPv4(127, 0, 0, 1),
81+
Mask: net.CIDRMask(32, 32),
82+
},
83+
Valid: true,
84+
}
85+
}
86+
6487
reason := req.GetConnection().GetReason()
6588
connLogger := *a.ConnectionLogger.Load()
6689
err = connLogger.Upsert(ctx, database.UpsertConnectionLogParams{
@@ -73,7 +96,7 @@ func (a *ConnLogAPI) ReportConnection(ctx context.Context, req *agentproto.Repor
7396
AgentName: workspaceAgent.Name,
7497
Type: connectionType,
7598
Code: code,
76-
Ip: database.ParseIP(req.GetConnection().GetIp()),
99+
Ip: logIP,
77100
ConnectionID: uuid.NullUUID{
78101
UUID: connectionID,
79102
Valid: true,

coderd/agentapi/connectionlog_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ func TestConnectionLog(t *testing.T) {
110110
mDB := dbmock.NewMockStore(gomock.NewController(t))
111111
mDB.EXPECT().GetWorkspaceByAgentID(gomock.Any(), agent.ID).Return(workspace, nil)
112112

113+
// TEMPORARY FIX for https://github.com/coder/coder/issues/20194
114+
if tt.ip == "" {
115+
tt.ip = "127.0.0.1"
116+
}
117+
113118
api := &agentapi.ConnLogAPI{
114119
ConnectionLogger: asAtomicPointer[connectionlog.ConnectionLogger](connLogger),
115120
Database: mDB,

0 commit comments

Comments
 (0)