forked from mtusi2026/mtusi2026lab4
Выполнить лабораторную 4: S3-вложения и согласованность хранилищ
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
"""Anonymous access outside API and least-privilege negative checks."""
|
||||
import json
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from botocore.exceptions import ClientError
|
||||
sys.path.insert(0,str(Path(__file__).resolve().parent))
|
||||
sys.path.insert(0,str(Path(__file__).resolve().parents[1]))
|
||||
from client import Client,check,run
|
||||
|
||||
|
||||
def main():
|
||||
from app import store,object_store
|
||||
c=Client('http://lb:8080',timeout=15)
|
||||
alice,bob,eve,chat=c.bootstrap()
|
||||
att,_=c.attachment(alice,chat)
|
||||
key='originals/'+att['id']
|
||||
anonymous=[]
|
||||
for suffix in ('','/'+key):
|
||||
try:
|
||||
urllib.request.urlopen('http://s3:9000/'+object_store.bucket+suffix,timeout=3)
|
||||
raise AssertionError('private bucket allowed anonymous access')
|
||||
except urllib.error.HTTPError as exc:
|
||||
check(exc.code==403,'anonymous access failed for an unexpected reason')
|
||||
anonymous.append(exc.code)
|
||||
# MinIO returns a filtered ListBuckets response to a ListBucket principal.
|
||||
visible=[b['Name'] for b in object_store.client.list_buckets().get('Buckets',[])]
|
||||
check(set(visible)<={object_store.bucket},'unrelated bucket names exposed')
|
||||
denied=[]
|
||||
for name,operation in [('create_bucket',lambda:object_store.client.create_bucket(Bucket='must-not-create-lab4')),
|
||||
('get_bucket_policy',lambda:object_store.client.get_bucket_policy(Bucket=object_store.bucket)),
|
||||
('write_outside_prefix',lambda:object_store.client.put_object(Bucket=object_store.bucket,Key='outside-policy',Body=b'no'))]:
|
||||
try:
|
||||
operation()
|
||||
raise AssertionError('application credentials have excessive privileges: '+name)
|
||||
except ClientError as exc:
|
||||
check(exc.response['Error']['Code']=='AccessDenied','unexpected permission error')
|
||||
denied.append(name)
|
||||
# The console is bound to 127.0.0.1 inside S3 and browser UI is disabled.
|
||||
import socket
|
||||
try:
|
||||
socket.create_connection(('s3',9001),timeout=1).close()
|
||||
raise AssertionError('S3 console reachable from another container')
|
||||
except OSError:
|
||||
pass
|
||||
print(json.dumps({'status':'PASS','anonymous_bucket_and_object_status':anonymous,
|
||||
'application_admin_operations_denied':denied,'visible_bucket_names':visible,'console_reachable':False},indent=2))
|
||||
object_store.close()
|
||||
|
||||
if __name__=='__main__':
|
||||
run(main)
|
||||
Reference in New Issue
Block a user