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

migrate to kubesec library #132

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
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
68 changes: 63 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,68 @@
module github.com/controlplaneio/kubectl-kubesec/v2

go 1.15
go 1.17

require (
github.com/spf13/cobra v1.2.1
github.com/stretchr/testify v1.7.0 // indirect
k8s.io/apimachinery v0.22.2
k8s.io/client-go v0.22.2
github.com/controlplaneio/kubesec/v2 v2.12.0
github.com/spf13/cobra v1.6.1
github.com/stretchr/testify v1.8.1
go.uber.org/zap v1.23.0
k8s.io/apimachinery v0.25.4
k8s.io/client-go v0.25.4
)

require (
github.com/PuerkitoBio/purell v1.2.0 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.10.1 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gnostic v0.6.9 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/in-toto/in-toto-golang v0.5.0 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/secure-systems-lab/go-securesystemslib v0.4.0 // indirect
github.com/shibumi/go-pathspec v1.3.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/thedevsaddam/gojsonq/v2 v2.5.2 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/yannh/kubeconform v0.5.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/crypto v0.3.0 // indirect
golang.org/x/net v0.2.0 // indirect
golang.org/x/oauth2 v0.2.0 // indirect
golang.org/x/sys v0.2.0 // indirect
golang.org/x/term v0.2.0 // indirect
golang.org/x/text v0.4.0 // indirect
golang.org/x/time v0.2.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.25.4 // indirect
k8s.io/klog/v2 v2.80.1 // indirect
k8s.io/kube-openapi v0.0.0-20221116234839-dd070e2c4cb3 // indirect
k8s.io/utils v0.0.0-20221108210102-8e77b1f39fe2 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
646 changes: 119 additions & 527 deletions go.sum

Large diffs are not rendered by default.

27 changes: 8 additions & 19 deletions pkg/cmd/daemonset.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package cmd

import (
"bufio"
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"os"
@@ -16,6 +15,7 @@ import (

var daemonsetCmd = &cobra.Command{
Use: `daemonset [name]`,
Aliases: []string{"ds"},
Short: "Scans daemonset object",
Example: ` daemonset podinfo --namespace=default`,
RunE: func(cmd *cobra.Command, args []string) error {
@@ -24,43 +24,32 @@ var daemonsetCmd = &cobra.Command{
}
name := args[0]

var buffer bytes.Buffer
writer := bufio.NewWriter(&buffer)

fmt.Println("scanning daemonset", name, "in namespace", namespace)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
ds, err := kubeClient.AppsV1().DaemonSets(namespace).Get(ctx, name, metav1.GetOptions{})
daemonset, err := kubeClient.AppsV1().DaemonSets(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
ds.TypeMeta = metav1.TypeMeta{
daemonset.TypeMeta = metav1.TypeMeta{
Kind: "DaemonSet",
APIVersion: "apps/v1",
}
err = serializer.Encode(ds, writer)
var buf []byte
buf, err = json.Marshal(daemonset)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

if err := writer.Flush(); err != nil {
return err
}

kc := kubesec.NewClient(scanURL, scanTimeOut)
rs, err := kc.ScanDefinition(buffer)

result, err := kubesec.NewClient().ScanDefinition(buf)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

if err := rs.Dump(os.Stdout); err != nil {
fmt.Println(err)
os.Exit(1)
}
kubesec.DumpReport(result, os.Stdout)

return nil
},
28 changes: 8 additions & 20 deletions pkg/cmd/deployment.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package cmd

import (
"bufio"
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"os"
@@ -16,6 +15,7 @@ import (

var deploymentCmd = &cobra.Command{
Use: `deployment [name]`,
Aliases: []string{"deploy"},
Short: "Scans deployment object",
Example: ` deployment podinfo --namespace=default`,
RunE: func(cmd *cobra.Command, args []string) error {
@@ -24,44 +24,32 @@ var deploymentCmd = &cobra.Command{
}
name := args[0]

var buffer bytes.Buffer
writer := bufio.NewWriter(&buffer)

fmt.Println("scanning deployment", name, "in namespace", namespace)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
dep, err := kubeClient.AppsV1().Deployments(namespace).Get(ctx, name, metav1.GetOptions{})
deployment, err := kubeClient.AppsV1().Deployments(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
dep.TypeMeta = metav1.TypeMeta{
deployment.TypeMeta = metav1.TypeMeta{
Kind: "Deployment",
APIVersion: "apps/v1",
}
err = serializer.Encode(dep, writer)
var buf []byte
buf, err = json.Marshal(deployment)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

if err := writer.Flush(); err != nil {
return err
}

kc := kubesec.NewClient(scanURL, scanTimeOut)

rs, err := kc.ScanDefinition(buffer)

result, err := kubesec.NewClient().ScanDefinition(buf)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

if err := rs.Dump(os.Stdout); err != nil {
fmt.Println(err)
os.Exit(1)
}
kubesec.DumpReport(result, os.Stdout)

return nil
},
23 changes: 6 additions & 17 deletions pkg/cmd/pod.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package cmd

import (
"bufio"
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"os"
@@ -16,6 +15,7 @@ import (

var podCmd = &cobra.Command{
Use: `pod [name]`,
Aliases: []string{"po"},
Short: "Scans pod object",
Example: ` pod podinfo --namespace=default`,
RunE: func(cmd *cobra.Command, args []string) error {
@@ -24,9 +24,6 @@ var podCmd = &cobra.Command{
}
name := args[0]

var buffer bytes.Buffer
writer := bufio.NewWriter(&buffer)

fmt.Println("scanning pod", name, "in namespace", namespace)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
@@ -39,28 +36,20 @@ var podCmd = &cobra.Command{
Kind: "Pod",
APIVersion: "v1",
}
err = serializer.Encode(pod, writer)
var buf []byte
buf, err = json.Marshal(pod)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

if err := writer.Flush(); err != nil {
return err
}

kc := kubesec.NewClient(scanURL, scanTimeOut)
rs, err := kc.ScanDefinition(buffer)

result, err := kubesec.NewClient().ScanDefinition(buf)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

if err := rs.Dump(os.Stdout); err != nil {
fmt.Println(err)
os.Exit(1)
}
kubesec.DumpReport(result, os.Stdout)

return nil
},
4 changes: 2 additions & 2 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
@@ -13,9 +13,9 @@ import (

var RootCmd = &cobra.Command{
Use: "scan",
Short: "kubesec.io kubectl plugin",
Short: "kubesec kubectl plugin",
Long: `
kubesec.io command line utilities`,
kubesec command line utilities`,
}

var (
27 changes: 8 additions & 19 deletions pkg/cmd/statefulset.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package cmd

import (
"bufio"
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"os"
@@ -16,6 +15,7 @@ import (

var statefulsetCmd = &cobra.Command{
Use: `statefulset [name]`,
Aliases: []string{"sts"},
Short: "Scans statefulset object",
Example: ` statefulset podinfo --namespace=default`,
RunE: func(cmd *cobra.Command, args []string) error {
@@ -24,43 +24,32 @@ var statefulsetCmd = &cobra.Command{
}
name := args[0]

var buffer bytes.Buffer
writer := bufio.NewWriter(&buffer)

fmt.Println("scanning statefulset", name, "in namespace", namespace)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
ss, err := kubeClient.AppsV1().StatefulSets(namespace).Get(ctx, name, metav1.GetOptions{})
statefulSet, err := kubeClient.AppsV1().StatefulSets(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
ss.TypeMeta = metav1.TypeMeta{
statefulSet.TypeMeta = metav1.TypeMeta{
Kind: "StatefulSet",
APIVersion: "apps/v1",
}
err = serializer.Encode(ss, writer)
var buf []byte
buf, err = json.Marshal(statefulSet)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

if err := writer.Flush(); err != nil {
return err
}

kc := kubesec.NewClient(scanURL, scanTimeOut)
rs, err := kc.ScanDefinition(buffer)

result, err := kubesec.NewClient().ScanDefinition(buf)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

if err := rs.Dump(os.Stdout); err != nil {
fmt.Println(err)
os.Exit(1)
}
kubesec.DumpReport(result, os.Stdout)

return nil
},
Loading