-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_acl.py
465 lines (448 loc) · 14.9 KB
/
test_acl.py
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
from collections import namedtuple
from datasette.app import Datasette
from datasette_acl import update_dynamic_groups
import pytest
import pytest_asyncio
ManageTableTest = namedtuple(
"ManageTableTest",
(
"description",
"setup_post_data",
"post_data",
"expected_acls",
"should_fail_then_succeed",
"expected_audit_rows",
),
)
@pytest_asyncio.fixture
async def ds():
datasette = Datasette(
config={
"plugins": {
"datasette-acl": {
"dynamic-groups": {
# Users with is_staff: True are in staff group
"staff": {"is_staff": True},
}
}
},
# Root user can edit permissions
"permissions": {"datasette-acl": {"id": "root"}},
}
)
db = datasette.add_memory_database("db")
await db.execute_write("create table t (id primary key)")
await datasette.invoke_startup()
yield datasette
# Need to manually drop because in-memory databases shared across tests
await db.execute_write("drop table t")
internal_db = datasette.get_internal_database()
for table in await internal_db.table_names():
if table.startswith("acl"):
await internal_db.execute_write(f"drop table {table}")
@pytest.mark.asyncio
@pytest.mark.parametrize(
ManageTableTest._fields,
(
ManageTableTest(
description="Group: add insert-row",
setup_post_data={},
post_data={"group_permissions_staff_insert-row": "on"},
expected_acls=[
{
"group_name": "staff",
"actor_id": None,
"action_name": "insert-row",
"database_name": "db",
"resource_name": "t",
}
],
should_fail_then_succeed=[
dict(
actor={"id": "simon", "is_staff": True},
action="insert-row",
resource=["db", "t"],
),
],
expected_audit_rows=[
{
"group_name": "staff",
"actor_id": None,
"action_name": "insert-row",
"database_name": "db",
"resource_name": "t",
"operation_by": "root",
"operation": "added",
}
],
),
ManageTableTest(
description="Group: remove insert-row, add update-row and delete-row",
setup_post_data={"group_permissions_staff_insert-row": "on"},
post_data={
"group_permissions_staff_update-row": "on",
"group_permissions_staff_delete-row": "on",
},
expected_acls=[
{
"group_name": "staff",
"actor_id": None,
"action_name": "delete-row",
"database_name": "db",
"resource_name": "t",
},
{
"group_name": "staff",
"actor_id": None,
"action_name": "update-row",
"database_name": "db",
"resource_name": "t",
},
],
should_fail_then_succeed=[
dict(
actor={"id": "simon", "is_staff": True},
action="delete-row",
resource=["db", "t"],
),
dict(
actor={"id": "simon", "is_staff": True},
action="update-row",
resource=["db", "t"],
),
],
expected_audit_rows=[
{
"group_name": "staff",
"actor_id": None,
"action_name": "insert-row",
"database_name": "db",
"resource_name": "t",
"operation_by": "root",
"operation": "added",
},
{
"group_name": "staff",
"actor_id": None,
"action_name": "insert-row",
"database_name": "db",
"resource_name": "t",
"operation_by": "root",
"operation": "removed",
},
{
"group_name": "staff",
"actor_id": None,
"action_name": "delete-row",
"database_name": "db",
"resource_name": "t",
"operation_by": "root",
"operation": "added",
},
{
"group_name": "staff",
"actor_id": None,
"action_name": "update-row",
"database_name": "db",
"resource_name": "t",
"operation_by": "root",
"operation": "added",
},
],
),
ManageTableTest(
description="New user: set with insert-row and update-row",
setup_post_data={},
post_data={
"new_actor_id": "newbie",
"new_user_insert-row": "on",
"new_user_update-row": "on",
},
expected_acls=[
{
"action_name": "insert-row",
"actor_id": "newbie",
"database_name": "db",
"group_name": None,
"resource_name": "t",
},
{
"action_name": "update-row",
"actor_id": "newbie",
"database_name": "db",
"group_name": None,
"resource_name": "t",
},
],
should_fail_then_succeed=[
dict(
actor={"id": "newbie"},
action="insert-row",
resource=["db", "t"],
),
dict(
actor={"id": "newbie"},
action="update-row",
resource=["db", "t"],
),
],
expected_audit_rows=[
{
"group_name": None,
"actor_id": "newbie",
"action_name": "insert-row",
"database_name": "db",
"resource_name": "t",
"operation_by": "root",
"operation": "added",
},
{
"group_name": None,
"actor_id": "newbie",
"action_name": "update-row",
"database_name": "db",
"resource_name": "t",
"operation_by": "root",
"operation": "added",
},
],
),
),
)
async def test_manage_table_permissions(
ds,
description,
setup_post_data,
post_data,
expected_acls,
should_fail_then_succeed,
expected_audit_rows,
):
internal_db = ds.get_internal_database()
# Staff dynamic group should have been created on startup
assert (
await internal_db.execute(
"select count(*) from acl_groups where name = 'staff'"
)
).single_value() == 1
csrf_token_response = await ds.client.get(
"/db/t/-/acl",
cookies={
"ds_actor": ds.client.actor_cookie({"id": "root"}),
},
)
csrftoken = csrf_token_response.cookies["ds_csrftoken"]
if setup_post_data:
setup_response = await ds.client.post(
"/db/t/-/acl",
data={**setup_post_data, "csrftoken": csrftoken},
cookies={
"ds_actor": ds.client.actor_cookie({"id": "root"}),
"ds_csrftoken": csrftoken,
},
)
assert setup_response.status_code == 302
# Permission checks should fail
for kwargs in should_fail_then_succeed:
assert not await ds.permission_allowed(**kwargs), f"Should have failed: {repr}"
# Use the /db/table/-/acl page to update permissions
response = await ds.client.post(
"/db/t/-/acl",
data={**post_data, "csrftoken": csrftoken},
cookies={
"ds_actor": ds.client.actor_cookie({"id": "root"}),
"ds_csrftoken": csrftoken,
},
)
assert response.status_code == 302
# Check ACLs
acls = [
dict(r)
for r in (
await internal_db.execute(
"""
select
acl_groups.name as group_name,
acl.actor_id,
acl_actions.name as action_name,
acl_resources.database as database_name,
acl_resources.resource as resource_name
from acl
left join acl_groups on acl.group_id = acl_groups.id
join acl_actions on acl.action_id = acl_actions.id
join acl_resources on acl.resource_id = acl_resources.id
"""
)
)
]
assert acls == expected_acls
# Permission checks should pass now
for kwargs in should_fail_then_succeed:
assert await ds.permission_allowed(
**kwargs
), f"Should have passed: {repr(kwargs)}"
# Check audit logs
AUDIT_SQL = """
select
acl_groups.name as group_name,
acl_audit.actor_id,
acl_actions.name as action_name,
acl_resources.database as database_name,
acl_resources.resource as resource_name,
acl_audit.operation_by,
acl_audit.operation
from acl_audit
left join acl_groups on acl_audit.group_id = acl_groups.id
join acl_actions on acl_audit.action_id = acl_actions.id
join acl_resources on acl_audit.resource_id = acl_resources.id
order by acl_audit.id
"""
audit_rows = [dict(r) for r in (await internal_db.execute(AUDIT_SQL))]
assert audit_rows == expected_audit_rows
@pytest.mark.asyncio
async def test_update_dynamic_groups():
datasette = Datasette(
config={
"plugins": {
"datasette-acl": {
"dynamic-groups": {
"staff": {"is_staff": True},
}
}
}
}
)
await datasette.invoke_startup()
db = datasette.get_internal_database()
# Should have those tables
tables = await db.table_names()
assert {
"acl_resources",
"acl_actions",
"acl_groups",
"acl_actor_groups",
"acl",
}.issubset(tables)
# Group tables should start populated
assert (await db.execute("select count(*) from acl_groups")).single_value() == 1
# But no actor groups
assert (
await db.execute("select count(*) from acl_actor_groups")
).single_value() == 0
# An actor with is_staff: True should be added to the group
await update_dynamic_groups(
datasette, {"is_staff": True, "id": "staff"}, skip_cache=True
)
assert [dict(r) for r in (await db.execute("select * from acl_groups")).rows] == [
{"id": 1, "name": "staff"},
]
assert [
dict(r)
for r in (
await db.execute(
"select actor_id, (select name from acl_groups where id = group_id) as group_name from acl_actor_groups"
)
).rows
] == [
{"actor_id": "staff", "group_name": "staff"},
]
# If that user changes they should drop from the group
await update_dynamic_groups(
datasette, {"is_staff": False, "id": "staff"}, skip_cache=True
)
assert [
dict(r)
for r in (
await db.execute(
"select actor_id, (select name from acl_groups where id = group_id) as group_name from acl_actor_groups"
)
).rows
] == []
# Groups that are not dynamic should not be modified
await db.execute_write("insert into acl_groups (id, name) values (2, 'static')")
await db.execute_write(
"insert into acl_actor_groups (actor_id, group_id) values ('staff', 2)"
)
await update_dynamic_groups(
datasette, {"is_staff": False, "id": "staff"}, skip_cache=True
)
assert [dict(r) for r in (await db.execute("select * from acl_groups")).rows] == [
{"id": 1, "name": "staff"},
{"id": 2, "name": "static"},
]
@pytest.mark.asyncio
async def test_table_creator_permissions():
datasette = Datasette(
config={
"plugins": {
"datasette-acl": {
"table-creator-permissions": [
"insert-row",
"delete-row",
]
}
},
"permissions": {"create-table": {"id": "*"}},
}
)
await datasette.invoke_startup()
db = datasette.add_memory_database("db")
# Create a table
actor_cookie = datasette.client.actor_cookie({"id": "simon"})
create_response = await datasette.client.post(
"/db/-/create",
json={
"table": "new_table",
"columns": [
{"name": "id", "type": "integer"},
{"name": "title", "type": "text"},
],
"pk": "id",
},
cookies={"ds_actor": actor_cookie},
)
assert create_response.status_code == 201
# That table should have insert-row and delete-row ACLs
acls = [
dict(r)
for r in (
await datasette.get_internal_database().execute(
"""
select
acl.actor_id,
acl_actions.name as action_name,
acl_resources.database as database_name,
acl_resources.resource as resource_name
from acl
join acl_actions on acl.action_id = acl_actions.id
join acl_resources on acl.resource_id = acl_resources.id
where acl_resources.database = 'db'
and acl_resources.resource = 'new_table'
"""
)
)
]
assert acls == [
{
"actor_id": "simon",
"action_name": "insert-row",
"database_name": "db",
"resource_name": "new_table",
},
{
"actor_id": "simon",
"action_name": "delete-row",
"database_name": "db",
"resource_name": "new_table",
},
]
# Permission checks too
assert await datasette.permission_allowed(
actor={"id": "simon"}, action="insert-row", resource=["db", "new_table"]
)
assert await datasette.permission_allowed(
actor={"id": "simon"}, action="delete-row", resource=["db", "new_table"]
)
assert not await datasette.permission_allowed(
actor={"id": "simon"}, action="update-row", resource=["db", "new_table"]
)