Skip to content

Commit 39b25e3

Browse files
committed
chore: fixes
Signed-off-by: Nathan Nguyen <[email protected]>
1 parent 228a4b6 commit 39b25e3

File tree

7 files changed

+70
-25
lines changed

7 files changed

+70
-25
lines changed

src/macaron/artifact/maven.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ def package_url(self) -> PackageURL:
7777
def from_package_url(cls, package_url: PackageURL) -> Self | None:
7878
"""Create a Maven artifact from a PackageURL.
7979
80+
Parameters
81+
----------
82+
package_url : PackageURL
83+
The PackageURL identifying a Maven artifact.
84+
8085
Returns
8186
-------
8287
Self | None
@@ -111,7 +116,18 @@ def from_artifact_name(
111116
group_id: str,
112117
version: str,
113118
) -> Self | None:
114-
"""Create a Maven artifact from a PackageURL.
119+
"""Create a Maven artifact given an artifact name.
120+
121+
The artifact type is determined based on the naming pattern of the artifact.
122+
123+
Parameters
124+
----------
125+
artifact_name : str
126+
The artifact name.
127+
group_id : str
128+
The group id.
129+
version : str
130+
The version
115131
116132
Returns
117133
-------
@@ -159,8 +175,8 @@ def get_subject_in_provenance_matching_purl(
159175
160176
Returns
161177
-------
162-
InTotoV01Subject
163-
The PackageURL identifying the matching subject.
178+
InTotoV01Subject | InTotoV1ResourceDescriptor | None
179+
The subject in the provenance matching the given PURL.
164180
"""
165181
if (maven_artifact := MavenArtifact.from_package_url(purl)) and is_witness_provenance_payload(
166182
payload=provenance_payload,

src/macaron/database/table_definitions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,8 +584,8 @@ def from_purl_and_provenance(
584584
Returns
585585
-------
586586
Self | None
587-
A ``ProvenanceSubject`` entry with the SHA256 of the provenance subject matching the
588-
given PURL.
587+
A ``ProvenanceSubject`` entry with the SHA256 digest of the provenance subject
588+
matching the given PURL.
589589
"""
590590
subject_artifact_types: list[ProvenanceSubjectPURLMatcher] = [MavenSubjectPURLMatcher]
591591

src/macaron/slsa_analyzer/analyzer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ def add_component(
565565
purl = analysis_target.parsed_purl
566566

567567
component = Component(
568-
purl=purl.to_string(),
568+
purl=str(purl),
569569
analysis=analysis,
570570
repository=repository,
571571
)

src/macaron/slsa_analyzer/checks/provenance_l3_content_check.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,15 @@ def run_check(self, ctx: AnalyzeContext) -> CheckResultData:
5858
logger.info("%s check was unable to find any expectations.", self.check_info.check_id)
5959
return CheckResultData(result_tables=[], result_type=CheckResultType.UNKNOWN)
6060

61-
if ctx.dynamic_data["provenance"] and expectation.validate(ctx.dynamic_data["provenance"]):
61+
if ctx.dynamic_data["provenance"]:
62+
if expectation.validate(ctx.dynamic_data["provenance"]):
63+
return CheckResultData(
64+
result_tables=[expectation],
65+
result_type=CheckResultType.PASSED,
66+
)
6267
return CheckResultData(
6368
result_tables=[expectation],
64-
result_type=CheckResultType.PASSED,
69+
result_type=CheckResultType.FAILED,
6570
)
6671

6772
package_registry_info_entries = ctx.dynamic_data["package_registries"]

src/macaron/slsa_analyzer/provenance/intoto/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,16 @@ def get_subject_in_provenance_matching_purl(
138138
This function assumes there is only one such subject. If there are multiple
139139
such subjects, the first matching subject is returned. However, this should not
140140
happen since the PackageURL should be specific enough to identify a single subject.
141+
142+
Parameters
143+
----------
144+
provenance_payload : InTotoPayload
145+
The provenance payload.
146+
purl : PackageURL
147+
The PackageURL identifying the matching subject.
148+
149+
Returns
150+
-------
151+
InTotoV01Subject | InTotoV1ResourceDescriptor | None
152+
The subject in the provenance matching the given PURL.
141153
"""

src/macaron/vsa/vsa.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def get_common_purl_from_artifact_purls(purl_strs: Iterable[str]) -> str | None:
179179
def create_vsa_statement(
180180
passed_components: dict[str, int],
181181
policy_content: str,
182-
) -> VsaStatement:
182+
) -> VsaStatement | None:
183183
"""Construct the Statement layer of the VSA.
184184
185185
Parameters
@@ -202,28 +202,40 @@ def create_vsa_statement(
202202
try:
203203
with Session(get_db_manager().engine) as session, session.begin():
204204
for purl, component_id in passed_components.items():
205-
query = sqlalchemy.select(ProvenanceSubject).where(ProvenanceSubject.component_id == component_id)
206205
try:
207-
provenance_subject = session.execute(query).scalars().one()
206+
provenance_subject = (
207+
session.execute(
208+
sqlalchemy.select(ProvenanceSubject).where(ProvenanceSubject.component_id == component_id)
209+
)
210+
.scalars()
211+
.one()
212+
)
208213
sha256 = provenance_subject.sha256
209-
subject: dict[str, JsonType] = {
210-
"uri": purl,
211-
}
212-
if sha256:
213-
subject["digest"] = {
214-
"sha256": sha256,
215-
}
216-
subjects.append(subject)
217-
except (sqlalchemy.orm.exc.NoResultFound, sqlalchemy.orm.exc.MultipleResultsFound) as e:
218-
logger.error(
214+
except sqlalchemy.orm.exc.NoResultFound:
215+
sha256 = None
216+
logger.debug("No digest stored for software component '%s'.", purl)
217+
except sqlalchemy.orm.exc.MultipleResultsFound as e:
218+
logger.debug(
219219
"Unexpected database query result. "
220-
"Expected exactly one result when retrieving SHA256 of a provenance subject. "
220+
"Expected no more than one result when retrieving SHA256 of a provenance subject. "
221221
"Error: %s",
222222
e,
223223
)
224+
continue
225+
226+
subject: dict[str, JsonType] = {
227+
"uri": purl,
228+
}
229+
if sha256:
230+
subject["digest"] = {
231+
"sha256": sha256,
232+
}
233+
234+
subjects.append(subject)
224235

225236
except sqlalchemy.exc.SQLAlchemyError as error:
226-
logger.critical("Database error %s", error)
237+
logger.debug("Cannot retrieve hash digest of software components: %s.", error)
238+
return None
227239

228240
return VsaStatement(
229241
_type="https://in-toto.io/Statement/v1",

tests/artifact/test_maven.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
pytest.param(
2727
"pkg:maven/com.fasterxml.jackson/[email protected]?type=sources",
2828
2,
29-
id="purl for java sources artifact",
29+
id="purl for java source artifact",
3030
),
3131
pytest.param(
3232
"pkg:maven/com.fasterxml.jackson/[email protected]?type=pom",
@@ -119,7 +119,7 @@ def test_to_maven_artifact_subject(
119119
version="2.9.9",
120120
artifact_type=MavenArtifactType.JAVA_SOURCE,
121121
),
122-
id="purl for java sources artifact",
122+
id="purl for java source artifact",
123123
),
124124
pytest.param(
125125
"pkg:maven/com.fasterxml.jackson/[email protected]?type=pom",

0 commit comments

Comments
 (0)