1
1
from argparse import ArgumentParser
2
2
from tarfile import TarFile , TarInfo , open as taropen
3
- from subprocess import run , PIPE ,STDOUT
3
+ from subprocess import run , PIPE , STDOUT
4
4
from dataclasses import dataclass
5
5
from io import BytesIO
6
6
import sys
7
7
import os
8
8
9
+
9
10
@dataclass
10
11
class Args :
11
12
bundle : str
@@ -14,8 +15,10 @@ class Args:
14
15
signing_alg : str
15
16
command : list [str ]
16
17
18
+
17
19
def parse_args () -> Args :
18
- parser = ArgumentParser (prog = "rules_opa::opa_signer" , description = "Tool to re-bundle an opa bundle with a signature file" )
20
+ parser = ArgumentParser (prog = "rules_opa::opa_signer" ,
21
+ description = "Tool to re-bundle an opa bundle with a signature file" )
19
22
20
23
parser .add_argument ("-b" , "--bundle" , required = True )
21
24
parser .add_argument ("-o" , "--output" , required = True )
@@ -33,28 +36,34 @@ def parse_args() -> Args:
33
36
ns .command ,
34
37
)
35
38
39
+
36
40
def perform_signature (args : Args ) -> str :
37
41
expected_file = ".signatures.json"
38
- completed_process = run (args .command + ['--signing-key' , args .signing_key , "--signing-alg" , args .signing_alg , "--bundle" , args .bundle ], stdout = PIPE , stderr = STDOUT )
42
+ completed_process = run (args .command + ['--signing-key' , args .signing_key , "--signing-alg" ,
43
+ args .signing_alg , "--bundle" , args .bundle ], stdout = PIPE , stderr = STDOUT )
39
44
returncode = completed_process .returncode
40
-
45
+
41
46
if returncode != 0 :
42
47
command = " " .join (completed_process .args )
43
48
stdout = completed_process .stdout .decode ()
44
- print (f"Command exited with non-zero return code { returncode } .\n { command } \n { stdout } " , file = sys .stderr )
49
+ print (
50
+ f"Command exited with non-zero return code { returncode } .\n { command } \n { stdout } " , file = sys .stderr )
45
51
sys .exit (1 )
46
-
52
+
47
53
if not os .path .exists (expected_file ):
48
54
command = " " .join (completed_process .args )
49
- print (f"File { expected_file } not found after running command:\n { command } " , file = sys .stderr )
55
+ print (
56
+ f"File { expected_file } not found after running command:\n { command } " , file = sys .stderr )
50
57
sys .exit (1 )
51
58
52
59
return expected_file
53
60
61
+
54
62
def transfer_files (output : TarFile , bundle : TarFile ):
55
63
for member in bundle .getmembers ():
56
64
output .addfile (member , bundle .extractfile (member ))
57
65
66
+
58
67
def addfile (output : TarFile , file_name : str ):
59
68
with open (file_name , mode = "rb" ) as f :
60
69
data = f .read ()
@@ -74,6 +83,8 @@ def main():
74
83
addfile (output , signature_file )
75
84
transfer_files (output , bundle )
76
85
86
+ os .chmod (args .output , 0o644 )
87
+
88
+
77
89
if __name__ == "__main__" :
78
90
main ()
79
-
0 commit comments