ctie-exercice/images/jupyter/test_buckets.py
Adrien Bouvais 8e8ebce721
All checks were successful
Build and Push Docker Images / build-and-push (push) Successful in 1m6s
Moved back test bucket in image
2026-08-16 13:52:05 +02:00

38 lines
1.3 KiB
Python

import os
import boto3
s3 = boto3.client(
's3',
endpoint_url=os.environ['S3_ENDPOINT'],
aws_access_key_id=os.environ['MINIO_ACCESS_KEY_ID'],
aws_secret_access_key=os.environ['MINIO_SECRET_ACCESS_KEY']
)
ref_bucket = os.environ['BUCKET_REF']
work_bucket = os.environ['BUCKET_WORK']
# 1. READ FROM REFERENCE BUCKET (Should PASS)
response = s3.list_objects_v2(Bucket=ref_bucket)
data = s3.get_object(Bucket=ref_bucket, Key="hello.txt")['Body'].read()
print("SUCCESS: List items in ref bucket:", response['KeyCount'], data.decode())
# 2. WRITE TO REFERENCE BUCKET (Should FAIL with 403 Forbidden)
try:
s3.put_object(Bucket=ref_bucket, Key="test.txt", Body=b"forbidden write")
print("ERROR: Write succeeded when it should be forbidden!")
except Exception as e:
print("SUCCESS: Write to reference bucket blocked:", e)
# 3. WRITE & READ WORK BUCKET (Should PASS)
s3.put_object(Bucket=work_bucket, Key="data.txt", Body=b"hello tenant")
data = s3.get_object(Bucket=work_bucket, Key="data.txt")['Body'].read()
print("SUCCESS: Read back from work bucket:", data.decode())
# 4. ACCESS OTHER TENANT BUCKET (Should FAIL with Access Denied)
try:
s3.list_objects_v2(Bucket="work-tenant-b")
print("ERROR: Accessed tenant-b bucket!")
except Exception as e:
print("SUCCESS: Access to tenant-b blocked:", e)