|
| 1 | +import argparse |
1 | 2 | import os
|
2 | 3 | import platform
|
3 | 4 | import shutil
|
|
6 | 7 | import zipfile
|
7 | 8 | import stat
|
8 | 9 | import sys
|
| 10 | +import yaml |
| 11 | + |
9 | 12 |
|
10 | 13 | if platform.system().lower() != 'darwin':
|
11 | 14 | print("Not a macos system, skipping macos signing.")
|
12 | 15 | sys.exit(0)
|
13 | 16 |
|
14 |
| -if len(sys.argv) < 2: |
15 |
| - print("Must provide at least 1 archive to sign.") |
16 |
| - sys.exit(1) |
17 |
| - |
18 | 17 | supported_archs = {
|
19 | 18 | 'arm64': 'arm64',
|
20 | 19 | 'x86_64': 'amd64'
|
|
25 | 24 | print(f"Unsupported platform uname arch: {arch}, must be {supported_archs.keys()}")
|
26 | 25 | sys.exit(1)
|
27 | 26 |
|
28 |
| -macnotary_name = f'darwin_{supported_archs[arch]}' |
| 27 | +expansions_file = "../expansions.yml" |
| 28 | +if not os.path.exists(expansions_file): |
| 29 | + print("Evergreen expansions file not found. Skipping macos_notary.") |
| 30 | + sys.exit(0) |
29 | 31 |
|
30 |
| -if os.environ['project'] == "mongodb-mongo-master-nightly": |
31 |
| - signing_type = 'notarizeAndSign' |
32 |
| -else: |
33 |
| - signing_type = 'sign' |
| 32 | +with open(expansions_file) as file: |
| 33 | + expansions = yaml.safe_load(file) |
| 34 | + |
| 35 | +should_sign = expansions.get("sign_macos_archive", None) |
| 36 | +if not should_sign: |
| 37 | + print("sign_macos_archive expansion not found not found or false. Skipping macos_notary.") |
| 38 | + sys.exit(0) |
| 39 | + |
| 40 | +macnotary_name = f'darwin_{supported_archs[arch]}' |
34 | 41 |
|
35 | 42 | macnotary_url = f'https://macos-notary-1628249594.s3.amazonaws.com/releases/client/latest/{macnotary_name}.zip'
|
36 | 43 | print(f'Fetching macnotary tool from: {macnotary_url}')
|
|
42 | 49 | os.chmod(f'{macnotary_name}/macnotary', st.st_mode | stat.S_IEXEC)
|
43 | 50 |
|
44 | 51 | failed = False
|
45 |
| -archives = sys.argv[1:] |
46 |
| - |
47 |
| -for archive in archives: |
48 |
| - archive_base, archive_ext = os.path.splitext(archive) |
49 |
| - unsigned_archive = f'{archive_base}_unsigned{archive_ext}' |
50 |
| - shutil.move(archive, unsigned_archive) |
51 |
| - |
52 |
| - signing_cmd = [ |
53 |
| - f'./{macnotary_name}/macnotary', |
54 |
| - '-f', f'{unsigned_archive}', |
55 |
| - '-m', f'{signing_type}', |
56 |
| - '-u', 'https://dev.macos-notary.build.10gen.cc/api', |
57 |
| - '-k', 'server', |
58 |
| - '--entitlements', 'etc/macos_entitlements.xml', |
59 |
| - '--verify', |
60 |
| - '-b', 'server.mongodb.com', |
61 |
| - '-i', f'{os.environ["task_id"]}', |
62 |
| - '-c', f'{os.environ["project"]}', |
63 |
| - '-o', f'{archive}' |
64 |
| - ] |
65 |
| - |
66 |
| - signing_env = os.environ.copy() |
67 |
| - signing_env['MACOS_NOTARY_SECRET'] = os.environ["macos_notarization_secret"] |
68 |
| - print(' '.join(signing_cmd)) |
69 |
| - p = subprocess.Popen(signing_cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=signing_env) |
70 |
| - |
71 |
| - print(f"Signing tool completed with exitcode: {p.returncode}") |
72 |
| - for line in iter(p.stdout.readline, b''): |
73 |
| - print(f'macnotary: {line.decode("utf-8").strip()}') |
74 |
| - p.wait() |
75 |
| - |
76 |
| - if p.returncode != 0: |
77 |
| - failed = True |
78 |
| - shutil.move(unsigned_archive, archive) |
79 |
| - else: |
80 |
| - os.unlink(unsigned_archive) |
| 52 | +parser = argparse.ArgumentParser( |
| 53 | + prog="MacOS Notary", |
| 54 | + description="Sign and/or notarize a tarball containing unsigned binaries.", |
| 55 | +) |
| 56 | +parser.add_argument("--archive-name", "-a", action="store", required=True) |
| 57 | +parser.add_argument("--entitlements-file", "-e", action="store", required=True) |
| 58 | +parser.add_argument("--signing-type", "-s", action="store", required=True) |
| 59 | +args = parser.parse_args() |
| 60 | +archive_name = args.archive_name |
| 61 | +entitlements_file = args.entitlements_file |
| 62 | +signing_type = args.signing_type |
| 63 | + |
| 64 | +archive_base, archive_ext = os.path.splitext(archive_name) |
| 65 | +unsigned_archive = f'{archive_base}_unsigned{archive_ext}' |
| 66 | +shutil.move(archive_name, unsigned_archive) |
| 67 | + |
| 68 | +signing_cmd = [ |
| 69 | + f'./{macnotary_name}/macnotary', |
| 70 | + '-f', f'{unsigned_archive}', |
| 71 | + '-m', f'{signing_type}', |
| 72 | + '-u', 'https://dev.macos-notary.build.10gen.cc/api', |
| 73 | + '-k', 'server', |
| 74 | + '--entitlements', entitlements_file, |
| 75 | + '--verify', |
| 76 | + '-b', 'server.mongodb.com', |
| 77 | + '-i', f'{expansions["task_id"]}', |
| 78 | + '-c', f'{expansions["project"]}', |
| 79 | + '-o', f'{archive_name}' |
| 80 | +] |
| 81 | + |
| 82 | +signing_env = os.environ.copy() |
| 83 | +signing_env['MACOS_NOTARY_SECRET'] = expansions.get("macos_notarization_secret", "") |
| 84 | +print(' '.join(signing_cmd)) |
| 85 | +p = subprocess.Popen(signing_cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=signing_env) |
| 86 | + |
| 87 | +print(f"Signing tool completed with exitcode: {p.returncode}") |
| 88 | +for line in iter(p.stdout.readline, b''): |
| 89 | + print(f'macnotary: {line.decode("utf-8").strip()}') |
| 90 | +p.wait() |
| 91 | + |
| 92 | +if p.returncode != 0: |
| 93 | + failed = True |
| 94 | + shutil.move(unsigned_archive, archive_name) |
| 95 | +else: |
| 96 | + os.unlink(unsigned_archive) |
81 | 97 |
|
82 | 98 | if failed:
|
83 | 99 | exit(1)
|
84 |
| - |
|
0 commit comments