-
Notifications
You must be signed in to change notification settings - Fork 347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ingest Go dependencies using Semgrep API #1368
Merged
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
defe865
Ingest dependencies using Semgrep API
hanzo eb79a6a
rm
hanzo dadaa2a
basic ingestion working
hanzo d549c02
add data models and parameterize functions
hanzo ed0573d
remove manual indices
hanzo 0cc44a3
cleanup
hanzo 6f995fc
Add CNCF to docs (#1369)
achantavy 8cdc540
0.95.0rc1 (#1370)
achantavy cdbd8cf
refactor sync functions into separate files
hanzo 6ba5000
Merge branch 'master' into semgrep-dependencies
hanzo 681d7af
fix test
hanzo d71ed6c
test fix
hanzo 6a7926e
move slug to common params
hanzo 9000aaa
add sync_deployment method
hanzo 55e4e04
better warnings
hanzo 3250dd9
move deployment test to separate file
hanzo 3164bcf
rm unused import
hanzo 53c5e0d
undo test changes
hanzo e58ddaf
move test functions to common.py
hanzo 61bffe8
refactor tests, start on deps tests
hanzo 6a7985b
tests
hanzo feeab74
tweak test
hanzo 4276faf
rename test
hanzo ea638fd
add back create_dependency_nodes
hanzo 6f44fbd
fix test
hanzo 0daf3ec
add specifier property
hanzo ce2c15a
update schema
hanzo c31b1d6
rename var
hanzo edf7c59
Merge branch 'master' into semgrep-dependencies
hanzo aa4a403
address review feedback
hanzo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
import logging | ||
from typing import Any | ||
from typing import Callable | ||
from typing import Dict | ||
from typing import List | ||
|
||
import neo4j | ||
import requests | ||
from requests.exceptions import HTTPError | ||
from requests.exceptions import ReadTimeout | ||
|
||
from cartography.client.core.tx import load | ||
from cartography.graph.job import GraphJob | ||
from cartography.models.semgrep.dependencies import SemgrepGoLibrarySchema | ||
from cartography.stats import get_stats_client | ||
from cartography.util import merge_module_sync_metadata | ||
from cartography.util import timeit | ||
|
||
logger = logging.getLogger(__name__) | ||
stat_handler = get_stats_client(__name__) | ||
_PAGE_SIZE = 10000 | ||
_TIMEOUT = (60, 60) | ||
_MAX_RETRIES = 3 | ||
|
||
|
||
@timeit | ||
def get_dependencies(semgrep_app_token: str, deployment_id: str, ecosystems: List[str]) -> List[Dict[str, Any]]: | ||
""" | ||
Gets all dependencies for the given ecosystems within the given Semgrep deployment ID. | ||
param: semgrep_app_token: The Semgrep App token to use for authentication. | ||
param: deployment_id: The Semgrep deployment ID to use for retrieving dependencies. | ||
param: ecosystems: One or more ecosystems to import dependencies from, e.g. "gomod" or "pypi". | ||
The list of supported ecosystems is defined here: | ||
https://semgrep.dev/api/v1/docs/#tag/SupplyChainService/operation/semgrep_app.products.sca.handlers.dependency.list_dependencies_conexxion | ||
""" | ||
all_deps = [] | ||
deps_url = f"https://semgrep.dev/api/v1/deployments/{deployment_id}/dependencies" | ||
has_more = True | ||
page = 0 | ||
retries = 0 | ||
headers = { | ||
"Content-Type": "application/json", | ||
"Authorization": f"Bearer {semgrep_app_token}", | ||
} | ||
|
||
request_data: dict[str, Any] = { | ||
"pageSize": _PAGE_SIZE, | ||
"dependencyFilter": { | ||
"ecosystem": ecosystems, | ||
}, | ||
} | ||
|
||
logger.info(f"Retrieving Semgrep dependencies for deployment '{deployment_id}'.") | ||
while has_more: | ||
try: | ||
response = requests.post(deps_url, json=request_data, headers=headers, timeout=_TIMEOUT) | ||
response.raise_for_status() | ||
data = response.json() | ||
except (ReadTimeout, HTTPError): | ||
logger.warning(f"Failed to retrieve Semgrep dependencies for page {page}. Retrying...") | ||
retries += 1 | ||
if retries >= _MAX_RETRIES: | ||
raise | ||
continue | ||
deps = data.get("dependencies", []) | ||
has_more = data.get("hasMore", False) | ||
logger.info(f"Processed page {page} of Semgrep dependencies.") | ||
all_deps.extend(deps) | ||
retries = 0 | ||
page += 1 | ||
request_data["cursor"] = data.get("cursor") | ||
|
||
logger.info(f"Retrieved {len(all_deps)} Semgrep dependencies in {page} pages.") | ||
return all_deps | ||
|
||
|
||
def transform_dependencies(raw_deps: List[Dict[str, Any]]) -> List[Dict[str, Any]]: | ||
""" | ||
Transforms the raw dependencies response from Semgrep API into a list of dicts | ||
that can be used to create the Dependency nodes. | ||
""" | ||
|
||
""" | ||
sample raw_dep as of November 2024: | ||
{ | ||
"repositoryId": "123456", | ||
"definedAt": { | ||
"path": "go.mod", | ||
"startLine": "6", | ||
"endLine": "6", | ||
"url": "https://github.com/org/repo-name/blob/00000000000000000000000000000000/go.mod#L6", | ||
"committedAt": "1970-01-01T00:00:00Z", | ||
"startCol": "0", | ||
"endCol": "0" | ||
}, | ||
"transitivity": "DIRECT", | ||
"package": { | ||
"name": "github.com/foo/bar", | ||
"versionSpecifier": "1.2.3" | ||
}, | ||
"ecosystem": "gomod", | ||
"licenses": [], | ||
"pathToTransitivity": [] | ||
}, | ||
""" | ||
deps = [] | ||
for raw_dep in raw_deps: | ||
|
||
# We could call a different endpoint to get all repo IDs and store a mapping of repo ID to URL, | ||
# but it's much simpler to just extract the URL from the definedAt field. | ||
repo_url = raw_dep["definedAt"]["url"].split("/blob/", 1)[0] | ||
|
||
name = raw_dep["package"]["name"] | ||
version = raw_dep["package"]["versionSpecifier"] | ||
id = f"{name}|{version}" | ||
|
||
# As of November 2024, Semgrep does not import dependencies with version specifiers such as >, <, etc. | ||
# For now, hardcode the specifier to ==<version> to align with GitHub-sourced Python dependencies. | ||
# If Semgrep eventually supports version specifiers, update this line accordingly. | ||
specifier = f"=={version}" | ||
|
||
deps.append({ | ||
# existing dependency properties: | ||
"id": id, | ||
"name": name, | ||
"specifier": specifier, | ||
"version": version, | ||
"repo_url": repo_url, | ||
|
||
# Semgrep-specific properties: | ||
"ecosystem": raw_dep["ecosystem"], | ||
"transitivity": raw_dep["transitivity"].lower(), | ||
"url": raw_dep["definedAt"]["url"], | ||
}) | ||
|
||
return deps | ||
|
||
|
||
@timeit | ||
def load_dependencies( | ||
neo4j_session: neo4j.Session, | ||
dependency_schema: Callable, | ||
dependencies: List[Dict], | ||
deployment_id: str, | ||
update_tag: int, | ||
) -> None: | ||
logger.info(f"Loading {len(dependencies)} {dependency_schema().label} objects into the graph.") | ||
load( | ||
neo4j_session, | ||
dependency_schema(), | ||
dependencies, | ||
lastupdated=update_tag, | ||
DEPLOYMENT_ID=deployment_id, | ||
) | ||
|
||
|
||
@timeit | ||
def cleanup( | ||
neo4j_session: neo4j.Session, | ||
common_job_parameters: Dict[str, Any], | ||
) -> None: | ||
logger.info("Running Semgrep Go Library cleanup job.") | ||
go_libraries_cleanup_job = GraphJob.from_node_schema( | ||
SemgrepGoLibrarySchema(), common_job_parameters, | ||
) | ||
go_libraries_cleanup_job.run(neo4j_session) | ||
|
||
|
||
@timeit | ||
def sync_dependencies( | ||
neo4j_session: neo4j.Session, | ||
semgrep_app_token: str, | ||
update_tag: int, | ||
common_job_parameters: Dict[str, Any], | ||
) -> None: | ||
|
||
deployment_id = common_job_parameters.get("DEPLOYMENT_ID") | ||
if not deployment_id: | ||
logger.warning( | ||
"Missing Semgrep deployment ID, ensure that sync_deployment() has been called." | ||
"Skipping Semgrep dependencies sync job.", | ||
) | ||
return | ||
|
||
logger.info("Running Semgrep dependencies sync job.") | ||
|
||
# fetch and load dependencies for the Go ecosystem | ||
raw_go_deps = get_dependencies(semgrep_app_token, deployment_id, ecosystems=["gomod"]) | ||
go_deps = transform_dependencies(raw_go_deps) | ||
load_dependencies(neo4j_session, SemgrepGoLibrarySchema, go_deps, deployment_id, update_tag) | ||
|
||
cleanup(neo4j_session, common_job_parameters) | ||
|
||
merge_module_sync_metadata( | ||
neo4j_session=neo4j_session, | ||
group_type='Semgrep', | ||
group_id=deployment_id, | ||
synced_type='SemgrepDependency', | ||
update_tag=update_tag, | ||
stat_handler=stat_handler, | ||
) |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I named this deployment.py instead of deployments.py to match the existing file models/deployment.py. The contents of this file have been moved here from |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import logging | ||
from typing import Any | ||
from typing import Dict | ||
|
||
import neo4j | ||
import requests | ||
|
||
from cartography.client.core.tx import load | ||
from cartography.models.semgrep.deployment import SemgrepDeploymentSchema | ||
from cartography.stats import get_stats_client | ||
from cartography.util import timeit | ||
|
||
logger = logging.getLogger(__name__) | ||
stat_handler = get_stats_client(__name__) | ||
_TIMEOUT = (60, 60) | ||
hanzo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
@timeit | ||
def get_deployment(semgrep_app_token: str) -> Dict[str, Any]: | ||
""" | ||
Gets the deployment associated with the passed Semgrep App token. | ||
param: semgrep_app_token: The Semgrep App token to use for authentication. | ||
""" | ||
deployment = {} | ||
deployment_url = "https://semgrep.dev/api/v1/deployments" | ||
headers = { | ||
"Content-Type": "application/json", | ||
"Authorization": f"Bearer {semgrep_app_token}", | ||
} | ||
response = requests.get(deployment_url, headers=headers, timeout=_TIMEOUT) | ||
response.raise_for_status() | ||
|
||
data = response.json() | ||
deployment["id"] = data["deployments"][0]["id"] | ||
deployment["name"] = data["deployments"][0]["name"] | ||
deployment["slug"] = data["deployments"][0]["slug"] | ||
|
||
return deployment | ||
|
||
|
||
@timeit | ||
def load_semgrep_deployment( | ||
neo4j_session: neo4j.Session, deployment: Dict[str, Any], update_tag: int, | ||
) -> None: | ||
logger.info(f"Loading SemgrepDeployment {deployment} into the graph.") | ||
load( | ||
neo4j_session, | ||
SemgrepDeploymentSchema(), | ||
[deployment], | ||
lastupdated=update_tag, | ||
) | ||
|
||
|
||
@timeit | ||
def sync_deployment( | ||
neo4j_session: neo4j.Session, | ||
semgrep_app_token: str, | ||
update_tag: int, | ||
common_job_parameters: Dict[str, Any], | ||
) -> None: | ||
|
||
semgrep_deployment = get_deployment(semgrep_app_token) | ||
deployment_id = semgrep_deployment["id"] | ||
deployment_slug = semgrep_deployment["slug"] | ||
load_semgrep_deployment(neo4j_session, semgrep_deployment, update_tag) | ||
common_job_parameters["DEPLOYMENT_ID"] = deployment_id | ||
common_job_parameters["DEPLOYMENT_SLUG"] = deployment_slug |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I considered what might cause this string split to give the wrong result, but I think it's very unlikely. Even if a repo stored its go.mod file inside a directory named
/blob/
(which would be really strange), the url returned from semgrep would be something likehttps://github.com/org/repo/blob/sha/blob/go.mod#L112
, so repo_url would still be set tohttps://github.com/org/repo
as expected.