Skip to content

Commit

Permalink
Change whitelist to allowlist (#6)
Browse files Browse the repository at this point in the history
Changes whitelist => allowlist, similar to elastic/cloud-on-k8s#3266, and yellowlist => maybelist to be consistent.

This is a breaking change as override files will need to be updated with the new field name.

Also adds expands the gitignore to include options from https://github.com/github/gitignore/blob/master/Go.gitignore

and begins a contributing doc to make the prereqs clearer.
  • Loading branch information
Anya Sabo authored Jun 23, 2020
1 parent d683999 commit 1621275
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 23 deletions.
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
go-licence-detector
out/
bin/

# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
3 changes: 3 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Prerequisites

To submit a PR, please ensure you run `make generate` to ensure the NOTICE file is up to date. This may require you have `pkger` installed already, which you can do with `go get github.com/markbates/pkger/cmd/pkger`.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Allowed licence types can be specified using a JSON file with the following stru

```json
{
"whitelist": [
"allowlist": [
"Apache-2.0",
"MIT"
]
Expand Down
2 changes: 1 addition & 1 deletion assets/rules.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"whitelist": [
"allowlist": [
"Apache-2.0",
"BSD-2-Clause",
"BSD-3-Clause",
Expand Down
2 changes: 1 addition & 1 deletion detector/detector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func TestDetect(t *testing.T) {
},

{
name: "LicenceNotWhitelisted",
name: "LicenceNotAllowed",
includeIndirect: true,
overrides: map[string]dependency.Info{
"github.com/davecgh/go-spew": {Name: "github.com/davecgh/go-spew", LicenceType: "Totally Legit License 2.0"},
Expand Down
2 changes: 1 addition & 1 deletion detector/pkged.go

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions detector/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ const embeddedRulesFile = "go.elastic.co/go-licence-detector:/assets/rules.json"

// rulesFile represents the structure of the rules file.
type rulesFile struct {
Whitelist []string `json:"whitelist"`
Yellowlist []string `json:"yellowlist"`
Allowlist []string `json:"allowlist"`
Maybelist []string `json:"maybelist"`
}

// Rules holds rules for the detector.
type Rules struct {
WhiteList map[string]struct{}
YellowList map[string]struct{}
AllowList map[string]struct{}
Maybelist map[string]struct{}
}

// LoadRules loads rules from the given path. Embedded rules file is loaded if the path is empty.
Expand Down Expand Up @@ -68,24 +68,24 @@ func LoadRules(path string) (*Rules, error) {
}

rules := &Rules{
WhiteList: make(map[string]struct{}, len(rf.Whitelist)),
YellowList: make(map[string]struct{}, len(rf.Yellowlist)),
AllowList: make(map[string]struct{}, len(rf.Allowlist)),
Maybelist: make(map[string]struct{}, len(rf.Maybelist)),
}

for _, w := range rf.Whitelist {
rules.WhiteList[w] = struct{}{}
for _, w := range rf.Allowlist {
rules.AllowList[w] = struct{}{}
}

for _, y := range rf.Yellowlist {
rules.YellowList[y] = struct{}{}
for _, y := range rf.Maybelist {
rules.Maybelist[y] = struct{}{}
}

return rules, nil
}

// IsAllowed returns true if the given licence is allowed by the rules.
func (r *Rules) IsAllowed(licenceID string) bool {
_, isWhiteListed := r.WhiteList[licenceID]
_, isYellowListed := r.YellowList[licenceID]
return isWhiteListed || isYellowListed
_, isAllowListed := r.AllowList[licenceID]
_, isMaybeListed := r.Maybelist[licenceID]
return isAllowListed || isMaybeListed
}
9 changes: 5 additions & 4 deletions detector/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package detector
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -29,27 +30,27 @@ func TestLoadRules(t *testing.T) {

require.NoError(t, err)
require.NotNil(t, rules)
require.True(t, len(rules.WhiteList) > 0)
assert.True(t, len(rules.AllowList) > 0, rules)
})

t.Run("external", func(t *testing.T) {
rules, err := LoadRules("testdata/rules.json")

require.NoError(t, err)
require.NotNil(t, rules)
require.True(t, len(rules.WhiteList) > 0)
assert.True(t, len(rules.AllowList) > 0)
})
}

func TestRulesWhiteList(t *testing.T) {
func TestRulesAllowList(t *testing.T) {
rules, err := LoadRules("testdata/rules.json")

require.NoError(t, err)
require.True(t, rules.IsAllowed("Apache-2.0"))
require.False(t, rules.IsAllowed("WTFPL"))
}

func TestRulesYellowList(t *testing.T) {
func TestRulesMaybeList(t *testing.T) {
rules, err := LoadRules("testdata/rules.json")

require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions detector/testdata/rules.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"whitelist": [
"allowlist": [
"Apache-2.0",
"BSD-2-Clause",
"BSD-3-Clause",
Expand All @@ -8,7 +8,7 @@
"MPL-2.0",
"Public Domain"
],
"yellowlist": [
"maybelist": [
"GPL-3.0"
]
}

0 comments on commit 1621275

Please sign in to comment.