meshagent-api 0.53.4__tar.gz → 0.54.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. {meshagent_api-0.53.4/meshagent_api.egg-info → meshagent_api-0.54.0}/PKG-INFO +1 -1
  2. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/__init__.py +42 -0
  3. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/client.py +10 -10
  4. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/client_test.py +22 -44
  5. meshagent_api-0.54.0/meshagent/api/file_permissions.py +133 -0
  6. meshagent_api-0.54.0/meshagent/api/files.py +344 -0
  7. meshagent_api-0.54.0/meshagent/api/files_test.py +235 -0
  8. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/oauth_scopes.py +11 -0
  9. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/oauth_scopes_test.py +11 -0
  10. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/participant_token.py +41 -14
  11. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/room_server_client.py +75 -0
  12. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/room_server_client_test.py +77 -0
  13. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/service_spec_test.py +38 -0
  14. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/specs/service.py +8 -2
  15. meshagent_api-0.54.0/meshagent/api/sqlite_batch_integration_test.py +79 -0
  16. meshagent_api-0.54.0/meshagent/api/storage_policy_test.py +44 -0
  17. meshagent_api-0.54.0/meshagent/api/version.py +1 -0
  18. {meshagent_api-0.53.4 → meshagent_api-0.54.0/meshagent_api.egg-info}/PKG-INFO +1 -1
  19. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent_api.egg-info/SOURCES.txt +5 -0
  20. meshagent_api-0.53.4/meshagent/api/version.py +0 -1
  21. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/LICENSE +0 -0
  22. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/MANIFEST.in +0 -0
  23. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/README.md +0 -0
  24. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/agent_content.py +0 -0
  25. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/aiohttp_redaction.py +0 -0
  26. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/chan.py +0 -0
  27. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/crdt.py +0 -0
  28. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/dataset_schema.py +0 -0
  29. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/entrypoint.js +0 -0
  30. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/error_codes.py +0 -0
  31. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/helpers.py +0 -0
  32. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/http.py +0 -0
  33. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/image_runtime.py +0 -0
  34. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/keys.py +0 -0
  35. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/llm_router_config.py +0 -0
  36. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/managed_agents.py +0 -0
  37. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/managed_agents_test.py +0 -0
  38. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/messaging.py +0 -0
  39. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/messaging_test.py +0 -0
  40. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/mime_types.py +0 -0
  41. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/mime_types_test.py +0 -0
  42. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/oauth.py +0 -0
  43. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/oauth_branding.py +0 -0
  44. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/participant.py +0 -0
  45. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/participant_token_test.py +0 -0
  46. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/port_forward.py +0 -0
  47. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/port_forward_test.py +0 -0
  48. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/protocol.py +0 -0
  49. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/protocol_test.py +0 -0
  50. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/py.typed +0 -0
  51. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/reasoning_schema.py +0 -0
  52. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/registry_auth.py +0 -0
  53. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/room_ports.py +0 -0
  54. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/runtime.py +0 -0
  55. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/runtime_test.py +0 -0
  56. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/schema.py +0 -0
  57. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/schema_document.py +0 -0
  58. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/schema_document_test.py +0 -0
  59. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/schema_registry.py +0 -0
  60. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/schema_test.py +0 -0
  61. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/schema_util.py +0 -0
  62. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/service_template_test.py +0 -0
  63. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/services.py +0 -0
  64. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/services_test.py +0 -0
  65. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/sql.py +0 -0
  66. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/sql_test.py +0 -0
  67. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/sqlite_transfer.py +0 -0
  68. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/sqlite_transfer_test.py +0 -0
  69. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/token_test.py +0 -0
  70. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/urls.py +0 -0
  71. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/webhooks.py +0 -0
  72. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/webhooks_test.py +0 -0
  73. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/websocket_protocol.py +0 -0
  74. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent/api/websocket_protocol_test.py +0 -0
  75. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent_api.egg-info/dependency_links.txt +0 -0
  76. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent_api.egg-info/requires.txt +0 -0
  77. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/meshagent_api.egg-info/top_level.txt +0 -0
  78. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/pyproject.toml +0 -0
  79. {meshagent_api-0.53.4 → meshagent_api-0.54.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: meshagent-api
3
- Version: 0.53.4
3
+ Version: 0.54.0
4
4
  Summary: Python Server API for Meshagent
5
5
  License-Expression: Apache-2.0
6
6
  Project-URL: Documentation, https://docs.meshagent.com
@@ -35,6 +35,14 @@ from .room_server_client import (
35
35
  MeshDocument,
36
36
  FileHandle,
37
37
  StorageEntry,
38
+ SqliteClient,
39
+ SqliteDatabaseClient,
40
+ SqliteDatabaseDetails,
41
+ SqliteSqlCommand,
42
+ SqliteSqlExecution,
43
+ SqliteSqlQuery,
44
+ SqliteSqlStatement,
45
+ SqliteSqlCancelResult,
38
46
  )
39
47
  from .client import (
40
48
  Meshagent,
@@ -49,6 +57,20 @@ from .client import (
49
57
  RoomLifecycleEvent,
50
58
  RoomStatus,
51
59
  )
60
+ from .files import (
61
+ FileAction,
62
+ FilePathRule,
63
+ FileRole,
64
+ FileLocation,
65
+ FileMetadata,
66
+ FilePermissions,
67
+ FilesClient,
68
+ FilesPage,
69
+ FilesPolicy,
70
+ FilesTarget,
71
+ PresignedFileUrl,
72
+ TargetFilesClient,
73
+ )
52
74
  from .participant_token import (
53
75
  ParticipantToken,
54
76
  ParticipantGrant,
@@ -115,6 +137,18 @@ from .mime_types import guess_mime_type
115
137
  from .http import MeshagentConsistency
116
138
 
117
139
  __all__ = [
140
+ "FileLocation",
141
+ "FileAction",
142
+ "FilePathRule",
143
+ "FileRole",
144
+ "FilesPolicy",
145
+ "FileMetadata",
146
+ "FilePermissions",
147
+ "FilesClient",
148
+ "FilesPage",
149
+ "FilesTarget",
150
+ "PresignedFileUrl",
151
+ "TargetFilesClient",
118
152
  "Meshagent",
119
153
  "RoomDisabledError",
120
154
  "MeshagentConsistency",
@@ -168,6 +202,14 @@ __all__ = [
168
202
  "ApiScope",
169
203
  "DatasetGrant",
170
204
  "SqliteDatabaseGrant",
205
+ "SqliteClient",
206
+ "SqliteDatabaseClient",
207
+ "SqliteDatabaseDetails",
208
+ "SqliteSqlCommand",
209
+ "SqliteSqlExecution",
210
+ "SqliteSqlQuery",
211
+ "SqliteSqlStatement",
212
+ "SqliteSqlCancelResult",
171
213
  "SqliteGrant",
172
214
  "SqliteTableGrant",
173
215
  "LLMGrant",
@@ -16,6 +16,7 @@ from pydantic import (
16
16
  )
17
17
  from meshagent.api.room_server_client import RoomDisabledError, RoomException
18
18
  from meshagent.api.participant_token import ApiScope, ParticipantToken
19
+ from meshagent.api.files import FILE_RELATIONS, FileRole, FilesClient
19
20
  from meshagent.api.helpers import is_valid_room_name, meshagent_base_url
20
21
  from meshagent.api.http import (
21
22
  MESHAGENT_CONSISTENCY_HEADER,
@@ -126,6 +127,7 @@ class MeshagentDomains(BaseModel):
126
127
  mail: str | None = None
127
128
  pages: str | None = None
128
129
  registry: str | None = None
130
+ files: str | None = None
129
131
 
130
132
 
131
133
  class MeshagentDeploymentConfig(BaseModel):
@@ -290,17 +292,11 @@ AccessRole = (
290
292
  | SecretRole
291
293
  | ServiceAccountRole
292
294
  | CustomDomainRole
295
+ | FileRole
293
296
  | Literal["list"]
294
297
  )
295
298
 
296
299
 
297
- def _validate_resource_policy_type(resource_type: AccessResourceType) -> None:
298
- if resource_type == "agent":
299
- raise ValueError(
300
- "managed agent resource policies are not supported; use agent run_as instead"
301
- )
302
-
303
-
304
300
  class AccessSubject(BaseModel):
305
301
  type: AccessSubjectType
306
302
  id: Optional[str] = None
@@ -515,6 +511,12 @@ class ProjectRoomGrant(RoleGrant):
515
511
 
516
512
  @property
517
513
  def permissions(self) -> ApiScope:
514
+ if set(self.direct_roles).intersection(FILE_RELATIONS) and not set(
515
+ self.direct_roles
516
+ ).intersection(
517
+ {"guest", "viewer", "site_user", "operator", "developer", "admin"}
518
+ ):
519
+ return ApiScope()
518
520
  if "admin" in self.direct_roles:
519
521
  return room_scope_for_role_compat("admin")
520
522
  if "developer" in self.direct_roles:
@@ -1301,6 +1303,7 @@ class Meshagent:
1301
1303
  self.consistency = normalize_meshagent_consistency(consistency)
1302
1304
  self._session_external = session is not None
1303
1305
  self._session = session or new_client_session()
1306
+ self.files = FilesClient(self)
1304
1307
 
1305
1308
  async def close(self):
1306
1309
  if not self._session_external and not self._session.closed:
@@ -5285,7 +5288,6 @@ class Meshagent:
5285
5288
  continuation_token: str | None = None,
5286
5289
  ) -> ResourcePolicyPage:
5287
5290
  """GET /accounts/projects/{project_id}/iam/{resource_type}/{resource_id}/policy."""
5288
- _validate_resource_policy_type(resource_type)
5289
5291
  params = {"page_size": str(page_size)}
5290
5292
  if continuation_token is not None:
5291
5293
  params["continuation_token"] = continuation_token
@@ -5338,7 +5340,6 @@ class Meshagent:
5338
5340
  invite_redirect_url: str | None = None,
5339
5341
  ) -> None:
5340
5342
  """POST /accounts/projects/{project_id}/iam/{resource_type}/{resource_id}/policy:grant."""
5341
- _validate_resource_policy_type(resource_type)
5342
5343
  payload = {
5343
5344
  "subject": subject.model_dump(mode="json", exclude_none=True),
5344
5345
  "roles": roles,
@@ -5366,7 +5367,6 @@ class Meshagent:
5366
5367
  subject: AccessSubject,
5367
5368
  ) -> None:
5368
5369
  """POST /accounts/projects/{project_id}/iam/{resource_type}/{resource_id}/policy:revoke."""
5369
- _validate_resource_policy_type(resource_type)
5370
5370
  payload = {
5371
5371
  "subject": subject.model_dump(mode="json", exclude_none=True),
5372
5372
  }
@@ -1602,6 +1602,7 @@ async def test_get_config_returns_typed_deployment_config():
1602
1602
  "mail": "mail.meshagent.life",
1603
1603
  "pages": "meshagent.life",
1604
1604
  "registry": "registry.meshagent.life",
1605
+ "files": "files.meshagent.life",
1605
1606
  },
1606
1607
  "version": "0.41.5",
1607
1608
  },
@@ -1613,6 +1614,7 @@ async def test_get_config_returns_typed_deployment_config():
1613
1614
  config = await client.get_config()
1614
1615
 
1615
1616
  assert config.domains.registry == "registry.meshagent.life"
1617
+ assert config.domains.files == "files.meshagent.life"
1616
1618
  assert config.domains.api == "api.meshagent.life"
1617
1619
  assert config.version == "0.41.5"
1618
1620
  assert session.calls == [
@@ -1624,6 +1626,13 @@ async def test_get_config_returns_typed_deployment_config():
1624
1626
  ]
1625
1627
 
1626
1628
 
1629
+ @pytest.mark.asyncio
1630
+ async def test_get_config_accepts_deployments_without_files_endpoint():
1631
+ session = _FakeSession([_FakeResponse(status=200, payload={"domains": {}})])
1632
+ client = Meshagent(base_url="http://example.test", token="token", session=session)
1633
+ assert (await client.get_config()).domains.files is None
1634
+
1635
+
1627
1636
  def _secret_payload(**overrides):
1628
1637
  payload = {
1629
1638
  "id": "secret-1",
@@ -2475,11 +2484,12 @@ async def test_service_account_and_scoped_api_key_methods_return_typed_models():
2475
2484
  ]
2476
2485
 
2477
2486
 
2487
+ @pytest.mark.parametrize("resource_type", ["room", "agent"])
2478
2488
  @pytest.mark.asyncio
2479
- async def test_resource_policy_methods_use_iam_policy_routes():
2489
+ async def test_resource_policy_methods_use_iam_policy_routes(resource_type):
2480
2490
  grant_payload = {
2481
2491
  "resource": {
2482
- "type": "room",
2492
+ "type": resource_type,
2483
2493
  "id": "room-1",
2484
2494
  "name": "demo",
2485
2495
  },
@@ -2492,7 +2502,7 @@ async def test_resource_policy_methods_use_iam_policy_routes():
2492
2502
  _FakeResponse(
2493
2503
  status=200,
2494
2504
  payload={
2495
- "resource": {"type": "room", "id": "room-1", "name": "demo"},
2505
+ "resource": {"type": resource_type, "id": "room-1", "name": "demo"},
2496
2506
  "access_grants": [grant_payload],
2497
2507
  "continuation_token": "next-token",
2498
2508
  },
@@ -2500,7 +2510,7 @@ async def test_resource_policy_methods_use_iam_policy_routes():
2500
2510
  _FakeResponse(
2501
2511
  status=200,
2502
2512
  payload={
2503
- "resource": {"type": "room", "id": "room-1", "name": "demo"},
2513
+ "resource": {"type": resource_type, "id": "room-1", "name": "demo"},
2504
2514
  "access_grants": [grant_payload],
2505
2515
  },
2506
2516
  ),
@@ -2511,25 +2521,25 @@ async def test_resource_policy_methods_use_iam_policy_routes():
2511
2521
 
2512
2522
  await client.grant_resource_policy(
2513
2523
  project_id="proj_123",
2514
- resource_type="room",
2524
+ resource_type=resource_type,
2515
2525
  resource_id="room-1",
2516
2526
  subject=AccessSubject(type="user", id="user-1"),
2517
2527
  roles=["operator", "list"],
2518
2528
  )
2519
2529
  page = await client.get_resource_policy_page(
2520
2530
  project_id="proj_123",
2521
- resource_type="room",
2531
+ resource_type=resource_type,
2522
2532
  resource_id="room-1",
2523
2533
  continuation_token="cursor-1",
2524
2534
  )
2525
2535
  grants = await client.get_resource_policy(
2526
2536
  project_id="proj_123",
2527
- resource_type="room",
2537
+ resource_type=resource_type,
2528
2538
  resource_id="room-1",
2529
2539
  )
2530
2540
  await client.revoke_resource_policy(
2531
2541
  project_id="proj_123",
2532
- resource_type="room",
2542
+ resource_type=resource_type,
2533
2543
  resource_id="room-1",
2534
2544
  subject=AccessSubject(type="user", id="user-1"),
2535
2545
  )
@@ -2541,7 +2551,7 @@ async def test_resource_policy_methods_use_iam_policy_routes():
2541
2551
  assert session.calls == [
2542
2552
  (
2543
2553
  "post",
2544
- "http://example.test/accounts/projects/proj_123/iam/room/room-1/policy:grant",
2554
+ f"http://example.test/accounts/projects/proj_123/iam/{resource_type}/room-1/policy:grant",
2545
2555
  {
2546
2556
  "subject": {
2547
2557
  "type": "user",
@@ -2552,17 +2562,17 @@ async def test_resource_policy_methods_use_iam_policy_routes():
2552
2562
  ),
2553
2563
  (
2554
2564
  "get",
2555
- "http://example.test/accounts/projects/proj_123/iam/room/room-1/policy",
2565
+ f"http://example.test/accounts/projects/proj_123/iam/{resource_type}/room-1/policy",
2556
2566
  {"page_size": "50", "continuation_token": "cursor-1"},
2557
2567
  ),
2558
2568
  (
2559
2569
  "get",
2560
- "http://example.test/accounts/projects/proj_123/iam/room/room-1/policy",
2570
+ f"http://example.test/accounts/projects/proj_123/iam/{resource_type}/room-1/policy",
2561
2571
  {"page_size": "50"},
2562
2572
  ),
2563
2573
  (
2564
2574
  "post",
2565
- "http://example.test/accounts/projects/proj_123/iam/room/room-1/policy:revoke",
2575
+ f"http://example.test/accounts/projects/proj_123/iam/{resource_type}/room-1/policy:revoke",
2566
2576
  {
2567
2577
  "subject": {
2568
2578
  "type": "user",
@@ -2573,38 +2583,6 @@ async def test_resource_policy_methods_use_iam_policy_routes():
2573
2583
  ]
2574
2584
 
2575
2585
 
2576
- @pytest.mark.asyncio
2577
- async def test_resource_policy_methods_reject_managed_agent_policies():
2578
- session = _FakeSession([])
2579
- client = Meshagent(base_url="http://example.test", token="token", session=session)
2580
-
2581
- with pytest.raises(ValueError, match="managed agent resource policies"):
2582
- await client.grant_resource_policy(
2583
- project_id="proj_123",
2584
- resource_type="agent",
2585
- resource_id="agent-1",
2586
- subject=AccessSubject(type="user", id="user-1"),
2587
- roles=["operator", "list"],
2588
- )
2589
-
2590
- with pytest.raises(ValueError, match="managed agent resource policies"):
2591
- await client.get_resource_policy_page(
2592
- project_id="proj_123",
2593
- resource_type="agent",
2594
- resource_id="agent-1",
2595
- )
2596
-
2597
- with pytest.raises(ValueError, match="managed agent resource policies"):
2598
- await client.revoke_resource_policy(
2599
- project_id="proj_123",
2600
- resource_type="agent",
2601
- resource_id="agent-1",
2602
- subject=AccessSubject(type="user", id="user-1"),
2603
- )
2604
-
2605
- assert session.calls == []
2606
-
2607
-
2608
2586
  @pytest.mark.asyncio
2609
2587
  async def test_access_evaluator_methods_use_access_routes():
2610
2588
  session = _FakeSession(
@@ -0,0 +1,133 @@
1
+ """Transport-independent file permissions used by REST and participant tokens."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from collections.abc import Iterable
6
+ from typing import Literal
7
+
8
+ from pydantic import BaseModel, ConfigDict, Field, field_validator
9
+
10
+
11
+ def validate_file_path(value: str) -> str:
12
+ if (
13
+ value.startswith("/")
14
+ or any(c == "\\" or ord(c) < 32 or 127 <= ord(c) < 160 for c in value)
15
+ or (
16
+ value
17
+ and any(
18
+ part in {"", ".", "..", ".meshagent-directory"}
19
+ or part.startswith(".meshagent-upload-")
20
+ for part in value.split("/")
21
+ )
22
+ )
23
+ ):
24
+ raise ValueError("Files paths must be canonical relative paths")
25
+ return value
26
+
27
+
28
+ FileAction = Literal["list", "read", "write", "delete", "presign"]
29
+ FileRole = Literal[
30
+ "files_list", "files_read", "files_write", "files_delete", "files_presign"
31
+ ]
32
+ FILE_ACTIONS: tuple[FileAction, ...] = ("list", "read", "write", "delete", "presign")
33
+ FILE_RELATIONS: tuple[str, ...] = tuple(f"files_{action}" for action in FILE_ACTIONS)
34
+
35
+
36
+ class FilePathRule(BaseModel):
37
+ """Segment-bounded file actions, with explicit excluded subtrees."""
38
+
39
+ model_config = ConfigDict(frozen=True, extra="forbid")
40
+
41
+ path: str
42
+ recursive: bool
43
+ actions: list[FileAction]
44
+ excluded_paths: list[str] = Field(default_factory=list, max_length=1000)
45
+
46
+ @field_validator("path")
47
+ @classmethod
48
+ def validate_path(cls, value: str) -> str:
49
+ return validate_file_path(value)
50
+
51
+ @field_validator("excluded_paths")
52
+ @classmethod
53
+ def validate_excluded_paths(cls, values: list[str]) -> list[str]:
54
+ return [validate_file_path(value) for value in values]
55
+
56
+ def allows(self, action: FileAction, path: str, *, subtree: bool = False) -> bool:
57
+ validate_file_path(path)
58
+ if action not in self.actions or (subtree and not self.recursive):
59
+ return False
60
+ if path != self.path and not (self.recursive and _descendant(self.path, path)):
61
+ return False
62
+ return not any(
63
+ path == excluded
64
+ or _descendant(excluded, path)
65
+ or (subtree and _descendant(path, excluded))
66
+ for excluded in self.excluded_paths
67
+ )
68
+
69
+
70
+ def _descendant(parent: str, path: str) -> bool:
71
+ return not parent or path.startswith(parent + "/")
72
+
73
+
74
+ class FilesPolicy(BaseModel):
75
+ model_config = ConfigDict(frozen=True, extra="forbid")
76
+
77
+ paths: list[FilePathRule] = Field(default_factory=list, max_length=1000)
78
+
79
+ def allows(self, action: FileAction, path: str, *, subtree: bool = False) -> bool:
80
+ validate_file_path(path)
81
+ return any(rule.allows(action, path, subtree=subtree) for rule in self.paths)
82
+
83
+ def can_list_directory(self, path: str) -> bool:
84
+ validate_file_path(path)
85
+ return self.allows("list", path) or any(
86
+ rule.allows("list", rule.path) and _descendant(path, rule.path)
87
+ for rule in self.paths
88
+ )
89
+
90
+
91
+ class FilePermissions(BaseModel):
92
+ actions: list[FileAction]
93
+ recursive_actions: list[FileAction]
94
+
95
+
96
+ _STORAGE_READ_ACTIONS: tuple[FileAction, ...] = ("list", "read")
97
+ _STORAGE_WRITE_ACTIONS: tuple[FileAction, ...] = ("list", "read", "write", "delete")
98
+
99
+
100
+ def legacy_storage_rules(
101
+ paths: Iterable[tuple[str, bool]] | None,
102
+ ) -> list[FilePathRule]:
103
+ """Convert ordered storage prefixes without granting URL delegation."""
104
+ if paths is None:
105
+ return [
106
+ FilePathRule(path="", recursive=True, actions=list(_STORAGE_WRITE_ACTIONS))
107
+ ]
108
+ previous: list[str] = []
109
+ result: list[FilePathRule] = []
110
+ for raw_path, read_only in paths:
111
+ path = raw_path.strip("/")
112
+ result.append(
113
+ FilePathRule(
114
+ path=path,
115
+ recursive=True,
116
+ actions=list(
117
+ _STORAGE_READ_ACTIONS if read_only else _STORAGE_WRITE_ACTIONS
118
+ ),
119
+ excluded_paths=list(previous),
120
+ )
121
+ )
122
+ previous.append(path)
123
+ return result
124
+
125
+
126
+ def storage_request_path(path: str) -> str:
127
+ """Retain legacy leading-slash/dot aliases while rejecting unsafe keys."""
128
+ while path.startswith("./"):
129
+ path = path[2:]
130
+ path = path.strip("/")
131
+ if path == ".":
132
+ path = ""
133
+ return validate_file_path(path)