Skip to content

Commit 4616ccf

Browse files
authored
fix(coderd): alter return signature of convertWorkspace, add check for requesterID (#11796)
1 parent 70dc282 commit 4616ccf

File tree

1 file changed

+73
-20
lines changed

1 file changed

+73
-20
lines changed

coderd/workspaces.go

+73-20
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,23 @@ func (api *API) workspace(rw http.ResponseWriter, r *http.Request) {
102102
})
103103
return
104104
}
105-
httpapi.Write(ctx, rw, http.StatusOK, convertWorkspace(
105+
106+
w, err := convertWorkspace(
106107
apiKey.UserID,
107108
workspace,
108109
data.builds[0],
109110
data.templates[0],
110111
ownerName,
111112
api.Options.AllowWorkspaceRenames,
112-
))
113+
)
114+
if err != nil {
115+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
116+
Message: "Internal error converting workspace.",
117+
Detail: err.Error(),
118+
})
119+
return
120+
}
121+
httpapi.Write(ctx, rw, http.StatusOK, w)
113122
}
114123

115124
// workspaces returns all workspaces a user can read.
@@ -280,14 +289,22 @@ func (api *API) workspaceByOwnerAndName(rw http.ResponseWriter, r *http.Request)
280289
})
281290
return
282291
}
283-
httpapi.Write(ctx, rw, http.StatusOK, convertWorkspace(
292+
w, err := convertWorkspace(
284293
apiKey.UserID,
285294
workspace,
286295
data.builds[0],
287296
data.templates[0],
288297
ownerName,
289298
api.Options.AllowWorkspaceRenames,
290-
))
299+
)
300+
if err != nil {
301+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
302+
Message: "Internal error converting workspace.",
303+
Detail: err.Error(),
304+
})
305+
return
306+
}
307+
httpapi.Write(ctx, rw, http.StatusOK, w)
291308
}
292309

293310
// Create a new workspace for the currently authenticated user.
@@ -590,14 +607,22 @@ func (api *API) postWorkspacesByOrganization(rw http.ResponseWriter, r *http.Req
590607
return
591608
}
592609

593-
httpapi.Write(ctx, rw, http.StatusCreated, convertWorkspace(
610+
w, err := convertWorkspace(
594611
apiKey.UserID,
595612
workspace,
596613
apiBuild,
597614
template,
598615
member.Username,
599616
api.Options.AllowWorkspaceRenames,
600-
))
617+
)
618+
if err != nil {
619+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
620+
Message: "Internal error converting workspace.",
621+
Detail: err.Error(),
622+
})
623+
return
624+
}
625+
httpapi.Write(ctx, rw, http.StatusCreated, w)
601626
}
602627

603628
// @Summary Update workspace metadata by ID
@@ -931,14 +956,23 @@ func (api *API) putWorkspaceDormant(rw http.ResponseWriter, r *http.Request) {
931956
}
932957

933958
aReq.New = workspace
934-
httpapi.Write(ctx, rw, http.StatusOK, convertWorkspace(
959+
960+
w, err := convertWorkspace(
935961
apiKey.UserID,
936962
workspace,
937963
data.builds[0],
938964
data.templates[0],
939965
ownerName,
940966
api.Options.AllowWorkspaceRenames,
941-
))
967+
)
968+
if err != nil {
969+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
970+
Message: "Internal error converting workspace.",
971+
Detail: err.Error(),
972+
})
973+
return
974+
}
975+
httpapi.Write(ctx, rw, http.StatusOK, w)
942976
}
943977

944978
// @Summary Extend workspace deadline by ID
@@ -1349,16 +1383,27 @@ func (api *API) watchWorkspace(rw http.ResponseWriter, r *http.Request) {
13491383
})
13501384
return
13511385
}
1386+
1387+
w, err := convertWorkspace(
1388+
apiKey.UserID,
1389+
workspace,
1390+
data.builds[0],
1391+
data.templates[0],
1392+
ownerName,
1393+
api.Options.AllowWorkspaceRenames,
1394+
)
1395+
if err != nil {
1396+
_ = sendEvent(ctx, codersdk.ServerSentEvent{
1397+
Type: codersdk.ServerSentEventTypeError,
1398+
Data: codersdk.Response{
1399+
Message: "Internal error converting workspace.",
1400+
Detail: err.Error(),
1401+
},
1402+
})
1403+
}
13521404
_ = sendEvent(ctx, codersdk.ServerSentEvent{
13531405
Type: codersdk.ServerSentEventTypeData,
1354-
Data: convertWorkspace(
1355-
apiKey.UserID,
1356-
workspace,
1357-
data.builds[0],
1358-
data.templates[0],
1359-
ownerName,
1360-
api.Options.AllowWorkspaceRenames,
1361-
),
1406+
Data: w,
13621407
})
13631408
}
13641409

@@ -1505,14 +1550,19 @@ func convertWorkspaces(requesterID uuid.UUID, workspaces []database.Workspace, d
15051550
continue
15061551
}
15071552

1508-
apiWorkspaces = append(apiWorkspaces, convertWorkspace(
1553+
w, err := convertWorkspace(
15091554
requesterID,
15101555
workspace,
15111556
build,
15121557
template,
15131558
owner.Username,
15141559
data.allowRenames,
1515-
))
1560+
)
1561+
if err != nil {
1562+
return nil, xerrors.Errorf("convert workspace: %w", err)
1563+
}
1564+
1565+
apiWorkspaces = append(apiWorkspaces, w)
15161566
}
15171567
return apiWorkspaces, nil
15181568
}
@@ -1524,7 +1574,10 @@ func convertWorkspace(
15241574
template database.Template,
15251575
ownerName string,
15261576
allowRenames bool,
1527-
) codersdk.Workspace {
1577+
) (codersdk.Workspace, error) {
1578+
if requesterID == uuid.Nil {
1579+
return codersdk.Workspace{}, xerrors.Errorf("developer error: requesterID cannot be uuid.Nil!")
1580+
}
15281581
var autostartSchedule *string
15291582
if workspace.AutostartSchedule.Valid {
15301583
autostartSchedule = &workspace.AutostartSchedule.String
@@ -1583,7 +1636,7 @@ func convertWorkspace(
15831636
AutomaticUpdates: codersdk.AutomaticUpdates(workspace.AutomaticUpdates),
15841637
AllowRenames: allowRenames,
15851638
Favorite: requesterFavorite,
1586-
}
1639+
}, nil
15871640
}
15881641

15891642
func convertWorkspaceTTLMillis(i sql.NullInt64) *int64 {

0 commit comments

Comments
 (0)