-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alan Tang <jmtangcs@gmail.com>
- Loading branch information
1 parent
8a7a5a6
commit bdc32f1
Showing
5 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,18 @@ | ||
package securityhub | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func SecurityHub() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "security-hub", | ||
Short: "Security Hub for managing security vulnerability", | ||
Long: "Security Hub provides tools to manage, monitor, and remediate security issues in repositories", | ||
} | ||
cmd.AddCommand( | ||
ListVulnerabilityCommand(), | ||
) | ||
|
||
return cmd | ||
} |
This file contains 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,42 @@ | ||
package securityhub | ||
|
||
import ( | ||
"github.com/goharbor/harbor-cli/pkg/api" | ||
"github.com/goharbor/harbor-cli/pkg/utils" | ||
"github.com/goharbor/harbor-cli/pkg/views/securityhub/list" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
func ListVulnerabilityCommand() *cobra.Command { | ||
var opts api.ListFlags | ||
|
||
cmd := &cobra.Command{ | ||
Use: "list", | ||
Short: "Show all the vulnerability list", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
vulnerability, err := api.ListVulnerability(opts) | ||
if err != nil { | ||
log.Fatalf("failed to get vulnerability list: %v", err) | ||
return | ||
} | ||
|
||
FormatFlag := viper.GetString("output-format") | ||
if FormatFlag != "" { | ||
err = utils.PrintFormat(vulnerability, FormatFlag) | ||
if err != nil { | ||
log.Error(err) | ||
} | ||
} else { | ||
list.ListVulnerability(vulnerability.Payload) | ||
} | ||
}, | ||
} | ||
|
||
flags := cmd.Flags() | ||
flags.Int64VarP(&opts.Page, "page", "", 1, "Page number") | ||
flags.Int64VarP(&opts.PageSize, "page-size", "", 10, "Size of per page") | ||
|
||
return cmd | ||
} |
This file contains 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,28 @@ | ||
package api | ||
|
||
import ( | ||
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/securityhub" | ||
"github.com/goharbor/harbor-cli/pkg/utils" | ||
) | ||
|
||
func ListVulnerability(opts ...ListFlags) (*securityhub.ListVulnerabilitiesOK, error) { | ||
ctx, client, err := utils.ContextWithClient() | ||
if err != nil { | ||
return &securityhub.ListVulnerabilitiesOK{}, err | ||
} | ||
|
||
var listFlags ListFlags | ||
if len(opts) > 0 { | ||
listFlags = opts[0] | ||
} | ||
|
||
response, err := client.Securityhub.ListVulnerabilities(ctx, | ||
&securityhub.ListVulnerabilitiesParams{ | ||
Page: &listFlags.Page, | ||
PageSize: &listFlags.PageSize, | ||
}) | ||
if err != nil { | ||
return &securityhub.ListVulnerabilitiesOK{}, err | ||
} | ||
return response, nil | ||
} |
This file contains 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,52 @@ | ||
package list | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/charmbracelet/bubbles/table" | ||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/goharbor/go-client/pkg/sdk/v2.0/models" | ||
"github.com/goharbor/harbor-cli/pkg/views/base/tablelist" | ||
) | ||
|
||
var columns = []table.Column{ | ||
{Title: "CVE ID", Width: 12}, | ||
{Title: "Repository Name", Width: 12}, | ||
{Title: "Digest", Width: 12}, | ||
{Title: "Tags", Width: 12}, | ||
{Title: "CVSS3", Width: 5}, | ||
{Title: "Severity", Width: 10}, | ||
{Title: "Package", Width: 10}, | ||
{Title: "Current version", Width: 15}, | ||
{Title: "Fixed in version", Width: 15}, | ||
} | ||
|
||
func ListVulnerability(vulnerability []*models.VulnerabilityItem) { | ||
var rows []table.Row | ||
for _, vul := range vulnerability { | ||
var tags string | ||
if len(tags) != 0 { | ||
tags = strings.Join(vul.Tags, ",") | ||
} | ||
rows = append(rows, table.Row{ | ||
vul.CVEID, | ||
vul.RepositoryName, | ||
vul.Digest, | ||
tags, | ||
fmt.Sprintf("%.1f", vul.CvssV3Score), | ||
vul.Severity, | ||
vul.Package, | ||
vul.Version, | ||
vul.FixedVersion, | ||
}) | ||
} | ||
|
||
m := tablelist.NewModel(columns, rows, len(rows)) | ||
|
||
if _, err := tea.NewProgram(m).Run(); err != nil { | ||
fmt.Println("Error running program:", err) | ||
os.Exit(1) | ||
} | ||
} |