-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathtest_user_forensics.py
More file actions
371 lines (315 loc) · 12.5 KB
/
test_user_forensics.py
File metadata and controls
371 lines (315 loc) · 12.5 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
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
import asyncio
import os
import time
import uuid
from urllib.parse import urljoin, urlparse
import numpy
import pytest
from minio import Minio
from minio.error import S3Error
from tiled.access_control.access_tags import AccessTagsCompiler
from tiled.access_control.scopes import ALL_SCOPES
from tiled.catalog import in_memory
from tiled.client import Context, from_context
from tiled.server.app import build_app, build_app_from_config
from tiled.utils import sanitize_uri
from tiled.validation_registration import ValidationRegistry
from .utils import enter_username_password
arr = numpy.ones((5, 5))
server_config = {
"access_control": {
"access_policy": "tiled.access_control.access_policies:TagBasedAccessPolicy",
"args": {
"provider": "toy",
"tags_db": {
"uri": "file:compiled_tags_mem?mode=memory&cache=shared" # in-memory and shareable
},
"access_tags_parser": "tiled.access_control.access_tags:AccessTagsParser",
},
},
"authentication": {
"tiled_admins": [{"provider": "toy", "id": "admin"}],
"allow_anonymous_access": True,
"secret_keys": ["SECRET"],
"providers": [
{
"provider": "toy",
"authenticator": "tiled.authenticators:DictionaryAuthenticator",
"args": {
"users_to_passwords": {
"alice": "alice",
"bob": "bob",
"admin": "admin",
},
},
},
],
},
}
access_tag_config = {
"roles": {
"facility_admin": {
"scopes": [
"read:data",
"read:metadata",
"write:data",
"write:metadata",
"delete:node",
"delete:revision",
"create:node",
"register",
]
},
},
"tags": {
"alice_tag": {
"users": [
{
"name": "alice",
"role": "facility_admin",
},
{
"name": "bob",
"role": "facility_admin",
},
],
},
},
"tag_owners": {
"alice_tag": {
"users": [
{
"name": "alice",
},
{
"name": "bob",
},
],
},
},
}
validation_registry = ValidationRegistry()
validation_registry.register("SomeSpec", lambda *args, **kwargs: None)
def group_parser(groupname):
return {
"physicists": ["alice", "bob"],
}[groupname]
@pytest.fixture(scope="module")
def compile_access_tags_db():
access_tags_compiler = AccessTagsCompiler(
ALL_SCOPES,
access_tag_config,
{"uri": "file:compiled_tags_mem?mode=memory&cache=shared"},
group_parser,
)
access_tags_compiler.load_tag_config()
access_tags_compiler.compile()
yield access_tags_compiler
access_tags_compiler.connection.close()
@pytest.fixture(scope="module")
def access_control_test_context_factory(tmpdir_module, compile_access_tags_db):
config = {
"trees": [
{
"tree": "tiled.catalog:in_memory",
"args": {
"named_memory": "catalog_foo",
"writable_storage": str(tmpdir_module / "foo"),
"top_level_access_blob": {"tags": ["alice_tag"]},
},
"path": "/foo",
},
{
"tree": "tiled.catalog:in_memory",
"args": {
"named_memory": "catalog_qux",
"writable_storage": str(tmpdir_module / "qux"),
"top_level_access_blob": {"tags": ["public"]},
},
"path": "/qux",
},
],
}
config.update(server_config)
contexts = []
clients = {}
def _create_and_login_context(username, password=None, api_key=None):
if not any([password, api_key]):
raise ValueError("Please provide either 'password' or 'api_key' for auth")
if client := clients.get(username, None):
return client
app = build_app_from_config(config)
context = Context.from_app(
app, uri=f"http://local-tiled-app-{username}/api/v1", api_key=api_key
)
contexts.append(context)
client = from_context(context, remember_me=False)
clients[username] = client
if api_key is None:
with enter_username_password(username, password):
client.context.login(remember_me=False)
return client
yield _create_and_login_context
for context in contexts:
context.close()
@pytest.fixture
def tmp_minio_bucket():
"""Create a temporary MinIO bucket and clean it up after tests."""
if uri := os.getenv("TILED_TEST_BUCKET"):
clean_uri, username, password = sanitize_uri(uri)
minio_client = Minio(
endpoint=urlparse(clean_uri).netloc, # e.g. only "localhost:9000"
access_key=username or "minioadmin",
secret_key=password or "minioadmin",
secure=False,
)
bucket_name = f"test-{uuid.uuid4().hex}"
minio_client.make_bucket(bucket_name=bucket_name)
try:
yield urljoin(uri, "/" + bucket_name) # full URI with credentials
finally:
# Cleanup: remove all objects and delete the bucket
try:
objects = minio_client.list_objects(
bucket_name=bucket_name, recursive=True
)
for obj in objects:
minio_client.remove_object(
bucket_name=bucket_name, object_name=obj.object_name
)
minio_client.remove_bucket(bucket_name=bucket_name)
except S3Error as e:
print(f"Warning: failed to delete test bucket {bucket_name}: {e}")
else:
yield None
@pytest.fixture
def tree(tmpdir, tmp_minio_bucket):
writable_storage = [f"duckdb:///{tmpdir / 'data.duckdb'}"]
if tmp_minio_bucket:
writable_storage.append(
{
"provider": "s3",
"uri": tmp_minio_bucket,
"config": {
"virtual_hosted_style_request": False,
"client_options": {"allow_http": True},
},
}
)
writable_storage.append(f"file://localhost{str(tmpdir / 'data')}")
return in_memory(writable_storage=writable_storage)
async def coro_test(c, keys):
child_node = await c.context.http_client.app.state.root_tree[
keys[0]
].lookup_adapter([keys[1]])
return child_node
def test_created_and_updated_info_with_users(access_control_test_context_factory):
"""
Test that created_by and updated_by fields are correctly set
on node creation and metadata update.
"""
alice_client = access_control_test_context_factory("alice", "alice")
bob_client = access_control_test_context_factory("bob", "bob")
top = "foo"
for data in ["data_M"]:
# Create a new node and check created_by and updated_by
alice_client[top].write_array(
arr,
key=data,
metadata={"description": "initial"},
access_tags=["alice_tag"],
)
coro_obj = coro_test(alice_client, [top, data])
result = asyncio.run(coro_obj)
# When the array is first created, created_by and updated_by should be the same
assert result.node.created_by == "alice"
assert result.node.updated_by == "alice"
assert result.node.time_created == result.node.time_updated
time.sleep(1) # ensure time_updated is different
bob_client[top][data].replace_metadata(metadata={"description": "updated"})
coro_obj = coro_test(bob_client, [top, data])
result = asyncio.run(coro_obj)
# After Bob updates the metadata, updated_by should be Bob, created_by should remain Alice
assert result.node.created_by != result.node.updated_by
assert result.node.time_created != result.node.time_updated
def test_created_and_updated_info_with_service(access_control_test_context_factory):
"""
Test that created_by and updated_by fields are correctly set
when a service (non-user) creates or updates a node.
"""
admin_client = access_control_test_context_factory("admin", "admin")
top = "qux"
# Create a new Service Principal. It is identified by its UUID.
# It has no "identities"; it cannot log in with a password.
sp_create = admin_client.context.admin.create_service_principal("admin")
sp_apikey_info = admin_client.context.admin.create_api_key(sp_create["uuid"])
# Create a client for the Service Principal using its API key
sp_create_client = access_control_test_context_factory(
sp_create["uuid"], api_key=sp_apikey_info["secret"]
)
# Create a new Service Principal for updating the node.
# It is identified by its UUID. It has no "identities"; it cannot log in with a password.
sp_update = admin_client.context.admin.create_service_principal("admin")
sp_update_apikey_info = admin_client.context.admin.create_api_key(sp_update["uuid"])
# Create a client for the new Service Principal using its API key
sp_update_client = access_control_test_context_factory(
sp_update["uuid"], api_key=sp_update_apikey_info["secret"]
)
for data in ["data_N"]:
# Create a new node with the service principal and check created_by and updated_by
sp_create_client[top].write_array(
arr, key=data, metadata={"description": "initial"}, access_tags=["public"]
)
coro_obj = coro_test(sp_create_client, [top, data])
result = asyncio.run(coro_obj)
# When the array is first created by the service principal,
# created_by and updated_by should indicate the service principal
assert sp_create["uuid"] in result.node.created_by
assert sp_create["uuid"] in result.node.updated_by
assert result.node.time_created == result.node.time_updated
time.sleep(1) # ensure time_updated is different
sp_update_client[top][data].replace_metadata(
metadata={"description": "updated"}
)
coro_obj = coro_test(sp_update_client, [top, data])
result = asyncio.run(coro_obj)
# After the service principal updates the metadata,
# updated_by should indicate the second service principal,
# created_by should indicate the first service principal
assert result.node.updated_by != result.node.created_by
assert sp_update["uuid"] in result.node.updated_by
assert result.node.time_created != result.node.time_updated
async def coro_test_anonymous(c, keys):
child_node = await c.context.http_client.app.state.root_tree.lookup_adapter(
[keys[0]]
)
return child_node
def test_created_and_updated_info_with_anonymous(tree):
"""
Test that created_by and updated_by fields are correctly set
when an anonymous user creates or updates a node.
"""
with Context.from_app(
build_app(tree, validation_registry=validation_registry)
) as context:
client = from_context(context)
# client.create_container(top)
for data in ["data_O"]:
client.write_array(arr, key=data, metadata={"description": "initial"})
coro_obj = coro_test_anonymous(client, [data])
result = asyncio.run(coro_obj)
# When the array is first created by the anonymous user,
# created_by and updated_by should be None
assert result.node.created_by is None
assert result.node.updated_by is None
assert result.node.time_created == result.node.time_updated
time.sleep(1) # ensure time_updated is different
client[data].replace_metadata(metadata={"description": "updated"})
coro_obj = coro_test_anonymous(client, [data])
result = asyncio.run(coro_obj)
# After the anonymous user updates the metadata,
# created_by and updated_by should still be None
# and time_created should be different from time_updated
assert result.node.created_by is None
assert result.node.updated_by is None
assert result.node.time_created != result.node.time_updated