Skip to content

Commit

Permalink
feat: Use templates for usage examples (#57)
Browse files Browse the repository at this point in the history
Starboard CLI can be run in a stand-alone mode or as
a kubectl plugin. This commit detects and reflects the
mode in the usage examples.

Signed-off-by: Daniel Pacak <pacak.daniel@gmail.com>
  • Loading branch information
danielpacak committed Jun 17, 2020
1 parent 8ff3a1d commit 7a157d6
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 28 deletions.
14 changes: 11 additions & 3 deletions cmd/starboard/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package main
import (
"flag"
"fmt"
"os"

"github.com/spf13/pflag"
"os"
"path/filepath"
"strings"

"github.com/aquasecurity/starboard/pkg/cmd"
"k8s.io/klog"
Expand All @@ -28,12 +29,19 @@ func main() {

version := cmd.VersionInfo{Version: version, Commit: commit, Date: date}

if err := cmd.NewRootCmd(version).Execute(); err != nil {
if err := cmd.NewRootCmd(executable(os.Args), version).Execute(); err != nil {
fmt.Printf("error: %v\n", err)
os.Exit(1)
}
}

func executable(args []string) string {
if strings.HasPrefix(filepath.Base(args[0]), "kubectl-") {
return "kubectl starboard"
}
return "starboard"
}

func initFlags() {
klog.InitFlags(nil)
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.14

require (
github.com/google/uuid v1.1.1
github.com/spf13/cobra v0.0.5
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.5.1
k8s.io/api v0.19.0-alpha.3
Expand Down
32 changes: 30 additions & 2 deletions go.sum

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/cmd/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"k8s.io/cli-runtime/pkg/genericclioptions"
)

func NewFindCmd(cf *genericclioptions.ConfigFlags) *cobra.Command {
func NewFindCmd(executable string, cf *genericclioptions.ConfigFlags) *cobra.Command {
findCmd := &cobra.Command{
Use: "find",
Short: "Manage security scanners",
}
findCmd.AddCommand(GetVulnerabilitiesCmd(cf))
findCmd.AddCommand(GetVulnerabilitiesCmd(executable, cf))

return findCmd
}
25 changes: 13 additions & 12 deletions pkg/cmd/find_vulnerabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"context"
"fmt"

"github.com/aquasecurity/starboard/pkg/find/vulnerabilities/crd"
"github.com/aquasecurity/starboard/pkg/find/vulnerabilities/trivy"
Expand All @@ -11,7 +12,7 @@ import (
"k8s.io/client-go/kubernetes"
)

func GetVulnerabilitiesCmd(cf *genericclioptions.ConfigFlags) *cobra.Command {
func GetVulnerabilitiesCmd(executable string, cf *genericclioptions.ConfigFlags) *cobra.Command {
cmd := &cobra.Command{
Aliases: []string{"vulns", "vuln"},
Use: "vulnerabilities (NAME | TYPE/NAME)",
Expand All @@ -21,32 +22,32 @@ func GetVulnerabilitiesCmd(cf *genericclioptions.ConfigFlags) *cobra.Command {
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: ` # Scan a pod with the specified name
kubectl starboard find vulnerabilities nginx
Example: fmt.Sprintf(` # Scan a pod with the specified name
%[1]s find vulnerabilities nginx
# Scan a pod with the specified name in the specified namespace
kubectl starboard find vulns po/nginx -n staging
%[1]s find vulns po/nginx -n staging
# Scan a replicaset with the specified name
kubectl starboard find vuln replicaset/nginx
%[1]s find vuln replicaset/nginx
# Scan a replicationcontroller with the given name
kubectl starboard find vulns rc/nginx
%[1]s find vulns rc/nginx
# Scan a deployment with the specified name
kubectl starboard find vulns deployments.apps/nginx
%[1]s find vulns deployments.apps/nginx
# Scan a daemonset with the specified name
kubectl starboard find vulns daemonsets/nginx
%[1]s starboard find vulns daemonsets/nginx
# Scan a statefulset with the specified name
kubectl starboard find vulns sts/redis
%[1]s vulns sts/redis
# Scan a job with the specified name
kubectl starboard find vulns job/my-job
%[1]s find vulns job/my-job
# Scan a cronjob with the specified name
kubectl starboard find vulns cj/my-cronjob`,
# Scan a cronjob with the specified name and the specified scan job timeout
%[1]s find vulns cj/my-cronjob --scan-job-timeout 2m`, executable),
RunE: func(cmd *cobra.Command, args []string) (err error) {
ctx := context.Background()
ns, _, err := cf.ToRawKubeConfigLoader().Namespace()
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"k8s.io/cli-runtime/pkg/genericclioptions"
)

func NewGetCmd(cf *genericclioptions.ConfigFlags) *cobra.Command {
func NewGetCmd(executable string, cf *genericclioptions.ConfigFlags) *cobra.Command {
getCmd := &cobra.Command{
Use: "get",
Short: "Get security reports",
}
getCmd.AddCommand(NewGetVulnerabilitiesCmd(cf))
getCmd.AddCommand(NewGetVulnerabilitiesCmd(executable, cf))
getCmd.AddCommand(NewGetConfigAuditCmd(cf))
getCmd.PersistentFlags().StringP("output", "o", "yaml", "Output format. One of yaml|json")

Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/get_vulnerabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"k8s.io/cli-runtime/pkg/genericclioptions"
)

func NewGetVulnerabilitiesCmd(cf *genericclioptions.ConfigFlags) *cobra.Command {
func NewGetVulnerabilitiesCmd(executable string, cf *genericclioptions.ConfigFlags) *cobra.Command {
cmd := &cobra.Command{
Aliases: []string{"vulns", "vuln"},
Use: "vulnerabilities (NAME | TYPE/NAME)",
Expand All @@ -29,7 +29,7 @@ NAME is the name of a particular Kubernetes workload.
%[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"),
%[1]s get vuln cj/my-job -o json`, executable),
RunE: func(cmd *cobra.Command, args []string) (err error) {
ns, _, err := cf.ToRawKubeConfigLoader().Namespace()
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"k8s.io/cli-runtime/pkg/genericclioptions"
)

func NewRootCmd(version VersionInfo) *cobra.Command {
func NewRootCmd(executable string, version VersionInfo) *cobra.Command {
var cf *genericclioptions.ConfigFlags

rootCmd := &cobra.Command{
Use: "starboard",
Short: "Kubernetes-native security",
Short: "Kubernetes-native security toolkit",
SilenceErrors: true,
SilenceUsage: true,
}
Expand All @@ -20,11 +20,11 @@ func NewRootCmd(version VersionInfo) *cobra.Command {
rootCmd.AddCommand(NewVersionCmd(version))
rootCmd.AddCommand(NewInitCmd(cf))
rootCmd.AddCommand(NewRBACCmd(cf))
rootCmd.AddCommand(NewFindCmd(cf))
rootCmd.AddCommand(NewFindCmd(executable, cf))
rootCmd.AddCommand(NewKubeBenchCmd(cf))
rootCmd.AddCommand(NewKubeHunterCmd(cf))
rootCmd.AddCommand(NewPolarisCmd(cf))
rootCmd.AddCommand(NewGetCmd(cf))
rootCmd.AddCommand(NewGetCmd(executable, cf))
rootCmd.AddCommand(NewCleanupCmd(cf))

SetGlobalFlags(cf, rootCmd)
Expand Down

0 comments on commit 7a157d6

Please sign in to comment.