Add safe attachment reconciliation and scoped maintenance checks

This commit is contained in:
Самохвалов Андрей Ремович
2026-10-02 14:08:12 +03:00
parent 9347fe35ae
commit dc4f5c6c7f
9 changed files with 403 additions and 0 deletions
+45
View File
@@ -0,0 +1,45 @@
"""Run in both API and maintenance containers; tests their distinct S3 identities."""
import io
import os
import unittest
from pathlib import Path
from urllib.error import HTTPError
from urllib.request import urlopen
from uuid import uuid4
from minio.error import S3Error
from src.attachments import BUCKET, storage
class StoragePermissionsTests(unittest.TestCase):
def test_scoped_identity_and_private_bucket(self):
maintenance = os.environ["S3_ACCESS_KEY_ID_FILE"].endswith("/s3_maintenance_key_id")
self.assertFalse(Path("/run/secrets/minio_root_user").exists())
if maintenance:
self.assertFalse(Path("/run/secrets/s3_access_key_id").exists())
else:
self.assertFalse(Path("/run/secrets/s3_maintenance_key_id").exists())
client = storage()
key = "permissions/" + uuid4().hex
client.put_object(BUCKET, key, io.BytesIO(b"private"), 7)
try:
if maintenance:
list(client.list_objects(BUCKET, prefix="originals/", recursive=True))
else:
with self.assertRaises(S3Error) as denied:
list(client.list_objects(BUCKET, prefix="originals/", recursive=True))
self.assertEqual(denied.exception.code, "AccessDenied")
with self.assertRaises(S3Error) as denied:
client.make_bucket("forbidden-" + uuid4().hex)
self.assertEqual(denied.exception.code, "AccessDenied")
with self.assertRaises(HTTPError) as denied:
urlopen(f"http://s3:9000/{BUCKET}/{key}", timeout=3)
self.assertEqual(denied.exception.code, 403)
finally:
client.remove_object(BUCKET, key)
if __name__ == "__main__":
unittest.main()