-
Notifications
You must be signed in to change notification settings - Fork 856
Scan local go mod licenses for golang packages #1645
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
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
f399e5c
support for scanning license files in golang packages
deitch 4fe1cbc
Merge remote-tracking branch 'upstream/main' into golang-licenses-local
kzantow 6f955d8
chore: refactor local go mod to use FileResolver and add configuration
kzantow 7b306e7
Merge remote-tracking branch 'upstream/main' into golang-licenses-local
kzantow dd36f8b
chore: add processCaps test function
kzantow 53eb828
chore: PR feedback, add more testing
kzantow 41b6724
chore: more PR feedback
kzantow 5db49e1
chore: update README
kzantow cab8224
chore: tweak go license test
kzantow 193fc15
chore: use t.Setenv
kzantow 2b13ab6
Merge remote-tracking branch 'upstream/main' into golang-licenses-local
kzantow 9009dba
chore: update to use homedir lib
kzantow 0333860
Merge remote-tracking branch 'upstream/main' into golang-licenses-local
kzantow 299bd2d
chore: update naming and address PR feedback
kzantow d9cb99b
chore: add licenses for replace directives & update tests
kzantow 606bd93
chore: fix flaky license sorting
kzantow 5cd38ad
chore: add configuration option for gopath
kzantow 252b980
chore: PR feedback
kzantow 334e0f0
Merge remote-tracking branch 'upstream/main' into golang-licenses-local
kzantow 4f1da6d
chore: add docs, adjust mod cache lookup behavior
kzantow 911f884
chore: correct doc
kzantow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,13 @@ | ||
| package config | ||
|
|
||
| import "github.com/spf13/viper" | ||
|
|
||
| type golang struct { | ||
| SearchLocalModCacheLicenses bool `json:"search-local-mod-cache-licenses" yaml:"search-local-mod-cache-licenses" mapstructure:"search-local-mod-cache-licenses"` | ||
| LocalModCacheDir string `json:"local-mod-cache-dir" yaml:"local-mod-cache-dir" mapstructure:"local-mod-cache-dir"` | ||
| } | ||
|
|
||
| func (cfg golang) loadDefaultValues(v *viper.Viper) { | ||
| v.SetDefault("golang.search-local-mod-cache-licenses", false) | ||
| v.SetDefault("golang.local-mod-cache-dir", "") | ||
| } |
This file contains hidden or 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,53 @@ | ||
| package licenses | ||
|
|
||
| import "github.com/anchore/syft/internal" | ||
|
|
||
| // all of these taken from https://github.com/golang/pkgsite/blob/8996ff632abee854aef1b764ca0501f262f8f523/internal/licenses/licenses.go#L338 | ||
| // which unfortunately is not exported. But fortunately is under BSD-style license. | ||
|
|
||
| var ( | ||
| FileNames = []string{ | ||
| "COPYING", | ||
| "COPYING.md", | ||
| "COPYING.markdown", | ||
| "COPYING.txt", | ||
| "LICENCE", | ||
| "LICENCE.md", | ||
| "LICENCE.markdown", | ||
| "LICENCE.txt", | ||
| "LICENSE", | ||
| "LICENSE.md", | ||
| "LICENSE.markdown", | ||
| "LICENSE.txt", | ||
| "LICENSE-2.0.txt", | ||
| "LICENCE-2.0.txt", | ||
| "LICENSE-APACHE", | ||
| "LICENCE-APACHE", | ||
| "LICENSE-APACHE-2.0.txt", | ||
| "LICENCE-APACHE-2.0.txt", | ||
| "LICENSE-MIT", | ||
| "LICENCE-MIT", | ||
| "LICENSE.MIT", | ||
| "LICENCE.MIT", | ||
| "LICENSE.code", | ||
| "LICENCE.code", | ||
| "LICENSE.docs", | ||
| "LICENCE.docs", | ||
| "LICENSE.rst", | ||
| "LICENCE.rst", | ||
| "MIT-LICENSE", | ||
| "MIT-LICENCE", | ||
| "MIT-LICENSE.md", | ||
| "MIT-LICENCE.md", | ||
| "MIT-LICENSE.markdown", | ||
| "MIT-LICENCE.markdown", | ||
| "MIT-LICENSE.txt", | ||
| "MIT-LICENCE.txt", | ||
| "MIT_LICENSE", | ||
| "MIT_LICENCE", | ||
| "UNLICENSE", | ||
| "UNLICENCE", | ||
| } | ||
|
|
||
| FileNameSet = internal.NewStringSet(FileNames...) | ||
| ) |
This file contains hidden or 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,33 @@ | ||
| package licenses | ||
|
|
||
| import ( | ||
| "io" | ||
|
|
||
| "github.com/google/licensecheck" | ||
| "golang.org/x/exp/slices" | ||
| ) | ||
|
|
||
| const ( | ||
| coverageThreshold = 75 | ||
| unknownLicenseType = "UNKNOWN" | ||
| ) | ||
|
|
||
| // Parse scans the contents of a license file to attempt to determine the type of license it is | ||
| func Parse(reader io.Reader) (licenses []string, err error) { | ||
| contents, err := io.ReadAll(reader) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| cov := licensecheck.Scan(contents) | ||
|
|
||
| if cov.Percent < float64(coverageThreshold) { | ||
| licenses = append(licenses, unknownLicenseType) | ||
| } | ||
| for _, m := range cov.Match { | ||
| if slices.Contains(licenses, m.ID) { | ||
| continue | ||
| } | ||
| licenses = append(licenses, m.ID) | ||
| } | ||
| return | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.