Skip to content

Commit

Permalink
chore: address PR comments
Browse files Browse the repository at this point in the history
Signed-off-by: behnazh-w <[email protected]>
  • Loading branch information
behnazh-w committed Nov 15, 2024
1 parent f36950c commit 17f3453
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 25 deletions.
22 changes: 4 additions & 18 deletions src/macaron/repo_finder/provenance_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,6 @@ class SLSAGithubGenericBuildDefinitionV01(ProvenanceBuildDefinition):
def get_build_invocation(self, statement: InTotoV01Statement | InTotoV1Statement) -> tuple[str | None, str | None]:
"""Retrieve the build invocation information from the given statement.
This method is intended to be implemented by subclasses to extract
specific invocation details from a provenance statement.
Parameters
----------
statement : InTotoV1Statement | InTotoV01Statement
Expand Down Expand Up @@ -456,9 +453,6 @@ class SLSAGithubActionsBuildDefinitionV1(ProvenanceBuildDefinition):
def get_build_invocation(self, statement: InTotoV01Statement | InTotoV1Statement) -> tuple[str | None, str | None]:
"""Retrieve the build invocation information from the given statement.
This method is intended to be implemented by subclasses to extract
specific invocation details from a provenance statement.
Parameters
----------
statement : InTotoV1Statement | InTotoV01Statement
Expand Down Expand Up @@ -496,9 +490,6 @@ class SLSANPMCLIBuildDefinitionV2(ProvenanceBuildDefinition):
def get_build_invocation(self, statement: InTotoV01Statement | InTotoV1Statement) -> tuple[str | None, str | None]:
"""Retrieve the build invocation information from the given statement.
This method is intended to be implemented by subclasses to extract
specific invocation details from a provenance statement.
Parameters
----------
statement : InTotoV1Statement | InTotoV01Statement
Expand Down Expand Up @@ -540,9 +531,6 @@ class SLSAGCBBuildDefinitionV1(ProvenanceBuildDefinition):
def get_build_invocation(self, statement: InTotoV01Statement | InTotoV1Statement) -> tuple[str | None, str | None]:
"""Retrieve the build invocation information from the given statement.
This method is intended to be implemented by subclasses to extract
specific invocation details from a provenance statement.
Parameters
----------
statement : InTotoV1Statement | InTotoV01Statement
Expand Down Expand Up @@ -576,9 +564,6 @@ class SLSAOCIBuildDefinitionV1(ProvenanceBuildDefinition):
def get_build_invocation(self, statement: InTotoV01Statement | InTotoV1Statement) -> tuple[str | None, str | None]:
"""Retrieve the build invocation information from the given statement.
This method is intended to be implemented by subclasses to extract
specific invocation details from a provenance statement.
Parameters
----------
statement : InTotoV1Statement | InTotoV01Statement
Expand Down Expand Up @@ -613,9 +598,6 @@ class WitnessGitLabBuildDefinitionV01(ProvenanceBuildDefinition):
def get_build_invocation(self, statement: InTotoV01Statement | InTotoV1Statement) -> tuple[str | None, str | None]:
"""Retrieve the build invocation information from the given statement.
This method is intended to be implemented by subclasses to extract
specific invocation details from a provenance statement.
Parameters
----------
statement : InTotoV1Statement | InTotoV01Statement
Expand Down Expand Up @@ -666,6 +648,7 @@ def get_build_type(statement: InTotoV1Statement | InTotoV01Statement) -> str | N
if statement["predicate"] is None:
return None

# Different build provenances might store the buildType field in different sections.
if build_type := json_extract(statement["predicate"], ["buildType"], str):
return build_type

Expand Down Expand Up @@ -695,6 +678,9 @@ def find_build_def(statement: InTotoV01Statement | InTotoV1Statement) -> Provena
Raised when the build definition cannot be found in the provenance statement.
"""
build_type = ProvenancePredicate.get_build_type(statement)
if build_type is None:
raise ProvenanceError("Unable to find buildType in the provenance statement.")

build_defs: list[ProvenanceBuildDefinition] = [
SLSAGithubGenericBuildDefinitionV01(),
SLSAGithubActionsBuildDefinitionV1(),
Expand Down
2 changes: 1 addition & 1 deletion src/macaron/repo_finder/repo_finder_deps_dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def _create_urls(self, purl: PackageURL) -> list[str]:
The list of created URLs.
"""
# See https://docs.deps.dev/api/v3alpha/
base_url = f"https://api.deps.dev/v3alpha/purl/{encode(str(purl)).replace('/', '%2F')}"
base_url = f"https://api.deps.dev/v3alpha/purl/{encode(str(purl), safe='')}"

if not base_url:
return []
Expand Down
2 changes: 1 addition & 1 deletion src/macaron/slsa_analyzer/checks/build_as_code_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def run_check(self, ctx: AnalyzeContext) -> CheckResultData:
job = callee.caller

# We always expect the caller of the node that calls a third-party
# or Reusable GitHub Action to be be a GitHubJobNode.
# or Reusable GitHub Action to be a GitHubJobNode.
if not isinstance(job, GitHubJobNode):
continue

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ def workflow_run_deleted(self, timestamp: datetime) -> bool:
# TODO: change this check if this issue is resolved:
# https://github.com/orgs/community/discussions/138249
if datetime.now(timezone.utc) - timedelta(days=400) > timestamp:
logger.debug("Artifact published at %s is older than 410 days.", timestamp)
logger.debug("Artifact published at %s is older than 400 days.", timestamp)
return True

return False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ def find_publish_timestamp(self, purl: str, registry_url: str | None = None) ->
purl_object = PackageURL.from_string(purl)
except ValueError as error:
logger.debug("Could not parse PURL: %s", error)

if not purl_object.version:
raise InvalidHTTPResponseError("The PackageURL of the software component misses version.")

query_params = [f"q=g:{purl_object.namespace}", f"a:{purl_object.name}", f"v:{purl_object.version}"]

try:
Expand All @@ -230,7 +234,7 @@ def find_publish_timestamp(self, purl: str, registry_url: str | None = None) ->
raise InvalidHTTPResponseError("Failed to construct the search URL for Maven Central.") from error

response = send_get_http_raw(url, headers=None, timeout=self.request_timeout)
if response and response.status_code == 200:
if response:
try:
res_obj = response.json()
except requests.exceptions.JSONDecodeError as error:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def find_publish_timestamp(self, purl: str, registry_url: str | None = None) ->
# is available for subsequent processing.

base_url_parsed = urllib.parse.urlparse(registry_url or "https://api.deps.dev")
path_params = "/".join(["v3alpha", "purl", encode(purl).replace("/", "%2F")])
path_params = "/".join(["v3alpha", "purl", encode(purl, safe="")])
try:
url = urllib.parse.urlunsplit(
urllib.parse.SplitResult(
Expand Down Expand Up @@ -118,8 +118,8 @@ def find_publish_timestamp(self, purl: str, registry_url: str | None = None) ->
logger.debug("Found timestamp: %s.", timestamp)

try:
return datetime.fromisoformat(timestamp.replace("Z", "+00:00"))
except (OverflowError, OSError) as error:
return datetime.fromisoformat(timestamp)
except ValueError as error:
raise InvalidHTTPResponseError(f"The timestamp returned by {url} is invalid") from error

raise InvalidHTTPResponseError(f"Invalid response from deps.dev for {url}.")

0 comments on commit 17f3453

Please sign in to comment.