-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a couple more check templates and built-in checks (#6)
- Loading branch information
1 parent
f5db165
commit 3dd7446
Showing
10 changed files
with
156 additions
and
1 deletion.
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,6 @@ | ||
name: "no-read-only-root-fs" | ||
description: "Alert on containers not running with a read-only root filesystem" | ||
scope: | ||
objectKinds: | ||
- DeploymentLike | ||
template: "read-only-root-fs" |
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,6 @@ | ||
name: "run-as-non-root" | ||
description: "Alert on containers not set to runAsNonRoot" | ||
scope: | ||
objectKinds: | ||
- DeploymentLike | ||
template: "run-as-non-root" |
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,22 @@ | ||
package defaultchecks | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"golang.stackrox.io/kube-linter/internal/builtinchecks" | ||
"golang.stackrox.io/kube-linter/internal/set" | ||
) | ||
|
||
func TestListReferencesOnlyValidChecks(t *testing.T) { | ||
allChecks, err := builtinchecks.List() | ||
require.NoError(t, err) | ||
allCheckNames := set.NewStringSet() | ||
for _, check := range allChecks { | ||
allCheckNames.Add(check.Name) | ||
} | ||
for _, defaultCheck := range List.AsSlice() { | ||
assert.True(t, allCheckNames.Contains(defaultCheck), "default check %s invalid", defaultCheck) | ||
} | ||
} |
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,39 @@ | ||
package readonlyrootfs | ||
|
||
import ( | ||
"fmt" | ||
|
||
"golang.stackrox.io/kube-linter/internal/check" | ||
"golang.stackrox.io/kube-linter/internal/diagnostic" | ||
"golang.stackrox.io/kube-linter/internal/extract" | ||
"golang.stackrox.io/kube-linter/internal/lintcontext" | ||
"golang.stackrox.io/kube-linter/internal/objectkinds" | ||
"golang.stackrox.io/kube-linter/internal/templates" | ||
) | ||
|
||
func init() { | ||
templates.Register(check.Template{ | ||
Name: "read-only-root-fs", | ||
Description: "Flag containers without read-only root file systems", | ||
SupportedObjectKinds: check.ObjectKindsDesc{ | ||
ObjectKinds: []string{objectkinds.DeploymentLike}, | ||
}, | ||
Parameters: nil, | ||
Instantiate: func(_ map[string]string) (check.Func, error) { | ||
return func(_ *lintcontext.LintContext, object lintcontext.Object) []diagnostic.Diagnostic { | ||
podSpec, found := extract.PodSpec(object.K8sObject) | ||
if !found { | ||
return nil | ||
} | ||
var results []diagnostic.Diagnostic | ||
for _, container := range podSpec.Containers { | ||
sc := container.SecurityContext | ||
if sc == nil || sc.ReadOnlyRootFilesystem == nil || !*sc.ReadOnlyRootFilesystem { | ||
results = append(results, diagnostic.Diagnostic{Message: fmt.Sprintf("container %q does not have a read-only root file system", container.Name)}) | ||
} | ||
} | ||
return results | ||
}, nil | ||
}, | ||
}) | ||
} |
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,72 @@ | ||
package runasnonroot | ||
|
||
import ( | ||
"fmt" | ||
|
||
"golang.stackrox.io/kube-linter/internal/check" | ||
"golang.stackrox.io/kube-linter/internal/diagnostic" | ||
"golang.stackrox.io/kube-linter/internal/extract" | ||
"golang.stackrox.io/kube-linter/internal/lintcontext" | ||
"golang.stackrox.io/kube-linter/internal/objectkinds" | ||
"golang.stackrox.io/kube-linter/internal/templates" | ||
v1 "k8s.io/api/core/v1" | ||
) | ||
|
||
func effectiveRunAsNonRoot(podSC *v1.PodSecurityContext, containerSC *v1.SecurityContext) bool { | ||
if containerSC != nil && containerSC.RunAsNonRoot != nil { | ||
return *containerSC.RunAsNonRoot | ||
} | ||
if podSC != nil && podSC.RunAsNonRoot != nil { | ||
return *podSC.RunAsNonRoot | ||
} | ||
return false | ||
} | ||
|
||
func effectiveRunAsUser(podSC *v1.PodSecurityContext, containerSC *v1.SecurityContext) *int64 { | ||
if containerSC != nil && containerSC.RunAsUser != nil { | ||
return containerSC.RunAsUser | ||
} | ||
if podSC != nil { | ||
return podSC.RunAsUser | ||
} | ||
return nil | ||
} | ||
|
||
func init() { | ||
templates.Register(check.Template{ | ||
Name: "run-as-non-root", | ||
Description: "Flag containers set to run as a root user", | ||
SupportedObjectKinds: check.ObjectKindsDesc{ | ||
ObjectKinds: []string{objectkinds.DeploymentLike}, | ||
}, | ||
Parameters: nil, | ||
Instantiate: func(_ map[string]string) (check.Func, error) { | ||
return func(_ *lintcontext.LintContext, object lintcontext.Object) []diagnostic.Diagnostic { | ||
podSpec, found := extract.PodSpec(object.K8sObject) | ||
if !found { | ||
return nil | ||
} | ||
var results []diagnostic.Diagnostic | ||
for _, container := range podSpec.Containers { | ||
runAsUser := effectiveRunAsUser(podSpec.SecurityContext, container.SecurityContext) | ||
// runAsUser explicitly set to non-root. All good. | ||
if runAsUser != nil && *runAsUser > 0 { | ||
continue | ||
} | ||
runAsNonRoot := effectiveRunAsNonRoot(podSpec.SecurityContext, container.SecurityContext) | ||
if runAsNonRoot { | ||
// runAsNonRoot set, but runAsUser set to 0. This will result in a runtime failure. | ||
if runAsUser != nil && *runAsUser == 0 { | ||
results = append(results, diagnostic.Diagnostic{ | ||
Message: fmt.Sprintf("container %q is set to runAsNonRoot, but runAsUser set to %d", container.Name, *runAsUser), | ||
}) | ||
} | ||
continue | ||
} | ||
results = append(results, diagnostic.Diagnostic{Message: fmt.Sprintf("container %q is not set to runAsNonRoot", container.Name)}) | ||
} | ||
return results | ||
}, nil | ||
}, | ||
}) | ||
} |