11package processor
22
33import (
4+ "fmt"
5+
46 "github.com/sysdiglabs/kube-psp-advisor/advisor/types"
57
68 "k8s.io/api/core/v1"
7- v12 "k8s.io/apimachinery/pkg/apis/meta/v1"
9+ v1meta "k8s.io/apimachinery/pkg/apis/meta/v1"
810)
911
1012const (
@@ -23,14 +25,15 @@ func (p *Processor) getSecuritySpecFromDaemonSets() ([]types.ContainerSecuritySp
2325 cspList := []types.ContainerSecuritySpec {}
2426 pspList := []types.PodSecuritySpec {}
2527
26- daemonSetList , err := clientset .AppsV1 ().DaemonSets (p .namespace ).List (v12 .ListOptions {})
28+ daemonSetList , err := clientset .AppsV1 ().DaemonSets (p .namespace ).List (v1meta .ListOptions {})
2729
2830 if err != nil {
2931 return cspList , pspList , err
3032 }
3133
3234 for _ , ds := range daemonSetList .Items {
33- sa := p .serviceAccountMap [ds .Spec .Template .Spec .ServiceAccountName ]
35+ sa := p .GetServiceAccount (ds .Namespace , ds .Spec .Template .Spec .ServiceAccountName )
36+
3437 cspList2 , podSecurityPosture := p .gen .GetSecuritySpecFromPodSpec (types.Metadata {
3538 Name : ds .Name ,
3639 Kind : DaemonSet ,
@@ -48,7 +51,7 @@ func (p *Processor) getSecuritySpecFromReplicaSets() ([]types.ContainerSecurityS
4851 cssList := []types.ContainerSecuritySpec {}
4952 pssList := []types.PodSecuritySpec {}
5053
51- replicaSetList , err := clientset .AppsV1 ().ReplicaSets (p .namespace ).List (v12 .ListOptions {})
54+ replicaSetList , err := clientset .AppsV1 ().ReplicaSets (p .namespace ).List (v1meta .ListOptions {})
5255
5356 if err != nil {
5457 return cssList , pssList , err
@@ -59,7 +62,7 @@ func (p *Processor) getSecuritySpecFromReplicaSets() ([]types.ContainerSecurityS
5962 continue
6063 }
6164
62- sa := p .serviceAccountMap [ rs .Spec .Template .Spec .ServiceAccountName ]
65+ sa := p .GetServiceAccount ( rs .Namespace , rs . Spec .Template .Spec .ServiceAccountName )
6366 cspList2 , psc := p .gen .GetSecuritySpecFromPodSpec (types.Metadata {
6467 Name : rs .Name ,
6568 Kind : ReplicaSet ,
@@ -77,14 +80,14 @@ func (p *Processor) getSecuritySpecFromStatefulSets() ([]types.ContainerSecurity
7780 cssList := []types.ContainerSecuritySpec {}
7881 pssList := []types.PodSecuritySpec {}
7982
80- statefulSetList , err := clientset .AppsV1 ().StatefulSets (p .namespace ).List (v12 .ListOptions {})
83+ statefulSetList , err := clientset .AppsV1 ().StatefulSets (p .namespace ).List (v1meta .ListOptions {})
8184
8285 if err != nil {
8386 return cssList , pssList , err
8487 }
8588
8689 for _ , sts := range statefulSetList .Items {
87- sa := p .serviceAccountMap [ sts .Spec .Template .Spec .ServiceAccountName ]
90+ sa := p .GetServiceAccount ( sts .Namespace , sts . Spec .Template .Spec .ServiceAccountName )
8891 cspList2 , pss := p .gen .GetSecuritySpecFromPodSpec (types.Metadata {
8992 Name : sts .Name ,
9093 Kind : StatefulSet ,
@@ -102,14 +105,14 @@ func (p *Processor) getSecuritySpecFromReplicationController() ([]types.Containe
102105 cssList := []types.ContainerSecuritySpec {}
103106 pssList := []types.PodSecuritySpec {}
104107
105- replicationControllerList , err := clientset .CoreV1 ().ReplicationControllers (p .namespace ).List (v12 .ListOptions {})
108+ replicationControllerList , err := clientset .CoreV1 ().ReplicationControllers (p .namespace ).List (v1meta .ListOptions {})
106109
107110 if err != nil {
108111 return cssList , pssList , err
109112 }
110113
111114 for _ , rc := range replicationControllerList .Items {
112- sa := p .serviceAccountMap [ rc .Spec .Template .Spec .ServiceAccountName ]
115+ sa := p .GetServiceAccount ( rc .Namespace , rc . Spec .Template .Spec .ServiceAccountName )
113116 cspList2 , pss := p .gen .GetSecuritySpecFromPodSpec (types.Metadata {
114117 Name : rc .Name ,
115118 Kind : ReplicationController ,
@@ -127,14 +130,14 @@ func (p *Processor) getSecuritySpecFromCronJobs() ([]types.ContainerSecuritySpec
127130 cssList := []types.ContainerSecuritySpec {}
128131 pssList := []types.PodSecuritySpec {}
129132
130- jobList , err := clientset .BatchV1beta1 ().CronJobs (p .namespace ).List (v12 .ListOptions {})
133+ jobList , err := clientset .BatchV1beta1 ().CronJobs (p .namespace ).List (v1meta .ListOptions {})
131134
132135 if err != nil {
133136 return cssList , pssList , err
134137 }
135138
136139 for _ , cronJob := range jobList .Items {
137- sa := p .serviceAccountMap [ cronJob .Spec .JobTemplate .Spec .Template .Spec .ServiceAccountName ]
140+ sa := p .GetServiceAccount ( cronJob .Namespace , cronJob . Spec .JobTemplate .Spec .Template .Spec .ServiceAccountName )
138141 cspList2 , pss := p .gen .GetSecuritySpecFromPodSpec (types.Metadata {
139142 Name : cronJob .Name ,
140143 Kind : CronJob ,
@@ -152,7 +155,7 @@ func (p *Processor) getSecuritySpecFromJobs() ([]types.ContainerSecuritySpec, []
152155 cssList := []types.ContainerSecuritySpec {}
153156 pssList := []types.PodSecuritySpec {}
154157
155- jobList , err := clientset .BatchV1 ().Jobs (p .namespace ).List (v12 .ListOptions {})
158+ jobList , err := clientset .BatchV1 ().Jobs (p .namespace ).List (v1meta .ListOptions {})
156159
157160 if err != nil {
158161 return cssList , pssList , err
@@ -162,7 +165,7 @@ func (p *Processor) getSecuritySpecFromJobs() ([]types.ContainerSecuritySpec, []
162165 if len (job .OwnerReferences ) > 0 {
163166 continue
164167 }
165- sa := p .serviceAccountMap [ job .Spec .Template .Spec .ServiceAccountName ]
168+ sa := p .GetServiceAccount ( job .Namespace , job . Spec .Template .Spec .ServiceAccountName )
166169 cspList2 , pss := p .gen .GetSecuritySpecFromPodSpec (types.Metadata {
167170 Name : job .Name ,
168171 Kind : Job ,
@@ -180,14 +183,14 @@ func (p *Processor) getSecuritySpecFromDeployments() ([]types.ContainerSecurityS
180183 cssList := []types.ContainerSecuritySpec {}
181184 pssList := []types.PodSecuritySpec {}
182185
183- deployments , err := clientset .AppsV1 ().Deployments (p .namespace ).List (v12 .ListOptions {})
186+ deployments , err := clientset .AppsV1 ().Deployments (p .namespace ).List (v1meta .ListOptions {})
184187
185188 if err != nil {
186189 return cssList , pssList , err
187190 }
188191
189192 for _ , deploy := range deployments .Items {
190- sa := p .serviceAccountMap [ deploy .Spec .Template .Spec .ServiceAccountName ]
193+ sa := p .GetServiceAccount ( deploy .Namespace , deploy . Spec .Template .Spec .ServiceAccountName )
191194 cspList2 , pss := p .gen .GetSecuritySpecFromPodSpec (types.Metadata {
192195 Name : deploy .Name ,
193196 Kind : Deployment ,
@@ -205,7 +208,7 @@ func (p *Processor) getSecuritySpecFromPods() ([]types.ContainerSecuritySpec, []
205208 cssList := []types.ContainerSecuritySpec {}
206209 pssList := []types.PodSecuritySpec {}
207210
208- pods , err := clientset .CoreV1 ().Pods (p .namespace ).List (v12 .ListOptions {})
211+ pods , err := clientset .CoreV1 ().Pods (p .namespace ).List (v1meta .ListOptions {})
209212
210213 if err != nil {
211214 return cssList , pssList , err
@@ -216,7 +219,7 @@ func (p *Processor) getSecuritySpecFromPods() ([]types.ContainerSecuritySpec, []
216219 continue
217220 }
218221
219- sa := p .serviceAccountMap [ pod .Spec .ServiceAccountName ]
222+ sa := p .GetServiceAccount ( pod .Namespace , pod . Spec .ServiceAccountName )
220223 cspList2 , podSecurityPosture := p .gen .GetSecuritySpecFromPodSpec (types.Metadata {
221224 Name : pod .Name ,
222225 Kind : Pod ,
@@ -232,14 +235,32 @@ func (p *Processor) getSecuritySpecFromPods() ([]types.ContainerSecuritySpec, []
232235func (p * Processor ) getServiceAccountMap () (map [string ]v1.ServiceAccount , error ) {
233236 serviceAccountMap := map [string ]v1.ServiceAccount {}
234237
235- serviceAccounts , err := p .k8sClient .CoreV1 ().ServiceAccounts (p .namespace ).List (v12 .ListOptions {})
238+ serviceAccounts , err := p .k8sClient .CoreV1 ().ServiceAccounts (p .namespace ).List (v1meta .ListOptions {})
236239 if err != nil {
237240 return serviceAccountMap , err
238241 }
239242
243+ // service account is an namespaced object
240244 for _ , sa := range serviceAccounts .Items {
241- serviceAccountMap [sa .Name ] = sa
245+ key := fmt .Sprintf ("%s:%s" , sa .Namespace , sa .Name )
246+ serviceAccountMap [key ] = sa
242247 }
243248
244249 return serviceAccountMap , nil
245250}
251+
252+ func (p * Processor ) GetServiceAccount (ns , saName string ) v1.ServiceAccount {
253+ if saName == "" {
254+ saName = "default"
255+ }
256+
257+ key := fmt .Sprintf ("%s:%s" , ns , saName )
258+
259+ sa , exists := p .serviceAccountMap [key ]
260+
261+ if ! exists {
262+ return v1.ServiceAccount {}
263+ }
264+
265+ return sa
266+ }
0 commit comments