Skip to content

Commit 5bb42c2

Browse files
committed
add slug or port to support separation of terminal and ports
1 parent 9608da1 commit 5bb42c2

File tree

4 files changed

+32
-16
lines changed

4 files changed

+32
-16
lines changed

coderd/database/dbmem/dbmem.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9262,13 +9262,14 @@ func (q *FakeQuerier) InsertWorkspaceAppAuditSession(_ context.Context, arg data
92629262

92639263
id := uuid.New()
92649264
q.workspaceAppAuditSessions = append(q.workspaceAppAuditSessions, database.WorkspaceAppAuditSession{
9265-
ID: id,
9266-
AgentID: arg.AgentID,
9267-
AppID: arg.AppID,
9268-
UserID: arg.UserID,
9269-
Ip: arg.Ip,
9270-
StartedAt: arg.StartedAt,
9271-
UpdatedAt: arg.UpdatedAt,
9265+
ID: id,
9266+
AgentID: arg.AgentID,
9267+
AppID: arg.AppID,
9268+
UserID: arg.UserID,
9269+
Ip: arg.Ip,
9270+
SlugOrPort: arg.SlugOrPort,
9271+
StartedAt: arg.StartedAt,
9272+
UpdatedAt: arg.UpdatedAt,
92729273
})
92739274

92749275
return id, nil
@@ -11043,6 +11044,9 @@ func (q *FakeQuerier) UpdateWorkspaceAppAuditSession(_ context.Context, arg data
1104311044
if s.Ip.IPNet.String() != arg.Ip.IPNet.String() {
1104411045
continue
1104511046
}
11047+
if s.SlugOrPort != arg.SlugOrPort {
11048+
continue
11049+
}
1104611050
staleTime := dbtime.Now().Add(-(time.Duration(arg.StaleIntervalMS) * time.Millisecond))
1104711051
if !s.UpdatedAt.After(staleTime) {
1104811052
continue

coderd/database/migrations/000301_add_workspace_app_audit_sessions.up.sql

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ CREATE UNLOGGED TABLE workspace_app_audit_sessions (
22
id UUID PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(),
33
agent_id UUID NOT NULL,
44
app_id UUID NULL,
5-
user_id UUID,
6-
ip inet,
5+
user_id UUID NULL,
6+
ip inet NOT NULL,
7+
slug_or_port TEXT NOT NULL,
78
started_at TIMESTAMP WITH TIME ZONE NOT NULL,
89
updated_at TIMESTAMP WITH TIME ZONE NOT NULL,
910
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE,
@@ -13,13 +14,14 @@ CREATE UNLOGGED TABLE workspace_app_audit_sessions (
1314

1415
COMMENT ON TABLE workspace_app_audit_sessions IS 'Audit sessions for workspace apps, the data in this table is ephemeral and is used to track the current session of a user in a workspace app.';
1516
COMMENT ON COLUMN workspace_app_audit_sessions.id IS 'Unique identifier for the workspace app audit session.';
16-
COMMENT ON COLUMN workspace_app_audit_sessions.user_id IS 'The user that is currently using the workspace app. This is nullable because the app may be public.';
17-
COMMENT ON COLUMN workspace_app_audit_sessions.ip IS 'The IP address of the user that is currently using the workspace app.';
1817
COMMENT ON COLUMN workspace_app_audit_sessions.agent_id IS 'The agent that is currently in the workspace app.';
1918
COMMENT ON COLUMN workspace_app_audit_sessions.app_id IS 'The app that is currently in the workspace app. This is nullable because ports are not associated with an app.';
19+
COMMENT ON COLUMN workspace_app_audit_sessions.user_id IS 'The user that is currently using the workspace app. This is nullable because the app may be public.';
20+
COMMENT ON COLUMN workspace_app_audit_sessions.ip IS 'The IP address of the user that is currently using the workspace app.';
21+
COMMENT ON COLUMN workspace_app_audit_sessions.slug_or_port IS 'The slug or port of the workspace app that the user is currently using.';
2022
COMMENT ON COLUMN workspace_app_audit_sessions.started_at IS 'The time the user started the session.';
2123
COMMENT ON COLUMN workspace_app_audit_sessions.updated_at IS 'The time the session was last updated.';
2224

23-
CREATE INDEX workspace_app_audit_sessions_agent_id_app_id ON workspace_app_audit_sessions (agent_id, app_id);
25+
CREATE INDEX workspace_app_audit_sessions_agent_id_app_id_slug_or_port ON workspace_app_audit_sessions (agent_id, app_id, slug_or_port);
2426

25-
COMMENT ON INDEX workspace_app_audit_sessions_agent_id_app_id IS 'Index for the agent_id and app_id columns to perform updates.';
27+
COMMENT ON INDEX workspace_app_audit_sessions_agent_id_app_id_slug_or_port IS 'Index for the agent_id and app_id columns to perform updates.';

coderd/database/queries/workspaceappaudit.sql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ INSERT INTO
55
app_id,
66
user_id,
77
ip,
8+
slug_or_port,
89
started_at,
910
updated_at
1011
)
@@ -15,7 +16,8 @@ VALUES
1516
$3,
1617
$4,
1718
$5,
18-
$6
19+
$6,
20+
$7
1921
)
2022
RETURNING
2123
id;
@@ -33,6 +35,7 @@ WHERE
3335
AND app_id IS NOT DISTINCT FROM @app_id
3436
AND user_id IS NOT DISTINCT FROM @user_id
3537
AND ip IS NOT DISTINCT FROM @ip
38+
AND slug_or_port = @slug_or_port
3639
AND updated_at > NOW() - (@stale_interval_ms::bigint || ' ms')::interval
3740
RETURNING
3841
id;

coderd/workspaceapps/db.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,15 +414,21 @@ func (p *DBTokenProvider) auditInitAutocommitRequest(ctx context.Context, w http
414414

415415
type additionalFields struct {
416416
audit.AdditionalFields
417-
App string `json:"app"`
417+
SlugOrPort string `json:"slug_or_port,omitempty"`
418418
}
419419
appInfo := additionalFields{
420420
AdditionalFields: audit.AdditionalFields{
421421
WorkspaceOwner: aReq.dbReq.Workspace.OwnerUsername,
422422
WorkspaceName: aReq.dbReq.Workspace.Name,
423423
WorkspaceID: aReq.dbReq.Workspace.ID,
424424
},
425-
App: aReq.dbReq.AppSlugOrPort,
425+
}
426+
switch {
427+
case aReq.dbReq.AccessMethod == AccessMethodTerminal:
428+
appInfo.SlugOrPort = "terminal"
429+
case aReq.dbReq.App.ID == uuid.Nil:
430+
// If this isn't an app or a terminal, it's a port.
431+
appInfo.SlugOrPort = aReq.dbReq.AppSlugOrPort
426432
}
427433

428434
appInfoBytes, err := json.Marshal(appInfo)
@@ -448,6 +454,7 @@ func (p *DBTokenProvider) auditInitAutocommitRequest(ctx context.Context, w http
448454
AppID: uuid.NullUUID{Valid: aReq.dbReq.App.ID != uuid.Nil, UUID: aReq.dbReq.App.ID},
449455
UserID: userID,
450456
Ip: aReq.ip,
457+
SlugOrPort: appInfo.SlugOrPort,
451458
UpdatedAt: aReq.time,
452459
StaleIntervalMS: p.WorkspaceAppAuditSessionTimeout.Milliseconds(),
453460
})

0 commit comments

Comments
 (0)