@@ -24,10 +24,10 @@ var (
24
24
type EscalationReport struct {
25
25
OverallEscalation bool `json:"escalation"`
26
26
OverallReduction bool `json:"reduction"`
27
- Privileged int `json:"privileged"`
28
- HostIPC int `json:"hostIPC"`
29
- HostNetwork int `json:"hostNetwork"`
30
- HostPID int `json:"hostPID"`
27
+ Privileged Escalation `json:"privileged"`
28
+ HostIPC Escalation `json:"hostIPC"`
29
+ HostNetwork Escalation `json:"hostNetwork"`
30
+ HostPID Escalation `json:"hostPID"`
31
31
NewHostPaths map [string ]bool `json:"-"`
32
32
RemovedHostPaths map [string ]bool `json:"-"`
33
33
NewVolumeTypes []string `json:"new_volume_types"`
@@ -36,15 +36,35 @@ type EscalationReport struct {
36
36
RemovedCapabilities []string `json:"reduced_capabilities"`
37
37
RunAsUserStrategy int `json:"run_as_user_strategy"`
38
38
RunAsGroupStrategy int `json:"un_as_group_strategy"`
39
- ReadOnlyRootFS int `json:"read_only_root_fs"`
39
+ ReadOnlyRootFS Escalation `json:"read_only_root_fs"`
40
+ }
41
+
42
+ type Escalation struct {
43
+ Status int `json:"status"`
44
+ StatusMessage string `json:"status_message"`
45
+ Previous string `json:"previous"`
46
+ Current string `json:"current"`
47
+ Workloads []Metadata `json:"workloads"`
48
+ workloadMap map [Metadata ]bool `json:"-"`
49
+ }
50
+
51
+ func InitEscalation () Escalation {
52
+ return Escalation {
53
+ Status : NoChange ,
54
+ StatusMessage : GetEscalatedStatus (NoChange ),
55
+ Previous : "" ,
56
+ Current : "" ,
57
+ Workloads : []Metadata {},
58
+ workloadMap : map [Metadata ]bool {},
59
+ }
40
60
}
41
61
42
62
func NewEscalationReport () * EscalationReport {
43
63
return & EscalationReport {
44
- Privileged : NoChange ,
45
- HostNetwork : NoChange ,
46
- HostIPC : NoChange ,
47
- HostPID : NoChange ,
64
+ Privileged : InitEscalation () ,
65
+ HostNetwork : InitEscalation () ,
66
+ HostIPC : InitEscalation () ,
67
+ HostPID : InitEscalation () ,
48
68
NewHostPaths : map [string ]bool {},
49
69
NewCapabilities : []string {},
50
70
NewVolumeTypes : []string {},
@@ -53,68 +73,68 @@ func NewEscalationReport() *EscalationReport {
53
73
RemovedVolumeTypes : []string {},
54
74
RunAsGroupStrategy : NoChange ,
55
75
RunAsUserStrategy : NoChange ,
56
- ReadOnlyRootFS : NoChange ,
76
+ ReadOnlyRootFS : InitEscalation () ,
57
77
}
58
78
}
59
79
60
80
func (e * EscalationReport ) PrivilegeEscalated () bool {
61
- return e .Privileged == Escalated
81
+ return e .Privileged . Status == Escalated
62
82
}
63
83
64
84
func (e * EscalationReport ) PrivilegeReduced () bool {
65
- return e .Privileged == Reduced
85
+ return e .Privileged . Status == Reduced
66
86
}
67
87
68
88
func (e * EscalationReport ) PrivilegeNoChange () bool {
69
- return e .Privileged == NoChange
89
+ return e .Privileged . Status == NoChange
70
90
}
71
91
72
92
func (e * EscalationReport ) HostIPCEscalated () bool {
73
- return e .HostIPC == Escalated
93
+ return e .HostIPC . Status == Escalated
74
94
}
75
95
76
96
func (e * EscalationReport ) HostIPCReduced () bool {
77
- return e .HostIPC == Reduced
97
+ return e .HostIPC . Status == Reduced
78
98
}
79
99
80
100
func (e * EscalationReport ) HostIPCNoChange () bool {
81
- return e .HostIPC == NoChange
101
+ return e .HostIPC . Status == NoChange
82
102
}
83
103
84
104
func (e * EscalationReport ) HostNetworkEscalated () bool {
85
- return e .HostNetwork == Escalated
105
+ return e .HostNetwork . Status == Escalated
86
106
}
87
107
88
108
func (e * EscalationReport ) HostNetworkReduced () bool {
89
- return e .HostNetwork == Reduced
109
+ return e .HostNetwork . Status == Reduced
90
110
}
91
111
92
112
func (e * EscalationReport ) HostNetworkNoChange () bool {
93
- return e .HostNetwork == NoChange
113
+ return e .HostNetwork . Status == NoChange
94
114
}
95
115
96
116
func (e * EscalationReport ) HostPIDEscalated () bool {
97
- return e .HostPID == Escalated
117
+ return e .HostPID . Status == Escalated
98
118
}
99
119
100
120
func (e * EscalationReport ) HostPIDReduced () bool {
101
- return e .HostPID == Reduced
121
+ return e .HostPID . Status == Reduced
102
122
}
103
123
104
124
func (e * EscalationReport ) HostPIDNoChange () bool {
105
- return e .HostPID == NoChange
125
+ return e .HostPID . Status == NoChange
106
126
}
107
127
108
128
func (e * EscalationReport ) ReadOnlyRootFSEscalated () bool {
109
- return e .ReadOnlyRootFS == Escalated
129
+ return e .ReadOnlyRootFS . Status == Escalated
110
130
}
111
131
112
132
func (e * EscalationReport ) ReadOnlyRootFSReduced () bool {
113
- return e .ReadOnlyRootFS == Reduced
133
+ return e .ReadOnlyRootFS . Status == Reduced
114
134
}
115
135
116
136
func (e * EscalationReport ) ReadOnlyRootFSNoChange () bool {
117
- return e .ReadOnlyRootFS == NoChange
137
+ return e .ReadOnlyRootFS . Status == NoChange
118
138
}
119
139
120
140
func (e * EscalationReport ) RunAsUserStrategyEscalated () bool {
@@ -176,19 +196,19 @@ func (e *EscalationReport) Reduced() bool {
176
196
}
177
197
178
198
func (e * EscalationReport ) NoChanges () bool {
179
- if e .Privileged != NoChange {
199
+ if e .Privileged . Status != NoChange {
180
200
return false
181
201
}
182
202
183
- if e .HostIPC != NoChange {
203
+ if e .HostIPC . Status != NoChange {
184
204
return false
185
205
}
186
206
187
- if e .HostPID != NoChange {
207
+ if e .HostPID . Status != NoChange {
188
208
return false
189
209
}
190
210
191
- if e .HostNetwork != NoChange {
211
+ if e .HostNetwork . Status != NoChange {
192
212
return false
193
213
}
194
214
@@ -200,7 +220,7 @@ func (e *EscalationReport) NoChanges() bool {
200
220
return false
201
221
}
202
222
203
- if e .ReadOnlyRootFS != NoChange {
223
+ if e .ReadOnlyRootFS . Status != NoChange {
204
224
return false
205
225
}
206
226
@@ -233,30 +253,38 @@ func (e *EscalationReport) GenerateEscalationReport(psp1, psp2 *v1beta1.PodSecur
233
253
234
254
// privileged mode
235
255
if ! spec1 .Privileged && spec2 .Privileged {
236
- e .Privileged = Escalated
256
+ e .Privileged .Status = Escalated
257
+ e .Privileged .StatusMessage = GetEscalatedStatus (Escalated )
237
258
} else if spec1 .Privileged && ! spec2 .Privileged {
238
- e .Privileged = Reduced
259
+ e .Privileged .Status = Reduced
260
+ e .Privileged .StatusMessage = GetEscalatedStatus (Reduced )
239
261
}
240
262
241
263
// hostNetwork
242
264
if ! spec1 .HostNetwork && spec2 .HostNetwork {
243
- e .HostNetwork = Escalated
265
+ e .HostNetwork .Status = Escalated
266
+ e .HostNetwork .StatusMessage = GetEscalatedStatus (Escalated )
244
267
} else if spec1 .HostNetwork && ! spec2 .HostNetwork {
245
- e .HostNetwork = Reduced
268
+ e .HostNetwork .Status = Reduced
269
+ e .HostNetwork .StatusMessage = GetEscalatedStatus (Reduced )
246
270
}
247
271
248
272
// hostPID
249
273
if ! spec1 .HostPID && spec2 .HostPID {
250
- e .HostPID = Escalated
274
+ e .HostPID .Status = Escalated
275
+ e .HostPID .StatusMessage = GetEscalatedStatus (Escalated )
251
276
} else if spec1 .HostPID && ! spec2 .HostPID {
252
- e .HostPID = Reduced
277
+ e .HostPID .Status = Reduced
278
+ e .HostPID .StatusMessage = GetEscalatedStatus (Reduced )
253
279
}
254
280
255
281
// hostIPC
256
282
if ! spec1 .HostIPC && spec2 .HostIPC {
257
- e .HostIPC = Escalated
283
+ e .HostIPC .Status = Escalated
284
+ e .HostIPC .StatusMessage = GetEscalatedStatus (Escalated )
258
285
} else if spec1 .HostIPC && ! spec2 .HostIPC {
259
- e .HostIPC = Reduced
286
+ e .HostIPC .Status = Reduced
287
+ e .HostIPC .StatusMessage = GetEscalatedStatus (Reduced )
260
288
}
261
289
262
290
//TODO: host paths
@@ -416,9 +444,11 @@ func (e *EscalationReport) GenerateEscalationReport(psp1, psp2 *v1beta1.PodSecur
416
444
417
445
// readOnlyFS
418
446
if spec1 .ReadOnlyRootFilesystem && ! spec2 .ReadOnlyRootFilesystem {
419
- e .ReadOnlyRootFS = Escalated
447
+ e .ReadOnlyRootFS .Status = Escalated
448
+ e .ReadOnlyRootFS .StatusMessage = GetEscalatedStatus (Escalated )
420
449
} else if ! spec1 .ReadOnlyRootFilesystem && spec2 .ReadOnlyRootFilesystem {
421
- e .ReadOnlyRootFS = Reduced
450
+ e .ReadOnlyRootFS .Status = Reduced
451
+ e .ReadOnlyRootFS .StatusMessage = GetEscalatedStatus (Reduced )
422
452
}
423
453
424
454
if e .Escalated () {
@@ -432,6 +462,141 @@ func (e *EscalationReport) GenerateEscalationReport(psp1, psp2 *v1beta1.PodSecur
432
462
return nil
433
463
}
434
464
465
+ func (e * EscalationReport ) EnrichEscalationReport (srcCssList , targetCssList []ContainerSecuritySpec , srcPssList , targetPssList []PodSecuritySpec ) {
466
+ srcCssMap := map [Metadata ]ContainerSecuritySpec {}
467
+ targetCssMap := map [Metadata ]ContainerSecuritySpec {}
468
+
469
+ srcPssMap := map [Metadata ]PodSecuritySpec {}
470
+ targetPssMap := map [Metadata ]PodSecuritySpec {}
471
+
472
+ for _ , css := range srcCssList {
473
+ srcCssMap [css .Metadata ] = css
474
+ }
475
+
476
+ for _ , css := range targetCssList {
477
+ targetCssMap [css .Metadata ] = css
478
+ }
479
+
480
+ for _ , pss := range srcPssList {
481
+ srcPssMap [pss .Metadata ] = pss
482
+ }
483
+
484
+ for _ , pss := range targetPssList {
485
+ targetPssMap [pss .Metadata ] = pss
486
+ }
487
+
488
+ // privileged
489
+ if e .Privileged .Status == Escalated {
490
+ for meta , targetCss := range targetCssMap {
491
+ srcCss , exits := srcCssMap [meta ]
492
+ if targetCss .Privileged && (! exits || ! srcCss .Privileged ) {
493
+ e .Privileged .workloadMap [meta ] = true
494
+ }
495
+ }
496
+ } else if e .Privileged .Status == Reduced {
497
+ for meta , srcCss := range srcCssMap {
498
+ targetCss , exists := targetCssMap [meta ]
499
+
500
+ if srcCss .Privileged && (! exists || ! targetCss .Privileged ) {
501
+ e .Privileged .workloadMap [meta ] = true
502
+ }
503
+ }
504
+ }
505
+
506
+ for w := range e .Privileged .workloadMap {
507
+ e .Privileged .Workloads = append (e .Privileged .Workloads , w )
508
+ }
509
+
510
+ // hostNetwork
511
+ if e .HostNetwork .Status == Escalated {
512
+ for meta , targetPss := range targetPssMap {
513
+ srcPss , exits := srcPssMap [meta ]
514
+ if targetPss .HostNetwork && (! exits || ! srcPss .HostNetwork ) {
515
+ e .HostNetwork .workloadMap [meta ] = true
516
+ }
517
+ }
518
+ } else if e .HostNetwork .Status == Reduced {
519
+ for meta , srcPss := range srcPssMap {
520
+ targetPss , exists := targetPssMap [meta ]
521
+
522
+ if srcPss .HostNetwork && (! exists || ! targetPss .HostNetwork ) {
523
+ e .HostNetwork .workloadMap [meta ] = true
524
+ }
525
+ }
526
+ }
527
+
528
+ for w := range e .HostNetwork .workloadMap {
529
+ e .HostNetwork .Workloads = append (e .HostNetwork .Workloads , w )
530
+ }
531
+
532
+ // HostIPC
533
+ if e .HostIPC .Status == Escalated {
534
+ for meta , targetPss := range targetPssMap {
535
+ srcPss , exits := srcPssMap [meta ]
536
+ if targetPss .HostIPC && (! exits || ! srcPss .HostIPC ) {
537
+ e .HostIPC .workloadMap [meta ] = true
538
+ }
539
+ }
540
+ } else if e .HostIPC .Status == Reduced {
541
+ for meta , srcPss := range srcPssMap {
542
+ targetPss , exists := targetPssMap [meta ]
543
+
544
+ if srcPss .HostIPC && (! exists || ! targetPss .HostIPC ) {
545
+ e .HostIPC .workloadMap [meta ] = true
546
+ }
547
+ }
548
+ }
549
+
550
+ for w := range e .HostIPC .workloadMap {
551
+ e .HostIPC .Workloads = append (e .HostIPC .Workloads , w )
552
+ }
553
+
554
+ // HostPID
555
+ if e .HostPID .Status == Escalated {
556
+ for meta , targetPss := range targetPssMap {
557
+ srcPss , exits := srcPssMap [meta ]
558
+ if targetPss .HostPID && (! exits || ! srcPss .HostPID ) {
559
+ e .HostPID .workloadMap [meta ] = true
560
+ }
561
+ }
562
+ } else if e .HostPID .Status == Reduced {
563
+ for meta , srcPss := range srcPssMap {
564
+ targetPss , exists := targetPssMap [meta ]
565
+
566
+ if srcPss .HostPID && (! exists || ! targetPss .HostPID ) {
567
+ e .HostPID .workloadMap [meta ] = true
568
+ }
569
+ }
570
+ }
571
+
572
+ for w := range e .HostPID .workloadMap {
573
+ e .HostPID .Workloads = append (e .HostPID .Workloads , w )
574
+ }
575
+
576
+ // ReadOnlyRootFS
577
+ if e .ReadOnlyRootFS .Status == Escalated {
578
+ for meta , targetCss := range targetCssMap {
579
+ srcCss , exits := srcCssMap [meta ]
580
+ if ! targetCss .ReadOnlyRootFS && (exits && srcCss .ReadOnlyRootFS ) {
581
+ e .ReadOnlyRootFS .workloadMap [meta ] = true
582
+ }
583
+ }
584
+ } else if e .ReadOnlyRootFS .Status == Reduced {
585
+ for meta , srcCss := range srcCssMap {
586
+ targetCss , exists := targetCssMap [meta ]
587
+
588
+ if ! srcCss .ReadOnlyRootFS && (exists && targetCss .ReadOnlyRootFS ) {
589
+ e .ReadOnlyRootFS .workloadMap [meta ] = true
590
+ }
591
+ }
592
+ }
593
+
594
+ for w := range e .ReadOnlyRootFS .workloadMap {
595
+ e .ReadOnlyRootFS .Workloads = append (e .ReadOnlyRootFS .Workloads , w )
596
+ }
597
+
598
+ }
599
+
435
600
func GetEscalatedStatus (status int ) string {
436
601
return m [status ]
437
602
}
0 commit comments