Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita1302bd

Browse files
committed
null exit code until disconnect
1 parentd42bdff commita1302bd

File tree

16 files changed

+117
-94
lines changed

16 files changed

+117
-94
lines changed

‎coderd/agentapi/connectionlog.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (a *ConnLogAPI) ReportConnection(ctx context.Context, req *agentproto.Repor
3434
ifconnectionID==uuid.Nil {
3535
returnnil,xerrors.New("connection ID cannot be nil")
3636
}
37-
action,err:=db2sdk.ConnectionActionFromAgentProtoConnectionAction(req.GetConnection().GetAction())
37+
action,err:=db2sdk.ConnectionLogStatusFromAgentProtoConnectionAction(req.GetConnection().GetAction())
3838
iferr!=nil {
3939
returnnil,err
4040
}
@@ -64,8 +64,11 @@ func (a *ConnLogAPI) ReportConnection(ctx context.Context, req *agentproto.Repor
6464
WorkspaceName:workspace.Name,
6565
AgentName:workspaceAgent.Name,
6666
Type:connectionType,
67-
Code:req.GetConnection().GetStatusCode(),
68-
Ip:database.ParseIP(req.GetConnection().GetIp()),
67+
Code: sql.NullInt32{
68+
Int32:req.GetConnection().GetStatusCode(),
69+
Valid:req.GetConnection().GetStatusCode()!=0,
70+
},
71+
Ip:database.ParseIP(req.GetConnection().GetIp()),
6972
ConnectionID: uuid.NullUUID{
7073
UUID:connectionID,
7174
Valid:true,
@@ -76,7 +79,7 @@ func (a *ConnLogAPI) ReportConnection(ctx context.Context, req *agentproto.Repor
7679
},
7780
// Used to populate whether the connection was established or closed
7881
// outside of the DB (slog).
79-
ConnectionAction:action,
82+
ConnectionStatus:action,
8083

8184
// It's not possible to tell which user connected. Once we have
8285
// the capability, this may be reported by the agent.

‎coderd/agentapi/connectionlog_test.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,14 @@ func TestConnectionLog(t *testing.T) {
140140
UUID:uuid.Nil,
141141
Valid:false,
142142
},
143-
ConnectionAction:connectionLogActionFromAgentProtoConnectionAction(t,*tt.action),
143+
ConnectionStatus:agentProtoConnectionActionToConnectionLog(t,*tt.action),
144144

145-
Code:tt.status,
145+
Code: sql.NullInt32{
146+
Int32:tt.status,
147+
Valid:tt.status!=0,
148+
},
146149
Ip: pqtype.Inet{Valid:true,IPNet: net.IPNet{IP:net.ParseIP(tt.ip),Mask:net.CIDRMask(32,32)}},
147-
Type:connectionLogConnectionTypeFromAgentProtoConnectionType(t,*tt.typ),
150+
Type:agentProtoConnectionTypeToConnectionLog(t,*tt.typ),
148151
CloseReason: sql.NullString{
149152
String:tt.reason,
150153
Valid:tt.reason!="",
@@ -158,14 +161,14 @@ func TestConnectionLog(t *testing.T) {
158161
}
159162
}
160163

161-
funcconnectionLogConnectionTypeFromAgentProtoConnectionType(t*testing.T,typ agentproto.Connection_Type) database.ConnectionType {
164+
funcagentProtoConnectionTypeToConnectionLog(t*testing.T,typ agentproto.Connection_Type) database.ConnectionType {
162165
a,err:=db2sdk.ConnectionLogConnectionTypeFromAgentProtoConnectionType(typ)
163166
require.NoError(t,err)
164167
returna
165168
}
166169

167-
funcconnectionLogActionFromAgentProtoConnectionAction(t*testing.T,action agentproto.Connection_Action) database.ConnectionAction {
168-
a,err:=db2sdk.ConnectionActionFromAgentProtoConnectionAction(action)
170+
funcagentProtoConnectionActionToConnectionLog(t*testing.T,action agentproto.Connection_Action) database.ConnectionStatus {
171+
a,err:=db2sdk.ConnectionLogStatusFromAgentProtoConnectionAction(action)
169172
require.NoError(t,err)
170173
returna
171174
}

‎coderd/connectionlog/connectionlog.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ func (m *MockConnectionLogger) Contains(t testing.TB, expected database.UpsertCo
9090
t.Logf("connection log %d: expected Type %s, got %s",idx+1,expected.Type,cl.Type)
9191
continue
9292
}
93-
ifexpected.Code!=0&&cl.Code!=expected.Code {
94-
t.Logf("connection log %d: expected Code %d, got %d",idx+1,expected.Code,cl.Code)
93+
ifexpected.Code.Valid&&cl.Code.Int32!=expected.Code.Int32 {
94+
t.Logf("connection log %d: expected Code %d, got %d",idx+1,expected.Code.Int32,cl.Code.Int32)
9595
continue
9696
}
9797
ifexpected.Ip.Valid&&cl.Ip.IPNet.String()!=expected.Ip.IPNet.String() {

‎coderd/database/db2sdk/db2sdk.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -738,17 +738,17 @@ func ConnectionLogConnectionTypeFromAgentProtoConnectionType(typ agentproto.Conn
738738
caseagentproto.Connection_RECONNECTING_PTY:
739739
returndatabase.ConnectionTypeReconnectingPty,nil
740740
default:
741-
// AlsoConnection_ACTION_UNSPECIFIED, no mapping.
741+
// AlsoConnection_TYPE_UNSPECIFIED, no mapping.
742742
return"",xerrors.Errorf("unknown agent connection type %q",typ)
743743
}
744744
}
745745

746-
funcConnectionActionFromAgentProtoConnectionAction(action agentproto.Connection_Action) (database.ConnectionAction,error) {
746+
funcConnectionLogStatusFromAgentProtoConnectionAction(action agentproto.Connection_Action) (database.ConnectionStatus,error) {
747747
switchaction {
748748
caseagentproto.Connection_CONNECT:
749-
returndatabase.ConnectionActionConnect,nil
749+
returndatabase.ConnectionStatusConnected,nil
750750
caseagentproto.Connection_DISCONNECT:
751-
returndatabase.ConnectionActionDisconnect,nil
751+
returndatabase.ConnectionStatusDisconnected,nil
752752
default:
753753
// Also Connection_ACTION_UNSPECIFIED, no mapping.
754754
return"",xerrors.Errorf("unknown agent connection action %q",action)

‎coderd/database/dbauthz/dbauthz_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ func (s *MethodTestSuite) TestConnectionLogs() {
351351
Type:database.ConnectionTypeSsh,
352352
WorkspaceID:ws.ID,
353353
OrganizationID:ws.OrganizationID,
354-
ConnectionAction:database.ConnectionActionConnect,
354+
ConnectionStatus:database.ConnectionStatusConnected,
355355
WorkspaceOwnerID:ws.OwnerID,
356356
}).Asserts(rbac.ResourceConnectionLog,policy.ActionCreate)
357357
}))

‎coderd/database/dbgen/dbgen.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ func ConnectionLog(t testing.TB, db database.Store, seed database.ConnectionLog)
8383
WorkspaceName:takeFirst(seed.WorkspaceName,testutil.GetRandomName(t)),
8484
AgentName:takeFirst(seed.AgentName,testutil.GetRandomName(t)),
8585
Type:takeFirst(seed.Type,database.ConnectionTypeSsh),
86-
Code:takeFirst(seed.Code,0),
86+
Code: sql.NullInt32{
87+
Int32:takeFirst(seed.Code.Int32,0),
88+
Valid:takeFirst(seed.Code.Valid,false),
89+
},
8790
Ip: pqtype.Inet{
8891
IPNet:takeFirstIP(seed.Ip.IPNet, net.IPNet{}),
8992
Valid:takeFirst(seed.Ip.Valid,false),
@@ -108,7 +111,7 @@ func ConnectionLog(t testing.TB, db database.Store, seed database.ConnectionLog)
108111
String:takeFirst(seed.CloseReason.String,""),
109112
Valid:takeFirst(seed.CloseReason.Valid,false),
110113
},
111-
ConnectionAction:database.ConnectionActionConnect,
114+
ConnectionStatus:database.ConnectionStatusConnected,
112115
})
113116
require.NoError(t,err,"insert connection log")
114117
returnlog

‎coderd/database/dbmem/dbmem.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12281,7 +12281,7 @@ func (q *FakeQuerier) UpsertConnectionLog(_ context.Context, arg database.Upsert
1228112281
ifexisting.ConnectionID==arg.ConnectionID&&
1228212282
existing.WorkspaceID==arg.WorkspaceID&&
1228312283
existing.AgentName==arg.AgentName {
12284-
ifarg.ConnectionAction!="disconnect" {
12284+
ifarg.ConnectionStatus!=database.ConnectionStatusDisconnected {
1228512285
returnq.connectionLogs[i],nil
1228612286
}
1228712287
// Update existing connection with close time and reason

‎coderd/database/dump.sql

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎coderd/database/migrations/000336_connection_logs.down.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ DROP TABLE IF EXISTS connection_logs;
77

88
DROPTYPE IF EXISTS connection_type;
99

10-
DROPTYPE IF EXISTSconnection_action;
10+
DROPTYPE IF EXISTSconnection_status;

‎coderd/database/migrations/000336_connection_logs.up.sql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
CREATETYPEconnection_actionAS ENUM (
2-
'connect',
3-
'disconnect'
1+
CREATETYPEconnection_statusAS ENUM (
2+
'connected',
3+
'disconnected'
44
);
55

66
CREATETYPEconnection_typeAS ENUM (
@@ -22,7 +22,7 @@ CREATE TABLE connection_logs (
2222
workspace_nametextNOT NULL,
2323
agent_nametextNOT NULL,
2424
type connection_typeNOT NULL,
25-
codeintegerNOT NULL,
25+
codeinteger,-- Null until we upsert a disconnected log for the same connection_id.
2626
ipinet,
2727

2828
-- Only set for 'web' logs.
@@ -32,14 +32,14 @@ CREATE TABLE connection_logs (
3232

3333
-- Null for 'web' logs.
3434
connection_id uuid,
35-
close_timetimestamp with time zone,-- Null until wereceive asecond event for the same connection_id.
35+
close_timetimestamp with time zone,-- Null until weupsert adisconnected log for the same connection_id.
3636
close_reasontext,
3737

3838
PRIMARY KEY (id)
3939
);
4040

4141

42-
COMMENT ON COLUMN connection_logs.code IS'Either the HTTP status code of the web request, or the exit code of an SSH connection.';
42+
COMMENT ON COLUMN connection_logs.code IS'Either the HTTP status code of the web request, or the exit code of an SSH connection. For SSH actions, this is Null if the connection is still open, or if we never received a disconnect log..';
4343

4444
COMMENT ON COLUMN connection_logs.user_agent IS'Null for SSH actions. For web connections, this is the User-Agent header from the request.';
4545

‎coderd/database/models.go

Lines changed: 25 additions & 25 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎coderd/database/querier_test.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,9 +2261,8 @@ func TestUpsertConnectionLog(t *testing.T) {
22612261
WorkspaceName:ws.Name,
22622262
AgentName:agentName,
22632263
Type:database.ConnectionTypeSsh,
2264-
Code:0,
22652264
ConnectionID: uuid.NullUUID{UUID:connectionID,Valid:true},
2266-
ConnectionAction:database.ConnectionActionConnect,
2265+
ConnectionStatus:database.ConnectionStatusConnected,
22672266
}
22682267

22692268
log1,err:=db.UpsertConnectionLog(ctx,connectParams)
@@ -2277,25 +2276,25 @@ func TestUpsertConnectionLog(t *testing.T) {
22772276
require.Len(t,rows,1)
22782277
require.Equal(t,int64(1),rows[0].Count)
22792278

2280-
// 2. Insert a 'disconnect' event for the same connection.
2279+
// 2. Insert a 'disconnected' event for the same connection.
22812280
disconnectTime:=connectTime.Add(time.Second)
22822281
disconnectParams:= database.UpsertConnectionLogParams{
22832282
ConnectionID: uuid.NullUUID{UUID:connectionID,Valid:true},
22842283
WorkspaceID:ws.ID,
22852284
AgentName:agentName,
2286-
ConnectionAction:database.ConnectionActionDisconnect,
2285+
ConnectionStatus:database.ConnectionStatusDisconnected,
22872286

22882287
// Updated to:
22892288
Time:disconnectTime,
22902289
CloseReason: sql.NullString{String:"test disconnect",Valid:true},
2290+
Code: sql.NullInt32{Int32:1,Valid:true},
22912291

22922292
// Ignored
22932293
ID:uuid.New(),
22942294
OrganizationID:ws.OrganizationID,
22952295
WorkspaceOwnerID:ws.OwnerID,
22962296
WorkspaceName:ws.Name,
22972297
Type:database.ConnectionTypeSsh,
2298-
Code:0,
22992298
}
23002299

23012300
log2,err:=db.UpsertConnectionLog(ctx,disconnectParams)
@@ -2334,9 +2333,8 @@ func TestUpsertConnectionLog(t *testing.T) {
23342333
WorkspaceName:ws.Name,
23352334
AgentName:agentName,
23362335
Type:database.ConnectionTypeSsh,
2337-
Code:0,
23382336
ConnectionID: uuid.NullUUID{UUID:connectionID,Valid:true},
2339-
ConnectionAction:database.ConnectionActionConnect,
2337+
ConnectionStatus:database.ConnectionStatusConnected,
23402338
}
23412339

23422340
log,err:=db.UpsertConnectionLog(ctx,connectParams)
@@ -2348,7 +2346,7 @@ func TestUpsertConnectionLog(t *testing.T) {
23482346
ConnectionID: uuid.NullUUID{UUID:connectionID,Valid:true},
23492347
WorkspaceID:ws.ID,
23502348
AgentName:agentName,
2351-
ConnectionAction:database.ConnectionActionConnect,
2349+
ConnectionStatus:database.ConnectionStatusConnected,
23522350

23532351
// Ignored
23542352
ID:uuid.New(),
@@ -2357,7 +2355,7 @@ func TestUpsertConnectionLog(t *testing.T) {
23572355
WorkspaceOwnerID:ws.OwnerID,
23582356
WorkspaceName:ws.Name,
23592357
Type:database.ConnectionTypeSsh,
2360-
Code:0,
2358+
Code:sql.NullInt32{Int32:0,Valid:false},
23612359
}
23622360

23632361
origLog,err:=db.UpsertConnectionLog(ctx,connectParams2)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp