Skip to content
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

feature: add support for allowlists #123

Merged
merged 6 commits into from
Oct 3, 2024

Conversation

NyanKiyoshi
Copy link
Contributor

This adds supports for denying all packages, and only allow selected ones by implementing support for mode: "allow".

Fixes: #101

Such as:

rules:
  - pattern: "BSD-*"
    name: "bsd-allow"
  - pattern: "*"
    name: "default-deny-all"
    mode: "deny"
Example

Config:

rules:
  - pattern: "BSD-*"
    name: "bsd-allow"
    mode: "allow"
    reason: "BSD is compatible with our project"
    exceptions:
      - asgiref
  - pattern: "*"
    name: "default-deny-all"
    mode: "deny"
    reason: "All licenses need to be explicitly approved (allowlist)"

Results:

$ ./main.bin check bom.json -o table --show-packages -vvv
[0000]  INFO grant version: [not provided]
[0000] DEBUG config:
  log:
      quiet: false
      level: trace
      file: ""
  dev:
      profile: none
  config: .grant.yaml
  output: table
  show-packages: true
  non-spdx: false
  quiet: false
  osi-approved: false
  rules:
      - name: bsd-allow
        reason: BSD is compatible with our project
        pattern: BSD-*
        severity: ""
        mode: allow
        exceptions:
          - asgiref
      - name: default-deny-all
        reason: All licenses need to be explicitly approved (allowlist)
        pattern: '*'
        severity: ""
        mode: deny
        exceptions: []
[0000] TRACE worker stopped component=eventloop
[0000] TRACE signal exit component=eventloop
* bom.json
  * license matches for rule: bsd-allow; matched with pattern BSD-*
    * BSD-3-Clause
      * asgiref
  * license matches for rule: default-deny-all; matched with pattern *
    * 0BSD
      * Authlib
    * New BSD
      * click-plugins
    * OSI Approved
      * oauthlib
check failed

This adds supports for denying all packages, and only allow selected ones by implementing support for `mode: "allow"`.

Such as:

```
rules:
  - pattern: "BSD-*"
    name: "bsd-allow"
  - pattern: "*"
    name: "default-deny-all"
    mode: "deny"
```

Signed-off-by: Mikail Kocak <[email protected]>
@NyanKiyoshi NyanKiyoshi force-pushed the feature/support-allowlists branch from 722d6d7 to 84db6a4 Compare September 4, 2024 19:58
grant/policy.go Outdated Show resolved Hide resolved
Comment on lines -47 to -58
if !l.IsSPDX() && ec.CheckNonSPDX {
if denied, rule := ec.Policy.IsDenied(l, pkg); denied {
var reason Reason
if rule != nil {
reason = Reason{
Detail: ReasonLicenseDeniedPolicy,
RuleName: rule.Name,
}
}
return NewLicenseEvaluation(l, pkg, ec.Policy, []Reason{reason}, false)
}
}
Copy link
Contributor Author

@NyanKiyoshi NyanKiyoshi Sep 4, 2024

Choose a reason for hiding this comment

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

If I'm not mistaken, the behavior was the same no matter whether it's SPDX or not here:

if !l.IsSPDX() && ec.CheckNonSPDX {
if denied, rule := ec.Policy.IsDenied(l, pkg); denied {
var reason Reason
if rule != nil {
reason = Reason{
Detail: ReasonLicenseDeniedPolicy,
RuleName: rule.Name,
}
}
return NewLicenseEvaluation(l, pkg, ec.Policy, []Reason{reason}, false)
}
}
if ec.OsiApproved && l.IsSPDX() {
if !l.IsOsiApproved {
return NewLicenseEvaluation(l, pkg, ec.Policy, []Reason{{
Detail: ReasonLicenseDeniedOSI,
RuleName: RuleNameNotOSIApproved,
}}, false)
}
}
if denied, rule := ec.Policy.IsDenied(l, pkg); denied {
var reason Reason
if rule != nil {
reason = Reason{
Detail: ReasonLicenseDeniedPolicy,
RuleName: rule.Name,
}
}
return NewLicenseEvaluation(l, pkg, ec.Policy, []Reason{reason}, false)
}

I believe ec.CheckNonSPDX should be passed to ec.Policy.IsDenied(l, pkg) but I do not know what is the expected behavior. If indeed the code previous code was invalid, then it could be handled in another PR.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I can take this on and move ec.CheckNonSPDX into the ec.Policy.IsDenied

The previous code was invalid.

@spiffcs
Copy link
Collaborator

spiffcs commented Oct 3, 2024

@NyanKiyoshi thank you so much for the PR - I'm working on getting #124 integrated and will then come back here, answer questions, and get this shaped up and ready for merge

@spiffcs spiffcs self-assigned this Oct 3, 2024
@spiffcs
Copy link
Collaborator

spiffcs commented Oct 3, 2024

Cleaning up this PR and responding to @NyanKiyoshi questions and getting it incorporated into the next release.

Thank you so so much @NyanKiyoshi for the contribution here

Signed-off-by: Christopher Phillips <[email protected]>
Signed-off-by: Christopher Phillips <[email protected]>
Signed-off-by: Christopher Phillips <[email protected]>
@spiffcs spiffcs marked this pull request as ready for review October 3, 2024 18:16
@spiffcs spiffcs merged commit 32c4643 into anchore:main Oct 3, 2024
3 checks passed
@NyanKiyoshi NyanKiyoshi deleted the feature/support-allowlists branch October 4, 2024 06:59
@NyanKiyoshi
Copy link
Contributor Author

Thank you @spiffcs!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

No way to deny all licenses while allowing specific ones
2 participants