forked from aquasecurity/starboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add subcommand to fetch vulnerability reports (#28)
Resolves: #22 Signed-off-by: Daniel Pacak <pacak.daniel@gmail.com>
- Loading branch information
1 parent
8dd2c97
commit 8d2b8f0
Showing
13 changed files
with
187 additions
and
16 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
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 cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"k8s.io/cli-runtime/pkg/genericclioptions" | ||
) | ||
|
||
func NewGetCmd(cf *genericclioptions.ConfigFlags) *cobra.Command { | ||
getCmd := &cobra.Command{ | ||
Use: "get", | ||
Short: "Get security reports", | ||
} | ||
getCmd.AddCommand(NewGetVulnerabilitiesCmd(cf)) | ||
getCmd.AddCommand(NewGetConfigAuditCmd(cf)) | ||
getCmd.PersistentFlags().StringP("output", "o", "yaml", "Output format. One of yaml|json") | ||
|
||
return getCmd | ||
} |
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,58 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
|
||
starboard "github.com/aquasecurity/starboard/pkg/apis/aquasecurity/v1alpha1" | ||
"github.com/spf13/cobra" | ||
"k8s.io/cli-runtime/pkg/genericclioptions" | ||
) | ||
|
||
func NewGetConfigAuditCmd(cf *genericclioptions.ConfigFlags) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "configaudit (NAME | TYPE/NAME)", | ||
Short: "Get configuration audit report", | ||
Long: `Get configuration audit report for the specified workload | ||
TYPE is a Kubernetes workload. Shortcuts and API groups will be resolved, e.g. 'po' or 'deployments.apps'. | ||
NAME is the name of a particular Kubernetes workload. | ||
`, | ||
Example: fmt.Sprintf(` # Get configuration audit for a Deployment with the specified name | ||
%[1]s get configauditreports.aquasecurity.github.io deploy/nginx | ||
# Get configuration audit for a Deployment with the specified name in the specified namespace | ||
%[1]s get configauditreports deploy/nginx -n staging | ||
# Get configuration audit for a ReplicaSet with the specified name | ||
%[1]s get configaudit replicaset/nginx | ||
# Get vulnerabilities for a CronJob with the specified name in JSON output format | ||
%[1]s get configaudit cj/my-job -o json`, "starboard"), | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
ns, _, err := cf.ToRawKubeConfigLoader().Namespace() | ||
if err != nil { | ||
return | ||
} | ||
workload, err := WorkloadFromArgs(ns, args) | ||
if err != nil { | ||
return | ||
} | ||
|
||
kubectlCmd := exec.Command("kubectl", | ||
"get", | ||
starboard.ConfigAuditReportCRName, | ||
fmt.Sprintf("-l=starboard.resource.kind=%s,starboard.resource.name=%s", workload.Kind, workload.Name), | ||
fmt.Sprintf("--namespace=%s", workload.Namespace), | ||
fmt.Sprintf("--output=%s", cmd.Flag("output").Value.String())) | ||
stdoutStderr, err := kubectlCmd.CombinedOutput() | ||
if err != nil { | ||
return | ||
} | ||
fmt.Printf("%s", stdoutStderr) | ||
return | ||
}, | ||
} | ||
|
||
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,59 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
|
||
starboard "github.com/aquasecurity/starboard/pkg/apis/aquasecurity/v1alpha1" | ||
"github.com/spf13/cobra" | ||
"k8s.io/cli-runtime/pkg/genericclioptions" | ||
) | ||
|
||
func NewGetVulnerabilitiesCmd(cf *genericclioptions.ConfigFlags) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Aliases: []string{"vulns", "vuln"}, | ||
Use: "vulnerabilities (NAME | TYPE/NAME)", | ||
Short: "Get vulnerabilities report", | ||
Long: `Get vulnerabilities report for the specified workload | ||
TYPE is a Kubernetes workload. Shortcuts and API groups will be resolved, e.g. 'po' or 'deployments.apps'. | ||
NAME is the name of a particular Kubernetes workload. | ||
`, | ||
Example: fmt.Sprintf(` # Get vulnerabilities for a Deployment with the specified name | ||
%[1]s get vulnerabilities.aquasecurity.github.io deploy/nginx | ||
# Get vulnerabilities for a Deployment with the specified name in the specified namespace | ||
%[1]s get vulnerabilities deploy/nginx -n staging | ||
# Get vulnerabilities for a ReplicaSet with the specified name | ||
%[1]s get vulns replicaset/nginx | ||
# Get vulnerabilities for a CronJob with the specified name in JSON output format | ||
%[1]s get vuln cj/my-job -o json`, "starboard"), | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
ns, _, err := cf.ToRawKubeConfigLoader().Namespace() | ||
if err != nil { | ||
return | ||
} | ||
workload, err := WorkloadFromArgs(ns, args) | ||
if err != nil { | ||
return | ||
} | ||
|
||
kubectlCmd := exec.Command("kubectl", | ||
"get", | ||
starboard.VulnerabilitiesCRName, | ||
fmt.Sprintf("-l=starboard.resource.kind=%s,starboard.resource.name=%s", workload.Kind, workload.Name), | ||
fmt.Sprintf("--namespace=%s", workload.Namespace), | ||
fmt.Sprintf("--output=%s", cmd.Flag("output").Value.String())) | ||
stdoutStderr, err := kubectlCmd.CombinedOutput() | ||
if err != nil { | ||
return | ||
} | ||
fmt.Printf("%s", stdoutStderr) | ||
return | ||
}, | ||
} | ||
|
||
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
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
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,25 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
type VersionInfo struct { | ||
Version string | ||
Commit string | ||
Date string | ||
} | ||
|
||
func NewVersionCmd(version VersionInfo) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "version", | ||
Short: "Print the version information", | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
fmt.Printf("Starboard Version: %+v\n", version) | ||
return | ||
}, | ||
} | ||
return cmd | ||
} |