-
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
bdc32f1
commit 27c9ec3
Showing
5 changed files
with
229 additions
and
12 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,106 @@ | ||
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/summary" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
func SummaryVulnerabilityCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "summary", | ||
Short: "The vulnerability summary of the system", | ||
} | ||
|
||
cmd.AddCommand( | ||
totalVulnerabilities(), | ||
MostDangerousArtifacts(), | ||
MostDangerousCVE(), | ||
) | ||
|
||
return cmd | ||
} | ||
|
||
func totalVulnerabilities() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "total", | ||
Short: "Total Vulnerabilities", | ||
Args: cobra.NoArgs, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
sum, err := api.GetTotalVulnerabilities(false, false) | ||
|
||
if err != nil { | ||
log.Fatalf("failed to get vulnerability summary: %v", err) | ||
return | ||
} | ||
FormatFlag := viper.GetString("output-format") | ||
if FormatFlag != "" { | ||
err = utils.PrintFormat(sum, FormatFlag) | ||
if err != nil { | ||
log.Error(err) | ||
} | ||
} else { | ||
summary.GetTotalVulnerability(sum.Payload) | ||
} | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func MostDangerousArtifacts() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "artifact", | ||
Short: "Top 5 Most Dangerous Artifacts", | ||
Args: cobra.NoArgs, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
sum, err := api.GetTotalVulnerabilities(true, false) | ||
|
||
if err != nil { | ||
log.Fatalf("failed to get most dangerous artifacts: %v", err) | ||
return | ||
} | ||
FormatFlag := viper.GetString("output-format") | ||
if FormatFlag != "" { | ||
err = utils.PrintFormat(sum, FormatFlag) | ||
if err != nil { | ||
log.Error(err) | ||
} | ||
} else { | ||
summary.ShowMostDangerousArtifacts(sum.Payload) | ||
} | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func MostDangerousCVE() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "cvelist", | ||
Short: "Top 5 Most Dangerous CVEs", | ||
Args: cobra.NoArgs, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
sum, err := api.GetTotalVulnerabilities(false, true) | ||
|
||
if err != nil { | ||
log.Fatalf("failed to get most dangerous cvelist: %v", err) | ||
return | ||
} | ||
FormatFlag := viper.GetString("output-format") | ||
if FormatFlag != "" { | ||
err = utils.PrintFormat(sum, FormatFlag) | ||
if err != nil { | ||
log.Error(err) | ||
} | ||
} else { | ||
summary.ShowMostDangerousCVE(sum.Payload) | ||
} | ||
}, | ||
} | ||
|
||
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
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,94 @@ | ||
package summary | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"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_total = []table.Column{ | ||
{Title: "Critical", Width: 10}, | ||
{Title: "High", Width: 10}, | ||
{Title: "Medium", Width: 10}, | ||
{Title: "Low", Width: 10}, | ||
{Title: "N/A", Width: 10}, | ||
{Title: "None", Width: 10}, | ||
} | ||
|
||
func GetTotalVulnerability(item *models.SecuritySummary) { | ||
var rows []table.Row | ||
fmt.Printf("%d total with %d fixable\n", item.TotalVuls, item.FixableCnt) | ||
rows = append(rows, table.Row{ | ||
fmt.Sprintf("%d", item.CriticalCnt), | ||
fmt.Sprintf("%d", item.HighCnt), | ||
fmt.Sprintf("%d", item.MediumCnt), | ||
fmt.Sprintf("%d", item.LowCnt), | ||
fmt.Sprintf("%d", item.UnknownCnt), | ||
fmt.Sprintf("%d", item.NoneCnt), | ||
}) | ||
|
||
m := tablelist.NewModel(columns_total, rows, len(rows)+1) | ||
|
||
if _, err := tea.NewProgram(m).Run(); err != nil { | ||
fmt.Println("Error running program:", err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
var columns_artifact = []table.Column{ | ||
{Title: "Repository Name", Width: 15}, | ||
{Title: "Digest", Width: 15}, | ||
{Title: "Critical", Width: 10}, | ||
{Title: "High", Width: 10}, | ||
{Title: "Medium", Width: 10}, | ||
} | ||
|
||
func ShowMostDangerousArtifacts(item *models.SecuritySummary) { | ||
var rows []table.Row | ||
for _, artifact := range item.DangerousArtifacts { | ||
rows = append(rows, table.Row{ | ||
artifact.RepositoryName, | ||
artifact.Digest[:16], | ||
fmt.Sprintf("%d", item.CriticalCnt), | ||
fmt.Sprintf("%d", item.HighCnt), | ||
fmt.Sprintf("%d", item.MediumCnt), | ||
}) | ||
} | ||
|
||
m := tablelist.NewModel(columns_artifact, rows, len(rows)+1) | ||
|
||
if _, err := tea.NewProgram(m).Run(); err != nil { | ||
fmt.Println("Error running program:", err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
var columns_cve = []table.Column{ | ||
{Title: "CVE ID", Width: 15}, | ||
{Title: "Severity", Width: 15}, | ||
{Title: "CVSS3", Width: 5}, | ||
{Title: "Package", Width: 15}, | ||
} | ||
|
||
func ShowMostDangerousCVE(item *models.SecuritySummary) { | ||
var rows []table.Row | ||
for _, cve := range item.DangerousCves { | ||
rows = append(rows, table.Row{ | ||
cve.CVEID, | ||
cve.Severity, | ||
fmt.Sprintf("%.1f", cve.CvssScoreV3), | ||
cve.Package, | ||
}) | ||
} | ||
|
||
m := tablelist.NewModel(columns_cve, rows, len(rows)+1) | ||
|
||
if _, err := tea.NewProgram(m).Run(); err != nil { | ||
fmt.Println("Error running program:", err) | ||
os.Exit(1) | ||
} | ||
} |