-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathworkspacesharing.go
More file actions
194 lines (175 loc) · 6.67 KB
/
Copy pathworkspacesharing.go
File metadata and controls
194 lines (175 loc) · 6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package coderd
import (
"fmt"
"net/http"
"strings"
"golang.org/x/xerrors"
"github.com/coder/coder/v2/coderd/audit"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbauthz"
"github.com/coder/coder/v2/coderd/database/dbtime"
"github.com/coder/coder/v2/coderd/httpapi"
"github.com/coder/coder/v2/coderd/httpmw"
"github.com/coder/coder/v2/coderd/rbac"
"github.com/coder/coder/v2/coderd/rbac/policy"
"github.com/coder/coder/v2/coderd/rbac/rolestore"
"github.com/coder/coder/v2/coderd/util/slice"
"github.com/coder/coder/v2/codersdk"
)
// @Summary Get workspace sharing settings for organization
// @ID get-workspace-sharing-settings-for-organization
// @Security CoderSessionToken
// @Produce json
// @Tags Enterprise
// @Param organization path string true "Organization ID" format(uuid)
// @Success 200 {object} codersdk.WorkspaceSharingSettings
// @Router /api/v2/organizations/{organization}/settings/workspace-sharing [get]
func (api *API) workspaceSharingSettings(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
org := httpmw.OrganizationParam(r)
if !api.Authorize(r, policy.ActionRead, org) {
httpapi.Forbidden(rw)
return
}
disabled := org.ShareableWorkspaceOwners == database.ShareableWorkspaceOwnersNone
globallyDisabled := bool(api.DeploymentValues.DisableWorkspaceSharing)
owners := codersdk.ShareableWorkspaceOwners(org.ShareableWorkspaceOwners)
if globallyDisabled {
owners = codersdk.ShareableWorkspaceOwnersNone
}
httpapi.Write(ctx, rw, http.StatusOK, codersdk.WorkspaceSharingSettings{
SharingGloballyDisabled: globallyDisabled,
SharingDisabled: disabled || globallyDisabled,
ShareableWorkspaceOwners: owners,
})
}
// @Summary Update workspace sharing settings for organization
// @ID update-workspace-sharing-settings-for-organization
// @Security CoderSessionToken
// @Produce json
// @Accept json
// @Tags Enterprise
// @Param organization path string true "Organization ID" format(uuid)
// @Param request body codersdk.UpdateWorkspaceSharingSettingsRequest true "Workspace sharing settings"
// @Success 200 {object} codersdk.WorkspaceSharingSettings
// @Router /api/v2/organizations/{organization}/settings/workspace-sharing [patch]
func (api *API) patchWorkspaceSharingSettings(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
org := httpmw.OrganizationParam(r)
auditor := *api.AGPL.Auditor.Load()
aReq, commitAudit := audit.InitRequest[database.Organization](rw, &audit.RequestParams{
Audit: auditor,
Log: api.Logger,
Request: r,
Action: database.AuditActionWrite,
OrganizationID: org.ID,
})
aReq.Old = org
defer commitAudit()
if !api.Authorize(r, policy.ActionUpdate, org) {
httpapi.Forbidden(rw)
return
}
var req codersdk.UpdateWorkspaceSharingSettingsRequest
if !httpapi.Read(ctx, rw, r, &req) {
return
}
// Resolve the effective enum value. Prefer the new field; fall
// back to the deprecated boolean for older clients (e.g
// tf-provider-coderd v0.0.16)
allowedOwners := req.ShareableWorkspaceOwners
if allowedOwners == "" {
if req.SharingDisabled {
allowedOwners = codersdk.ShareableWorkspaceOwnersNone
} else {
allowedOwners = codersdk.ShareableWorkspaceOwnersEveryone
}
}
if !database.ShareableWorkspaceOwners(allowedOwners).Valid() {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: "Invalid shareable workspace owners value.",
Validations: []codersdk.ValidationError{{
Field: "shareable_workspace_owners",
Detail: fmt.Sprintf("invalid value %q, must be one of [%s]",
allowedOwners,
strings.Join(slice.ToStrings(database.AllShareableWorkspaceOwnersValues()), ", ")),
}},
})
return
}
err := api.Database.InTx(func(tx database.Store) error {
//nolint:gocritic // System context required to look up and reconcile the
// system roles; callers only need `organization:update`
sysCtx := dbauthz.AsSystemRestricted(ctx)
// Serialize organization workspace-sharing updates with system role
// reconciliation across coderd instances (e.g. during rolling restarts).
// This prevents conflicting writes to the system roles.
// TODO(geokat): Consider finer-grained locks as we add more system roles.
err := tx.AcquireLock(ctx, database.LockIDReconcileSystemRoles)
if err != nil {
return xerrors.Errorf("acquire system roles reconciliation lock: %w", err)
}
org, err = tx.UpdateOrganizationWorkspaceSharingSettings(ctx, database.UpdateOrganizationWorkspaceSharingSettingsParams{
ID: org.ID,
ShareableWorkspaceOwners: database.ShareableWorkspaceOwners(allowedOwners),
UpdatedAt: dbtime.Now(),
})
if err != nil {
return xerrors.Errorf("update workspace sharing settings for organization %s: %w",
org.ID, err)
}
roles, err := tx.CustomRoles(sysCtx, database.CustomRolesParams{
LookupRoles: []database.NameOrganizationPair{
{
Name: rbac.RoleOrgMember(),
OrganizationID: org.ID,
},
{
Name: rbac.RoleOrgServiceAccount(),
OrganizationID: org.ID,
},
},
// Satisfy linter that requires all fields to be set.
OrganizationID: org.ID,
ExcludeOrgRoles: false,
IncludeSystemRoles: true,
})
if err != nil || len(roles) != 2 {
return xerrors.Errorf("get member and service-account roles for organization %s: %w",
org.ID, err)
}
for _, role := range roles {
_, _, err = rolestore.ReconcileSystemRole(sysCtx, tx, role, org)
if err != nil {
return xerrors.Errorf("reconcile %s role for organization %s: %w",
role.Name, org.ID, err)
}
}
// If sharing is not enabled, delete workspace ACLs to prevent
// ongoing shared use. In "service_accounts" mode, preserve
// ACLs on SA workspaces.
if org.ShareableWorkspaceOwners != database.ShareableWorkspaceOwnersEveryone {
err = tx.DeleteWorkspaceACLsByOrganization(sysCtx, database.DeleteWorkspaceACLsByOrganizationParams{
OrganizationID: org.ID,
ExcludeServiceAccounts: org.ShareableWorkspaceOwners == database.ShareableWorkspaceOwnersServiceAccounts,
})
if err != nil {
return xerrors.Errorf("delete workspace ACLs for organization %s: %w",
org.ID, err)
}
}
return nil
}, nil)
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error updating workspace sharing settings.",
Detail: err.Error(),
})
return
}
aReq.New = org
httpapi.Write(ctx, rw, http.StatusOK, codersdk.WorkspaceSharingSettings{
SharingDisabled: org.ShareableWorkspaceOwners == database.ShareableWorkspaceOwnersNone,
ShareableWorkspaceOwners: codersdk.ShareableWorkspaceOwners(org.ShareableWorkspaceOwners),
})
}