Skip to content
This repository was archived by the owner on Jun 21, 2022. It is now read-only.

feat(dpkg): license parser added#461

Open
DmitriyLewen wants to merge 10 commits intoaquasecurity:mainfrom
DmitriyLewen:add_debian_license
Open

feat(dpkg): license parser added#461
DmitriyLewen wants to merge 10 commits intoaquasecurity:mainfrom
DmitriyLewen:add_debian_license

Conversation

@DmitriyLewen
Copy link
Copy Markdown
Collaborator

@DmitriyLewen DmitriyLewen commented Apr 11, 2022

Description

Debian based system hasn't requirement to write license information.
But almost every package in Debian contains a copyright file in /usr/share/doc/<packagename>/copyright.
More information here.

Added parser for copyright files and hook for adding licenses to dpkgs.

For parsing copyright files are used:

@DmitriyLewen DmitriyLewen self-assigned this Apr 11, 2022
var licenses []string
var buf bytes.Buffer

tee := io.TeeReader(content, &buf) // Save stream in buffer for re-read with 'licenseclassifier'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems scanner instance is available within func (a dpkgAnalyzer) Analyze(...)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used this scanner, thanks!

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used this scanner, thanks!

licenses = append(licenses, l)
}
continue
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else - ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

var (
cl, _ = classifier.DefaultClassifier()
copyrightFileRegexp = regexp.MustCompile(`^usr/share/doc/([0-9A-Za-z_.-]+)/copyright$`)
commonLicensesRegexp = regexp.MustCompile(`/?usr/share/common-licenses/([0-9A-Za-z_.+-]+[0-9A-Za-z+])`)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

commonLicenseReferenceRegexp as it for lines reference one of common licenses

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

// "License: *" pattern is used
if strings.HasPrefix(line, "License:") {
l := strings.TrimSpace(line[8:])
if !utils.StringInSlice(l, licenses) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As Alim suggested let's use build in Go 18 method https://pkg.go.dev/golang.org/x/exp/slices#Contains

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed


type dpkgLicenseHook struct{}

// Hook add licenses to dpkg files
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Hook adds"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

for _, pkg := range pkgInfo.Packages {
license, ok := licenses[pkg.Name]
if ok {
pkg.License = license
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just modify existing package through index
pkgInfo.Packages[i].License = license

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used this, thanks!

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used this, thanks!

@knqyf263
Copy link
Copy Markdown
Collaborator

@DmitriyLewen Can you resolve conflicts?

@DmitriyLewen DmitriyLewen marked this pull request as ready for review June 20, 2022 10:52
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants