Skip to content

Commit

Permalink
add sync_deployment method
Browse files Browse the repository at this point in the history
Signed-off-by: Hans Wernetti <[email protected]>
  • Loading branch information
hanzo committed Oct 30, 2024
1 parent 6a7926e commit 9000aaa
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
12 changes: 2 additions & 10 deletions cartography/intel/semgrep/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

from cartography.config import Config
from cartography.intel.semgrep.dependencies import sync_dependencies
from cartography.intel.semgrep.deployment import get_deployment
from cartography.intel.semgrep.deployment import load_semgrep_deployment
from cartography.intel.semgrep.deployment import sync_deployment
from cartography.intel.semgrep.findings import sync_findings
from cartography.util import timeit

Expand All @@ -24,13 +23,6 @@ def start_semgrep_ingestion(
logger.info('Semgrep import is not configured - skipping this module. See docs to configure.')
return

# fetch and load the Semgrep deployment
semgrep_deployment = get_deployment(config.semgrep_app_token)
deployment_id = semgrep_deployment["id"]
deployment_slug = semgrep_deployment["slug"]
load_semgrep_deployment(neo4j_session, semgrep_deployment, config.update_tag)
common_job_parameters["DEPLOYMENT_ID"] = deployment_id
common_job_parameters["DEPLOYMENT_SLUG"] = deployment_slug

sync_deployment(neo4j_session, config.semgrep_app_token, config.update_tag, common_job_parameters)
sync_dependencies(neo4j_session, config.semgrep_app_token, config.update_tag, common_job_parameters)
sync_findings(neo4j_session, config.semgrep_app_token, config.update_tag, common_job_parameters)
18 changes: 18 additions & 0 deletions cartography/intel/semgrep/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,21 @@ def load_semgrep_deployment(
[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

# TODO: cleanup? the existing code doesn't seem to clean up the deployment
16 changes: 2 additions & 14 deletions tests/integration/cartography/intel/semgrep/test_findings.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import cartography.intel.semgrep.deployment
import cartography.intel.semgrep.findings
import tests.data.semgrep.sca
from cartography.intel.semgrep.deployment import load_semgrep_deployment
from cartography.intel.semgrep.deployment import sync_deployment
from cartography.intel.semgrep.findings import sync_findings
from tests.integration.util import check_nodes
from tests.integration.util import check_rels
Expand Down Expand Up @@ -99,20 +99,8 @@ def test_sync_findings(mock_get_sca_vulns, mock_get_deployment, neo4j_session):
"UPDATE_TAG": TEST_UPDATE_TAG,
}

# semgrep_deployment = cartography.intel.semgrep.deployment.get_deployment(semgrep_app_token)
# deployment_id = semgrep_deployment["id"]
# deployment_slug = semgrep_deployment["slug"]
# load_semgrep_deployment(neo4j_session, semgrep_deployment, TEST_UPDATE_TAG)
# common_job_parameters["DEPLOYMENT_ID"] = deployment_id

# # TODO: restructure tests to avoid this
semgrep_deployment = tests.data.semgrep.sca.DEPLOYMENTS
load_semgrep_deployment(neo4j_session, semgrep_deployment, TEST_UPDATE_TAG)

common_job_parameters["DEPLOYMENT_ID"] = semgrep_deployment["id"]
common_job_parameters["DEPLOYMENT_SLUG"] = semgrep_deployment["slug"]

# # Act
sync_deployment(neo4j_session, semgrep_app_token, TEST_UPDATE_TAG, common_job_parameters)
sync_findings(neo4j_session, semgrep_app_token, TEST_UPDATE_TAG, common_job_parameters)

# Assert
Expand Down

0 comments on commit 9000aaa

Please sign in to comment.