Skip to content
This repository has been archived by the owner on Mar 2, 2022. It is now read-only.

Commit

Permalink
Add ability to create a patch with component adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
jlarfors committed Sep 14, 2021
1 parent d292ec5 commit 699b5b7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
4 changes: 2 additions & 2 deletions adapter/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ func TestRego(t *testing.T) {
result, err := RunFromFile(
tc.adapter,
WithInputFileSlice(tc.inputFiles),
WithTracing(true),
// WithTracing(true),
)
require.NoError(t, err)
for _, trace := range result.Traces {
fmt.Println(trace)
}
t.Logf("result: %#v", result.CodeScan)
t.Logf("%s result: %#v", name, result.CodeScan)
})
}
}
2 changes: 2 additions & 0 deletions adapter/testdata/snyk.rego
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ component := [comp |
"vulnerabilities": [{
"vid": vuln.id,
"severity_score": vuln.cvssScore,
# if you want to add a patch which becomes a vulnerability review
# "patch": {"message": "test patch"},
}],
}
]
5 changes: 5 additions & 0 deletions store/api/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ type (

Vulnerability struct {
ent.VulnerabilityModelCreate `validate:"required" mapstructure:",squash"`
Patch *VulnerabilityPatch `json:"patch,omitempty" mapstructure:"patch"`
}

VulnerabilityPatch struct {
Message *string `json:"message,omitempty" validate:"required" mapstructure:"message"`
}

ComponentRead struct {
Expand Down
38 changes: 31 additions & 7 deletions store/codescan.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"github.com/valocode/bubbly/ent/gitcommit"
"github.com/valocode/bubbly/ent/release"
"github.com/valocode/bubbly/ent/releasecomponent"
"github.com/valocode/bubbly/ent/releasevulnerability"
"github.com/valocode/bubbly/ent/vulnerability"
"github.com/valocode/bubbly/ent/vulnerabilityreview"
"github.com/valocode/bubbly/store/api"
)

Expand Down Expand Up @@ -126,14 +128,36 @@ func (h *Handler) saveCodeScan(dbRelease *ent.Release, scan *api.CodeScan) (*ent
return HandleEntError(err, "vulnerability")
}
}
_, err = tx.ReleaseVulnerability.Create().
SetRelease(dbRelease).
SetVulnerability(existingVuln).
SetScan(codeScan).
SetComponent(relComp).
Save(h.ctx)
// Check if the release vulnerability already exists, which is
// the combination of release ID and vulnerability ID
dbRelVuln, err := tx.ReleaseVulnerability.Query().Where(
releasevulnerability.HasReleaseWith(release.ID(dbRelease.ID)),
releasevulnerability.HasVulnerabilityWith(vulnerability.ID(existingVuln.ID)),
).Only(h.ctx)
if err != nil {
return HandleEntError(err, "release vulnerability")
if !ent.IsNotFound(err) {
return HandleEntError(err, "query release vulnerability")
}
dbRelVuln, err = tx.ReleaseVulnerability.Create().
SetRelease(dbRelease).
SetVulnerability(existingVuln).
SetScan(codeScan).
SetComponent(relComp).
Save(h.ctx)
if err != nil {
return HandleEntError(err, "create release vulnerability")
}
}
if vuln.Patch != nil {
_, err := tx.VulnerabilityReview.Create().
SetName(*vuln.Patch.Message).
SetDecision(vulnerabilityreview.DecisionPatched).
SetVulnerability(existingVuln).
AddInstanceIDs(dbRelVuln.ID).
Save(h.ctx)
if err != nil {
return HandleEntError(err, "create vulnerability patch")
}
}
}
}
Expand Down

0 comments on commit 699b5b7

Please sign in to comment.