-
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.
Merge pull request #30 from atomist-skills/lift-from-goal-eval
lift common policy code from goal-eval
- Loading branch information
Showing
29 changed files
with
1,940 additions
and
2 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 |
---|---|---|
|
@@ -20,7 +20,7 @@ vars: | |
tasks: | ||
go:test: | ||
cmds: | ||
- go test -v | ||
- go test -v ./... --count=1 | ||
|
||
go:build: | ||
cmds: | ||
|
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,16 @@ | ||
package test_util | ||
|
||
import "github.com/atomist-skills/go-skill" | ||
|
||
func CreateEmptyLogger() skill.Logger { | ||
return skill.Logger{ | ||
Debug: func(msg string) {}, | ||
Debugf: func(format string, a ...any) {}, | ||
Info: func(msg string) {}, | ||
Infof: func(format string, a ...any) {}, | ||
Warn: func(msg string) {}, | ||
Warnf: func(format string, a ...any) {}, | ||
Error: func(msg string) {}, | ||
Errorf: func(format string, a ...any) {}, | ||
} | ||
} |
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,7 @@ | ||
package test_util | ||
|
||
// Pointer is useful for making pointers of literals in test cases | ||
// e.g. Pointer(3) or Pointer("string") | ||
func Pointer[T any](some T) *T { | ||
return &some | ||
} |
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,152 @@ | ||
package data | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/atomist-skills/go-skill/policy/graphql" | ||
) | ||
|
||
func getPurlsFromPackages(packages []MetadataPackage) []string { | ||
purls := []string{} | ||
for _, pkg := range packages { | ||
purls = append(purls, pkg.Purl) | ||
} | ||
return purls | ||
} | ||
|
||
func getPackagesByPurl(packages []graphql.VulnerabilitiesByPackage) map[string]graphql.VulnerabilitiesByPackage { | ||
result := map[string]graphql.VulnerabilitiesByPackage{} | ||
for _, pkg := range packages { | ||
result[pkg.Purl] = pkg | ||
} | ||
|
||
return result | ||
} | ||
|
||
func convertGraphqlToPackages(imagePackages graphql.ImagePackagesByDigest) ([]Package, error) { | ||
nonEmptyHistories := []graphql.ImageHistory{} | ||
for _, history := range imagePackages.ImageHistories { | ||
if !history.EmptyLayer { | ||
nonEmptyHistories = append(nonEmptyHistories, history) | ||
} | ||
} | ||
|
||
pkgs := []Package{} | ||
for _, p := range imagePackages.ImagePackages.Packages { | ||
locations := []PackageLocation{} | ||
for _, location := range p.PackageLocations { | ||
layerOrdinal := -1 | ||
for _, layer := range imagePackages.ImageLayers.Layers { | ||
if location.DiffId == layer.DiffId { | ||
layerOrdinal = layer.Ordinal | ||
break | ||
} | ||
} | ||
|
||
historyOrdinal := -1 | ||
if len(nonEmptyHistories) > 0 && layerOrdinal > -1 { | ||
historyOrdinal = nonEmptyHistories[layerOrdinal].Ordinal | ||
} | ||
|
||
locations = append(locations, PackageLocation{ | ||
LayerOrdinal: historyOrdinal, | ||
Path: location.Path, | ||
}) | ||
} | ||
|
||
var namespace string | ||
if p.Package.Namespace == nil { | ||
namespace = "" | ||
} else { | ||
namespace = *p.Package.Namespace | ||
} | ||
|
||
vulnerabilities, err := convertToVulnerabilities(p.Package.Vulnerabilities) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
pkgs = append(pkgs, Package{ | ||
Purl: p.Package.Purl, | ||
Licenses: p.Package.Licenses, | ||
Name: p.Package.Name, | ||
Namespace: namespace, | ||
Version: p.Package.Version, | ||
Type: p.Package.Type, | ||
Locations: locations, | ||
Vulnerabilities: vulnerabilities, | ||
}) | ||
} | ||
|
||
return pkgs, nil | ||
} | ||
|
||
func convertMetadataPackagesToPackages(metadataPackages []MetadataPackage, vulnerabilitiesByPackage []graphql.VulnerabilitiesByPackage) ([]Package, error) { | ||
pkgsByPurl := getPackagesByPurl(vulnerabilitiesByPackage) | ||
|
||
packages := []Package{} | ||
for _, mPkg := range metadataPackages { | ||
pkg := pkgsByPurl[mPkg.Purl] | ||
vulnerabilities, err := convertToVulnerabilities(pkg.Vulnerabilities) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
packages = append(packages, Package{ | ||
Licenses: mPkg.Licenses, | ||
Name: mPkg.Name, | ||
Namespace: mPkg.Namespace, | ||
Version: mPkg.Version, | ||
Purl: mPkg.Purl, | ||
Type: mPkg.Type, | ||
Vulnerabilities: vulnerabilities, | ||
}) | ||
} | ||
|
||
return packages, nil | ||
} | ||
|
||
func convertToVulnerabilities(vulnerabilities []graphql.Vulnerability) ([]Vulnerability, error) { | ||
result := []Vulnerability{} | ||
|
||
for _, vulnerability := range vulnerabilities { | ||
publishedAt, err := time.Parse(time.RFC3339, vulnerability.PublishedAt) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
updatedAt, err := time.Parse(time.RFC3339, vulnerability.UpdatedAt) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
vulnerabilityResult := Vulnerability{ | ||
Cvss: Cvss{}, | ||
PublishedAt: publishedAt, | ||
Source: vulnerability.Source, | ||
SourceID: vulnerability.SourceID, | ||
UpdatedAt: updatedAt, | ||
VulnerableRange: vulnerability.VulnerableRange, | ||
} | ||
|
||
if vulnerability.Cvss.Score != nil { | ||
vulnerabilityResult.Cvss.Score = *vulnerability.Cvss.Score | ||
} | ||
|
||
if vulnerability.Cvss.Severity != nil { | ||
vulnerabilityResult.Cvss.Severity = *vulnerability.Cvss.Severity | ||
} | ||
|
||
if vulnerability.URL != nil { | ||
vulnerabilityResult.URL = *vulnerability.URL | ||
} | ||
|
||
if vulnerability.FixedBy != nil { | ||
vulnerabilityResult.FixedBy = *vulnerability.FixedBy | ||
} | ||
|
||
result = append(result, vulnerabilityResult) | ||
} | ||
|
||
return result, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package data | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/atomist-skills/go-skill/policy/graphql" | ||
"github.com/atomist-skills/go-skill/policy/query" | ||
) | ||
|
||
// FixedDataSource is only used for tests | ||
type FixedDataSource struct { | ||
Packages map[string][]Package | ||
ImageDetailsByDigest *graphql.ImageDetailsByDigest | ||
} | ||
|
||
func (s FixedDataSource) GetPackages(ctx context.Context, digest string) (*GetPackagesResult, error) { | ||
return &GetPackagesResult{ | ||
AsyncQueryMade: false, | ||
Result: s.Packages[digest], | ||
}, nil | ||
} | ||
|
||
func (s FixedDataSource) GetImageDetailsByDigest(ctx context.Context, digest string, platform query.ImagePlatform) (*GetImageDetailsByDigestResult, error) { | ||
return &GetImageDetailsByDigestResult{ | ||
AsyncQueryMade: false, | ||
Result: s.ImageDetailsByDigest, | ||
}, nil | ||
} |
Oops, something went wrong.