Skip to content

Commit c7ffd80

Browse files
authored
Merge pull request #961 from l3montree-dev/scanner-multiple-python-env
modifies dockerfile to create multiple python envs - making sure depe…
2 parents da03743 + b8bb908 commit c7ffd80

4 files changed

Lines changed: 60 additions & 15 deletions

File tree

Dockerfile.scanner

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,33 @@ RUN CGO_ENABLED=0 make devguard-scanner
5555
FROM alpine:3.22.1@sha256:4bcff63911fcb4448bd4fdacec207030997caf25e9bea4045fa6c8c44de311d1
5656

5757
RUN apk add --no-cache git python3
58-
RUN python3 -m venv /usr/local/bin/venv && \
59-
/usr/local/bin/venv/bin/pip install --upgrade pip && \
60-
/usr/local/bin/venv/bin/pip install semgrep checkov
58+
# Create virtualenvs
59+
ENV VENV_DIR=/opt/tools
60+
RUN python -m venv ${VENV_DIR}/semgrep && \
61+
python -m venv ${VENV_DIR}/checkov
62+
63+
# Install semgrep in its venv
64+
RUN ${VENV_DIR}/semgrep/bin/pip install --upgrade pip && \
65+
${VENV_DIR}/semgrep/bin/pip install semgrep==1.131.0
66+
67+
# Install checkov in its venv
68+
RUN ${VENV_DIR}/checkov/bin/pip install --upgrade pip && \
69+
${VENV_DIR}/checkov/bin/pip install checkov==3.2.457
70+
71+
72+
RUN cat <<EOF > /usr/local/bin/semgrep
73+
#!/bin/sh
74+
exec ${VENV_DIR}/semgrep/bin/semgrep "\$@"
75+
EOF
76+
77+
RUN chmod +x /usr/local/bin/semgrep
78+
79+
RUN cat <<EOF > /usr/local/bin/checkov
80+
#!/bin/sh
81+
exec ${VENV_DIR}/checkov/bin/checkov "\$@"
82+
EOF
83+
84+
RUN chmod +x /usr/local/bin/checkov
6185

6286
# add venv bin to path
6387
ENV PATH="/usr/local/bin/venv/bin:$PATH"

internal/core/vuln/dependency_vuln_service.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"context"
2020
"fmt"
2121
"log/slog"
22+
"slices"
2223
"time"
2324

2425
"github.com/google/uuid"
@@ -105,7 +106,16 @@ func (s *service) UserDetectedExistingVulnOnDifferentBranch(tx core.DB, scannerI
105106
ev := models.NewDetectedOnAnotherBranchEvent(dependencyVuln.CalculateHash(), models.VulnTypeDependencyVuln, "system", riskReport, scannerID, assetVersion.Name)
106107
events[i] = append(events[i], ev)
107108
// replay all events on the dependencyVuln
108-
for _, ev := range alreadyExistingEvents[i] {
109+
// but sort them by the time they were created ascending
110+
slices.SortStableFunc(events[i], func(a, b models.VulnEvent) int {
111+
if a.CreatedAt.Before(b.CreatedAt) {
112+
return -1
113+
} else if a.CreatedAt.After(b.CreatedAt) {
114+
return 1
115+
}
116+
return 0
117+
})
118+
for _, ev := range events[i] {
109119
ev.Apply(&dependencyVulns[i])
110120
}
111121
}

internal/core/vulndb/scan/scan_integration_test.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,6 @@ func TestScanning(t *testing.T) {
177177
// should be only a single vulnerability
178178
assert.Nil(t, err)
179179
assert.Len(t, vulns, 1)
180-
// mark the vuln as accepted
181-
vulns[0].State = models.VulnStateAccepted
182-
// save it
183-
err = dependencyVulnRepository.Save(nil, &vulns[0])
184-
assert.Nil(t, err)
185-
186180
// create an accepted event inside the database
187181
acceptedEvent := models.NewAcceptedEvent(vulns[0].ID, vulns[0].GetType(), "abc", "accepting the vulnerability")
188182
err = dependencyVulnRepository.ApplyAndSave(nil, &vulns[0], &acceptedEvent)
@@ -219,12 +213,27 @@ func TestScanning(t *testing.T) {
219213
newVuln = v
220214
}
221215
}
216+
222217
assert.NotEmpty(t, newVuln.Events)
223218
lastTwoEvents := newVuln.Events[len(newVuln.Events)-2:]
224-
assert.Equal(t, models.EventTypeAccepted, lastTwoEvents[0].Type)
225-
assert.Equal(t, "accepting the vulnerability", *lastTwoEvents[0].Justification)
226-
assert.Equal(t, "main", *lastTwoEvents[0].OriginalAssetVersionName)
227-
assert.Equal(t, models.EventTypeDetectedOnAnotherBranch, lastTwoEvents[1].Type)
219+
220+
// we can not really rely on the created_at since the events are created in the same second
221+
// nevertheless - one has to be the accepted event and the other the detected on different branch event
222+
var accEvent models.VulnEvent
223+
var detectedOnAnotherBranchEvent models.VulnEvent
224+
for _, ev := range lastTwoEvents {
225+
if ev.Type == models.EventTypeAccepted {
226+
accEvent = ev
227+
} else {
228+
detectedOnAnotherBranchEvent = ev
229+
}
230+
}
231+
232+
assert.NotEmpty(t, accEvent)
233+
assert.NotEmpty(t, detectedOnAnotherBranchEvent)
234+
assert.Equal(t, models.EventTypeAccepted, accEvent.Type)
235+
assert.Equal(t, "accepting the vulnerability", *accEvent.Justification)
236+
assert.Equal(t, "main", *accEvent.OriginalAssetVersionName)
228237
})
229238
}
230239

internal/database/repositories/vulnerability_repository.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ func (r *VulnerabilityRepository[T]) GetByAssetID(
112112

113113
var vulns = []T{}
114114
// get all vulnerabilities of the asset
115-
if err := r.Repository.GetDB(tx).Where("asset_id = ?", assetID).Preload("Events").Find(&vulns).Error; err != nil {
115+
if err := r.Repository.GetDB(tx).Where("asset_id = ?", assetID).Preload("Events", func(db *gorm.DB) *gorm.DB {
116+
return db.Order("vuln_events.created_at ASC")
117+
}).Find(&vulns).Error; err != nil {
116118
return nil, err
117119
}
118120
return vulns, nil

0 commit comments

Comments
 (0)