Skip to content

Commit 4150a99

Browse files
committed
fix: prevent deleted workspaces from updating agent stats
1 parent 185400d commit 4150a99

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

coderd/database/dbauthz/dbauthz.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ var (
228228
rbac.ResourceOrgRoleAssignment.Type: {rbac.ActionCreate},
229229
rbac.ResourceUser.Type: {rbac.ActionCreate, rbac.ActionUpdate, rbac.ActionDelete},
230230
rbac.ResourceUserData.Type: {rbac.ActionCreate, rbac.ActionUpdate},
231-
rbac.ResourceWorkspace.Type: {rbac.ActionUpdate},
231+
rbac.ResourceWorkspace.Type: {rbac.ActionUpdate, rbac.ActionDelete},
232232
rbac.ResourceWorkspaceBuild.Type: {rbac.ActionUpdate},
233233
rbac.ResourceWorkspaceExecution.Type: {rbac.ActionCreate},
234234
rbac.ResourceWorkspaceProxy.Type: {rbac.ActionCreate, rbac.ActionUpdate, rbac.ActionDelete},

coderd/workspaceagents.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,6 +1653,13 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques
16531653
return
16541654
}
16551655

1656+
if workspace.Deleted {
1657+
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
1658+
Message: "Workspace has been deleted.",
1659+
})
1660+
return
1661+
}
1662+
16561663
var req agentsdk.Stats
16571664
if !httpapi.Read(ctx, rw, r, &req) {
16581665
return

coderd/workspaceagents_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/coder/coder/v2/coderd"
2727
"github.com/coder/coder/v2/coderd/coderdtest"
2828
"github.com/coder/coder/v2/coderd/database"
29+
"github.com/coder/coder/v2/coderd/database/dbauthz"
2930
"github.com/coder/coder/v2/coderd/database/dbfake"
3031
"github.com/coder/coder/v2/coderd/database/dbmem"
3132
"github.com/coder/coder/v2/coderd/database/dbtime"
@@ -876,6 +877,62 @@ func TestWorkspaceAgentReportStats(t *testing.T) {
876877
"%s is not after %s", newWorkspace.LastUsedAt, r.Workspace.LastUsedAt,
877878
)
878879
})
880+
881+
t.Run("FailDeleted", func(t *testing.T) {
882+
t.Parallel()
883+
884+
client, db := coderdtest.NewWithDatabase(t, nil)
885+
user := coderdtest.CreateFirstUser(t, client)
886+
r := dbfake.WorkspaceBuild(t, db, database.Workspace{
887+
OrganizationID: user.OrganizationID,
888+
OwnerID: user.UserID,
889+
}).WithAgent().Do()
890+
891+
agentClient := agentsdk.New(client.URL)
892+
agentClient.SetSessionToken(r.AgentToken)
893+
894+
_, err := agentClient.PostStats(context.Background(), &agentsdk.Stats{
895+
ConnectionsByProto: map[string]int64{"TCP": 1},
896+
// Set connection count to 1 but all session counts to zero to
897+
// assert we aren't updating last_used_at for a connections that may
898+
// be spawned passively by the dashboard.
899+
ConnectionCount: 1,
900+
RxPackets: 1,
901+
RxBytes: 1,
902+
TxPackets: 1,
903+
TxBytes: 1,
904+
SessionCountVSCode: 0,
905+
SessionCountJetBrains: 0,
906+
SessionCountReconnectingPTY: 0,
907+
SessionCountSSH: 0,
908+
ConnectionMedianLatencyMS: 10,
909+
})
910+
require.NoError(t, err)
911+
912+
newWorkspace, err := client.Workspace(context.Background(), r.Workspace.ID)
913+
require.NoError(t, err)
914+
915+
err = db.UpdateWorkspaceDeletedByID(dbauthz.AsSystemRestricted(context.Background()), database.UpdateWorkspaceDeletedByIDParams{
916+
ID: newWorkspace.ID,
917+
Deleted: true,
918+
})
919+
require.NoError(t, err)
920+
921+
_, err = agentClient.PostStats(context.Background(), &agentsdk.Stats{
922+
ConnectionsByProto: map[string]int64{"TCP": 1},
923+
ConnectionCount: 1,
924+
RxPackets: 1,
925+
RxBytes: 1,
926+
TxPackets: 1,
927+
TxBytes: 1,
928+
SessionCountVSCode: 1,
929+
SessionCountJetBrains: 0,
930+
SessionCountReconnectingPTY: 0,
931+
SessionCountSSH: 0,
932+
ConnectionMedianLatencyMS: 10,
933+
})
934+
require.ErrorContains(t, err, "Workspace has been deleted.")
935+
})
879936
}
880937

881938
func TestWorkspaceAgent_LifecycleState(t *testing.T) {

0 commit comments

Comments
 (0)