-
Notifications
You must be signed in to change notification settings - Fork 587
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create single license scanner for all catalogers (#3348)
* add single license scanner instance Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * rename testing license scanner Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> --------- Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
- Loading branch information
Showing
16 changed files
with
277 additions
and
148 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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package licenses | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
type licenseScannerKey struct{} | ||
|
||
func SetContextLicenseScanner(ctx context.Context, s Scanner) context.Context { | ||
return context.WithValue(ctx, licenseScannerKey{}, s) | ||
} | ||
|
||
func ContextLicenseScanner(ctx context.Context) Scanner { | ||
if s, ok := ctx.Value(licenseScannerKey{}).(Scanner); ok { | ||
return s | ||
} | ||
return NewDefaultScanner() | ||
} |
This file was deleted.
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,68 @@ | ||
package licenses | ||
|
||
import ( | ||
"context" | ||
"io" | ||
|
||
"github.com/google/licensecheck" | ||
|
||
"github.com/anchore/syft/internal/log" | ||
) | ||
|
||
const coverageThreshold = 75 // determined by experimentation | ||
|
||
type Scanner interface { | ||
IdentifyLicenseIDs(context.Context, io.Reader) ([]string, error) | ||
} | ||
|
||
var _ Scanner = (*scanner)(nil) | ||
|
||
type scanner struct { | ||
coverageThreshold float64 // between 0 and 100 | ||
scanner func([]byte) licensecheck.Coverage | ||
} | ||
|
||
// NewDefaultScanner returns a scanner that uses a new instance of the default licensecheck package scanner. | ||
func NewDefaultScanner() Scanner { | ||
s, err := licensecheck.NewScanner(licensecheck.BuiltinLicenses()) | ||
if err != nil { | ||
log.WithFields("error", err).Trace("unable to create default license scanner") | ||
s = nil | ||
} | ||
return &scanner{ | ||
coverageThreshold: coverageThreshold, | ||
scanner: s.Scan, | ||
} | ||
} | ||
|
||
// TestingOnlyScanner returns a scanner that uses the built-in license scanner from the licensecheck package. | ||
// THIS IS ONLY MEANT FOR TEST CODE, NOT PRODUCTION CODE. | ||
func TestingOnlyScanner() Scanner { | ||
return &scanner{ | ||
coverageThreshold: coverageThreshold, | ||
scanner: licensecheck.Scan, | ||
} | ||
} | ||
|
||
func (s scanner) IdentifyLicenseIDs(_ context.Context, reader io.Reader) ([]string, error) { | ||
if s.scanner == nil { | ||
return nil, nil | ||
} | ||
|
||
content, err := io.ReadAll(reader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cov := s.scanner(content) | ||
if cov.Percent < s.coverageThreshold { | ||
// unknown or no licenses here? | ||
return nil, nil | ||
} | ||
|
||
var ids []string | ||
for _, m := range cov.Match { | ||
ids = append(ids, m.ID) | ||
} | ||
return ids, nil | ||
} |
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,28 @@ | ||
package licenses | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/anchore/syft/syft/file" | ||
"github.com/anchore/syft/syft/license" | ||
"github.com/anchore/syft/syft/pkg" | ||
) | ||
|
||
// Search scans the contents of a license file to attempt to determine the type of license it is | ||
func Search(ctx context.Context, scanner Scanner, reader file.LocationReadCloser) (licenses []pkg.License, err error) { | ||
licenses = make([]pkg.License, 0) | ||
|
||
ids, err := scanner.IdentifyLicenseIDs(ctx, reader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, id := range ids { | ||
lic := pkg.NewLicenseFromLocations(id, reader.Location) | ||
lic.Type = license.Concluded | ||
|
||
licenses = append(licenses, lic) | ||
} | ||
|
||
return licenses, nil | ||
} |
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
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
Oops, something went wrong.