-
Notifications
You must be signed in to change notification settings - Fork 14
Add state cve report #1240
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
mdrohmann
merged 9 commits into
martind/state-cve-176449951
from
martind/state-cve-report-176449977
Feb 9, 2021
Merged
Add state cve report #1240
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3596b24
add state cve report command
mdrohmann 0977427
cleanup
mdrohmann 47828fe
add integration tests
mdrohmann 1853542
fix default namespace intialization
mdrohmann 09c95ee
fix int test
mdrohmann 794c4e6
Apply suggestions from code review
mdrohmann b8170cd
use forked project in int test
mdrohmann 59ea441
move package vulnerability extraction to model
mdrohmann 5c04583
fix one more int test
mdrohmann 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| package cve | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "sort" | ||
| "strconv" | ||
| "time" | ||
|
|
||
| "github.com/ActiveState/cli/internal/errs" | ||
| "github.com/ActiveState/cli/internal/locale" | ||
| "github.com/ActiveState/cli/internal/logging" | ||
| "github.com/ActiveState/cli/internal/output" | ||
| medmodel "github.com/ActiveState/cli/pkg/platform/api/mediator/model" | ||
| "github.com/ActiveState/cli/pkg/platform/authentication" | ||
| "github.com/ActiveState/cli/pkg/platform/model" | ||
| "github.com/ActiveState/cli/pkg/project" | ||
| ) | ||
|
|
||
| type Report struct { | ||
| proj *project.Project | ||
| auth *authentication.Auth | ||
| out output.Outputer | ||
| } | ||
|
|
||
| type ReportInfo struct { | ||
| Project string `locale:"project,Project"` | ||
| CommitID string `locale:"commit_id,Commit ID"` | ||
| Date string `locale:"generated_on,Generated on"` | ||
| } | ||
|
|
||
| func NewReport(prime primeable) *Report { | ||
| return &Report{prime.Project(), prime.Auth(), prime.Output()} | ||
| } | ||
|
|
||
| type ReportParams struct { | ||
| Namespace *project.Namespaced | ||
| } | ||
|
|
||
| type reportData struct { | ||
| Project string `json:"project"` | ||
| CommitID string `json:"commitID"` | ||
| Date time.Time `json:"generated_on"` | ||
| Histogram []medmodel.SeverityCount `json:"vulnerability_histogram"` | ||
| Packages []model.PackageVulnerability `json:"packages"` | ||
| } | ||
|
|
||
| type reportDataPrinter struct { | ||
| output output.Outputer | ||
| data *reportData | ||
| } | ||
|
|
||
| func (r *Report) Run(params *ReportParams) error { | ||
| ns := params.Namespace | ||
| if !ns.IsValid() { | ||
| if r.proj == nil { | ||
| return locale.NewInputError("err_no_project") | ||
| } | ||
| ns = r.proj.Namespace() | ||
| } | ||
|
|
||
| if !r.auth.Authenticated() { | ||
| return errs.AddTips( | ||
| locale.NewError("cve_needs_authentication"), | ||
| locale.T("auth_tip"), | ||
| ) | ||
| } | ||
|
|
||
| logging.Debug("Fetching vulnerabilities for %s", ns.String()) | ||
| resp, err := model.FetchProjectVulnerabilities(r.auth, ns.Owner, ns.Project) | ||
| if err != nil { | ||
| return locale.WrapError(err, "cve_mediator_resp", "Failed to retrieve vulnerability information") | ||
| } | ||
|
|
||
| packageVulnerabilities := model.ExtractPackageVulnerabilities(resp.Project.Commit.Ingredients) | ||
|
|
||
| reportOutput := &reportData{ | ||
| Project: ns.String(), | ||
| CommitID: resp.Project.Commit.CommitID, | ||
| Date: time.Now(), | ||
|
|
||
| Histogram: resp.Project.Commit.VulnerabilityHistogram, | ||
| Packages: packageVulnerabilities, | ||
| } | ||
|
|
||
| rdp := &reportDataPrinter{ | ||
| r.out, | ||
| reportOutput, | ||
| } | ||
|
|
||
| r.out.Print(rdp) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (rd *reportDataPrinter) MarshalOutput(format output.Format) interface{} { | ||
| if format != output.PlainFormatName { | ||
| return rd.data | ||
| } | ||
| ri := &ReportInfo{ | ||
| fmt.Sprintf("[ACTIONABLE]%s[/RESET]", rd.data.Project), | ||
| rd.data.CommitID, | ||
| rd.data.Date.Format("01/02/06"), | ||
| } | ||
| rd.output.Print(struct { | ||
| *ReportInfo `opts:"verticalTable"` | ||
| }{ri}) | ||
|
|
||
| if len(rd.data.Histogram) == 0 { | ||
| rd.output.Print("") | ||
| rd.output.Print(fmt.Sprintf("[SUCCESS]✔ %s[/RESET]", locale.Tl("no_cves", "No CVEs detected!"))) | ||
|
|
||
| return output.Suppress | ||
| } | ||
|
|
||
| hist := make([]*SeverityCountOutput, 0, len(rd.data.Histogram)) | ||
| totalCount := 0 | ||
| for _, h := range rd.data.Histogram { | ||
| totalCount += h.Count | ||
| var ho *SeverityCountOutput | ||
| if h.Severity == "CRITICAL" { | ||
| ho = &SeverityCountOutput{ | ||
| fmt.Sprintf("[ERROR]%d[/RESET]", h.Count), | ||
| fmt.Sprintf("[ERROR]%s[/RESET]", h.Severity), | ||
| } | ||
| } else { | ||
| ho = &SeverityCountOutput{ | ||
| fmt.Sprintf("%d", h.Count), | ||
| h.Severity, | ||
| } | ||
| } | ||
| hist = append(hist, ho) | ||
| } | ||
| rd.output.Print(output.Heading(fmt.Sprintf("%d Vulnerabilities", totalCount))) | ||
| rd.output.Print(hist) | ||
|
|
||
| rd.output.Print(output.Heading(fmt.Sprintf("%d Affected Packages", len(rd.data.Packages)))) | ||
| for _, ap := range rd.data.Packages { | ||
| rd.output.Print(fmt.Sprintf("[NOTICE]%s %s[/RESET]", ap.Name, ap.Version)) | ||
| rd.output.Print(locale.Tl("report_package_vulnerabilities", "{{.V0}} Vulnerabilities", strconv.Itoa(len(ap.Details)))) | ||
|
|
||
| sort.SliceStable(ap.Details, func(i, j int) bool { | ||
| sevI := ap.Details[i].Severity | ||
| sevJ := ap.Details[j].Severity | ||
| si := medmodel.ParseSeverityIndex(sevI) | ||
| sj := medmodel.ParseSeverityIndex(sevJ) | ||
| if si < sj { | ||
| return true | ||
| } | ||
| if si == sj { | ||
| return sevI < sevJ | ||
| } | ||
| return false | ||
| }) | ||
|
|
||
| for i, d := range ap.Details { | ||
| bar := "├─" | ||
| if i == len(ap.Details)-1 { | ||
| bar = "└─" | ||
| } | ||
| severity := d.Severity | ||
| if severity == "CRITICAL" { | ||
| severity = fmt.Sprintf("[ERROR]%-10s[/RESET]", severity) | ||
| } | ||
| rd.output.Print(fmt.Sprintf(" %s %-10s [ACTIONABLE]%s[/RESET]", bar, severity, d.CveId)) | ||
| } | ||
| rd.output.Print("") | ||
| } | ||
|
|
||
| rd.output.Print("") | ||
| rd.output.Print([]string{ | ||
| locale.Tl("cve_report_hint_cve", "To view a specific CVE, run [ACTIONABLE]state cve open [cve-id][/RESET]."), | ||
| }) | ||
mdrohmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return output.Suppress | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why initialize an empty namespace? Shouldn't this be nil if it wasn't provided?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is how we did in other places to set the namespace as an argument, by calling its
Set()method as required by theArgMarshalerinterface.