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

Extend PR vulnerability checks with a configurable action to set commit status #966

Merged
merged 3 commits into from
Sep 19, 2023

Conversation

jhrozek
Copy link
Contributor

@jhrozek jhrozek commented Sep 15, 2023

Because using code reviews to block commits might be problematic for
self-enrollment, this patch adds a new action enum value named
commit_status. When the PR vulnerability check is enabled and this
action is selected, mediator will either mark the commit status as
succeeded if no vulns are found or failed if vulnerabilities are found.

In the repository branch protection rules, the user can then configure a
rule that would require the mediator.stacklok.dev/pr-vulncheck rule to
pass.

The vulnerable dependencies are still pointed out in review comments,
but the review is submitted as "COMMENT", the blocking decision is made
by the commit status.

Fixes: #935

@jhrozek
Copy link
Contributor Author

jhrozek commented Sep 15, 2023

I'm working on adding tests, feel free to leave comments about the code while I'm doing that, but let's not merge the PR without tests..

Copy link
Contributor

@JAORMX JAORMX left a comment

Choose a reason for hiding this comment

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

the code LGTM, was just gonna comment about tests.

@jhrozek
Copy link
Contributor Author

jhrozek commented Sep 15, 2023

I added a unit test. More cleanups to review.go are incoming (we need to handle non-200 errors from APIs more gracefully and we need to reuse http.Client across calls) but this gives us a baseline test coverage.

evankanderson
evankanderson previously approved these changes Sep 16, 2023
examples/github/rule-types/pr_vulnerability_check.yaml Outdated Show resolved Hide resolved
@@ -129,6 +133,10 @@ func locateDepInPr(
return &loc, nil
}

func reviewBodyWithSuggestion(comment string) string {
return fmt.Sprintf("```suggestion\n"+"%s\n"+"```\n", comment)
Copy link
Member

Choose a reason for hiding this comment

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

Why use + to combine these strings?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

because I'm using copilot and didn't check what it suggested :-) Fixed, thanks!

internal/engine/eval/vulncheck/review.go Show resolved Hide resolved
}

return &commitStatusPrHandler{
reviewPrHandler: *rph.(*reviewPrHandler),
Copy link
Member

Choose a reason for hiding this comment

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

Can we change the return type of newReviewPrHandler to be (*reviewPrHandler, error) and have it still match the target schema? That would avoid this (potentially panicking) cast here without needing to either introduce error-handling code or otherwise clue in the compiler that this cast is safe.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ha, good idea, thank you.

Comment on lines 375 to 376
err := csh.reviewPrHandler.submit(ctx)
if err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

Slight preference for the more compact define-err-in-condition-body:

Suggested change
err := csh.reviewPrHandler.submit(ctx)
if err != nil {
if err := csh.reviewPrHandler.submit(ctx); err != nil {

Note that this causes a new instance of err to be allocated, so isn't always appropriate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

do you have a rule of thumb as per when do you use one or the other? I've been honestly treating them mostly as a matter of style and didn't think about the extra allocation as something worth noting much.

that said, suggestion taken.

Comment on lines 409 to 412
if err != nil {
return err
}
return nil
Copy link
Member

Choose a reason for hiding this comment

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

Can this just be:

Suggested change
if err != nil {
return err
}
return nil
return err

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure, why not (it is easier on my eyes to see an explicit return nil in the happy path, but I don't really care one way or the other)

…it status

Because using code reviews to block commits might be problematic for
self-enrollment, this patch adds a new `action` enum value named
`commit_status`. When the PR vulnerability check is enabled and this
action is selected, mediator will either mark the commit status as
succeeded if no vulns are found or failed if vulnerabilities are found.

In the repository branch protection rules, the user can then configure a
rule that would require the `mediator.stacklok.dev/pr-vulncheck` rule to
pass.

The vulnerable dependencies are still pointed out in review comments,
but the review is submitted as "COMMENT", the blocking decision is made
by the commit status.

Fixes: #935
@jhrozek jhrozek merged commit 8b21f00 into mindersec:main Sep 19, 2023
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Based on rule configuration, mark commit status as failed in case vulnerable packages are detected in a PR
3 participants