-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathauthorize_test.go
More file actions
276 lines (248 loc) · 8.98 KB
/
Copy pathauthorize_test.go
File metadata and controls
276 lines (248 loc) · 8.98 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package coderd_test
import (
"context"
"fmt"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/coderd/coderdtest"
"github.com/coder/coder/v2/coderd/rbac"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/testutil"
)
func TestCheckPermissions(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
t.Cleanup(cancel)
adminClient := coderdtest.New(t, &coderdtest.Options{
IncludeProvisionerDaemon: true,
})
// Create adminClient, member, and org adminClient
adminUser := coderdtest.CreateFirstUser(t, adminClient)
memberClient, _ := coderdtest.CreateAnotherUser(t, adminClient, adminUser.OrganizationID)
memberUser, err := memberClient.User(ctx, codersdk.Me)
require.NoError(t, err)
orgAdminClient, _ := coderdtest.CreateAnotherUser(t, adminClient, adminUser.OrganizationID, rbac.ScopedRoleOrgAdmin(adminUser.OrganizationID))
orgAdminUser, err := orgAdminClient.User(ctx, codersdk.Me)
require.NoError(t, err)
version := coderdtest.CreateTemplateVersion(t, adminClient, adminUser.OrganizationID, nil)
coderdtest.AwaitTemplateVersionJobCompleted(t, adminClient, version.ID)
template := coderdtest.CreateTemplate(t, adminClient, adminUser.OrganizationID, version.ID)
// With admin, member, and org admin
const (
readAllUsers = "read-all-users"
readOrgWorkspaces = "read-org-workspaces"
readMyself = "read-myself"
readOwnWorkspaces = "read-own-workspaces"
updateSpecificTemplate = "update-specific-template"
)
params := map[string]codersdk.AuthorizationCheck{
readAllUsers: {
Object: codersdk.AuthorizationObject{
ResourceType: codersdk.ResourceUser,
},
Action: "read",
},
readOrgWorkspaces: {
Object: codersdk.AuthorizationObject{
ResourceType: codersdk.ResourceWorkspace,
OrganizationID: adminUser.OrganizationID.String(),
},
Action: "read",
},
readMyself: {
Object: codersdk.AuthorizationObject{
ResourceType: codersdk.ResourceUser,
OwnerID: "me",
},
Action: "read",
},
readOwnWorkspaces: {
Object: codersdk.AuthorizationObject{
ResourceType: codersdk.ResourceWorkspace,
OrganizationID: adminUser.OrganizationID.String(),
OwnerID: "me",
},
Action: "read",
},
updateSpecificTemplate: {
Object: codersdk.AuthorizationObject{
ResourceType: codersdk.ResourceTemplate,
ResourceID: template.ID.String(),
},
Action: "update",
},
}
testCases := []struct {
Name string
Client *codersdk.Client
UserID uuid.UUID
Check codersdk.AuthorizationResponse
}{
{
Name: "Admin",
Client: adminClient,
UserID: adminUser.UserID,
Check: map[string]bool{
readAllUsers: true,
readOrgWorkspaces: true,
readMyself: true,
readOwnWorkspaces: true,
updateSpecificTemplate: true,
},
},
{
Name: "OrgAdmin",
Client: orgAdminClient,
UserID: orgAdminUser.ID,
Check: map[string]bool{
readAllUsers: true,
readOrgWorkspaces: true,
readMyself: true,
readOwnWorkspaces: true,
updateSpecificTemplate: true,
},
},
{
Name: "Member",
Client: memberClient,
UserID: memberUser.ID,
Check: map[string]bool{
readAllUsers: false,
readOrgWorkspaces: false,
readMyself: true,
readOwnWorkspaces: true,
updateSpecificTemplate: false,
},
},
}
for _, c := range testCases {
t.Run("CheckAuthorization/"+c.Name, func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
t.Cleanup(cancel)
resp, err := c.Client.AuthCheck(ctx, codersdk.AuthorizationRequest{Checks: params})
require.NoError(t, err, "check perms")
require.Equal(t, c.Check, resp)
})
}
// Enough same-typed checks in one request to push a group past the batching
// threshold, exercising the grouped partial-evaluation path and the key
// mapping. Reading org members is allowed for any member; updating them is
// admin-only, so the two actions must map back to their keys distinctly.
t.Run("CheckAuthorization/Grouped", func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
t.Cleanup(cancel)
grouped := make(map[string]codersdk.AuthorizationCheck)
adminExpected := make(map[string]bool)
memberExpected := make(map[string]bool)
for i := 0; i < 55; i++ {
readKey := fmt.Sprintf("read-members-%d", i)
grouped[readKey] = codersdk.AuthorizationCheck{
Object: codersdk.AuthorizationObject{
ResourceType: codersdk.ResourceOrganizationMember,
OrganizationID: adminUser.OrganizationID.String(),
},
Action: "read",
}
adminExpected[readKey] = true
memberExpected[readKey] = true
updateKey := fmt.Sprintf("update-members-%d", i)
grouped[updateKey] = codersdk.AuthorizationCheck{
Object: codersdk.AuthorizationObject{
ResourceType: codersdk.ResourceOrganizationMember,
OrganizationID: adminUser.OrganizationID.String(),
},
Action: "update",
}
adminExpected[updateKey] = true
memberExpected[updateKey] = false
}
adminResp, err := adminClient.AuthCheck(ctx, codersdk.AuthorizationRequest{Checks: grouped})
require.NoError(t, err)
require.Equal(t, adminExpected, map[string]bool(adminResp))
memberResp, err := memberClient.AuthCheck(ctx, codersdk.AuthorizationRequest{Checks: grouped})
require.NoError(t, err)
require.Equal(t, memberExpected, map[string]bool(memberResp))
})
// A member may create a workspace in an org it belongs to, so an
// any_org=true check is allowed. Repeating it past the batching threshold
// must not change the answer: AnyOrgOwner objects have no verified partial-
// evaluation semantics, so they must stay on the per-object path rather than
// being grouped into rbac.Filter's prepared query, which denies them.
t.Run("CheckAuthorization/AnyOrg", func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
t.Cleanup(cancel)
check := codersdk.AuthorizationCheck{
Object: codersdk.AuthorizationObject{
ResourceType: codersdk.ResourceWorkspace,
OwnerID: "me",
AnyOrgOwner: true,
},
Action: "create",
}
// Below the threshold: a single check is evaluated in full and allowed.
single, err := memberClient.AuthCheck(ctx, codersdk.AuthorizationRequest{
Checks: map[string]codersdk.AuthorizationCheck{"create-any-org": check},
})
require.NoError(t, err)
require.True(t, single["create-any-org"], "single any_org check should be allowed")
// The same check repeated past the threshold must stay allowed.
grouped := make(map[string]codersdk.AuthorizationCheck)
expected := make(map[string]bool)
for i := 0; i < 55; i++ {
key := fmt.Sprintf("create-any-org-%d", i)
grouped[key] = check
expected[key] = true
}
resp, err := memberClient.AuthCheck(ctx, codersdk.AuthorizationRequest{Checks: grouped})
require.NoError(t, err)
require.Equal(t, expected, map[string]bool(resp))
})
// A single (read, workspace) group past the batching threshold mixing
// resource_id checks (concrete workspaces fetched via the maxFetch path)
// with org-scoped checks (synthetic org-level objects). The member owns the
// fetched workspace so those keys are allowed, but cannot read all org
// workspaces so the org-scoped keys are denied. Both object shapes must map
// back to the correct per-key verdict through the one prepared query.
t.Run("CheckAuthorization/MixedGroup", func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
t.Cleanup(cancel)
workspace := coderdtest.CreateWorkspace(t, memberClient, template.ID)
coderdtest.AwaitWorkspaceBuildJobCompleted(t, memberClient, workspace.LatestBuild.ID)
grouped := make(map[string]codersdk.AuthorizationCheck)
expected := make(map[string]bool)
// resource_id checks are capped at maxFetch (10); the member owns the
// workspace, so reading it is allowed.
for i := 0; i < 10; i++ {
key := fmt.Sprintf("read-own-workspace-%d", i)
grouped[key] = codersdk.AuthorizationCheck{
Object: codersdk.AuthorizationObject{
ResourceType: codersdk.ResourceWorkspace,
ResourceID: workspace.ID.String(),
},
Action: "read",
}
expected[key] = true
}
// Org-scoped checks push the (read, workspace) group past the threshold.
// A member cannot read every workspace in the org, so these are denied.
for i := 0; i < 45; i++ {
key := fmt.Sprintf("read-org-workspace-%d", i)
grouped[key] = codersdk.AuthorizationCheck{
Object: codersdk.AuthorizationObject{
ResourceType: codersdk.ResourceWorkspace,
OrganizationID: adminUser.OrganizationID.String(),
},
Action: "read",
}
expected[key] = false
}
resp, err := memberClient.AuthCheck(ctx, codersdk.AuthorizationRequest{Checks: grouped})
require.NoError(t, err)
require.Equal(t, expected, map[string]bool(resp))
})
}