-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fetch vulnerabilities if SBOM doesn't provide them (#51)
Signed-off-by: felipecruz91 <[email protected]>
- Loading branch information
1 parent
a4f20cd
commit 424f469
Showing
15 changed files
with
176 additions
and
90 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package data | |
import ( | ||
"context" | ||
"fmt" | ||
|
||
"olympos.io/encoding/edn" | ||
) | ||
|
||
|
This file was deleted.
Oops, something went wrong.
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
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,50 @@ | ||
package mocks | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/atomist-skills/go-skill" | ||
"github.com/atomist-skills/go-skill/policy/policy_handler/legacy" | ||
"github.com/atomist-skills/go-skill/policy/types" | ||
"olympos.io/encoding/edn" | ||
) | ||
|
||
func BuildLocalEvalMocks(ctx context.Context, req skill.RequestContext, sb *types.SBOM) (map[edn.Keyword]edn.RawMessage, error) { | ||
m := map[edn.Keyword]edn.RawMessage{} | ||
if sb == nil { | ||
req.Log.Info("No SBOM provided, returning empty map") | ||
return m, nil | ||
} | ||
|
||
// Image packages by digest | ||
imgPkgsMock, err := MockImagePackagesByDigest(ctx, req, sb) | ||
if err != nil { | ||
return m, err | ||
} | ||
m[legacy.ImagePackagesByDigestQueryName], err = edn.Marshal(imgPkgsMock) | ||
if err != nil { | ||
return m, fmt.Errorf("failed to marshal image packages by digest mock: %w", err) | ||
} | ||
|
||
// User | ||
if sb.Source.Image != nil && sb.Source.Image.Config != nil { | ||
userMock := MockGetUser(sb.Source.Image.Config.Config.User) | ||
m[GetUserQueryName], err = edn.Marshal(userMock) | ||
if err != nil { | ||
return m, fmt.Errorf("failed to marshal get user mock: %w", err) | ||
} | ||
} | ||
|
||
// Attestations | ||
req.Log.Infof("SBOM has %d attestations", len(sb.Attestations)) | ||
if len(sb.Attestations) > 0 { | ||
attestMock := MockGetInTotoAttestations(sb, req.Log) | ||
m[GetInTotoAttestationsQueryName], err = edn.Marshal(attestMock) | ||
if err != nil { | ||
return m, fmt.Errorf("failed to marshal attestations mock: %w", err) | ||
} | ||
} | ||
|
||
return m, nil | ||
} |
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
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,79 @@ | ||
package mocks | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/atomist-skills/go-skill" | ||
"github.com/atomist-skills/go-skill/policy/policy_handler/legacy" | ||
"github.com/atomist-skills/go-skill/policy/types" | ||
) | ||
|
||
func MockImagePackagesByDigest(ctx context.Context, req skill.RequestContext, sb *types.SBOM) (legacy.ImagePackagesByDigestResponse, error) { | ||
req.Log.Info("Building local evaluation mocks for image packages by digest") | ||
|
||
if len(sb.Vulnerabilities) == 0 { | ||
req.Log.Info("SBOM doesn't provide any vulnerabilities directly, fetching them from GraphQL") | ||
|
||
var pkgs []legacy.Package | ||
for _, a := range sb.Artifacts { | ||
pkgs = append(pkgs, legacy.Package{ | ||
Type: a.Type, | ||
Namespace: a.Namespace, | ||
Name: a.Name, | ||
Version: a.Version, | ||
Purl: a.Purl, | ||
Licenses: a.Licenses, | ||
}) | ||
} | ||
res, err := legacy.MockImagePackagesByDigest(ctx, req, pkgs) | ||
if err != nil { | ||
return res, err | ||
} | ||
return res, nil | ||
} | ||
|
||
req.Log.Info("SBOM provides vulnerabilities") | ||
vulns := map[string][]legacy.Vulnerability{} | ||
for _, tuple := range sb.Vulnerabilities { | ||
vulnsForPurl := []legacy.Vulnerability{} | ||
for _, v := range tuple.Vulnerabilities { | ||
vulnsForPurl = append(vulnsForPurl, legacy.Vulnerability{ | ||
Cvss: legacy.Cvss{ | ||
Severity: &v.Cvss.Severity, | ||
Score: &v.Cvss.Score, | ||
}, | ||
PublishedAt: v.PublishedAt, | ||
UpdatedAt: v.UpdatedAt, | ||
FixedBy: &v.FixedBy, | ||
Source: v.Source, | ||
SourceID: v.SourceId, | ||
URL: &v.Url, | ||
VulnerableRange: v.VulnerableRange, | ||
}) | ||
} | ||
vulns[tuple.Purl] = vulnsForPurl | ||
} | ||
|
||
pkgs := []legacy.Packages{} | ||
for _, a := range sb.Artifacts { | ||
pkgs = append(pkgs, legacy.Packages{ | ||
Package: legacy.PackageWithLicenses{ | ||
Licenses: a.Licenses, | ||
Name: a.Name, | ||
Namespace: &a.Namespace, | ||
Version: a.Version, | ||
Purl: a.Purl, | ||
Type: a.Type, | ||
Vulnerabilities: vulns[a.Purl], | ||
}, | ||
}) | ||
} | ||
|
||
return legacy.ImagePackagesByDigestResponse{ | ||
ImagePackagesByDigest: &legacy.ImagePackagesByDigest{ | ||
ImagePackages: legacy.ImagePackages{ | ||
Packages: pkgs, | ||
}, | ||
}, | ||
}, nil | ||
} |
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
File renamed without changes.
File renamed without changes.
Oops, something went wrong.