#!/usr/bin/env python3 """Oficina ID's signing contract, without reading a profile or Keychain.""" import importlib.util import json import os from pathlib import Path import plistlib import tempfile from unittest.mock import patch root = Path(__file__).resolve().parents[1] config = json.loads((root / 'mac-updates.json').read_text()) old_default = '9U4A84C4WK.works.oficina.equipo' for relative, group, push in [ ('ios/EquipoPhone.entitlements', 'group.works.oficina', 'aps-environment'), ('macos/DeveloperID.entitlements', '9U4A84C4WK.works.oficina', 'com.apple.developer.aps-environment'), ]: values = plistlib.loads((root / relative).read_bytes()) # An unspecified access group still selects the old app-private group. assert values['keychain-access-groups'] == [old_default, '9U4A84C4WK.works.oficina.id'], relative assert values['com.apple.security.application-groups'] == [group], relative assert values['com.apple.developer.associated-domains'] == ['webcredentials:id.oficina.works'], relative assert push in values, relative assert config['bundle_id'] == 'works.oficina.equipo' kit = Path(os.environ.get('OFICINA_NATIVE_PACKAGE_PATH', root / 'apple/.build/checkouts/native')) spec = importlib.util.spec_from_file_location('mac_updates', kit / 'tools/mac_updates.py') module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) # Exercise the bundler used by build-mac.sh, with dummy bytes and mocked # signing. Never read the private profile or invoke codesign in this check. with tempfile.TemporaryDirectory() as temp: temp = Path(temp) app = temp / 'Equipo.app' (app / 'Contents').mkdir(parents=True) (app / 'Contents/Info.plist').write_bytes(plistlib.dumps({'CFBundleIdentifier': config['bundle_id']})) profile = temp / 'fixture.provisionprofile' profile.write_bytes(b'profile fixture') entitlements = root / config['entitlements'] assert module.signing_declaration(None, entitlements, profile, {}) == (entitlements, profile) with patch.object(module, 'decode_profile', return_value={}), \ patch.object(module, 'signing_entitlements', return_value=({}, [])), \ patch.object(module, 'verify_signature'), patch.object(module.subprocess, 'run'): module.sign_bundle(app, 'fixture identity', entitlements, profile) assert (app / 'Contents/embedded.provisionprofile').read_bytes() == profile.read_bytes() # Ad hoc builds neither open the real profile nor retain restricted grants. with patch.object(module, 'decode_profile', side_effect=AssertionError('opened profile')), \ patch.object(module.subprocess, 'run'): granted = module.sign_bundle(app, '-', entitlements, temp / 'absent-profile') assert 'keychain-access-groups' not in granted assert not (app / 'Contents/embedded.provisionprofile').exists() print('Oficina ID: old default Keychain group, shared grants and Mac profile embedding checked')