Skip to content

Commit

Permalink
Multiple style fixes for linters compliance
Browse files Browse the repository at this point in the history
  • Loading branch information
mtardy committed Sep 30, 2022
1 parent 65e6d75 commit 712ecdc
Show file tree
Hide file tree
Showing 30 changed files with 141 additions and 140 deletions.
8 changes: 6 additions & 2 deletions commands/dig.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ var sideEffects bool
// flag to force admission creation
var admForce bool

// output formats
const outputHuman = "human"
const outputJSON = "json"

// digCmd represents the dig command
var digCmd = &cobra.Command{
Use: "dig [buckets]",
Expand All @@ -48,7 +52,7 @@ arguments.`,
}

// apply default colored human only if the color flag was not set
if !cmd.Flags().Changed("color") && output == "human" {
if !cmd.Flags().Changed("color") && output == outputHuman {
color = true
}

Expand Down Expand Up @@ -107,7 +111,7 @@ arguments.`,
if err != nil {
// loading the context failed and is required so skip this
// execution after printing the error with the name
err := printError(fmt.Errorf("failed loading context to initialize client: %s", err.Error()), name)
err := printError(fmt.Errorf("failed loading context to initialize client: %w", err), name)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion commands/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ boolean flags to disabled security features. Examples:
pod := kgen.Generate(opts)

var p printers.ResourcePrinter
if output == "json" {
if output == outputJSON {
p = &printers.JSONPrinter{}
} else {
p = &printers.YAMLPrinter{}
Expand Down
16 changes: 8 additions & 8 deletions commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ cluster. For that you can use multiples buckets. Buckets are plugins that can
scan specific aspects of a cluster or bring expertise to automate the Kubernetes
pentest process.`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if output != "human" && output != "json" {
return fmt.Errorf("ouput flag must be one of human|json, got %q", output)
if output != outputHuman && output != outputJSON {
return fmt.Errorf("output flag must be one of %s|%s, got %q", outputHuman, outputJSON, output)
}
return nil
},
Expand All @@ -58,8 +58,8 @@ pentest process.`,
func init() {
cobra.OnInitialize(registerBuckets)

rootCmd.PersistentFlags().StringVarP(&output, "output", "o", "human", "Output format. One of: human|json.")
rootCmd.PersistentFlags().IntVarP(&outputWidth, "width", "w", 140, "Width for the human output")
rootCmd.PersistentFlags().StringVarP(&output, "output", "o", outputHuman, fmt.Sprintf("Output format. One of: %s|%s.", outputHuman, outputJSON))
rootCmd.PersistentFlags().IntVarP(&outputWidth, "width", "w", 140, fmt.Sprintf("Width for the %s output", outputHuman))
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand Down Expand Up @@ -99,9 +99,9 @@ func registerBuckets() {
// printResults prints results with the output format selected by the flags
func printResults(r bucket.Results, opts bucket.ResultsOpts) error {
switch output {
case "human":
case outputHuman:
fmt.Print(r.Human(opts))
case "json":
case outputJSON:
p, err := r.JSON(opts)
if err != nil {
return err
Expand All @@ -117,10 +117,10 @@ func printResults(r bucket.Results, opts bucket.ResultsOpts) error {
// struct that can contains the error directly?
func printError(err error, name string) error {
switch output {
case "human":
case outputHuman:
fmt.Printf("### %s ###\n", strings.ToUpper(name))
fmt.Printf("Error: %s\n", err.Error())
case "json":
case outputJSON:
jsonErr := struct {
Bucket string `json:"bucket"`
Error string `json:"error"`
Expand Down
4 changes: 3 additions & 1 deletion pkg/automaticontext/automaticontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import (
"strings"

"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"

// import all auth clients
_ "k8s.io/client-go/plugin/pkg/client/auth"
)

func Config(kubeconfigPath string) (*rest.Config, error) {
Expand Down
10 changes: 5 additions & 5 deletions pkg/bucket/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (

var ErrMissingClient = errors.New("buckets need a kubernetes client for initialization")

type ErrUnknownBucket struct {
type UnknownBucketError struct {
name string
}

func (e ErrUnknownBucket) Error() string {
func (e UnknownBucketError) Error() string {
return fmt.Sprintf("unknown bucket %q", e.name)
}

Expand Down Expand Up @@ -170,14 +170,14 @@ func (bs *Buckets) InitBucket(name string, config Config) (Interface, error) {
}

bucket, found, err := bs.getBucket(name, config)
if err == ErrMissingClient {
if errors.Is(err, ErrMissingClient) {
return nil, err
}
if err != nil {
return nil, fmt.Errorf("couldn't init bucket %q: %v", name, err)
return nil, fmt.Errorf("couldn't init bucket %q: %w", name, err)
}
if !found {
err := ErrUnknownBucket{
err := UnknownBucketError{
name: name,
}
return nil, err
Expand Down
6 changes: 2 additions & 4 deletions pkg/bucket/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// checkWidthsCoherence checks if headers and data are both sets, that the
// width is consistant between them.
// width is consistent between them.
func (r Results) checkWidthsCoherence() bool {
headerWidth := len(r.headers)

Expand All @@ -28,10 +28,8 @@ func (r Results) checkWidthsCoherence() bool {
// one is not set
if headerWidth == 0 || dataWidth == 0 {
return true
} else {
return headerWidth == dataWidth
}

return headerWidth == dataWidth
}

func nrColumnsToMaxWidth(termWidth int, n int) int {
Expand Down
32 changes: 15 additions & 17 deletions pkg/plugins/admission/admission.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ var bucketAliases = []string{"admissions", "adm"}

var currentNamespace string

// AdmissionBucket implements Bucket
type AdmissionBucket struct {
// Bucket implements Bucket
type Bucket struct {
client kubernetes.Interface

podFactoryChain []podFactory
Expand Down Expand Up @@ -53,7 +53,7 @@ func Register(b *bucket.Buckets) {
}

// Run runs the admission test.
func (a *AdmissionBucket) Run() (bucket.Results, error) {
func (a *Bucket) Run() (bucket.Results, error) {
res := bucket.NewResults(bucketName)
if !a.config.AdmForce && !a.CanIDelete() {
return *res, errors.New("cannot delete pod, will not be able to clean the scan artifacts, force creation --admission-force")
Expand All @@ -62,7 +62,7 @@ func (a *AdmissionBucket) Run() (bucket.Results, error) {
c := make(chan admissionResult, len(a.podFactoryChain))

for _, f := range a.podFactoryChain {
go func(a *AdmissionBucket, f podFactory, c chan admissionResult) {
go func(a *Bucket, f podFactory, c chan admissionResult) {
err := a.use(f)
if err != nil {
// if kerrors.IsForbidden(err) {
Expand Down Expand Up @@ -99,26 +99,24 @@ func (a *AdmissionBucket) Run() (bucket.Results, error) {
err := a.Cleanup()
if a.config.AdmForce {
return *res, nil
} else {
return *res, err
}
return *res, err
}

func (a *AdmissionBucket) use(f podFactory) error {
func (a *Bucket) use(f podFactory) error {
pod := f.NewPod()
pod, err := a.client.CoreV1().Pods(pod.Namespace).Create(context.TODO(), pod, metav1.CreateOptions{})
if err != nil {
return err
} else {
a.cleaningLock.Lock()
a.podsToClean = append(a.podsToClean, pod)
a.cleaningLock.Unlock()
}
a.cleaningLock.Lock()
a.podsToClean = append(a.podsToClean, pod)
a.cleaningLock.Unlock()
return nil
}

// initialize initiliazes the pod factory chain to use during the scan.
func (a *AdmissionBucket) initialize() {
func (a *Bucket) initialize() {
a.podFactoryChain = []podFactory{
privilegedPod{},
hostPathPod{},
Expand All @@ -129,14 +127,14 @@ func (a *AdmissionBucket) initialize() {
}
}

func (a AdmissionBucket) CanIDelete() bool {
func (a Bucket) CanIDelete() bool {
err := a.client.CoreV1().Pods(currentNamespace).Delete(context.TODO(), "delete-test", metav1.DeleteOptions{})
return !kerrors.IsForbidden(err)
}

// Cleanup deletes side effects pods that were successfuly created during the scan.
// Cleanup deletes side effects pods that were successfully created during the scan.
// TODO parallelize maybe?
func (a AdmissionBucket) Cleanup() error {
func (a Bucket) Cleanup() error {
for _, p := range a.podsToClean {
err := a.client.CoreV1().Pods(p.Namespace).Delete(context.TODO(), p.Name, metav1.DeleteOptions{})
if err != nil {
Expand All @@ -147,12 +145,12 @@ func (a AdmissionBucket) Cleanup() error {
}

// NewAdmissionBucket creates a new admission bucket with the kubernetes client.
func NewAdmissionBucket(cf bucket.Config) (*AdmissionBucket, error) {
func NewAdmissionBucket(cf bucket.Config) (*Bucket, error) {
if cf.Client == nil {
return nil, bucket.ErrMissingClient
}
currentNamespace = cf.Namespace
return &AdmissionBucket{
return &Bucket{
client: cf.Client,
cleaningLock: &sync.Mutex{},
config: cf,
Expand Down
8 changes: 4 additions & 4 deletions pkg/plugins/apiresources/apiresources.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ const (

var bucketAliases = []string{"api", "apiresource"}

type APIResourcesBucket struct {
type Bucket struct {
config bucket.Config
}

func (n APIResourcesBucket) Run() (bucket.Results, error) {
func (n Bucket) Run() (bucket.Results, error) {
// executes here the code of your plugin
res := bucket.NewResults(bucketName)

Expand Down Expand Up @@ -51,11 +51,11 @@ func Register(b *bucket.Buckets) {
})
}

func NewAPIResourcesBucket(config bucket.Config) (*APIResourcesBucket, error) {
func NewAPIResourcesBucket(config bucket.Config) (*Bucket, error) {
if config.Client == nil {
return nil, bucket.ErrMissingClient
}
return &APIResourcesBucket{
return &Bucket{
config: config,
}, nil
}
8 changes: 4 additions & 4 deletions pkg/plugins/authorization/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ const (

var bucketAliases = []string{"authorizations", "auth"}

type AuthorizationBucket struct {
type Bucket struct {
config bucket.Config
}

func (n AuthorizationBucket) Run() (bucket.Results, error) {
func (n Bucket) Run() (bucket.Results, error) {
res := bucket.NewResults(bucketName)

// create the self subject rules review object
Expand Down Expand Up @@ -80,11 +80,11 @@ func Register(b *bucket.Buckets) {
})
}

func NewAuthorizationBucket(c bucket.Config) (*AuthorizationBucket, error) {
func NewAuthorizationBucket(c bucket.Config) (*Bucket, error) {
if c.Client == nil {
return nil, bucket.ErrMissingClient
}
return &AuthorizationBucket{
return &Bucket{
config: c,
}, nil
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/plugins/capabilities/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ var dangerousCap = []capability.Cap{
capability.CAP_SETFCAP,
}

type CapabilitiesBucket struct{}
type Bucket struct{}

func (n CapabilitiesBucket) Run() (bucket.Results, error) {
func (n Bucket) Run() (bucket.Results, error) {
capabilities, err := getCapabilities(0)

if err != nil {
Expand Down Expand Up @@ -88,11 +88,11 @@ func Register(b *bucket.Buckets) {
})
}

func NewCapabilitiesBucket(config bucket.Config) (*CapabilitiesBucket, error) {
func NewCapabilitiesBucket(config bucket.Config) (*Bucket, error) {
if !config.Color {
text.DisableColors()
}
return &CapabilitiesBucket{}, nil
return &Bucket{}, nil
}

func isDangerousCap(cap capability.Cap) bool {
Expand Down
10 changes: 5 additions & 5 deletions pkg/plugins/cgroups/cgroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ const (

var bucketAliases = []string{"cgroup", "cg"}

type CgroupsBucket struct{}
type Bucket struct{}

type Cgroup struct {
HierarchyID string
ControllerList string
CgroupPath string
}

func (n CgroupsBucket) Run() (bucket.Results, error) {
func (n Bucket) Run() (bucket.Results, error) {
// executes here the code of your plugin
cgroups, err := readCgroupFile()
if err != nil {
Expand All @@ -34,7 +34,7 @@ func (n CgroupsBucket) Run() (bucket.Results, error) {
res := bucket.NewResults(bucketName)
if len(cgroups) <= 1 {
// https://stackoverflow.com/a/69005753
res.AddComment("This kernel might use cgroups v2, thus explaning the lack of information.")
res.AddComment("This kernel might use cgroups v2, thus explaining the lack of information.")
}
res.SetHeaders([]string{"hierarchyID", "controllerList", "cgroupPath"})
for _, cgroup := range cgroups {
Expand All @@ -56,8 +56,8 @@ func Register(b *bucket.Buckets) {
})
}

func NewCgroupsBucket(config bucket.Config) (*CgroupsBucket, error) {
return &CgroupsBucket{}, nil
func NewCgroupsBucket(config bucket.Config) (*Bucket, error) {
return &Bucket{}, nil
}

func readCgroupFile() ([]Cgroup, error) {
Expand Down
Loading

0 comments on commit 712ecdc

Please # to comment.