1
1
package dbauthz_test
2
2
3
3
import (
4
- "encoding/json"
5
4
"testing"
6
5
7
6
"github.com/google/uuid"
@@ -11,10 +10,12 @@ import (
11
10
"cdr.dev/slog"
12
11
"github.com/coder/coder/v2/coderd/coderdtest"
13
12
"github.com/coder/coder/v2/coderd/database"
13
+ "github.com/coder/coder/v2/coderd/database/db2sdk"
14
14
"github.com/coder/coder/v2/coderd/database/dbauthz"
15
15
"github.com/coder/coder/v2/coderd/database/dbmem"
16
16
"github.com/coder/coder/v2/coderd/rbac"
17
17
"github.com/coder/coder/v2/coderd/rbac/policy"
18
+ "github.com/coder/coder/v2/codersdk"
18
19
"github.com/coder/coder/v2/testutil"
19
20
)
20
21
@@ -60,17 +61,21 @@ func TestUpsertCustomRoles(t *testing.T) {
60
61
return all
61
62
}
62
63
63
- orgID := uuid .New ()
64
+ orgID := uuid.NullUUID {
65
+ UUID : uuid .New (),
66
+ Valid : true ,
67
+ }
64
68
testCases := []struct {
65
69
name string
66
70
67
71
subject rbac.ExpandableRoles
68
72
69
73
// Perms to create on new custom role
70
- site []rbac.Permission
71
- org map [string ][]rbac.Permission
72
- user []rbac.Permission
73
- errorContains string
74
+ organizationID uuid.NullUUID
75
+ site []codersdk.Permission
76
+ org []codersdk.Permission
77
+ user []codersdk.Permission
78
+ errorContains string
74
79
}{
75
80
{
76
81
// No roles, so no assign role
@@ -84,144 +89,129 @@ func TestUpsertCustomRoles(t *testing.T) {
84
89
subject : merge (canAssignRole ),
85
90
},
86
91
{
87
- name : "mixed-scopes" ,
88
- subject : merge (canAssignRole , rbac .RoleOwner ()),
89
- site : rbac .Permissions (map [string ][]policy.Action {
90
- rbac .ResourceWorkspace .Type : {policy .ActionRead },
92
+ name : "mixed-scopes" ,
93
+ subject : merge (canAssignRole , rbac .RoleOwner ()),
94
+ organizationID : orgID ,
95
+ site : codersdk .CreatePermissions (map [codersdk.RBACResource ][]codersdk.RBACAction {
96
+ codersdk .ResourceWorkspace : {codersdk .ActionRead },
97
+ }),
98
+ org : codersdk .CreatePermissions (map [codersdk.RBACResource ][]codersdk.RBACAction {
99
+ codersdk .ResourceWorkspace : {codersdk .ActionRead },
91
100
}),
92
- org : map [string ][]rbac.Permission {
93
- uuid .New ().String (): rbac .Permissions (map [string ][]policy.Action {
94
- rbac .ResourceWorkspace .Type : {policy .ActionRead },
95
- }),
96
- },
97
101
errorContains : "cannot assign both org and site permissions" ,
98
102
},
99
- {
100
- name : "multiple-org" ,
101
- subject : merge (canAssignRole , rbac .RoleOwner ()),
102
- org : map [string ][]rbac.Permission {
103
- uuid .New ().String (): rbac .Permissions (map [string ][]policy.Action {
104
- rbac .ResourceWorkspace .Type : {policy .ActionRead },
105
- }),
106
- uuid .New ().String (): rbac .Permissions (map [string ][]policy.Action {
107
- rbac .ResourceWorkspace .Type : {policy .ActionRead },
108
- }),
109
- },
110
- errorContains : "cannot assign permissions to more than 1" ,
111
- },
112
103
{
113
104
name : "invalid-action" ,
114
105
subject : merge (canAssignRole , rbac .RoleOwner ()),
115
- site : rbac . Permissions (map [string ][]policy. Action {
106
+ site : codersdk . CreatePermissions (map [codersdk. RBACResource ][]codersdk. RBACAction {
116
107
// Action does not go with resource
117
- rbac .ResourceWorkspace . Type : {policy .ActionViewInsights },
108
+ codersdk .ResourceWorkspace : {codersdk .ActionViewInsights },
118
109
}),
119
110
errorContains : "invalid action" ,
120
111
},
121
112
{
122
113
name : "invalid-resource" ,
123
114
subject : merge (canAssignRole , rbac .RoleOwner ()),
124
- site : rbac . Permissions (map [string ][]policy. Action {
125
- "foobar" : {policy .ActionViewInsights },
115
+ site : codersdk . CreatePermissions (map [codersdk. RBACResource ][]codersdk. RBACAction {
116
+ "foobar" : {codersdk .ActionViewInsights },
126
117
}),
127
118
errorContains : "invalid resource" ,
128
119
},
129
120
{
130
121
// Not allowing these at this time.
131
122
name : "negative-permission" ,
132
123
subject : merge (canAssignRole , rbac .RoleOwner ()),
133
- site : []rbac .Permission {
124
+ site : []codersdk .Permission {
134
125
{
135
126
Negate : true ,
136
- ResourceType : rbac .ResourceWorkspace . Type ,
137
- Action : policy .ActionRead ,
127
+ ResourceType : codersdk .ResourceWorkspace ,
128
+ Action : codersdk .ActionRead ,
138
129
},
139
130
},
140
131
errorContains : "no negative permissions" ,
141
132
},
142
133
{
143
134
name : "wildcard" , // not allowed
144
135
subject : merge (canAssignRole , rbac .RoleOwner ()),
145
- site : rbac . Permissions (map [string ][]policy. Action {
146
- rbac .ResourceWorkspace . Type : {policy . WildcardSymbol },
136
+ site : codersdk . CreatePermissions (map [codersdk. RBACResource ][]codersdk. RBACAction {
137
+ codersdk .ResourceWorkspace : {"*" },
147
138
}),
148
139
errorContains : "no wildcard symbols" ,
149
140
},
150
141
// escalation checks
151
142
{
152
143
name : "read-workspace-escalation" ,
153
144
subject : merge (canAssignRole ),
154
- site : rbac . Permissions (map [string ][]policy. Action {
155
- rbac .ResourceWorkspace . Type : {policy .ActionRead },
145
+ site : codersdk . CreatePermissions (map [codersdk. RBACResource ][]codersdk. RBACAction {
146
+ codersdk .ResourceWorkspace : {codersdk .ActionRead },
156
147
}),
157
148
errorContains : "not allowed to grant this permission" ,
158
149
},
159
150
{
160
- name : "read-workspace-outside-org" ,
161
- subject : merge (canAssignRole , rbac .RoleOrgAdmin (orgID )),
162
- org : map [string ][]rbac.Permission {
163
- // The org admin is for a different org
164
- uuid .NewString (): rbac .Permissions (map [string ][]policy.Action {
165
- rbac .ResourceWorkspace .Type : {policy .ActionRead },
166
- }),
151
+ name : "read-workspace-outside-org" ,
152
+ organizationID : uuid.NullUUID {
153
+ UUID : uuid .New (),
154
+ Valid : true ,
167
155
},
156
+ subject : merge (canAssignRole , rbac .RoleOrgAdmin (orgID .UUID )),
157
+ org : codersdk .CreatePermissions (map [codersdk.RBACResource ][]codersdk.RBACAction {
158
+ codersdk .ResourceWorkspace : {codersdk .ActionRead },
159
+ }),
168
160
errorContains : "not allowed to grant this permission" ,
169
161
},
170
162
{
171
163
name : "user-escalation" ,
172
164
// These roles do not grant user perms
173
- subject : merge (canAssignRole , rbac .RoleOrgAdmin (orgID )),
174
- user : rbac . Permissions (map [string ][]policy. Action {
175
- rbac .ResourceWorkspace . Type : {policy .ActionRead },
165
+ subject : merge (canAssignRole , rbac .RoleOrgAdmin (orgID . UUID )),
166
+ user : codersdk . CreatePermissions (map [codersdk. RBACResource ][]codersdk. RBACAction {
167
+ codersdk .ResourceWorkspace : {codersdk .ActionRead },
176
168
}),
177
169
errorContains : "not allowed to grant this permission" ,
178
170
},
179
171
{
180
172
name : "template-admin-escalation" ,
181
173
subject : merge (canAssignRole , rbac .RoleTemplateAdmin ()),
182
- site : rbac . Permissions (map [string ][]policy. Action {
183
- rbac .ResourceWorkspace . Type : {policy .ActionRead }, // ok!
184
- rbac .ResourceDeploymentConfig . Type : {policy .ActionUpdate }, // not ok!
174
+ site : codersdk . CreatePermissions (map [codersdk. RBACResource ][]codersdk. RBACAction {
175
+ codersdk .ResourceWorkspace : {codersdk .ActionRead }, // ok!
176
+ codersdk .ResourceDeploymentConfig : {codersdk .ActionUpdate }, // not ok!
185
177
}),
186
- user : rbac . Permissions (map [string ][]policy. Action {
187
- rbac .ResourceWorkspace . Type : {policy .ActionRead }, // ok!
178
+ user : codersdk . CreatePermissions (map [codersdk. RBACResource ][]codersdk. RBACAction {
179
+ codersdk .ResourceWorkspace : {codersdk .ActionRead }, // ok!
188
180
}),
189
181
errorContains : "deployment_config" ,
190
182
},
191
183
// ok!
192
184
{
193
185
name : "read-workspace-template-admin" ,
194
186
subject : merge (canAssignRole , rbac .RoleTemplateAdmin ()),
195
- site : rbac . Permissions (map [string ][]policy. Action {
196
- rbac .ResourceWorkspace . Type : {policy .ActionRead },
187
+ site : codersdk . CreatePermissions (map [codersdk. RBACResource ][]codersdk. RBACAction {
188
+ codersdk .ResourceWorkspace : {codersdk .ActionRead },
197
189
}),
198
190
},
199
191
{
200
- name : "read-workspace-in-org" ,
201
- subject : merge (canAssignRole , rbac .RoleOrgAdmin (orgID )),
202
- org : map [string ][]rbac.Permission {
203
- // Org admin of this org, this is ok!
204
- orgID .String (): rbac .Permissions (map [string ][]policy.Action {
205
- rbac .ResourceWorkspace .Type : {policy .ActionRead },
206
- }),
207
- },
192
+ name : "read-workspace-in-org" ,
193
+ subject : merge (canAssignRole , rbac .RoleOrgAdmin (orgID .UUID )),
194
+ organizationID : orgID ,
195
+ org : codersdk .CreatePermissions (map [codersdk.RBACResource ][]codersdk.RBACAction {
196
+ codersdk .ResourceWorkspace : {codersdk .ActionRead },
197
+ }),
208
198
},
209
199
{
210
200
name : "user-perms" ,
211
201
// This is weird, but is ok
212
202
subject : merge (canAssignRole , rbac .RoleMember ()),
213
- user : rbac . Permissions (map [string ][]policy. Action {
214
- rbac .ResourceWorkspace . Type : {policy .ActionRead },
203
+ user : codersdk . CreatePermissions (map [codersdk. RBACResource ][]codersdk. RBACAction {
204
+ codersdk .ResourceWorkspace : {codersdk .ActionRead },
215
205
}),
216
206
},
217
207
{
218
208
name : "site+user-perms" ,
219
209
subject : merge (canAssignRole , rbac .RoleMember (), rbac .RoleTemplateAdmin ()),
220
- site : rbac . Permissions (map [string ][]policy. Action {
221
- rbac .ResourceWorkspace . Type : {policy .ActionRead },
210
+ site : codersdk . CreatePermissions (map [codersdk. RBACResource ][]codersdk. RBACAction {
211
+ codersdk .ResourceWorkspace : {codersdk .ActionRead },
222
212
}),
223
- user : rbac . Permissions (map [string ][]policy. Action {
224
- rbac .ResourceWorkspace . Type : {policy .ActionRead },
213
+ user : codersdk . CreatePermissions (map [codersdk. RBACResource ][]codersdk. RBACAction {
214
+ codersdk .ResourceWorkspace : {codersdk .ActionRead },
225
215
}),
226
216
},
227
217
}
@@ -244,9 +234,10 @@ func TestUpsertCustomRoles(t *testing.T) {
244
234
_ , err := az .UpsertCustomRole (ctx , database.UpsertCustomRoleParams {
245
235
Name : "test-role" ,
246
236
DisplayName : "" ,
247
- SitePermissions : must (json .Marshal (tc .site )),
248
- OrgPermissions : must (json .Marshal (tc .org )),
249
- UserPermissions : must (json .Marshal (tc .user )),
237
+ OrganizationID : tc .organizationID ,
238
+ SitePermissions : db2sdk .List (tc .site , convertSDKPerm ),
239
+ OrgPermissions : db2sdk .List (tc .org , convertSDKPerm ),
240
+ UserPermissions : db2sdk .List (tc .user , convertSDKPerm ),
250
241
})
251
242
if tc .errorContains != "" {
252
243
require .ErrorContains (t , err , tc .errorContains )
@@ -256,3 +247,11 @@ func TestUpsertCustomRoles(t *testing.T) {
256
247
})
257
248
}
258
249
}
250
+
251
+ func convertSDKPerm (perm codersdk.Permission ) database.CustomRolePermission {
252
+ return database.CustomRolePermission {
253
+ Negate : perm .Negate ,
254
+ ResourceType : string (perm .ResourceType ),
255
+ Action : policy .Action (perm .Action ),
256
+ }
257
+ }
0 commit comments