Skip to content
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

Fix scan status #1782

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions deepfence_worker/cronjobs/neo4j.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/deepfence/ThreatMapper/deepfence_utils/log"
"github.com/deepfence/ThreatMapper/deepfence_utils/utils"
"github.com/neo4j/neo4j-go-driver/v4/neo4j"
ingestersUtil "github.com/deepfence/ThreatMapper/deepfence_utils/utils/ingesters"
)

const (
Expand Down Expand Up @@ -219,7 +220,7 @@ func CleanUpDB(ctx context.Context, task *asynq.Task) error {
if _, err = session.Run(`
MATCH (n:ContainerImage)
WHERE n.active = false
AND ((NOT exists((n) <-[:SCANNED]-())
AND ((NOT exists((n) <-[:SCANNED]-())
AND n.updated_at < TIMESTAMP() - $delete_threshold_ms)
OR n.updated_at < TIMESTAMP()-$old_time_ms)
WITH n LIMIT 10000
Expand All @@ -235,8 +236,8 @@ func CleanUpDB(ctx context.Context, task *asynq.Task) error {
if _, err = session.Run(`
MATCH (n:Container)
WHERE n.active = false
AND ((NOT exists((n) <-[:SCANNED]-())
AND n.updated_at < TIMESTAMP() - $delete_threshold_ms)
AND ((NOT exists((n) <-[:SCANNED]-())
AND n.updated_at < TIMESTAMP() - $delete_threshold_ms)
OR n.updated_at < TIMESTAMP()-$old_time_ms)
WITH n LIMIT 10000
DETACH DELETE n`,
Expand Down Expand Up @@ -282,17 +283,21 @@ func CleanUpDB(ctx context.Context, task *asynq.Task) error {
return err
}

if _, err = session.Run(`
MATCH (n) -[:SCANNED]-> ()
WHERE n.retries >= 3
WITH n LIMIT 10000
SET n.status = $new_status`,
map[string]interface{}{
"time_ms": dbScanTimeout.Milliseconds(),
"new_status": utils.ScanStatusFailed,
}, txConfig); err != nil {
log.Error().Msgf("Error in Clean up DB task: %v", err)
return err
for ts := range ingestersUtil.ScanStatusField {
if _, err = session.Run(`
MATCH (n:`+string(ts)+`) -[:SCANNED]-> (r)
WHERE n.retries >= 3
WITH n, r LIMIT 10000
SET n.status = $new_status,
r.`+ingestersUtil.ScanStatusField[ts]+`=n.status,
r.`+ingestersUtil.LatestScanIDField[ts]+`=n.node_id`,
map[string]interface{}{
"time_ms": dbScanTimeout.Milliseconds(),
"new_status": utils.ScanStatusFailed,
}, txConfig); err != nil {
log.Error().Msgf("Error in Clean up DB task: %v", err)
return err
}
}

if _, err = session.Run(`
Expand Down
10 changes: 5 additions & 5 deletions deepfence_worker/ingesters/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func CommitFuncStatus[Status any](ts utils.Neo4jScanType) func(ns string, data [
n.status_message = row.scan_message,
n.updated_at = TIMESTAMP()
WITH n
OPTIONAL MATCH (m) -[:DETECTED]- (n)
OPTIONAL MATCH (n) -[:DETECTED]-> (m)
WITH n, count(m) as m_count
MATCH (n) -[:SCANNED]- (r)
SET r.` + ingestersUtil.ScanStatusField[ts] + `=n.status,
Expand Down Expand Up @@ -214,10 +214,10 @@ func getEntityIdFromScanID(scanId, scanType string,
entityId := ""
query := `MATCH (s:` + scanType + `{node_id:'` + scanId + `'}) - [:SCANNED] -> (n)
WITH labels(n) as label, n
RETURN
CASE
WHEN 'ContainerImage' IN label or 'Container' in label
THEN [(ci:ContainerImage{node_id:n.docker_image_id}) - [:IS] -> (cis) | cis.node_id]
RETURN
CASE
WHEN 'ContainerImage' IN label or 'Container' in label
THEN [(ci:ContainerImage{node_id:n.docker_image_id}) - [:IS] -> (cis) | cis.node_id]
ELSE [n.node_id]
END`
res, err := tx.Run(query, map[string]interface{}{})
Expand Down
Loading