|
10 | 10 |
|
11 | 11 | from packageurl import PackageURL |
12 | 12 |
|
| 13 | +from macaron.slsa_analyzer.provenance.intoto import InTotoPayload |
| 14 | +from macaron.slsa_analyzer.provenance.intoto.v01 import InTotoV01Subject |
| 15 | +from macaron.slsa_analyzer.provenance.intoto.v1 import InTotoV1ResourceDescriptor |
| 16 | +from macaron.slsa_analyzer.provenance.witness import ( |
| 17 | + extract_build_artifacts_from_witness_subjects, |
| 18 | + is_witness_provenance_payload, |
| 19 | + load_witness_verifier_config, |
| 20 | +) |
| 21 | + |
13 | 22 |
|
14 | 23 | class _MavenArtifactType(NamedTuple): |
15 | 24 | filename_pattern: str |
@@ -143,3 +152,52 @@ def from_artifact_name( |
143 | 152 | artifact_type=maven_artifact_type, |
144 | 153 | ) |
145 | 154 | return None |
| 155 | + |
| 156 | + |
| 157 | +class MavenSubjectPURLMatcher: |
| 158 | + """A matcher matching a PURL identifying a Maven artifact to a provenance subject.""" |
| 159 | + |
| 160 | + @staticmethod |
| 161 | + def get_subject_in_provenance_matching_purl( |
| 162 | + provenance_payload: InTotoPayload, purl: PackageURL |
| 163 | + ) -> InTotoV01Subject | InTotoV1ResourceDescriptor | None: |
| 164 | + """Get the subject in the provenance matching the PURL. |
| 165 | +
|
| 166 | + In this case where the provenance is assumed to be built from a Java project, |
| 167 | + the subject must be a Maven artifact. |
| 168 | +
|
| 169 | + Parameters |
| 170 | + ---------- |
| 171 | + provenance_payload : InTotoPayload |
| 172 | + The provenance payload. |
| 173 | + purl : PackageURL |
| 174 | + The PackageURL identifying the matching subject. |
| 175 | +
|
| 176 | + Returns |
| 177 | + ------- |
| 178 | + InTotoV01Subject | InTotoV1ResourceDescriptor | None |
| 179 | + The subject in the provenance matching the given PURL. |
| 180 | + """ |
| 181 | + if (maven_artifact := MavenArtifact.from_package_url(purl)) and is_witness_provenance_payload( |
| 182 | + payload=provenance_payload, |
| 183 | + predicate_types=load_witness_verifier_config().predicate_types, |
| 184 | + ): |
| 185 | + artifact_subjects = extract_build_artifacts_from_witness_subjects(provenance_payload) |
| 186 | + |
| 187 | + maven_artifact_subject_pairs = [] |
| 188 | + for subject in artifact_subjects: |
| 189 | + _, _, artifact_name = subject["name"].rpartition("/") |
| 190 | + artifact = MavenArtifact.from_artifact_name( |
| 191 | + artifact_name=artifact_name, |
| 192 | + group_id=maven_artifact.group_id, |
| 193 | + version=maven_artifact.version, |
| 194 | + ) |
| 195 | + if artifact is None: |
| 196 | + continue |
| 197 | + maven_artifact_subject_pairs.append((artifact, subject)) |
| 198 | + |
| 199 | + for artifact, subject in maven_artifact_subject_pairs: |
| 200 | + if artifact.package_url == purl: |
| 201 | + return subject |
| 202 | + |
| 203 | + return None |
0 commit comments