Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion cmd/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ var httpCmd = &cobra.Command{
stopCh := server.SetupSignalHandler()
jsonLogger, err := NewLogger("info", "json")
if err != nil {
return fmt.Errorf("Unable to create new logger: %w", err)
return fmt.Errorf("unable to create new logger: %w", err)
}

ver := os.Getenv("K8S_SCHEMA_VER")
Expand Down
13 changes: 11 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package cmd

import (
"errors"
"fmt"
"log"
"os"
"strings"
"syscall"

"github.com/spf13/cobra"
"go.uber.org/zap"
Expand Down Expand Up @@ -36,8 +38,15 @@ func Execute() {
if err != nil {
log.Fatalf("can't initialize zap logger: %v", err)
}

defer logger.Sync()
defer func() {
if err := logger.Sync(); err != nil {
// exclude invalid argument error
// https://github.com/uber-go/zap/issues/328
if !errors.Is(err, syscall.EINVAL) {
log.Fatalf("failed to cleanup logger: %+v", err)
}
}
}()

rootCmd.SetArgs(os.Args[1:])
if err := rootCmd.Execute(); err != nil {
Expand Down
10 changes: 8 additions & 2 deletions cmd/print-rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,20 @@ func init() {

printTableFn := func(w io.Writer) error {
tw := util.NewTabWriter(w)
fmt.Fprintf(tw, "ID\tReason\tPoints\tKinds\n")
_, err := fmt.Fprintf(tw, "ID\tReason\tPoints\tKinds\n")
if err != nil {
logger.Fatalf("could not write to tabwriter: %v", err)
}
for _, rule := range ruleSet.Rules {
fmt.Fprintf(tw, "%s\t%s\t%d\t%s\t\n",
_, err = fmt.Fprintf(tw, "%s\t%s\t%d\t%s\t\n",
rule.ID,
rule.Reason,
rule.Points,
strings.Join(rule.Kinds, ","),
)
if err != nil {
logger.Fatalf("could not write to tabwriter: %v", err)
}
}
return tw.Flush()
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/report/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func WriteReports(format string, output io.Writer, reports reports, outputTempla
return err
}
default:
return errors.New("Unrecognized format specified")
return errors.New("unrecognized format specified")
}

if err := writer.Write(reports); err != nil {
Expand Down
8 changes: 4 additions & 4 deletions pkg/rules/automountServiceAccountToken.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ package rules

import (
"bytes"

"github.com/thedevsaddam/gojsonq/v2"
)

func AutomountServiceAccountToken(json []byte) int {
spec := getSpecSelector(json)

res := gojsonq.New().Reader(bytes.NewReader(json)).
From(spec + ".automountServiceAccountToken").Get()

if res != nil {
if v, ok := res.(bool); ok && !v {
return 1
}
}

return 0
}
10 changes: 5 additions & 5 deletions pkg/rules/automountServiceAccountToken_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package rules

import (
"testing"

"github.com/ghodss/yaml"
)

Expand Down Expand Up @@ -67,7 +67,7 @@ spec:
want: 0,
},
}

for _, testCase := range testCases {
tc := testCase
t.Run(tc.name, func(t *testing.T) {
Expand Down Expand Up @@ -226,7 +226,7 @@ spec:
want: 0,
},
}

for _, testCase := range testCases {
tc := testCase
t.Run(tc.name, func(t *testing.T) {
Expand Down Expand Up @@ -354,7 +354,7 @@ spec:
want: 0,
},
}

for _, testCase := range testCases {
tc := testCase
t.Run(tc.name, func(t *testing.T) {
Expand Down Expand Up @@ -486,7 +486,7 @@ spec:
want: 0,
},
}

for _, testCase := range testCases {
tc := testCase
t.Run(tc.name, func(t *testing.T) {
Expand Down
12 changes: 8 additions & 4 deletions pkg/rules/dockerSock.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@ package rules
import (
"bytes"
"fmt"
"github.com/thedevsaddam/gojsonq/v2"
"strings"

"github.com/thedevsaddam/gojsonq/v2"
)

func DockerSock(json []byte) int {
spec := getSpecSelector(json)
found := 0

paths := gojsonq.New().Reader(bytes.NewReader(json)).
data := gojsonq.New().Reader(bytes.NewReader(json)).
From(spec + ".volumes").
Only("hostPath.path")

if paths != nil && strings.Contains(fmt.Sprintf("%v", paths), "/var/run/docker.sock") {
found++
paths, ok := data.([]interface{})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be cast to []string instead

if ok && paths != nil {
if strings.Contains(fmt.Sprintf("%v", paths), "/var/run/docker.sock") {
found++
}
}

return found
Expand Down
7 changes: 4 additions & 3 deletions pkg/rules/limitsCPU.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ func LimitsCPU(json []byte) int {
spec := getSpecSelector(json)
found := 0

paths := gojsonq.New().Reader(bytes.NewReader(json)).
data := gojsonq.New().Reader(bytes.NewReader(json)).
From(spec + ".containers").
Only("resources.limits.cpu")

if paths != nil {
found += len(paths.([]interface{}))
paths, ok := data.([]interface{})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to double check if this can be cast to []string instead

if ok && paths != nil {
found += len(paths)
}

return found
Expand Down
8 changes: 5 additions & 3 deletions pkg/rules/limitsMemory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ package rules

import (
"bytes"

"github.com/thedevsaddam/gojsonq/v2"
)

func LimitsMemory(json []byte) int {
spec := getSpecSelector(json)
found := 0

paths := gojsonq.New().Reader(bytes.NewReader(json)).
data := gojsonq.New().Reader(bytes.NewReader(json)).
From(spec + ".containers").
Only("resources.limits.memory")

if paths != nil {
found += len(paths.([]interface{}))
paths, ok := data.([]interface{})
if ok && paths != nil {
found += len(paths)
}

return found
Expand Down
9 changes: 6 additions & 3 deletions pkg/rules/procMount.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ func ProcMount(json []byte) int {
spec := getSpecSelector(json)
found := 0

paths := gojsonq.New().Reader(bytes.NewReader(json)).
data := gojsonq.New().Reader(bytes.NewReader(json)).
From(spec + ".volumes").
Only("hostPath.path")

if paths != nil && strings.Contains(fmt.Sprintf("%v", paths), "/proc") {
found++
paths, ok := data.([]interface{})
if ok && paths != nil {
if strings.Contains(fmt.Sprintf("%v", paths), "/proc") {
found++
}
}

return found
Expand Down
8 changes: 5 additions & 3 deletions pkg/rules/requestsCPU.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ package rules

import (
"bytes"

"github.com/thedevsaddam/gojsonq/v2"
)

func RequestsCPU(json []byte) int {
spec := getSpecSelector(json)
found := 0

paths := gojsonq.New().Reader(bytes.NewReader(json)).
data := gojsonq.New().Reader(bytes.NewReader(json)).
From(spec + ".containers").
Only("resources.requests.cpu")

if paths != nil {
found += len(paths.([]interface{}))
paths, ok := data.([]interface{})
if ok && paths != nil {
found += len(paths)
}

return found
Expand Down
8 changes: 5 additions & 3 deletions pkg/rules/requestsMemory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ package rules

import (
"bytes"

"github.com/thedevsaddam/gojsonq/v2"
)

func RequestsMemory(json []byte) int {
spec := getSpecSelector(json)
found := 0

paths := gojsonq.New().Reader(bytes.NewReader(json)).
data := gojsonq.New().Reader(bytes.NewReader(json)).
From(spec + ".containers").
Only("resources.requests.memory")

if paths != nil {
found += len(paths.([]interface{}))
paths, ok := data.([]interface{})
if ok && paths != nil {
found += len(paths)
}

return found
Expand Down
9 changes: 6 additions & 3 deletions pkg/rules/volumeClaimAccessModeReadWriteOnce.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ func VolumeClaimAccessModeReadWriteOnce(json []byte) int {

found := 0

paths := gojsonq.New().Reader(bytes.NewReader(json)).
data := gojsonq.New().Reader(bytes.NewReader(json)).
From("spec.volumeClaimTemplates").
Only("spec.accessModes")

if paths != nil && strings.Contains(fmt.Sprintf("%v", paths), "accessModes:[ReadWriteOnce]") {
found++
paths, ok := data.([]interface{})
if ok && paths != nil {
if strings.Contains(fmt.Sprintf("%v", paths), "accessModes:[ReadWriteOnce]") {
found++
}
}

return found
Expand Down
Loading