77
88import pexpect
99
10+ from sign_node .utils .locking import exclusive_lock
11+ from sign_node .utils .pgp_utils import restart_gpg_agent
12+
1013__all__ = [
1114 "sign_rpm_package" ,
1215 "PackageSignError" ,
@@ -19,8 +22,14 @@ class PackageSignError(Exception):
1922 pass
2023
2124
22- def sign_rpm_package (path , keyid , password , sign_files = False ,
23- sign_files_cert_path = '/etc/pki/ima/ima-sign.key' ):
25+ def sign_rpm_package (
26+ path ,
27+ keyid ,
28+ password ,
29+ sign_files = False ,
30+ sign_files_cert_path = '/etc/pki/ima/ima-sign.key' ,
31+ locks_dir_path : str = '/tmp/gpg_locks' ,
32+ ):
2433 """
2534 Signs an RPM package.
2635
@@ -55,19 +64,23 @@ def sign_rpm_package(path, keyid, password, sign_files=False,
5564 logging .debug ('Deleting signature from %s' , pkg_path )
5665 code , out , err = plumbum .local ['rpmsign' ].run (
5766 args = ('--delsign' , pkg_path ),
58- retcode = None
67+ retcode = None ,
5968 )
6069 logging .debug ('Command result: %d, %s\n %s' , code , out , err )
6170 if code != 0 :
6271 full_out = '\n ' .join ((out , err ))
63- raise PackageSignError (f'Cannot delete package signature: { full_out } ' )
64- out , status = pexpect .run (
65- command = final_cmd ,
66- events = {"Enter passphrase:.*" : f"{ password } \r " },
67- env = {"LC_ALL" : "en_US.UTF-8" },
68- timeout = 100000 ,
69- withexitstatus = True ,
70- )
72+ raise PackageSignError (
73+ f'Cannot delete package signature: { full_out } '
74+ )
75+ with exclusive_lock (locks_dir_path , keyid ):
76+ out , status = pexpect .run (
77+ command = final_cmd ,
78+ events = {"Enter passphrase:.*" : f"{ password } \r " },
79+ env = {"LC_ALL" : "en_US.UTF-8" },
80+ timeout = 100000 ,
81+ withexitstatus = True ,
82+ )
83+ restart_gpg_agent ()
7184 if status is None :
7285 message = (
7386 f"The RPM signing command is failed with timeout."
@@ -79,7 +92,10 @@ def sign_rpm_package(path, keyid, password, sign_files=False,
7992 logging .error (
8093 "The RPM signing command is failed with %s exit code."
8194 "\n Command: %s\n Output:\n %s.\n Traceback: %s" ,
82- status , final_cmd , out , traceback .format_exc ()
95+ status ,
96+ final_cmd ,
97+ out ,
98+ traceback .format_exc (),
8399 )
84100 raise PackageSignError (
85101 f"RPM sign failed with { status } exit code.\n "
0 commit comments