-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add whitelist option * Add PR hook to GH action * Address CR comments
- Loading branch information
1 parent
a1d9780
commit 9ffa081
Showing
10 changed files
with
214 additions
and
29 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,9 @@ on: | |
push: | ||
branches: | ||
- '*' | ||
pull_request: | ||
branches: | ||
- '*' | ||
jobs: | ||
|
||
build: | ||
|
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,11 @@ | ||
{ | ||
"whitelist": [ | ||
"Apache-2.0", | ||
"BSD-2-Clause", | ||
"BSD-3-Clause", | ||
"ISC", | ||
"MIT", | ||
"MPL-2.0", | ||
"Public Domain" | ||
] | ||
} |
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
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package detector | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
|
||
"github.com/markbates/pkger" | ||
) | ||
|
||
const embeddedRulesFile = "github.com/elastic/go-licence-detector:/assets/rules.json" | ||
|
||
// rulesFile represents the structure of the rules file. | ||
type rulesFile struct { | ||
Whitelist []string `json:"whitelist"` | ||
} | ||
|
||
// Rules holds rules for the detector. | ||
type Rules struct { | ||
WhiteList map[string]struct{} | ||
} | ||
|
||
// LoadRules loads rules from the given path. Embedded rules file is loaded if the path is empty. | ||
func LoadRules(path string) (*Rules, error) { | ||
var f io.ReadCloser | ||
var err error | ||
|
||
if path == "" { | ||
f, err = pkger.Open(embeddedRulesFile) | ||
} else { | ||
f, err = os.Open(path) | ||
} | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("failed to open rules file: %w", err) | ||
} | ||
defer f.Close() | ||
|
||
ruleBytes, err := ioutil.ReadAll(f) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read rules: %w", err) | ||
} | ||
|
||
var rf rulesFile | ||
if err := json.Unmarshal(ruleBytes, &rf); err != nil { | ||
return nil, fmt.Errorf("failed to unmarshal rules: %w", err) | ||
} | ||
|
||
rules := &Rules{ | ||
WhiteList: make(map[string]struct{}, len(rf.Whitelist)), | ||
} | ||
|
||
for _, w := range rf.Whitelist { | ||
rules.WhiteList[w] = struct{}{} | ||
} | ||
|
||
return rules, nil | ||
} | ||
|
||
// IsAllowed returns true if the given licence is allowed by the rules. | ||
func (r *Rules) IsAllowed(licenceID string) bool { | ||
_, ok := r.WhiteList[licenceID] | ||
return ok | ||
} |
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 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package detector | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLoadRules(t *testing.T) { | ||
t.Run("embedded", func(t *testing.T) { | ||
rules, err := LoadRules("") | ||
|
||
require.NoError(t, err) | ||
require.NotNil(t, rules) | ||
require.True(t, len(rules.WhiteList) > 0) | ||
}) | ||
|
||
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) | ||
}) | ||
} | ||
|
||
func TestRulesWhiteList(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")) | ||
} |
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,11 @@ | ||
{ | ||
"whitelist": [ | ||
"Apache-2.0", | ||
"BSD-2-Clause", | ||
"BSD-3-Clause", | ||
"ISC", | ||
"MIT", | ||
"MPL-2.0", | ||
"Public Domain" | ||
] | ||
} |
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