|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +import json |
| 5 | +import os |
| 6 | +import glob |
| 7 | +import pprint |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | +from pathlib import Path |
| 11 | +import zipfile |
| 12 | + |
| 13 | +AAD_ID = os.environ['AZURE_AAD_ID'] |
| 14 | +WORKSPACE = Path(os.environ["WORKSPACE"]) |
| 15 | +TENANT_ID = os.environ['TENANT_ID'] |
| 16 | +KEY_CODE = os.environ['KEY_CODE'] |
| 17 | + |
| 18 | +esrp_tool = os.path.join("esrp", "tools", "EsrpClient.exe") |
| 19 | +SOURCE = WORKSPACE / "osx-x64" |
| 20 | +DESTINATION = WORKSPACE / "Mac_signed" |
| 21 | + |
| 22 | +zip_file = SOURCE / "mac_dylibs.zip" |
| 23 | +extensions = [".dylib",".a",".Cli"] |
| 24 | + |
| 25 | + # zipping the files |
| 26 | +with zipfile.ZipFile(zip_file, 'w', zipfile.ZIP_DEFLATED) as zip_obj: |
| 27 | + for path in Path(SOURCE).iterdir(): |
| 28 | + if path.suffix in extensions and path.is_file(): |
| 29 | + zip_obj.write(path, path.relative_to(SOURCE)) |
| 30 | + |
| 31 | +if not zip_file.exists(): |
| 32 | + sys.exit("Error: cannot find file to sign") |
| 33 | +else: |
| 34 | + print(f"Found file: {zip_file}") |
| 35 | + |
| 36 | + |
| 37 | +auth_json = { |
| 38 | + "Version": "1.0.0", |
| 39 | + "AuthenticationType": "AAD_CERT", |
| 40 | + "TenantId": TENANT_ID, |
| 41 | + "ClientId": AAD_ID, |
| 42 | + "AuthCert": { |
| 43 | + "SubjectName": f"CN={AAD_ID}.microsoft.com", |
| 44 | + "StoreLocation": "CurrentUser", |
| 45 | + "StoreName": "My", |
| 46 | + }, |
| 47 | + "RequestSigningCert": { |
| 48 | + "SubjectName": f"CN={AAD_ID}", |
| 49 | + "StoreLocation": "CurrentUser", |
| 50 | + "StoreName": "My", |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +input_json = { |
| 55 | + "Version": "1.0.0", |
| 56 | + "SignBatches": [ |
| 57 | + { |
| 58 | + "SourceLocationType": "UNC", |
| 59 | + "SourceRootDirectory": SOURCE, |
| 60 | + "DestinationLocationType": "UNC", |
| 61 | + "DestinationRootDirectory": DESTINATION, |
| 62 | + "SignRequestFiles": [ |
| 63 | + { |
| 64 | + "CustomerCorrelationId": "01A7F55F-6CDD-4123-B255-77E6F212CDAD", |
| 65 | + "SourceLocation": str(zip_file), |
| 66 | + "DestinationLocation": str(DESTINATION / "mac_dylibs.zip"), |
| 67 | + } |
| 68 | + ], |
| 69 | + "SigningInfo": { |
| 70 | + "Operations": [ |
| 71 | + { |
| 72 | + "KeyCode": KEY_CODE, |
| 73 | + "OperationCode": "MacAppDeveloperSign", |
| 74 | + "Parameters" : { |
| 75 | + "OpusName" : "Microsoft", |
| 76 | + "OpusInfo" : "http://www.microsoft.com", |
| 77 | + "FileDigest" : "/fd \"SHA256\"", |
| 78 | + "PageHash" : "/NPH", |
| 79 | + "TimeStamp" : "/tr \"http://rfc3161.gtm.corp.microsoft.com/TSS/HttpTspServer\" /td sha256" |
| 80 | + }, |
| 81 | + "ToolName": "sign", |
| 82 | + "ToolVersion": "1.0", |
| 83 | + } |
| 84 | + ] |
| 85 | + |
| 86 | + } |
| 87 | + } |
| 88 | + ] |
| 89 | +} |
| 90 | + |
| 91 | +policy_json = { |
| 92 | + "Version": "1.0.0", |
| 93 | + "Intent": "production release", |
| 94 | + "ContentType": "Signed Binaries", |
| 95 | +} |
| 96 | + |
| 97 | +configs = [ |
| 98 | + ("auth.json", auth_json), |
| 99 | + ("input.json", input_json), |
| 100 | + ("policy.json", policy_json), |
| 101 | +] |
| 102 | + |
| 103 | +for filename, data in configs: |
| 104 | + with open(filename, 'w') as fp: |
| 105 | + json.dump(data, fp) |
| 106 | + |
| 107 | +# Run ESRP Client |
| 108 | +esrp_out = "esrp_out.json" |
| 109 | +result = subprocess.run( |
| 110 | + [esrp_tool, "sign", |
| 111 | + "-a", "auth.json", |
| 112 | + "-i", "input.json", |
| 113 | + "-p", "policy.json", |
| 114 | + "-o", esrp_out, |
| 115 | + "-l", "Verbose"], |
| 116 | + cwd=WORKSPACE) |
| 117 | + |
| 118 | +if result.returncode != 0: |
| 119 | + sys.exit("Failed to run ESRPClient.exe") |
| 120 | + |
| 121 | +if os.path.isfile(esrp_out): |
| 122 | + print("ESRP output json:") |
| 123 | + with open(esrp_out, 'r') as fp: |
| 124 | + pprint.pp(json.load(fp)) |
| 125 | + |
| 126 | +signed_zip_file = os.path.join(DESTINATION, "mac_dylibs.zip") |
| 127 | + |
| 128 | +if not signed_zip_file: |
| 129 | + sys.exit("Error: no signed file found") |
| 130 | +else: |
| 131 | + print(f"The Zipped file with signed binaries: {signed_zip_file}") |
| 132 | + |
| 133 | +#Extracting all the signed file and removing the zip file to cleanup temporary files |
| 134 | +with zipfile.ZipFile(signed_zip_file, 'r') as zipObj: |
| 135 | + zipObj.extractall(DESTINATION) |
| 136 | + |
| 137 | +signed_zip_file.unlink() |
| 138 | + |
| 139 | +#list of signed files |
| 140 | +signed_binaries = [f for f in DESTINATION if os.path.isfile(f)] |
| 141 | + |
| 142 | +if not signed_binaries: |
| 143 | + sys.exit("Error: no signed files found") |
| 144 | + |
| 145 | +print(f"Signed {len(signed_binaries)} files:") |
| 146 | +pprint.pp(signed_binaries) |
0 commit comments