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

chore: lift index into flat slice of evaluation #7

Merged
merged 1 commit into from
Dec 8, 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
73 changes: 70 additions & 3 deletions grant/evalutation/license_evalutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,74 @@ import (
)

func NewLicenseEvaluations(ec EvaluationConfig, c grant.Case) LicenseEvaluations {
panic("not implemented")
evaluations := make([]LicenseEvaluation, 0)
// TODO: probably want to use some concurrency here
for _, sb := range c.SBOMS {
for pkg := range sb.Artifacts.Packages.Enumerate() {
grantPkg := convertSyftPackage(pkg)
// since we use syft as a library to generate the sbom we need to convert its packages/licenses to grant types
if len(grantPkg.Licenses) == 0 {
evaluations = append(evaluations, LicenseEvaluation{
License: grant.License{},
Package: grantPkg,
Policy: ec.Policy,
Reason: []Reason{ReasonNoLicenseFound},
Pass: true,
})
continue
}

for _, l := range grantPkg.Licenses {
if !l.IsSPDX() {
// TODO: check if the config wants us to check for non-SPDX licenses
}
if ec.Policy.IsDenied(l) {
evaluations = append(evaluations, LicenseEvaluation{
License: l,
Package: grantPkg,
Policy: ec.Policy,
Reason: []Reason{ReasonLicenseDenied},
Pass: false,
})
continue
}
// otherwise, the license is allowed
evaluations = append(evaluations, LicenseEvaluation{
License: l,
Package: grantPkg,
Policy: ec.Policy,
Reason: []Reason{ReasonLicenseAllowed},
Pass: true,
})
}
}
}

for _, l := range c.Licenses {
if !l.IsSPDX() {
// TODO: check if the config wants us to check for non-SPDX licenses
}
if ec.Policy.IsDenied(l) {
evaluations = append(evaluations, LicenseEvaluation{
License: l,
Package: nil,
Policy: ec.Policy,
Reason: []Reason{ReasonLicenseDenied},
Pass: false,
})
continue
}
// otherwise, the license is allowed
evaluations = append(evaluations, LicenseEvaluation{
License: l,
Package: nil,
Policy: ec.Policy,
Reason: []Reason{ReasonLicenseAllowed},
Pass: true,
})
}

return evaluations
}

type LicenseEvaluations []LicenseEvaluation
Expand All @@ -18,10 +85,10 @@ type LicenseEvaluation struct {
Package *grant.Package // any artifact license is evaluated with

// what's used to evaluate...
Policy *grant.Policy // what the determination was made against
Policy grant.Policy // what the determination was made against

// the output of an evaluation...
Reason []string // reasons that the evaluation value the way it is
Reason []Reason // reasons that the evaluation value the way it is
Pass bool // The final evaluation
}

Expand Down
2 changes: 1 addition & 1 deletion grant/evalutation/license_evalutation_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "github.com/anchore/grant/grant"
type EvaluationConfig struct {
// Policy is the policy to evaluate against
// if non is supplied, the default policy is used (grant.DefaultPolicy())
Policy *grant.Policy
Policy grant.Policy
// CheckNonSPDX is true if non-SPDX licenses should be checked
CheckNonSPDX bool
}
9 changes: 9 additions & 0 deletions grant/evalutation/reason.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package evalutation

type Reason string

var (
ReasonNoLicenseFound Reason = "no license found"
ReasonLicenseDenied Reason = "license denied by policy"
ReasonLicenseAllowed Reason = "license allowed by policy"
)
113 changes: 0 additions & 113 deletions grant/evalutation/sbom.go

This file was deleted.

4 changes: 2 additions & 2 deletions grant/evalutation/syft.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
syftPkg "github.com/anchore/syft/syft/pkg"
)

func convertSyftPackage(p syftPkg.Package) grant.Package {
func convertSyftPackage(p syftPkg.Package) *grant.Package {
locations := p.Locations.ToSlice()
packageLocations := make([]string, 0)
for _, location := range locations {
packageLocations = append(packageLocations, location.RealPath)
}

return grant.Package{
return &grant.Package{
Name: p.Name,
Version: p.Version,
Licenses: convertSyftLicenses(p.Licenses),
Expand Down
4 changes: 2 additions & 2 deletions grant/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func DefaultPolicy() *Policy {
}
}

// NewPolicy builds a policy from lists of allow and deny glob patterns
// NewPolicy builds a policy from lists of allow, deny, and ignore glob patterns
// It lower cases all patterns to make matching against the spdx license set case-insensitive
func NewPolicy(allowLicenses, denyLicenses, ignoreLicenses []string) (p *Policy, err error) {
if len(allowLicenses) == 0 && len(denyLicenses) == 0 {
Expand Down Expand Up @@ -138,7 +138,7 @@ func (p Policy) IsDenied(license License) bool {
return false
}

// IsAllowed is a convenience function for library consumers
// IsAllowed is a convenience function for library usage of IsDenied negation
func (p Policy) IsAllowed(license License) bool {
return !p.IsDenied(license)
}