Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cde0b1c

Browse files
committed
change interface
1 parent 04d2bec commit cde0b1c

File tree

6 files changed

+48
-34
lines changed

6 files changed

+48
-34
lines changed

coderd/portsharing/portsharing.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
package portsharing
22

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

137
type AGPLPortSharer struct{}
148

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

1913
var DefaultPortSharer PortSharer = AGPLPortSharer{}

coderd/templates.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
533533
ctx = r.Context()
534534
template = httpmw.TemplateParam(r)
535535
auditor = *api.Auditor.Load()
536+
portSharer = *api.PortSharer.Load()
536537
aReq, commitAudit = audit.InitRequest[database.Template](rw, &audit.RequestParams{
537538
Audit: auditor,
538539
Log: api.Logger,
@@ -629,6 +630,19 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
629630
if req.TimeTilDormantAutoDeleteMillis < 0 || (req.TimeTilDormantAutoDeleteMillis > 0 && req.TimeTilDormantAutoDeleteMillis < minTTL) {
630631
validErrs = append(validErrs, codersdk.ValidationError{Field: "time_til_dormant_autodelete_ms", Detail: "Value must be at least one minute."})
631632
}
633+
if req.MaxPortSharingLevel != nil && *req.MaxPortSharingLevel < 0 || *req.MaxPortSharingLevel > 2 {
634+
if *req.MaxPortSharingLevel < 0 || *req.MaxPortSharingLevel > 2 {
635+
validErrs = append(validErrs, codersdk.ValidationError{Field: "max_port_sharing_level", Detail: "Value must be between 0 and 2."})
636+
} else {
637+
if !portSharer.CanRestrictSharing() {
638+
validErrs = append(validErrs, codersdk.ValidationError{Field: "max_port_sharing_level", Detail: "Restricting port sharing level is an enterprise feature that is not enabled."})
639+
}
640+
}
641+
}
642+
maxPortShareLevel := template.MaxPortSharingLevel
643+
if req.MaxPortSharingLevel != nil {
644+
maxPortShareLevel = *req.MaxPortSharingLevel
645+
}
632646

633647
if len(validErrs) > 0 {
634648
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
@@ -657,7 +671,8 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
657671
req.TimeTilDormantMillis == time.Duration(template.TimeTilDormant).Milliseconds() &&
658672
req.TimeTilDormantAutoDeleteMillis == time.Duration(template.TimeTilDormantAutoDelete).Milliseconds() &&
659673
req.RequireActiveVersion == template.RequireActiveVersion &&
660-
(deprecationMessage == template.Deprecated) {
674+
(deprecationMessage == template.Deprecated) &&
675+
maxPortShareLevel == template.MaxPortSharingLevel {
661676
return nil
662677
}
663678

@@ -682,6 +697,7 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
682697
Icon: req.Icon,
683698
AllowUserCancelWorkspaceJobs: req.AllowUserCancelWorkspaceJobs,
684699
GroupACL: groupACL,
700+
MaxPortSharingLevel: maxPortShareLevel,
685701
})
686702
if err != nil {
687703
return xerrors.Errorf("update template metadata: %w", err)

coderd/workspaceportsharing.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,19 @@ func (api *API) postWorkspacePortShareLevel(rw http.ResponseWriter, r *http.Requ
2929
return
3030
}
3131

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
32+
if portSharer.CanRestrictSharing() {
33+
template, err := api.Database.GetTemplateByID(ctx, workspace.TemplateID)
34+
if err != nil {
35+
httpapi.InternalServerError(rw, err)
36+
return
37+
}
38+
39+
if req.ShareLevel > template.MaxPortSharingLevel {
40+
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
41+
Message: "Port sharing level not allowed.",
42+
})
43+
return
44+
}
3845
}
3946

4047
agents, err := api.Database.GetWorkspaceAgentsInLatestBuildByWorkspaceID(ctx, workspace.ID)
@@ -60,15 +67,15 @@ func (api *API) postWorkspacePortShareLevel(rw http.ResponseWriter, r *http.Requ
6067
psl, err := api.Database.GetWorkspaceAgentPortShare(ctx, database.GetWorkspaceAgentPortShareParams{
6168
WorkspaceID: workspace.ID,
6269
AgentName: req.AgentName,
63-
Port: int32(req.Port),
70+
Port: req.Port,
6471
})
6572
if err != nil {
6673
if errors.Is(err, sql.ErrNoRows) {
6774
httpapi.InternalServerError(rw, err)
6875
return
6976
}
7077

71-
if req.ShareLevel == int(codersdk.WorkspaceAgentPortSharingLevelOwner) {
78+
if req.ShareLevel == int32(codersdk.WorkspaceAgentPortSharingLevelOwner) {
7279
// If the port is not shared, and the user is trying to set it to owner,
7380
// we don't need to do anything.
7481
rw.WriteHeader(http.StatusOK)
@@ -78,8 +85,8 @@ func (api *API) postWorkspacePortShareLevel(rw http.ResponseWriter, r *http.Requ
7885
err = api.Database.CreateWorkspaceAgentPortShare(ctx, database.CreateWorkspaceAgentPortShareParams{
7986
WorkspaceID: workspace.ID,
8087
AgentName: req.AgentName,
81-
Port: int32(req.Port),
82-
ShareLevel: int32(req.ShareLevel),
88+
Port: req.Port,
89+
ShareLevel: req.ShareLevel,
8390
})
8491
if err != nil {
8592
httpapi.InternalServerError(rw, err)
@@ -96,7 +103,7 @@ func (api *API) postWorkspacePortShareLevel(rw http.ResponseWriter, r *http.Requ
96103
err = api.Database.DeleteWorkspaceAgentPortShare(ctx, database.DeleteWorkspaceAgentPortShareParams{
97104
WorkspaceID: workspace.ID,
98105
AgentName: req.AgentName,
99-
Port: int32(req.Port),
106+
Port: req.Port,
100107
})
101108
if err != nil {
102109
httpapi.InternalServerError(rw, err)
@@ -111,7 +118,7 @@ func (api *API) postWorkspacePortShareLevel(rw http.ResponseWriter, r *http.Requ
111118
WorkspaceID: psl.WorkspaceID,
112119
AgentName: psl.AgentName,
113120
Port: psl.Port,
114-
ShareLevel: int32(req.ShareLevel),
121+
ShareLevel: req.ShareLevel,
115122
})
116123
if err != nil {
117124
httpapi.InternalServerError(rw, err)

codersdk/templates.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ type Template struct {
6161
// RequireActiveVersion mandates that workspaces are built with the active
6262
// template version.
6363
RequireActiveVersion bool `json:"require_active_version"`
64+
MaxPortShareLevel int `json:"max_port_share_level"`
6465
}
6566

6667
// WeekdaysToBitmap converts a list of weekdays to a bitmap in accordance with
@@ -246,7 +247,8 @@ type UpdateTemplateMeta struct {
246247
// If this is set to true, the template will not be available to all users,
247248
// and must be explicitly granted to users or groups in the permissions settings
248249
// of the template.
249-
DisableEveryoneGroupAccess bool `json:"disable_everyone_group_access"`
250+
DisableEveryoneGroupAccess bool `json:"disable_everyone_group_access"`
251+
MaxPortSharingLevel *int32 `json:"max_port_sharing_level"`
250252
}
251253

252254
type TemplateExample struct {

codersdk/workspaceportsharing.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ const (
99
type WorkspacePortSharingLevel int
1010
type UpdateWorkspaceAgentPortSharingLevelRequest struct {
1111
AgentName string `json:"agent_name"`
12-
Port int `json:"port"`
13-
ShareLevel int `json:"share_level"`
12+
Port int32 `json:"port"`
13+
ShareLevel int32 `json:"share_level"`
1414
}
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
package portsharing
22

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

116
func NewEnterprisePortSharer() *EnterprisePortSharer {
127
return &EnterprisePortSharer{}
138
}
149

15-
func (EnterprisePortSharer) ShareLevelAllowed(_ uuid.UUID, _ codersdk.WorkspacePortSharingLevel) bool {
16-
return false
10+
func (EnterprisePortSharer) CanRestrictSharing() bool {
11+
return true
1712
}

0 commit comments

Comments
 (0)