-
Notifications
You must be signed in to change notification settings - Fork 183
feat/vuln list command #748
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
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0efac18
feat(api): add vuln list handler for Security Hub endpoint
Sypher845 ef78343
feat(vuln): vuln list command and view
Sypher845 8ef8542
docs: cli and man page docs for vuln list command
Sypher845 fe84b23
refactor: vuln filtering logic
Sypher845 faae44b
refractor: add 'all' flag to fetch all vuln upto 1000
Sypher845 bab112d
chore(vuln): remove raw --query flag
Sypher845 2d9a4cc
refactor(vuln): improve vulnerability list filtering flow
Sypher845 b656089
feat: add CSV output format support using gocsv
Sypher845 5f678ca
doc(csv): docs for output-format csv
Sypher845 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| // Copyright Project Harbor Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| package vulnerability | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/goharbor/go-client/pkg/sdk/v2.0/models" | ||
| "github.com/goharbor/harbor-cli/pkg/api" | ||
| "github.com/goharbor/harbor-cli/pkg/utils" | ||
| vulnlist "github.com/goharbor/harbor-cli/pkg/views/vulnerability/list" | ||
| log "github.com/sirupsen/logrus" | ||
| "github.com/spf13/cobra" | ||
| "github.com/spf13/viper" | ||
| ) | ||
|
|
||
| func ListVulnerabilitiesCommand() *cobra.Command { | ||
| var opts api.ListVulnerabilityOptions | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "list", | ||
| Short: "List vulnerabilities in Security Hub", | ||
| Long: "List vulnerabilities from Harbor Security Hub", | ||
| Example: ` harbor vulnerability list`, | ||
| Args: cobra.NoArgs, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| if opts.Page < 1 { | ||
| return fmt.Errorf("page number must be greater than or equal to 1") | ||
| } | ||
|
|
||
| if opts.PageSize <= 0 || opts.PageSize > 100 { | ||
| return fmt.Errorf("page size must be greater than 0 and less than or equal to 100") | ||
| } | ||
|
|
||
| allVulnerabilities, hasNext, err := fetchVulnerabilities(opts) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to list vulnerabilities: %v", utils.ParseHarborErrorMsg(err)) | ||
| } | ||
|
|
||
| if len(allVulnerabilities) == 0 { | ||
| log.Info("No vulnerabilities found") | ||
| return nil | ||
| } | ||
|
|
||
| formatFlag := viper.GetString("output-format") | ||
| if formatFlag != "" { | ||
| err = utils.PrintFormat(allVulnerabilities, formatFlag) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } else { | ||
| vulnlist.ViewVulnerabilityList(allVulnerabilities, hasNext) | ||
| } | ||
|
|
||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| flags := cmd.Flags() | ||
| flags.Int64VarP(&opts.Page, "page", "", 1, "Page number") | ||
| flags.Int64VarP(&opts.PageSize, "page-size", "", 10, "Size of per page") | ||
| flags.StringVarP(&opts.CVEID, "cve-id", "", "", "Filter by exact CVE ID") | ||
| flags.StringVarP(&opts.CVSSScore, "cvss-score", "", "", "Filter by CVSS v3 score range (e.g. 7.0~10.0) or exact score (e.g. 7.0)") | ||
| flags.StringVarP(&opts.Severity, "severity", "", "", "Filter by severity level") | ||
| flags.StringVarP(&opts.Repository, "repository", "", "", "Filter by exact repository name") | ||
| flags.StringVarP(&opts.ProjectName, "project-name", "", "", "Filter by exact project name") | ||
| flags.StringVarP(&opts.Package, "package", "", "", "Filter by exact package name") | ||
| flags.StringVarP(&opts.Tag, "tag", "", "", "Filter by exact artifact tag") | ||
| flags.StringVarP(&opts.Digest, "digest", "", "", "Filter by exact artifact digest") | ||
| flags.StringVarP(&opts.Exclude, "exclude", "", "", "Exclude vulnerabilities using a ',' separated query string (e.g., k=v or k=[min~max])") | ||
| flags.BoolVarP(&opts.Fixable, "fixable", "", false, "Only show fixable vulnerabilities") | ||
| flags.BoolVarP(&opts.All, "all", "", false, "Show all vulnerabilities (up to 1000)") | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func fetchVulnerabilities(opts api.ListVulnerabilityOptions) ([]*models.VulnerabilityItem, bool, error) { | ||
| if opts.All { | ||
| var allVulnerabilities []*models.VulnerabilityItem | ||
|
|
||
| log.Debug("Fetching all vulnerabilities") | ||
| opts.PageSize = 100 | ||
| opts.Page = 1 | ||
|
|
||
| for { | ||
| vulnerabilities, err := api.ListVulnerabilities(opts) | ||
| if err != nil { | ||
| return nil, false, err | ||
| } | ||
|
|
||
| if len(vulnerabilities) == 0 { | ||
| break | ||
| } | ||
|
|
||
| allVulnerabilities = append(allVulnerabilities, vulnerabilities...) | ||
| opts.Page++ | ||
|
|
||
| if opts.Page > 10 { | ||
| return allVulnerabilities, true, nil | ||
| } | ||
| } | ||
|
|
||
| return allVulnerabilities, false, nil | ||
| } | ||
|
|
||
| vulnerabilities, err := api.ListVulnerabilities(opts) | ||
| if err != nil { | ||
| return nil, false, err | ||
| } | ||
|
|
||
| return vulnerabilities, false, nil | ||
| } | ||
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
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
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
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
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.