1+ import json
2+ import os
3+ import glob
4+ import pprint
5+ import subprocess
6+ import sys
7+
8+ esrp_tool = os .path .join ("esrp" , "tools" , "EsrpClient.exe" )
9+
10+ AAD_ID = "38aa33bc-a7e7-4007-bfb2-e8b17f04aadc"
11+ WORKSPACE = os .environ ['GITHUB_WORKSPACE' ].strip ()
12+ ARTIFACTS_DIR = os .environ ['ARTIFACTS_DIR' ].strip ()
13+
14+ def main ():
15+ source_root_location = os .path .join (WORKSPACE , ARTIFACTS_DIR , "unsigned" )
16+ destination_location = os .path .join (WORKSPACE , ARTIFACTS_DIR )
17+
18+ files = glob .glob (os .path .join (source_root_location , "*.deb" ))
19+
20+ print ("Found files:" )
21+ pprint .pp (files )
22+
23+ if len (files ) < 1 or not files [0 ].endswith (".deb" ):
24+ print ("Error: cannot find .deb to sign" )
25+ exit (1 )
26+
27+ file_to_sign = os .path .basename (files [0 ])
28+
29+ auth_json = {
30+ "Version" : "1.0.0" ,
31+ "AuthenticationType" : "AAD_CERT" ,
32+ "TenantId" : "72f988bf-86f1-41af-91ab-2d7cd011db47" ,
33+ "ClientId" : AAD_ID ,
34+ "AuthCert" : {
35+ "SubjectName" : f"CN={ AAD_ID } .microsoft.com" ,
36+ "StoreLocation" : "LocalMachine" ,
37+ "StoreName" : "My" ,
38+ },
39+ "RequestSigningCert" : {
40+ "SubjectName" : f"CN={ AAD_ID } " ,
41+ "StoreLocation" : "LocalMachine" ,
42+ "StoreName" : "My" ,
43+ }
44+ }
45+
46+ input_json = {
47+ "Version" : "1.0.0" ,
48+ "SignBatches" : [
49+ {
50+ "SourceLocationType" : "UNC" ,
51+ "SourceRootDirectory" : source_root_location ,
52+ "DestinationLocationType" : "UNC" ,
53+ "DestinationRootDirectory" : destination_location ,
54+ "SignRequestFiles" : [
55+ {
56+ "CustomerCorrelationId" : "01A7F55F-6CDD-4123-B255-77E6F212CDAD" ,
57+ "SourceLocation" : file_to_sign ,
58+ "DestinationLocation" : os .path .join ("signed" , file_to_sign ),
59+ }
60+ ],
61+ "SigningInfo" : {
62+ "Operations" : [
63+ {
64+ "KeyCode" : "CP-450779-Pgp" ,
65+ "OperationCode" : "LinuxSign" ,
66+ "Parameters" : {},
67+ "ToolName" : "sign" ,
68+ "ToolVersion" : "1.0" ,
69+ }
70+ ]
71+ }
72+ }
73+ ]
74+ }
75+
76+ policy_json = {
77+ "Version" : "1.0.0" ,
78+ "Intent" : "production release" ,
79+ "ContentType" : "Debian package" ,
80+ }
81+
82+ configs = [
83+ ("auth.json" , auth_json ),
84+ ("input.json" , input_json ),
85+ ("policy.json" , policy_json ),
86+ ]
87+
88+ for filename , data in configs :
89+ with open (filename , 'w' ) as fp :
90+ json .dump (data , fp )
91+
92+ # Run ESRP Client
93+ esrp_out = "esrp_out.json"
94+ result = subprocess .run (
95+ [esrp_tool , "sign" ,
96+ "-a" , "auth.json" ,
97+ "-i" , "input.json" ,
98+ "-p" , "policy.json" ,
99+ "-o" , esrp_out ,
100+ "-l" , "Verbose" ],
101+ cwd = WORKSPACE )
102+
103+ if result .returncode != 0 :
104+ print ("Failed to run ESRPClient.exe" )
105+ sys .exit (1 )
106+
107+ if os .path .isfile (esrp_out ):
108+ print ("ESRP output json:" )
109+ with open (esrp_out , 'r' ) as fp :
110+ pprint .pp (json .load (fp ))
111+
112+ signed_file = os .path .join (destination_location , "signed" , file_to_sign )
113+ if os .path .isfile (signed_file ):
114+ print (f"Success!\n Signed { signed_file } " )
115+
116+ if __name__ == "__main__" :
117+ main ()
0 commit comments