| 1 | #!/usr/bin/env python3 |
| 2 | """Oficina ID's signing contract, without reading a profile or Keychain.""" |
| 3 | import importlib.util |
| 4 | import json |
| 5 | import os |
| 6 | from pathlib import Path |
| 7 | import plistlib |
| 8 | import tempfile |
| 9 | from unittest.mock import patch |
| 10 | |
| 11 | root = Path(__file__).resolve().parents[1] |
| 12 | config = json.loads((root / 'mac-updates.json').read_text()) |
| 13 | old_default = '9U4A84C4WK.works.oficina.equipo' |
| 14 | for relative, group, push in [ |
| 15 | ('ios/EquipoPhone.entitlements', 'group.works.oficina', 'aps-environment'), |
| 16 | ('macos/DeveloperID.entitlements', '9U4A84C4WK.works.oficina', 'com.apple.developer.aps-environment'), |
| 17 | ]: |
| 18 | values = plistlib.loads((root / relative).read_bytes()) |
| 19 | # An unspecified access group still selects the old app-private group. |
| 20 | assert values['keychain-access-groups'] == [old_default, '9U4A84C4WK.works.oficina.id'], relative |
| 21 | assert values['com.apple.security.application-groups'] == [group], relative |
| 22 | assert values['com.apple.developer.associated-domains'] == ['webcredentials:id.oficina.works'], relative |
| 23 | assert push in values, relative |
| 24 | assert config['bundle_id'] == 'works.oficina.equipo' |
| 25 | |
| 26 | kit = Path(os.environ.get('OFICINA_NATIVE_PACKAGE_PATH', root / 'apple/.build/checkouts/native')) |
| 27 | spec = importlib.util.spec_from_file_location('mac_updates', kit / 'tools/mac_updates.py') |
| 28 | module = importlib.util.module_from_spec(spec) |
| 29 | spec.loader.exec_module(module) |
| 30 | # Exercise the bundler used by build-mac.sh, with dummy bytes and mocked |
| 31 | # signing. Never read the private profile or invoke codesign in this check. |
| 32 | with tempfile.TemporaryDirectory() as temp: |
| 33 | temp = Path(temp) |
| 34 | app = temp / 'Equipo.app' |
| 35 | (app / 'Contents').mkdir(parents=True) |
| 36 | (app / 'Contents/Info.plist').write_bytes(plistlib.dumps({'CFBundleIdentifier': config['bundle_id']})) |
| 37 | profile = temp / 'fixture.provisionprofile' |
| 38 | profile.write_bytes(b'profile fixture') |
| 39 | entitlements = root / config['entitlements'] |
| 40 | assert module.signing_declaration(None, entitlements, profile, {}) == (entitlements, profile) |
| 41 | with patch.object(module, 'decode_profile', return_value={}), \ |
| 42 | patch.object(module, 'signing_entitlements', return_value=({}, [])), \ |
| 43 | patch.object(module, 'verify_signature'), patch.object(module.subprocess, 'run'): |
| 44 | module.sign_bundle(app, 'fixture identity', entitlements, profile) |
| 45 | assert (app / 'Contents/embedded.provisionprofile').read_bytes() == profile.read_bytes() |
| 46 | # Ad hoc builds neither open the real profile nor retain restricted grants. |
| 47 | with patch.object(module, 'decode_profile', side_effect=AssertionError('opened profile')), \ |
| 48 | patch.object(module.subprocess, 'run'): |
| 49 | granted = module.sign_bundle(app, '-', entitlements, temp / 'absent-profile') |
| 50 | assert 'keychain-access-groups' not in granted |
| 51 | assert not (app / 'Contents/embedded.provisionprofile').exists() |
| 52 | print('Oficina ID: old default Keychain group, shared grants and Mac profile embedding checked') |
| 53 | |