Skip to content

Commit ea91c1b

Browse files
committed
use port sharer
1 parent f8e7094 commit ea91c1b

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

coderd/portsharing/portsharing.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
package portsharing
22

3+
import (
4+
"github.com/google/uuid"
5+
6+
"github.com/coder/coder/v2/codersdk"
7+
)
8+
39
type PortSharer interface {
4-
CanRestrictShareLevel() bool
10+
ShareLevelAllowed(workspaceID uuid.UUID, level codersdk.WorkspacePortSharingLevel) bool
511
}
612

713
type AGPLPortSharer struct{}
814

9-
func (AGPLPortSharer) CanRestrictShareLevel() bool {
10-
return false
15+
func (AGPLPortSharer) ShareLevelAllowed(_ uuid.UUID, _ codersdk.WorkspacePortSharingLevel) bool {
16+
return true
1117
}
1218

1319
var DefaultPortSharer PortSharer = AGPLPortSharer{}

coderd/workspaceportsharing.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,20 @@ import (
2323
func (api *API) postWorkspacePortShareLevel(rw http.ResponseWriter, r *http.Request) {
2424
ctx := r.Context()
2525
workspace := httpmw.WorkspaceParam(r)
26+
portSharer := *api.PortSharer.Load()
2627
var req codersdk.UpdateWorkspaceAgentPortSharingLevelRequest
2728
if !httpapi.Read(ctx, rw, r, &req) {
2829
return
2930
}
3031

32+
shareLevelAllowed := portSharer.ShareLevelAllowed(workspace.ID, codersdk.WorkspacePortSharingLevel(req.ShareLevel))
33+
if !shareLevelAllowed {
34+
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
35+
Message: "Port sharing level not allowed.",
36+
})
37+
return
38+
}
39+
3140
agents, err := api.Database.GetWorkspaceAgentsInLatestBuildByWorkspaceID(ctx, workspace.ID)
3241
if err != nil {
3342
httpapi.InternalServerError(rw, err)
@@ -59,7 +68,12 @@ func (api *API) postWorkspacePortShareLevel(rw http.ResponseWriter, r *http.Requ
5968
return
6069
}
6170

62-
if (req.ShareLevel == codersdk.Shar)
71+
if req.ShareLevel == int(codersdk.WorkspaceAgentPortSharingLevelOwner) {
72+
// If the port is not shared, and the user is trying to set it to owner,
73+
// we don't need to do anything.
74+
rw.WriteHeader(http.StatusOK)
75+
return
76+
}
6377

6478
err = api.Database.CreateWorkspacePortShareLevel(ctx, database.CreateWorkspacePortShareLevelParams{
6579
WorkspaceID: workspace.ID,
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
package portsharing
22

3+
import (
4+
"github.com/google/uuid"
5+
6+
"github.com/coder/coder/v2/codersdk"
7+
)
8+
39
type EnterprisePortSharer struct{}
410

511
func NewEnterprisePortSharer() *EnterprisePortSharer {
612
return &EnterprisePortSharer{}
713
}
814

9-
func (EnterprisePortSharer) CanRestrictShareLevel() bool {
10-
return true
15+
func (EnterprisePortSharer) ShareLevelAllowed(_ uuid.UUID, _ codersdk.WorkspacePortSharingLevel) bool {
16+
return false
1117
}

0 commit comments

Comments
 (0)