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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/harbor/root/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/goharbor/harbor-cli/cmd/harbor/root/artifact"
"github.com/goharbor/harbor-cli/cmd/harbor/root/project"
"github.com/goharbor/harbor-cli/cmd/harbor/root/registry"
"github.com/goharbor/harbor-cli/cmd/harbor/root/security"
repositry "github.com/goharbor/harbor-cli/cmd/harbor/root/repository"
"github.com/goharbor/harbor-cli/cmd/harbor/root/user"
"github.com/goharbor/harbor-cli/pkg/utils"
Expand Down Expand Up @@ -109,6 +110,7 @@ harbor help
repositry.Repository(),
user.User(),
artifact.Artifact(),
security.Security(),
)

return root
Expand Down
17 changes: 17 additions & 0 deletions cmd/harbor/root/security/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package security

import "github.com/spf13/cobra"

func Security() *cobra.Command {
cmd := &cobra.Command{
Use: "security",
Short: "Manage security-related operations in Harbor",
}

cmd.AddCommand(
getSecuritySummaryCommand(),
listVulnerabilitiesCommand(),
)

return cmd
}
23 changes: 23 additions & 0 deletions cmd/harbor/root/security/summary.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package security

import (
"fmt"
"github.com/goharbor/harbor-cli/pkg/api"
"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 {
err := api.GetSecuritySummary()
if err != nil {
return fmt.Errorf("error getting security summary: %w", err)
}
return nil
},
}

return cmd
}
27 changes: 27 additions & 0 deletions cmd/harbor/root/security/vulnerabilities.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package security

import (
"fmt"
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/spf13/cobra"
)

func listVulnerabilitiesCommand() *cobra.Command {
var query string

cmd := &cobra.Command{
Use: "vulnerabilities",
Short: "List vulnerabilities in the system",
RunE: func(cmd *cobra.Command, args []string) error {
err := api.ListVulnerabilities(query)
if err != nil {
return fmt.Errorf("error listing vulnerabilities: %w", err)
}
return nil
},
}

cmd.Flags().StringVarP(&query, "query", "q", "", "Query condition for filtering vulnerabilities")

return cmd
}
41 changes: 41 additions & 0 deletions pkg/api/security_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package api

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

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

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

utils.PrintPayloadInJSONFormat(response.Payload)
return 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
}