Files
MeltMind/tests/test_track_b_gino_greif_drive_acl.py
T

140 lines
7.6 KiB
Python

"""Exercise the real Greif custody gate with synthetic Drive API metadata."""
from contextlib import ExitStack
import hashlib
from pathlib import Path
from tempfile import TemporaryDirectory
import unittest
from unittest.mock import patch
import track_b_gino_greif_custody as custody
import track_b_gino_greif_drive_acl as acl
import track_b_gino_greif_entry as entry
from track_b_gino_shapenet_entry import read, save, sha
EVIDENCE = {}
OWNER_EMAIL = 'fixture-owner@example.invalid'
ARCHIVE_ID = 'fixture-archive'
class CustodyACL(unittest.TestCase):
def replay(self, mode):
with TemporaryDirectory() as folder, ExitStack() as stack:
root = Path(folder)
for name in ('contract.json','models-frozen.json','predictions-frozen.json'):
save(root/name, {})
archive = root/'gino-greif-models-and-predictions.zip'
archive.write_bytes(b'synthetic immutable archive')
digest, size = sha(archive), archive.stat().st_size
scope = dict(private_drive_folder_id=acl.PROJECT_FOLDER)
if mode == 'other-folder':
scope['private_drive_folder_id'] = 'out-of-scope'
bundle = dict(path=str(archive), sha256=digest, bytes=size,contract_sha256=sha(root/'contract.json'),
model_index_sha256=sha(root/'models-frozen.json'),
prediction_index_sha256=sha(root/'predictions-frozen.json'))
save(root/'drive-upload-index.json',bundle)
# This is the assertion that formerly bypassed all permission checks.
save(root/'drive-readback.json',dict(sha256Checksum=digest,size=str(size),
parent_id=scope['private_drive_folder_id'],file_id=ARCHIVE_ID,project_private=True,
contract_sha256=bundle['contract_sha256']))
case = 'synthetic_part'
compare = root/'comparison.json'
reference = root/'reference.json'
save(reference,dict(status='COMPLETE',rows=[dict(case=case)]+[
dict(case=f'other-{i}') for i in range(551)]))
save(compare,dict(roster=[dict(case=case,fold=0)],h2_score=dict(path=str(reference),sha256=sha(reference))))
pilot, query = root/'pilot.json',root/'query.json'
save(pilot,{})
save(query,dict(rows=[dict(case=case,included=True)]))
pins = {str(p):sha(p) for p in (pilot,query)}
c=dict(budgets={'score_cpu_seconds':3600},comparison_contract=str(compare),comparison_contract_sha256=sha(compare))
forecasts=[dict(case=case,fold=0,seed=s) for s in (0,1)]
owner=dict(id='fixture-owner-id',type='user',role='owner',emailAddress=OWNER_EMAIL)
inherited=dict(id='foreign-id',type='user',role='reader',emailAddress='foreign@example.invalid',
permissionDetails=[dict(inherited=True,inheritedFrom=acl.PROJECT_MY_DRIVE_ROOT)])
linked=dict(id='anyone',type='anyone',role='reader',allowFileDiscovery=False)
calls=[]
def provider(path, query, token):
self.assertEqual(token,'fixture-token')
calls.append((path, query.get('pageToken')))
if path.endswith('/permissions'):
self.assertEqual(query['includePermissionsForView'],'published')
if mode=='missing-acl' and path==ARCHIVE_ID+'/permissions':
return {}
if mode=='anyone-link-page2' and path==ARCHIVE_ID+'/permissions':
if query.get('pageToken')=='fixture-page-2':
return dict(permissions=[linked])
return dict(permissions=[owner],nextPageToken='fixture-page-2')
if mode=='inherited-outside' and path==acl.PROJECT_MY_DRIVE_ROOT+'/permissions':
return dict(permissions=[owner,inherited])
return dict(permissions=[owner])
item=dict(id=path,ownedByMe=True,trashed=False,version='1',permissionIds=[owner['id']],mimeType=acl.FOLDER_TYPE)
if path==ARCHIVE_ID:
return dict(item,parents=[acl.PROJECT_FOLDER],mimeType='application/zip',size=str(size),sha256Checksum=digest)
if path==acl.PROJECT_FOLDER:
return dict(item,parents=[acl.PROJECT_MY_DRIVE_ROOT])
if path=='root':
return dict(item,id=acl.PROJECT_MY_DRIVE_ROOT)
raise AssertionError('Unexpected item outside the fixed chain')
stack.enter_context(patch.dict('os.environ',CUDA_VISIBLE_DEVICES=''))
stack.enter_context(patch.object(entry,'ROOT',root))
stack.enter_context(patch.object(entry,'authorize',return_value=(scope,0.)))
for k,v in dict(PILOT=pilot,QUERY=query,PINS=pins).items():
stack.enter_context(patch.object(custody,k,v))
stack.enter_context(patch.object(custody,'frozen',return_value=([],forecasts)))
stack.enter_context(patch.object(custody.resource,'setrlimit'))
stack.enter_context(patch.object(acl,'PROJECT_OWNER_SHA256',hashlib.sha256(OWNER_EMAIL.encode()).hexdigest()))
if mode=='no-transport':
custody.os.environ.pop(acl.TOKEN_FD,None)
else:
stack.enter_context(patch.object(acl,'_credential',return_value='fixture-token'))
stack.enter_context(patch.object(acl,'_get_json',side_effect=provider))
stack.enter_context(patch.object(custody,'summary',return_value={}))
read_target=stack.enter_context(patch.object(custody,'target_array',return_value='synthetic target'))
def case_reached(*_):
proof=root/'score/drive-acl-evidence.json'
self.assertEqual(read(root/'score/custody-start.json')['drive_acl_evidence_sha256'],sha(proof))
read_target({},case)
return dict(case=case)
reader=stack.enter_context(patch.object(custody,'one_case',side_effect=case_reached))
if mode=='project-only':
self.assertEqual(custody.score(c),0)
read_target.assert_called_once()
proof=read(root/'score/drive-acl-evidence.json')
self.assertEqual([x['file_id'] for x in proof['chain']],
[ARCHIVE_ID,acl.PROJECT_FOLDER,acl.PROJECT_MY_DRIVE_ROOT])
EVIDENCE[mode] = dict(custody_reached=True,synthetic_reads=1,provider_queries=calls)
else:
with self.assertRaises(PermissionError):
custody.score(c)
reader.assert_not_called()
read_target.assert_not_called()
self.assertFalse((root/'score').exists())
EVIDENCE[mode] = dict(custody_reached=False,synthetic_reads=0,provider_queries=calls)
def test_missing_current_transport_and_missing_effective_acl_stop(self):
for mode in ('no-transport','missing-acl'):
self.replay(mode)
def test_unlisted_link_reader_is_blocked_even_on_later_page(self):
self.replay('anyone-link-page2')
self.assertIn((ARCHIVE_ID+'/permissions','fixture-page-2'), EVIDENCE['anyone-link-page2']['provider_queries'])
def test_inherited_non_project_user_is_blocked(self):
self.replay('inherited-outside')
def test_wrong_folder_is_rejected_before_provider_access(self):
self.replay('other-folder')
self.assertEqual(EVIDENCE['other-folder']['provider_queries'],[])
def test_project_acl_and_entire_ancestry_positive_control(self):
self.replay('project-only')
if __name__=='__main__':
unittest.main()