@@ -26,6 +26,8 @@ const (
2626type EscalationReport struct {
2727 TotalSourceWorkloads int `json:"total_source_workloads"`
2828 TotalTargetWorkloads int `json:"total_target_workloads"`
29+ TotalSourceImages int `json:"total_source_images"`
30+ TotalTargetImages int `json:"total_target_images"`
2931 TotalEscalation int `json:"escalation_count"`
3032 TotalReduction int `json:"reduction_count"`
3133 Escalations []Metadata `json:"escalations"`
@@ -62,6 +64,7 @@ type Escalation struct {
6264 workloadMap map [Metadata ]bool `json:"-"`
6365}
6466
67+ // InitEscalation returns an initialized escalation object
6568func InitEscalation (status int , prev , cur string ) * Escalation {
6669 return & Escalation {
6770 Status : status ,
@@ -73,6 +76,7 @@ func InitEscalation(status int, prev, cur string) *Escalation {
7376 }
7477}
7578
79+ // SetEscalation set escalation status
7680func (e * Escalation ) SetEscalation (status int , prev , cur string ) {
7781 e .Status = status
7882 e .StatusMessage = getEscalatedStatus (status )
@@ -88,6 +92,21 @@ func (e *Escalation) AddWorkload(w Metadata) {
8892 e .workloadMap [w ] = true
8993}
9094
95+ func (e * Escalation ) ConsolidateWorkloadImage () {
96+ m := map [Metadata ]bool {}
97+
98+ for w := range e .workloadMap {
99+ w .Image = ""
100+ m [w ] = true
101+ }
102+
103+ for w := range m {
104+ e .Workloads = append (e .Workloads , w )
105+ }
106+
107+ e .WorkloadCount = len (e .Workloads )
108+ }
109+
91110func (e * Escalation ) ConsolidateWorkload () {
92111 for w := range e .workloadMap {
93112 e .Workloads = append (e .Workloads , w )
@@ -108,6 +127,7 @@ func (e *Escalation) IsReduced() bool {
108127 return e .Status == Reduced && e .UseSecurityContext ()
109128}
110129
130+ // NewEscalationReport returns an escalation report object
111131func NewEscalationReport () * EscalationReport {
112132 return & EscalationReport {
113133 TotalSourceWorkloads : 0 ,
@@ -139,108 +159,133 @@ func NewEscalationReport() *EscalationReport {
139159 }
140160}
141161
142- func (er * EscalationReport ) PrivilegeEscalated () bool {
162+ // privileged mode
163+ func (er * EscalationReport ) PrivilegedEscalated () bool {
143164 return er .NewPrivileged .IsEscalated ()
144165}
145166
146- func (er * EscalationReport ) PrivilegeReduced () bool {
167+ // privileged mode
168+ func (er * EscalationReport ) PrivilegedReduced () bool {
147169 return er .RemovedPrivileged .IsReduced ()
148170}
149171
150- func (er * EscalationReport ) PrivilegeNoChange () bool {
151- return ! er .PrivilegeReduced () && ! er .PrivilegeReduced ()
172+ // privileged mode
173+ func (er * EscalationReport ) PrivilegedNoChange () bool {
174+ return ! er .PrivilegedReduced () && ! er .PrivilegedReduced ()
152175}
153176
177+ // HostIPC
154178func (er * EscalationReport ) HostIPCEscalated () bool {
155179 return er .NewHostIPC .IsEscalated ()
156180}
157181
182+ // HostIPC
158183func (er * EscalationReport ) HostIPCReduced () bool {
159184 return er .RemovedHostIPC .IsReduced ()
160185}
161186
187+ // HostIPC
162188func (er * EscalationReport ) HostIPCNoChange () bool {
163189 return ! er .HostIPCEscalated () && ! er .HostIPCReduced ()
164190}
165191
192+ // HostNetwork
166193func (er * EscalationReport ) HostNetworkEscalated () bool {
167194 return er .NewHostNetwork .IsEscalated ()
168195}
169196
197+ // HostNetwork
170198func (er * EscalationReport ) HostNetworkReduced () bool {
171199 return er .RemovedHostNetwork .IsReduced ()
172200}
173201
202+ // HostNetwork
174203func (er * EscalationReport ) HostNetworkNoChange () bool {
175204 return ! er .HostNetworkEscalated () && ! er .HostNetworkReduced ()
176205}
177206
207+ // HostPID
178208func (er * EscalationReport ) HostPIDEscalated () bool {
179209 return er .NewHostPID .IsEscalated ()
180210}
181211
212+ // HostPID
182213func (er * EscalationReport ) HostPIDReduced () bool {
183214 return er .RemovedHostPID .IsReduced ()
184215}
185216
217+ // HostPID
186218func (er * EscalationReport ) HostPIDNoChange () bool {
187219 return ! er .HostPIDEscalated () && ! er .HostPIDReduced ()
188220}
189221
222+ // ReadOnlyRootFileSystem
190223func (er * EscalationReport ) ReadOnlyRootFSEscalated () bool {
191224 return er .RemovedReadOnlyRootFS .IsEscalated ()
192225}
193226
227+ // ReadOnlyRootFileSystem
194228func (er * EscalationReport ) ReadOnlyRootFSReduced () bool {
195229 return er .NewReadOnlyRootFS .IsReduced ()
196230}
197231
232+ // ReadOnlyRootFileSystem
198233func (er * EscalationReport ) ReadOnlyRootFSNoChange () bool {
199234 return ! er .ReadOnlyRootFSEscalated () && ! er .ReadOnlyRootFSReduced ()
200235}
201236
237+ // runAsUser (non root -> root)
202238func (er * EscalationReport ) RunUserAsRootEscalated () bool {
203239 return er .NewRunUserAsRoot .IsEscalated ()
204240}
205241
242+ // runAsUser (root -> non root)
206243func (er * EscalationReport ) RunUserAsRootReduced () bool {
207244 return er .RemovedRunUserAsRoot .IsReduced ()
208245}
209246
247+ // runAsUser
210248func (er * EscalationReport ) RunUserAsRootNoChange () bool {
211249 return ! er .RunUserAsRootEscalated () && ! er .RunUserAsRootReduced ()
212250}
213251
252+ // runAsGroup (non root -> root)
214253func (er * EscalationReport ) RunGroupAsRootEscalated () bool {
215254 return er .NewRunGroupAsRoot .IsEscalated ()
216255}
217256
257+ // runAsGroup (root -> non root)
218258func (er * EscalationReport ) RunGroupAsRootReduced () bool {
219259 return er .RemovedRunGroupAsRoot .IsReduced ()
220260}
221261
262+ // runAsGroup
222263func (er * EscalationReport ) RunGroupAsRootNoChange () bool {
223264 return er .NewRunGroupAsRoot .NoChanges ()
224265}
225266
267+ // newly added volume types
226268func (er * EscalationReport ) AddedVolumes () bool {
227269 return len (er .NewVolumeTypes ) > 0
228270}
229271
272+ // removed volume types
230273func (er * EscalationReport ) RemovedVolumes () bool {
231274 return len (er .RemovedVolumeTypes ) > 0
232275}
233276
277+ // added capabilities
234278func (er * EscalationReport ) AddedCapabilities () bool {
235279 return len (er .NewCapabilities ) > 0
236280}
237281
282+ // dropped capabilities
238283func (er * EscalationReport ) DroppedCapabilities () bool {
239284 return len (er .RemovedCapabilities ) > 0
240285}
241286
242287func (er * EscalationReport ) Escalated () bool {
243- if er .PrivilegeEscalated () || er .HostNetworkEscalated () || er .HostPIDEscalated () || er .HostIPCEscalated () || er .AddedVolumes () ||
288+ if er .PrivilegedEscalated () || er .HostNetworkEscalated () || er .HostPIDEscalated () || er .HostIPCEscalated () || er .AddedVolumes () ||
244289 er .AddedCapabilities () || er .ReadOnlyRootFSEscalated () || er .RunGroupAsRootEscalated () || er .RunUserAsRootEscalated () {
245290 return true
246291 }
@@ -249,88 +294,25 @@ func (er *EscalationReport) Escalated() bool {
249294}
250295
251296func (er * EscalationReport ) Reduced () bool {
252- if er .PrivilegeReduced () || er .HostNetworkReduced () || er .HostPIDReduced () || er .HostIPCReduced () || er .RemovedVolumes () ||
297+ if er .PrivilegedReduced () || er .HostNetworkReduced () || er .HostPIDReduced () || er .HostIPCReduced () || er .RemovedVolumes () ||
253298 er .DroppedCapabilities () || er .ReadOnlyRootFSReduced () || er .RunGroupAsRootReduced () || er .RunUserAsRootReduced () {
254299 return true
255300 }
256301
257302 return false
258303}
259304
260- func (er * EscalationReport ) NoChanges () bool {
261- if ! er .NewPrivileged .NoChanges () {
262- return false
263- }
264-
265- if ! er .NewHostIPC .NoChanges () {
266- return false
267- }
268-
269- if ! er .NewHostPID .NoChanges () {
270- return false
271- }
272-
273- if ! er .NewHostNetwork .NoChanges () {
274- return false
275- }
276-
277- if ! er .NewRunGroupAsRoot .NoChanges () {
278- return false
279- }
280-
281- if ! er .NewRunUserAsRoot .NoChanges () {
282- return false
283- }
284-
285- if ! er .NewReadOnlyRootFS .NoChanges () {
286- return false
287- }
288-
289- if len (er .RemovedCapabilities ) > 0 {
290- return false
291- }
292-
293- if len (er .NewCapabilities ) > 0 {
294- return false
295- }
296-
297- if len (er .RemovedVolumeTypes ) > 0 {
298- return false
299- }
300-
301- if len (er .NewVolumeTypes ) > 0 {
302- return false
303- }
304-
305- return true
306- }
307-
305+ // GenerateEscalationReportFromSecurityContext returns a escalation report after comparing the source and target YAML files
308306func (er * EscalationReport ) GenerateEscalationReportFromSecurityContext (srcCssList , targetCssList []ContainerSecuritySpec , srcPssList , targetPssList []PodSecuritySpec ) {
309- srcCssMap := map [ Metadata ] ContainerSecuritySpec {}
310- targetCssMap := map [ Metadata ] ContainerSecuritySpec {}
307+ srcCssMap := NewContainerSecuritySpecMap ( srcCssList )
308+ targetCssMap := NewContainerSecuritySpecMap ( targetCssList )
311309
312- srcPssMap := map [ Metadata ] PodSecuritySpec {}
313- targetPssMap := map [ Metadata ] PodSecuritySpec {}
310+ srcPssMap := NewPodSecuritySpecMap ( srcPssList )
311+ targetPssMap := NewPodSecuritySpecMap ( targetPssList )
314312
315313 escalations := InitEscalation (Escalated , "" , "" )
316314 reductions := InitEscalation (Reduced , "" , "" )
317315
318- for _ , css := range srcCssList {
319- srcCssMap [css .Metadata ] = css
320- }
321-
322- for _ , css := range targetCssList {
323- targetCssMap [css .Metadata ] = css
324- }
325-
326- for _ , pss := range srcPssList {
327- srcPssMap [pss .Metadata ] = pss
328- }
329-
330- for _ , pss := range targetPssList {
331- targetPssMap [pss .Metadata ] = pss
332- }
333-
334316 // privileged - false to true (escalated)
335317 for meta , targetCss := range targetCssMap {
336318 srcCss , exits := srcCssMap [meta ]
@@ -545,8 +527,8 @@ func (er *EscalationReport) GenerateEscalationReportFromSecurityContext(srcCssLi
545527 e .ConsolidateWorkload ()
546528 }
547529
548- escalations .ConsolidateWorkload ()
549- reductions .ConsolidateWorkload ()
530+ escalations .ConsolidateWorkloadImage ()
531+ reductions .ConsolidateWorkloadImage ()
550532
551533 er .Escalations = append (er .Escalations , escalations .Workloads ... )
552534 er .Reductions = append (er .Reductions , reductions .Workloads ... )
@@ -555,6 +537,8 @@ func (er *EscalationReport) GenerateEscalationReportFromSecurityContext(srcCssLi
555537 er .TotalReduction = len (er .Reductions )
556538 er .TotalSourceWorkloads = len (srcPssMap )
557539 er .TotalTargetWorkloads = len (targetPssMap )
540+ er .TotalSourceImages = len (srcCssMap )
541+ er .TotalTargetImages = len (targetCssMap )
558542}
559543
560544func getEscalatedStatus (status int ) string {
0 commit comments