Skip to content

Commit d9c63e6

Browse files
committed
modify test and create sample psp grant yaml
Signed-off-by: kaizhe <[email protected]>
1 parent 7c12817 commit d9c63e6

File tree

4 files changed

+172
-7
lines changed

4 files changed

+172
-7
lines changed

advisor/types/pspgrant.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package types
22

33
import (
44
"fmt"
5+
"strings"
56

67
"k8s.io/apimachinery/pkg/apis/meta/v1"
78

@@ -75,20 +76,29 @@ func (s *SASecuritySpec) GenerateComment() string {
7576
decision := "will be"
7677

7778
if s.IsDefaultServiceAccount() {
78-
decision = "will not be"
79+
decision = "will NOT be"
7980
}
8081

81-
return fmt.Sprintf("# Pod security policies %s created for service account: %s in namespace %s for images: %s", decision, s.ServiceAccount, s.Namespace, s.GetImages())
82+
commentsForWorkloads := []string{}
83+
comment := fmt.Sprintf("# Pod security policies %s created for service account '%s' in namespace '%s' with following workdloads:\n", decision, s.ServiceAccount, s.Namespace)
84+
for _, wlImg := range s.GetWorkloadImages() {
85+
commentsForWorkloads = append(commentsForWorkloads, fmt.Sprintf("#\t%s", wlImg))
86+
}
87+
88+
comment += strings.Join(commentsForWorkloads, "\n")
89+
return comment
8290
}
8391

84-
func (s *SASecuritySpec) GetImages() []string {
85-
imageList := []string{}
92+
// GetWorkloadImages returns a list of workload images in the format of "kind, Name, Image Name"
93+
func (s *SASecuritySpec) GetWorkloadImages() []string {
94+
workLoadImageList := []string{}
8695

8796
for _, css := range s.ContainerSecuritySpecList {
88-
imageList = append(imageList, css.ImageName)
97+
workLoadImage := fmt.Sprintf("Kind: %s, Name: %s, Image: %s", css.Metadata.Kind, css.Metadata.Name, css.ImageName)
98+
workLoadImageList = append(workLoadImageList, workLoadImage)
8999
}
90100

91-
return imageList
101+
return workLoadImageList
92102
}
93103

94104
func (s *SASecuritySpec) GenerateRole() *v1rbac.Role {

scripts/test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ kubectl apply -f test-yaml/base-busybox.yaml
77

88
sleep 5
99

10-
./kube-psp-advisor inpsect
10+
./kube-psp-advisor inspect

test-yaml/base-busybox.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@ metadata:
44
name: psp-test
55
---
66
apiVersion: v1
7+
kind: ServiceAccount
8+
metadata:
9+
name: sa-1
10+
namespace: psp-test
11+
---
12+
apiVersion: v1
13+
kind: ServiceAccount
14+
metadata:
15+
name: sa-2
16+
namespace: psp-test
17+
---
18+
apiVersion: v1
19+
kind: ServiceAccount
20+
metadata:
21+
name: sa-3
22+
namespace: psp-test
23+
---
24+
apiVersion: v1
725
kind: Pod
826
metadata:
927
name: busy-pod
@@ -27,6 +45,7 @@ spec:
2745
hostPID: false
2846
hostIPC: false
2947
hostNetwork: true
48+
serviceAccount: sa-1
3049
volumes:
3150
- name: test-volume
3251
hostPath:
@@ -43,6 +62,7 @@ metadata:
4362
spec:
4463
template:
4564
spec:
65+
serviceAccount: sa-2
4666
restartPolicy: Never
4767
containers:
4868
- name: my-busybox
@@ -88,6 +108,7 @@ spec:
88108
labels:
89109
app: busy-deploy
90110
spec:
111+
serviceAccount: sa-2
91112
containers:
92113
- name: my-busybox
93114
image: busybox
@@ -179,6 +200,7 @@ spec:
179200
labels:
180201
app: busy-ds
181202
spec:
203+
serviceAccount: sa-2
182204
containers:
183205
- name: my-busybox
184206
image: busybox
@@ -245,6 +267,7 @@ spec:
245267
hostPID: true
246268
hostIPC: true
247269
hostNetwork: true
270+
serviceAccount: sa-1
248271
volumes:
249272
- name: config-vol
250273
configMap:

test-yaml/psp-grant.yaml

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# Pod security policies will NOT be created for service account 'default' in namespace 'psp-test' with following workdloads:
2+
# Kind: ReplicationController, Name: busy-rc, Image: busybox
3+
---
4+
# Pod security policies will be created for service account 'sa-1' in namespace 'psp-test' with following workdloads:
5+
# Kind: ReplicaSet, Name: busy-rs, Image: busybox
6+
# Kind: Pod, Name: busy-pod, Image: busybox
7+
apiVersion: policy/v1beta1
8+
kind: PodSecurityPolicy
9+
metadata:
10+
creationTimestamp: null
11+
name: psp-for-psp-test-sa-1
12+
spec:
13+
allowedCapabilities:
14+
- SYS_ADMIN
15+
allowedHostPaths:
16+
- pathPrefix: /usr/bin
17+
readOnly: true
18+
fsGroup:
19+
rule: RunAsAny
20+
hostIPC: true
21+
hostNetwork: true
22+
hostPID: true
23+
runAsUser:
24+
rule: RunAsAny
25+
seLinux:
26+
rule: RunAsAny
27+
supplementalGroups:
28+
rule: RunAsAny
29+
volumes:
30+
- configMap
31+
- secret
32+
- hostPath
33+
---
34+
apiVersion: rbac.authorization.k8s.io/v1
35+
kind: Role
36+
metadata:
37+
creationTimestamp: null
38+
name: use-psp-by-psp-test:sa-1
39+
namespace: psp-test
40+
rules:
41+
- apiGroups:
42+
- policy
43+
resourceNames:
44+
- psp-for-psp-test-sa-1
45+
resources:
46+
- podsecuritypolicies
47+
verbs:
48+
- use
49+
---
50+
apiVersion: rbac.authorization.k8s.io/v1
51+
kind: RoleBinding
52+
metadata:
53+
creationTimestamp: null
54+
name: use-psp-by-psp-test:sa-1-binding
55+
namespace: psp-test
56+
roleRef:
57+
apiGroup: rbac.authorization.k8s.io
58+
kind: Role
59+
name: use-psp-by-psp-test:sa-1
60+
subjects:
61+
- kind: ServiceAccount
62+
name: sa-1
63+
namespace: psp-test
64+
---
65+
# Pod security policies will be created for service account 'sa-2' in namespace 'psp-test' with following workdloads:
66+
# Kind: DaemonSet, Name: busy-ds, Image: busybox
67+
# Kind: Deployment, Name: busy-deploy, Image: busybox
68+
# Kind: Job, Name: busy-job, Image: busybox
69+
apiVersion: policy/v1beta1
70+
kind: PodSecurityPolicy
71+
metadata:
72+
creationTimestamp: null
73+
name: psp-for-psp-test-sa-2
74+
spec:
75+
allowedCapabilities:
76+
- SYS_ADMIN
77+
- NET_ADMIN
78+
allowedHostPaths:
79+
- pathPrefix: /bin
80+
readOnly: true
81+
- pathPrefix: /tmp
82+
readOnly: true
83+
- pathPrefix: /usr/bin
84+
readOnly: true
85+
fsGroup:
86+
rule: RunAsAny
87+
hostIPC: true
88+
hostPID: true
89+
runAsUser:
90+
ranges:
91+
- max: 10001
92+
min: 10001
93+
rule: MustRunAs
94+
seLinux:
95+
rule: RunAsAny
96+
supplementalGroups:
97+
rule: RunAsAny
98+
volumes:
99+
- hostPath
100+
- secret
101+
---
102+
apiVersion: rbac.authorization.k8s.io/v1
103+
kind: Role
104+
metadata:
105+
creationTimestamp: null
106+
name: use-psp-by-psp-test:sa-2
107+
namespace: psp-test
108+
rules:
109+
- apiGroups:
110+
- policy
111+
resourceNames:
112+
- psp-for-psp-test-sa-2
113+
resources:
114+
- podsecuritypolicies
115+
verbs:
116+
- use
117+
---
118+
apiVersion: rbac.authorization.k8s.io/v1
119+
kind: RoleBinding
120+
metadata:
121+
creationTimestamp: null
122+
name: use-psp-by-psp-test:sa-2-binding
123+
namespace: psp-test
124+
roleRef:
125+
apiGroup: rbac.authorization.k8s.io
126+
kind: Role
127+
name: use-psp-by-psp-test:sa-2
128+
subjects:
129+
- kind: ServiceAccount
130+
name: sa-2
131+
namespace: psp-test
132+
---

0 commit comments

Comments
 (0)