11from pathlib import Path
22from tempfile import NamedTemporaryFile
33
4- from pulpcore .plugin .models import Upload , UploadChunk , Artifact , CreatedResource , PulpTemporaryFile
5- from pulpcore .plugin .tasking import general_create
4+ from pulpcore .plugin .models import (
5+ Upload ,
6+ UploadChunk ,
7+ Artifact ,
8+ ContentArtifact ,
9+ CreatedResource ,
10+ PulpTemporaryFile ,
11+ )
12+ from pulpcore .plugin .tasking import add_and_remove , general_create
613from pulpcore .plugin .util import get_url
714
8- from pulp_deb .app .models .signing_service import AptPackageSigningService
15+ from pulp_deb .app .models .signing_service import AptPackageSigningService , DebPackageSigningResult
16+ from pulp_deb .app .models import AptRepository , Package
917
1018
1119def _save_file (fileobj , final_package ):
@@ -22,6 +30,20 @@ def _save_upload(uploadobj, final_package):
2230 final_package .flush ()
2331
2432
33+ def _sign_file (package_file , signing_service , signing_fingerprint ):
34+ result = signing_service .sign (
35+ package_file .name , pubkey_fingerprint = signing_fingerprint
36+ )
37+ signed_package_path = Path (result ["deb_package" ])
38+ if not signed_package_path .exists ():
39+ raise Exception (f"Signing script did not create the signed package: { result } " )
40+ artifact = Artifact .init_and_validate (str (signed_package_path ))
41+ artifact .save ()
42+ resource = CreatedResource (content_object = artifact )
43+ resource .save ()
44+ return artifact
45+
46+
2547def sign_and_create (
2648 app_label ,
2749 serializer_name ,
@@ -43,16 +65,7 @@ def sign_and_create(
4365 uploaded_package = Upload .objects .get (pk = temporary_file_pk )
4466 _save_upload (uploaded_package , final_package )
4567
46- result = package_signing_service .sign (
47- final_package .name , pubkey_fingerprint = signing_fingerprint
48- )
49- signed_package_path = Path (result ["deb_package" ])
50- if not signed_package_path .exists ():
51- raise Exception (f"Signing script did not create the signed package: { result } " )
52- artifact = Artifact .init_and_validate (str (signed_package_path ))
53- artifact .save ()
54- resource = CreatedResource (content_object = artifact )
55- resource .save ()
68+ artifact = _sign_file (final_package , package_signing_service , signing_fingerprint )
5669 uploaded_package .delete ()
5770 # Create Package content
5871 data ["artifact" ] = get_url (artifact )
@@ -64,3 +77,60 @@ def sign_and_create(
6477 if "upload" in data :
6578 del data ["upload" ]
6679 general_create (app_label , serializer_name , data = data , context = context , * args , ** kwargs )
80+
81+
82+ def signed_add_and_remove (
83+ repository_pk , add_content_units , remove_content_units , base_version_pk = None
84+ ):
85+ repo = AptRepository .objects .get (pk = repository_pk )
86+
87+ if repo .package_signing_service :
88+ # sign each package and replace it in the add_content_units list
89+ signed_packages = []
90+
91+ for package in Package .objects .filter (pk__in = add_content_units ):
92+ content_artifact = package .contentartifact_set .first ()
93+ artifact_obj = content_artifact .artifact
94+
95+ with NamedTemporaryFile (mode = "wb" , dir = "." , delete = False ) as final_package :
96+ artifact_file = artifact_obj .file
97+ _save_file (artifact_file , final_package )
98+
99+ # TODO: check if the package is already signed with our fingerprint
100+
101+ # check if the package has been signed in the past with our fingerprint
102+ if existing_result := DebPackageSigningResult .objects .filter (
103+ sha256 = content_artifact .artifact .sha256 ,
104+ package_signing_fingerprint = repo .package_signing_fingerprint ,
105+ ).first ():
106+ signed_packages .append (existing_result .result_id )
107+ continue
108+
109+ # create a new signed version of the package
110+ artifact = _sign_file (
111+ final_package , repo .package_signing_service , repo .package_signing_fingerprint
112+ )
113+ signed_package = package
114+ signed_package .pk = None
115+ signed_package .pulp_id = None
116+ signed_package .pkgId = artifact .sha256
117+ signed_package .checksum_type = "sha256"
118+ signed_package .save ()
119+ ContentArtifact .objects .create (
120+ artifact = artifact ,
121+ content = signed_package ,
122+ relative_path = content_artifact .relative_path ,
123+ )
124+ DebPackageSigningResult .objects .create (
125+ sha256 = artifact_obj .sha256 ,
126+ package_signing_fingerprint = repo .package_signing_fingerprint ,
127+ result = signed_package ,
128+ )
129+
130+ resource = CreatedResource (content_object = signed_package )
131+ resource .save ()
132+ signed_packages .append (signed_package .pk )
133+
134+ add_content_units = signed_packages
135+
136+ return add_and_remove (repository_pk , add_content_units , remove_content_units , base_version_pk )
0 commit comments