1+ # --------------------------------------------------------------------------------------------
2+ # Copyright (c) Microsoft Corporation. All rights reserved.
3+ # Licensed under the MIT License. See License.txt in the project root for license information.
4+ # --------------------------------------------------------------------------------------------
5+
6+ import hashlib
7+ import platform
8+ import re
9+ import requests
10+ import subprocess
11+
12+ from typing import Iterable
13+ from pathlib import Path
14+
15+ from azext_confcom .lib .paths import get_binaries_dir
16+
17+
18+ _binaries_dir = get_binaries_dir ()
19+ _cosesign1_binaries = {
20+ "Linux" : {
21+ "path" : _binaries_dir / "sign1util" ,
22+ "url" : "https://github.com/microsoft/cosesign1go/releases/download/v1.4.0/sign1util" ,
23+ "sha256" : "526b54aeb6293fc160e8fa1f81be6857300aba9641d45955f402f8b082a4d4a5" ,
24+ },
25+ "Windows" : {
26+ "path" : _binaries_dir / "sign1util.exe" ,
27+ "url" : "https://github.com/microsoft/cosesign1go/releases/download/v1.4.0/sign1util.exe" ,
28+ "sha256" : "f33cccf2b1bb8c3a495c730984b47d0f0715678981dbfe712248a2452dd53303" ,
29+ },
30+ }
31+
32+
33+ def cose_get ():
34+ for binary_info in _cosesign1_binaries .values ():
35+ cosesign1_fetch_resp = requests .get (binary_info ["url" ], verify = True )
36+ cosesign1_fetch_resp .raise_for_status ()
37+
38+ assert hashlib .sha256 (cosesign1_fetch_resp .content ).hexdigest () == binary_info ["sha256" ]
39+
40+ with open (binary_info ["path" ], "wb" ) as f :
41+ f .write (cosesign1_fetch_resp .content )
42+
43+
44+ def cose_run (args : Iterable [str ]) -> subprocess .CompletedProcess :
45+ return subprocess .run (
46+ [_cosesign1_binaries [platform .system ()]["path" ], * args ],
47+ check = True ,
48+ stdout = subprocess .PIPE ,
49+ text = True ,
50+ )
51+
52+
53+ def cose_print (file_path : Path ):
54+ return cose_run ([
55+ "print" ,
56+ "--in" , file_path .as_posix (),
57+ ]).stdout .strip ()
58+
59+
60+ def cose_get_properties (file_path : Path ):
61+ cose_print_output = cose_print (file_path )
62+ return {
63+ "iss" : re .search (r"^iss:\s*(.*)$" , cose_print_output , re .MULTILINE ).group (1 ),
64+ "feed" : re .search (r"^feed:\s*(.*)$" , cose_print_output , re .MULTILINE ).group (1 ),
65+ "payload" : re .search (r"^payload:\s*(.*)" , cose_print_output , re .MULTILINE | re .DOTALL ).group (1 ),
66+ }
0 commit comments