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

feat add security command #168

Closed
wants to merge 2 commits into from
Closed

Conversation

mahbub570
Copy link
Contributor

Implements functions to fetch and display the security summary and vulnerabilities list

New Command:
harbor security summary
harbor security vulnerabilities

Fix: #94

Signed-off-by: mahbub570 <[email protected]>
@mahbub570
Copy link
Contributor Author

summary:
summary

vulnerabilities:
vulnerabilities

Copy link
Collaborator

@bupd bupd left a comment

Choose a reason for hiding this comment

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

Giving Table as defautlt for summary would be better.

@Althaf66
Copy link
Collaborator

@mahbub570 Changes in cmd/harbor/root/security/summary.go

package security

import (
	"fmt"

	"github.com/goharbor/harbor-cli/pkg/api"
	"github.com/goharbor/harbor-cli/pkg/views/security/summary"
	"github.com/spf13/cobra"
)

func getSecuritySummaryCommand() *cobra.Command {
	cmd := &cobra.Command{
		Use:   "summary",
		Short: "Get the security summary of the system",
		RunE: func(cmd *cobra.Command, args []string) error {
			response,err := api.GetSecuritySummary()
			if err != nil {
				return fmt.Errorf("error getting security summary: %w", err)
			}
			summary.SecuritySummary(response.Payload)
			return nil
		},
	}

	return cmd
}

@Althaf66
Copy link
Collaborator

Changes in pkg/api/security_handler.go

package api

import (
	"github.com/goharbor/go-client/pkg/sdk/v2.0/client/securityhub"
	"github.com/goharbor/harbor-cli/pkg/utils"
)

func GetSecuritySummary() (*securityhub.GetSecuritySummaryOK, error) {
	ctx, client, err := utils.ContextWithClient()
	if err != nil {
		return nil, err
	}

	response, err := client.Securityhub.GetSecuritySummary(ctx,&securityhub.GetSecuritySummaryParams{})
	if err != nil {
		return nil,err
	}

	return response,nil
}

func ListVulnerabilities(query string) error {
	ctx, client, err := utils.ContextWithClient()
	if err != nil {
		return err
	}

	params := &securityhub.ListVulnerabilitiesParams{
		Q: &query,
	}

	response, err := client.Securityhub.ListVulnerabilities(ctx, params)
	if err != nil {
		return err
	}

	utils.PrintPayloadInJSONFormat(response.Payload)
	return nil
}

@Althaf66
Copy link
Collaborator

Create new file in pkg/views/security/summary/view.go

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 = []table.Column{
	{Title: "Summary", Width: 25},
	{Title: "Value", Width: 15},
}

func SecuritySummary(summary *models.SecuritySummary) {
	var rows []table.Row

	rows = append(rows, table.Row{"Critical Count", fmt.Sprintf("%d",summary.CriticalCnt)})
	rows = append(rows, table.Row{"Fixable Count", fmt.Sprintf("%d",summary.FixableCnt)})
	rows = append(rows, table.Row{"High Count", fmt.Sprintf("%d",summary.HighCnt)})
	rows = append(rows, table.Row{"Low Count", fmt.Sprintf("%d",summary.LowCnt)})
	rows = append(rows, table.Row{"Medium Count", fmt.Sprintf("%d",summary.MediumCnt)})
	rows = append(rows, table.Row{"Scanned Count", fmt.Sprintf("%d",summary.ScannedCnt)})
	rows = append(rows, table.Row{"None Count", fmt.Sprintf("%d",summary.NoneCnt)})
	rows = append(rows, table.Row{"Unknown Count", fmt.Sprintf("%d",summary.UnknownCnt)})
	rows = append(rows, table.Row{"Total Artifact", fmt.Sprintf("%d",summary.TotalArtifact)})
	rows = append(rows, table.Row{"Total Vulnerability", fmt.Sprintf("%d",summary.TotalVuls)})
	
	m := tablelist.NewModel(columns, rows, len(rows))
	if _, err := tea.NewProgram(m).Run(); err != nil {
		fmt.Println("Error running program:", err)
		os.Exit(1)
	}
}

@Althaf66
Copy link
Collaborator

harbor security summary
image

Signed-off-by: axif <[email protected]>
@mahbub570 mahbub570 closed this Sep 15, 2024
@mahbub570 mahbub570 deleted the security branch September 15, 2024 07:55
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.

📌 Tracker: Missing and To-Be-Added CLI Commands
4 participants