Skip to content

Commit d9ff5bb

Browse files
author
Nirav Patel
authored
Default service account check: skip when AutomountServiceAccountToken=false (#166)
1 parent 24d1fe3 commit d9ff5bb

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

pkg/templates/serviceaccount/template.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ import (
1515
"golang.stackrox.io/kube-linter/pkg/templates/serviceaccount/internal/params"
1616
)
1717

18+
const (
19+
templateKey = "service-account"
20+
)
21+
1822
func init() {
1923
templates.Register(check.Template{
2024
HumanName: "Service Account",
21-
Key: "service-account",
25+
Key: templateKey,
2226
Description: "Flag containers which use a matching service account",
2327
SupportedObjectKinds: config.ObjectKindsDesc{
2428
ObjectKinds: []string{objectkinds.DeploymentLike},
@@ -35,6 +39,9 @@ func init() {
3539
if !found {
3640
return nil
3741
}
42+
if podSpec.AutomountServiceAccountToken != nil && !*podSpec.AutomountServiceAccountToken {
43+
return nil
44+
}
3845
sa := stringutils.OrDefault(podSpec.ServiceAccountName, podSpec.DeprecatedServiceAccount)
3946
if saMatcher(sa) {
4047
return []diagnostic.Diagnostic{{Message: fmt.Sprintf("found matching serviceAccount (%q)", sa)}}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package serviceaccount
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/suite"
7+
"golang.stackrox.io/kube-linter/internal/pointers"
8+
"golang.stackrox.io/kube-linter/pkg/diagnostic"
9+
"golang.stackrox.io/kube-linter/pkg/lintcontext/mocks"
10+
"golang.stackrox.io/kube-linter/pkg/templates"
11+
"golang.stackrox.io/kube-linter/pkg/templates/serviceaccount/internal/params"
12+
appsV1 "k8s.io/api/apps/v1"
13+
)
14+
15+
func TestServiceAccount(t *testing.T) {
16+
suite.Run(t, new(ServiceAccountTestSuite))
17+
}
18+
19+
type ServiceAccountTestSuite struct {
20+
templates.TemplateTestSuite
21+
22+
ctx *mocks.MockLintContext
23+
}
24+
25+
func (s *ServiceAccountTestSuite) SetupTest() {
26+
s.Init(templateKey)
27+
s.ctx = mocks.NewMockContext()
28+
}
29+
30+
func (s *ServiceAccountTestSuite) addDeploymentWithServiceAccount(name, serviceAccountName string, automountServiceAccountToken *bool) {
31+
s.ctx.AddMockDeployment(s.T(), name)
32+
s.ctx.ModifyDeployment(s.T(), name, func(deployment *appsV1.Deployment) {
33+
deployment.Spec.Template.Spec.ServiceAccountName = serviceAccountName
34+
deployment.Spec.Template.Spec.AutomountServiceAccountToken = automountServiceAccountToken
35+
})
36+
}
37+
38+
func (s *ServiceAccountTestSuite) TestServiceAccountName() {
39+
const (
40+
matchingSAWithAutoMountTokenNil = "match-sa-token-mount-nil"
41+
matchingSAWithAutoMountTokenTrue = "match-sa-token-mount-true"
42+
matchingSAWithAutoMountTokenFalse = "match-sa-token-mount-false"
43+
nonMatchingSAWithAutoMountTokenNil = "non-match-sa-token-mount-nil"
44+
nonMatchingSAWithAutoMountTokenTrue = "non-match-sa-token-mount-true"
45+
nonMatchingSAWithAutoMountTokenFalse = "non-match-sa-token-mount-false"
46+
)
47+
48+
s.addDeploymentWithServiceAccount(matchingSAWithAutoMountTokenNil, "non-default", nil)
49+
s.addDeploymentWithServiceAccount(matchingSAWithAutoMountTokenTrue, "non-default", pointers.Bool(true))
50+
s.addDeploymentWithServiceAccount(matchingSAWithAutoMountTokenFalse, "non-default", pointers.Bool(false))
51+
s.addDeploymentWithServiceAccount(nonMatchingSAWithAutoMountTokenNil, "", nil)
52+
s.addDeploymentWithServiceAccount(nonMatchingSAWithAutoMountTokenTrue, "", pointers.Bool(true))
53+
s.addDeploymentWithServiceAccount(nonMatchingSAWithAutoMountTokenFalse, "", pointers.Bool(false))
54+
55+
s.Validate(s.ctx, []templates.TestCase{
56+
{
57+
Param: params.Params{
58+
ServiceAccount: "non-default",
59+
},
60+
Diagnostics: map[string][]diagnostic.Diagnostic{
61+
matchingSAWithAutoMountTokenNil: {
62+
{Message: "found matching serviceAccount (\"non-default\")"},
63+
},
64+
matchingSAWithAutoMountTokenTrue: {
65+
{Message: "found matching serviceAccount (\"non-default\")"},
66+
},
67+
matchingSAWithAutoMountTokenFalse: {},
68+
nonMatchingSAWithAutoMountTokenNil: {},
69+
nonMatchingSAWithAutoMountTokenTrue: {},
70+
nonMatchingSAWithAutoMountTokenFalse: {},
71+
},
72+
ExpectInstantiationError: false,
73+
},
74+
{
75+
Param: params.Params{
76+
ServiceAccount: "[a)", // Wrong Regex which should raise an error
77+
},
78+
ExpectInstantiationError: true,
79+
},
80+
})
81+
}

0 commit comments

Comments
 (0)