@@ -41,10 +41,10 @@ def _save_upload(uploadobj, final_package):
4141 final_package .flush ()
4242
4343
44- def _verify_package_fingerprint (package_file , signing_fingerprint ):
45- """Verify if the packge_file is signed with signing_fingerprint or not."""
44+ def _verify_package_fingerprint (path , signing_fingerprint ):
45+ """Verify if the package at path is signed with signing_fingerprint or not."""
4646 completed_process = subprocess .run (
47- ("rpm" , "-Kv" , package_file . name ),
47+ ("rpm" , "-Kv" , path ),
4848 stdout = subprocess .PIPE ,
4949 stderr = subprocess .PIPE ,
5050 text = True ,
@@ -68,26 +68,40 @@ def _verify_package_fingerprint(package_file, signing_fingerprint):
6868 return False
6969
7070
71- def _create_signed_artifact (signed_package_path , result ):
72- if not signed_package_path .exists ():
73- raise Exception (f"Signing script did not create the signed package: { result } " )
74- artifact = Artifact .init_and_validate (str (signed_package_path ))
75- artifact .save ()
76- resource = CreatedResource (content_object = artifact )
77- resource .save ()
78- return artifact
71+ def _update_signing_keys (package_file , keys ):
72+ """Return a filtered list of signing keys verified against the package file.
73+
74+ Verifies each key in keys against the package file and removes any that are not
75+ present on the package.
76+ """
77+ return [key for key in (keys or []) if _verify_package_fingerprint (package_file , key )]
7978
8079
8180def _sign_file (package_file , signing_service , signing_fingerprint ):
81+ """Sign a package and return the local path of the signed file."""
8282 result = signing_service .sign (package_file .name , pubkey_fingerprint = signing_fingerprint )
8383 signed_package_path = Path (result ["rpm_package" ])
84- return _create_signed_artifact (signed_package_path , result )
84+ if not signed_package_path .exists ():
85+ raise Exception (f"Signing script did not create the signed package: { result } " )
86+ return signed_package_path
8587
8688
8789async def _asign_file (package_file , signing_service , signing_fingerprint ):
90+ """Sign a package asynchronously and return the local path of the signed file."""
8891 result = await signing_service .asign (package_file .name , pubkey_fingerprint = signing_fingerprint )
8992 signed_package_path = Path (result ["rpm_package" ])
90- return await asyncio .to_thread (_create_signed_artifact , signed_package_path , result )
93+ if not signed_package_path .exists ():
94+ raise Exception (f"Signing script did not create the signed package: { result } " )
95+ return signed_package_path
96+
97+
98+ def _save_artifact (artifact_path ):
99+ """Save an artifact."""
100+ artifact = Artifact .init_and_validate (str (artifact_path ))
101+ artifact .save ()
102+ resource = CreatedResource (content_object = artifact )
103+ resource .save ()
104+ return artifact
91105
92106
93107def _sign_package (package , signing_service , signing_fingerprint ):
@@ -108,7 +122,7 @@ def _sign_package(package, signing_service, signing_fingerprint):
108122 _save_file (artifact_file , final_package )
109123
110124 # check if the package is already signed with our fingerprint
111- if _verify_package_fingerprint (final_package , signing_fingerprint ):
125+ if _verify_package_fingerprint (final_package . name , signing_fingerprint ):
112126 return None
113127
114128 # check if the package has been signed in the past with our fingerprint and replace
@@ -121,12 +135,19 @@ def _sign_package(package, signing_service, signing_fingerprint):
121135
122136 # create a new signed version of the package
123137 log .info (f"Signing package { package .filename } ." )
124- artifact = _sign_file (final_package , signing_service , signing_fingerprint )
138+ signed_package_path = _sign_file (final_package , signing_service , signing_fingerprint )
139+ # Compute signing keys while the signed file is still on the local filesystem.
140+ signing_keys = _update_signing_keys (
141+ str (signed_package_path ),
142+ (package .signing_keys or []) + [signing_fingerprint ],
143+ )
144+ artifact = _save_artifact (signed_package_path )
125145 signed_package = package
126146 signed_package .pk = None
127147 signed_package .pulp_id = None
128148 signed_package .pkgId = artifact .sha256
129149 signed_package .checksum_type = CHECKSUM_TYPES .SHA256
150+ signed_package .signing_keys = signing_keys
130151 signed_package .save ()
131152 ContentArtifact .objects .create (
132153 artifact = artifact ,
@@ -167,7 +188,10 @@ def sign_and_create(
167188 uploaded_package = Upload .objects .get (pk = temporary_file_pk )
168189 _save_upload (uploaded_package , final_package )
169190
170- artifact = _sign_file (final_package , package_signing_service , signing_fingerprint )
191+ signed_package_path = _sign_file (
192+ final_package , package_signing_service , signing_fingerprint
193+ )
194+ artifact = _save_artifact (signed_package_path )
171195 uploaded_package .delete ()
172196
173197 # Create Package content
@@ -179,6 +203,12 @@ def sign_and_create(
179203 # request data like we do for a file. Instead, we'll delete it here.
180204 if "upload" in data :
181205 del data ["upload" ]
206+
207+ # set the signing key in the context so that it gets added to the created package's
208+ # signing_keys field. if this package is being created then it won't have been previously
209+ # signed by Pulp.
210+ context ["signing_key" ] = signing_fingerprint
211+
182212 general_create (app_label , serializer_name , data = data , context = context , * args , ** kwargs )
183213
184214
0 commit comments