Skip to content

Commit

Permalink
fix: Update to support #101 (#124)
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Frederick <mike.frederick@cesium.com>
Co-authored-by: Christopher Angelo Phillips <32073428+spiffcs@users.noreply.github.com>
  • Loading branch information
psududemike and spiffcs authored Oct 3, 2024
1 parent e6860fe commit 7a8dc6b
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 8 deletions.
22 changes: 14 additions & 8 deletions grant/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
)

// Policy is a structure of rules that define how licenses are denied
// TODO: maybe there should be a strict option that denies all and then only allows what is explicitly allowed
type Policy struct {
Rules Rules
MatchNonSPDX bool
Expand Down Expand Up @@ -51,10 +50,6 @@ func (p Policy) IsEmpty() bool {
// IsDenied returns true if the given license is denied by the policy
func (p Policy) IsDenied(license License, pkg *Package) (bool, *Rule) {
for _, rule := range p.Rules {
if rule.Mode != Deny {
continue
}

var toMatch string
if license.IsSPDX() {
toMatch = strings.ToLower(license.LicenseID)
Expand All @@ -63,16 +58,27 @@ func (p Policy) IsDenied(license License, pkg *Package) (bool, *Rule) {
}

toMatch = strings.ToLower(toMatch)
// If there is a match and the content to match is not an empty string
if rule.Glob.Match(toMatch) && toMatch != "" {
var returnVal bool
// set the return value based on the rule mode
if rule.Mode == Allow {
returnVal = false
} else {
returnVal = true
}
if pkg == nil {
return true, &rule
return returnVal, &rule
}
for _, exception := range rule.Exceptions {
if exception.Match(pkg.Name) {
return false, &rule
// flip the return value based on the exception
returnVal = !returnVal

return returnVal, &rule
}
}
return true, &rule
return returnVal, &rule
}
}
return false, nil
Expand Down
136 changes: 136 additions & 0 deletions grant/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,139 @@ func Test_Policy_DenyAll(t *testing.T) {
})
}
}

func Test_Policy_Exceptions(t *testing.T) {
tests := []struct {
name string
p Policy
want struct {
denied bool
rule *Rule
}
}{
{
name: "Policy Default Deny All denies all licenses",
p: Policy{
Rules: Rules{Rule{
Name: "default-deny-all",
Glob: glob.MustCompile("*"),
Exceptions: []glob.Glob{glob.MustCompile("MIT")},
Mode: Deny,
Reason: "grant by default will deny all licenses",
}},
MatchNonSPDX: false,
},
want: struct {
denied bool
rule *Rule
}{
denied: false,
rule: &Rule{
Name: "default-deny-all",
Glob: glob.MustCompile("*"),
Exceptions: []glob.Glob{glob.MustCompile("MIT")},
Mode: Deny,
Reason: "grant by default will deny all licenses",
},
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
denied, rule := tc.p.IsDenied(License{LicenseID: "MIT", SPDXExpression: "MIT"}, &Package{ID: "MIT", Name: "MIT"})
if denied != tc.want.denied {
t.Errorf("Expected %t, got %t", tc.want.denied, denied)
}
if diff := cmp.Diff(tc.want.rule, rule); diff != "" {
t.Errorf("IsDenied() mismatch (-want +got):\n%s", diff)
}
})
}
}

func Test_Policy_Allow(t *testing.T) {
tests := []struct {
name string
p Policy
want struct {
denied bool
rule *Rule
}
}{
{
name: "Policy Allow MIT",
p: Policy{
Rules: Rules{Rule{
Name: "allow MIT",
Glob: glob.MustCompile("mit"),
Exceptions: []glob.Glob{},
Mode: Allow,
Reason: "grant allow MIT",
}, Rule{
Name: "deny gpl",
Glob: glob.MustCompile("gpl"),
Exceptions: []glob.Glob{},
Mode: Deny,
Reason: "grant deny gpl",
}},
MatchNonSPDX: false,
},
want: struct {
denied bool
rule *Rule
}{
denied: false,
rule: &Rule{
Name: "allow MIT",
Glob: glob.MustCompile("mit"),
Exceptions: []glob.Glob{},
Mode: Allow,
Reason: "grant allow MIT",
},
},
},
{
name: "Policy Deny MIT",
p: Policy{
Rules: Rules{Rule{
Name: "deny MIT",
Glob: glob.MustCompile("mit"),
Exceptions: []glob.Glob{},
Mode: Deny,
Reason: "grant deny MIT",
}, Rule{
Name: "allow gpl",
Glob: glob.MustCompile("gpl"),
Exceptions: []glob.Glob{},
Mode: Allow,
Reason: "grant allow gpl",
}},
MatchNonSPDX: false,
},
want: struct {
denied bool
rule *Rule
}{
denied: true,
rule: &Rule{
Name: "deny MIT",
Glob: glob.MustCompile("mit"),
Exceptions: []glob.Glob{},
Mode: Deny,
Reason: "grant deny MIT",
},
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
denied, rule := tc.p.IsDenied(License{LicenseID: "MIT", SPDXExpression: "MIT"}, nil)
if denied != tc.want.denied {
t.Errorf("Expected %t, got %t", tc.want.denied, denied)
}
if diff := cmp.Diff(tc.want.rule, rule); diff != "" {
t.Errorf("IsDenied() mismatch (-want +got):\n%s", diff)
}
})
}
}

0 comments on commit 7a8dc6b

Please sign in to comment.