-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OpenShift SecurityContextConstraints object linting (#650)
Co-authored-by: Cereberus <[email protected]>
- Loading branch information
1 parent
238ec49
commit 41071a5
Showing
14 changed files
with
400 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
10 changes: 10 additions & 0 deletions
10
pkg/builtinchecks/yamls/scc-deny-privileged-container.yaml
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,10 @@ | ||
name: "scc-deny-privileged-container" | ||
description: "Indicates when allowPrivilegedContainer SecurityContextConstraints set to true" | ||
remediation: >- | ||
SecurityContextConstraints has AllowPrivilegedContainer set to "true". Using this option is dangerous, please consider using allowedCapabilities instead. Refer to https://docs.openshift.com/container-platform/4.12/authentication/managing-security-context-constraints.html#scc-settings_configuring-internal-oauth for details. | ||
scope: | ||
objectKinds: | ||
- SecurityContextConstraints | ||
template: "scc-deny-privileged-container" | ||
params: | ||
AllowPrivilegedContainer: true |
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,14 @@ | ||
package extract | ||
|
||
import ( | ||
ocpSecV1 "github.com/openshift/api/security/v1" | ||
"golang.stackrox.io/kube-linter/pkg/k8sutil" | ||
) | ||
|
||
// SCCallowPrivilegedContainer extracts allowPrivilegedContainer from the given object, if available. | ||
func SCCallowPrivilegedContainer(obj k8sutil.Object) (bool, bool) { | ||
if scc, ok := obj.(*ocpSecV1.SecurityContextConstraints); ok { | ||
return scc.AllowPrivilegedContainer, true | ||
} | ||
return false, false | ||
} |
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,23 @@ | ||
package mocks | ||
|
||
import ( | ||
"testing" | ||
|
||
ocpSecV1 "github.com/openshift/api/security/v1" | ||
"github.com/stretchr/testify/require" | ||
"golang.stackrox.io/kube-linter/pkg/objectkinds" | ||
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// AddMockSecurityContextConstraints adds a mock SecurityContextConstraints to LintContext | ||
func (l *MockLintContext) AddMockSecurityContextConstraints(t *testing.T, name string, allowFlag bool) { | ||
require.NotEmpty(t, name) | ||
l.objects[name] = &ocpSecV1.SecurityContextConstraints{ | ||
TypeMeta: metaV1.TypeMeta{ | ||
Kind: objectkinds.SecurityContextConstraints, | ||
APIVersion: objectkinds.GetSCCAPIVersion(), | ||
}, | ||
ObjectMeta: metaV1.ObjectMeta{Name: name}, | ||
AllowPrivilegedContainer: allowFlag, | ||
} | ||
} |
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,26 @@ | ||
package objectkinds | ||
|
||
import ( | ||
ocpSecV1 "github.com/openshift/api/security/v1" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
) | ||
|
||
const ( | ||
// Service represents Kubernetes Service objects. | ||
SecurityContextConstraints = "SecurityContextConstraints" | ||
) | ||
|
||
var ( | ||
sccGVK = ocpSecV1.SchemeGroupVersion.WithKind("SecurityContextConstraints") | ||
) | ||
|
||
func init() { | ||
RegisterObjectKind(SecurityContextConstraints, MatcherFunc(func(gvk schema.GroupVersionKind) bool { | ||
return gvk == sccGVK | ||
})) | ||
} | ||
|
||
// GetSCCAPIVersion returns SCC's apiversion | ||
func GetSCCAPIVersion() string { | ||
return sccGVK.GroupVersion().String() | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,8 @@ | ||
package params | ||
|
||
// Params represents the params accepted by this template. | ||
type Params struct { | ||
|
||
// allowPrivilegedContainer value | ||
AllowPrivilegedContainer bool | ||
} |
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,42 @@ | ||
package sccdenypriv | ||
|
||
import ( | ||
"fmt" | ||
|
||
"golang.stackrox.io/kube-linter/pkg/check" | ||
"golang.stackrox.io/kube-linter/pkg/config" | ||
"golang.stackrox.io/kube-linter/pkg/diagnostic" | ||
"golang.stackrox.io/kube-linter/pkg/extract" | ||
"golang.stackrox.io/kube-linter/pkg/lintcontext" | ||
"golang.stackrox.io/kube-linter/pkg/objectkinds" | ||
"golang.stackrox.io/kube-linter/pkg/templates" | ||
"golang.stackrox.io/kube-linter/pkg/templates/sccdenypriv/internal/params" | ||
) | ||
|
||
const ( | ||
templateKey = "scc-deny-privileged-container" | ||
) | ||
|
||
func init() { | ||
templates.Register(check.Template{ | ||
HumanName: "SecurityContextConstraints allowPrivilegedContainer", | ||
Key: templateKey, | ||
Description: "Flag SCC with allowPrivilegedContainer set to true", | ||
SupportedObjectKinds: config.ObjectKindsDesc{ | ||
ObjectKinds: []string{objectkinds.SecurityContextConstraints}, | ||
}, | ||
Parameters: params.ParamDescs, | ||
ParseAndValidateParams: params.ParseAndValidate, | ||
Instantiate: params.WrapInstantiateFunc(func(p params.Params) (check.Func, error) { | ||
return func(_ lintcontext.LintContext, object lintcontext.Object) []diagnostic.Diagnostic { | ||
state, found := extract.SCCallowPrivilegedContainer(object.K8sObject) | ||
if found && state == p.AllowPrivilegedContainer { | ||
return []diagnostic.Diagnostic{ | ||
{Message: fmt.Sprintf("SCC has allowPrivilegedContainer set to %v", state)}, | ||
} | ||
} | ||
return nil | ||
}, 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,70 @@ | ||
package sccdenypriv | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/suite" | ||
"golang.stackrox.io/kube-linter/pkg/diagnostic" | ||
"golang.stackrox.io/kube-linter/pkg/lintcontext/mocks" | ||
"golang.stackrox.io/kube-linter/pkg/templates" | ||
"golang.stackrox.io/kube-linter/pkg/templates/sccdenypriv/internal/params" | ||
) | ||
|
||
func TestSCCPriv(t *testing.T) { | ||
suite.Run(t, new(SCCPrivTestSuite)) | ||
} | ||
|
||
type SCCPrivTestSuite struct { | ||
templates.TemplateTestSuite | ||
|
||
ctx *mocks.MockLintContext | ||
} | ||
|
||
func (s *SCCPrivTestSuite) SetupTest() { | ||
s.Init(templateKey) | ||
s.ctx = mocks.NewMockContext() | ||
} | ||
|
||
func (s *SCCPrivTestSuite) addSCCWithPriv(name string, allowFlag bool) { | ||
s.ctx.AddMockSecurityContextConstraints(s.T(), name, allowFlag) | ||
} | ||
|
||
func (s *SCCPrivTestSuite) TestPrivFalse() { | ||
const acceptableScc = "scc-priv-false" | ||
|
||
s.addSCCWithPriv(acceptableScc, false) | ||
|
||
s.Validate(s.ctx, []templates.TestCase{ | ||
{ | ||
Param: params.Params{ | ||
AllowPrivilegedContainer: true, | ||
}, | ||
Diagnostics: map[string][]diagnostic.Diagnostic{ | ||
acceptableScc: nil, | ||
}, | ||
ExpectInstantiationError: false, | ||
}, | ||
}) | ||
} | ||
|
||
func (s *SCCPrivTestSuite) TestPrivTrue() { | ||
const ( | ||
unacceptableScc = "scc-priv-true" | ||
) | ||
|
||
s.addSCCWithPriv(unacceptableScc, true) | ||
|
||
s.Validate(s.ctx, []templates.TestCase{ | ||
{ | ||
Param: params.Params{ | ||
AllowPrivilegedContainer: true, | ||
}, | ||
Diagnostics: map[string][]diagnostic.Diagnostic{ | ||
unacceptableScc: { | ||
{Message: "SCC has allowPrivilegedContainer set to true"}, | ||
}, | ||
}, | ||
ExpectInstantiationError: false, | ||
}, | ||
}) | ||
} |
Oops, something went wrong.