Skip to content

Commit e65c869

Browse files
committed
enrich escalation report
Signed-off-by: kaizhe <derek0405@gmail.com>
1 parent 8ef3417 commit e65c869

File tree

8 files changed

+369
-83
lines changed

8 files changed

+369
-83
lines changed

advisor/types/escalation.go

Lines changed: 205 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ var (
2424
type EscalationReport struct {
2525
OverallEscalation bool `json:"escalation"`
2626
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"`
3131
NewHostPaths map[string]bool `json:"-"`
3232
RemovedHostPaths map[string]bool `json:"-"`
3333
NewVolumeTypes []string `json:"new_volume_types"`
@@ -36,15 +36,35 @@ type EscalationReport struct {
3636
RemovedCapabilities []string `json:"reduced_capabilities"`
3737
RunAsUserStrategy int `json:"run_as_user_strategy"`
3838
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+
}
4060
}
4161

4262
func NewEscalationReport() *EscalationReport {
4363
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(),
4868
NewHostPaths: map[string]bool{},
4969
NewCapabilities: []string{},
5070
NewVolumeTypes: []string{},
@@ -53,68 +73,68 @@ func NewEscalationReport() *EscalationReport {
5373
RemovedVolumeTypes: []string{},
5474
RunAsGroupStrategy: NoChange,
5575
RunAsUserStrategy: NoChange,
56-
ReadOnlyRootFS: NoChange,
76+
ReadOnlyRootFS: InitEscalation(),
5777
}
5878
}
5979

6080
func (e *EscalationReport) PrivilegeEscalated() bool {
61-
return e.Privileged == Escalated
81+
return e.Privileged.Status == Escalated
6282
}
6383

6484
func (e *EscalationReport) PrivilegeReduced() bool {
65-
return e.Privileged == Reduced
85+
return e.Privileged.Status == Reduced
6686
}
6787

6888
func (e *EscalationReport) PrivilegeNoChange() bool {
69-
return e.Privileged == NoChange
89+
return e.Privileged.Status == NoChange
7090
}
7191

7292
func (e *EscalationReport) HostIPCEscalated() bool {
73-
return e.HostIPC == Escalated
93+
return e.HostIPC.Status == Escalated
7494
}
7595

7696
func (e *EscalationReport) HostIPCReduced() bool {
77-
return e.HostIPC == Reduced
97+
return e.HostIPC.Status == Reduced
7898
}
7999

80100
func (e *EscalationReport) HostIPCNoChange() bool {
81-
return e.HostIPC == NoChange
101+
return e.HostIPC.Status == NoChange
82102
}
83103

84104
func (e *EscalationReport) HostNetworkEscalated() bool {
85-
return e.HostNetwork == Escalated
105+
return e.HostNetwork.Status == Escalated
86106
}
87107

88108
func (e *EscalationReport) HostNetworkReduced() bool {
89-
return e.HostNetwork == Reduced
109+
return e.HostNetwork.Status == Reduced
90110
}
91111

92112
func (e *EscalationReport) HostNetworkNoChange() bool {
93-
return e.HostNetwork == NoChange
113+
return e.HostNetwork.Status == NoChange
94114
}
95115

96116
func (e *EscalationReport) HostPIDEscalated() bool {
97-
return e.HostPID == Escalated
117+
return e.HostPID.Status == Escalated
98118
}
99119

100120
func (e *EscalationReport) HostPIDReduced() bool {
101-
return e.HostPID == Reduced
121+
return e.HostPID.Status == Reduced
102122
}
103123

104124
func (e *EscalationReport) HostPIDNoChange() bool {
105-
return e.HostPID == NoChange
125+
return e.HostPID.Status == NoChange
106126
}
107127

108128
func (e *EscalationReport) ReadOnlyRootFSEscalated() bool {
109-
return e.ReadOnlyRootFS == Escalated
129+
return e.ReadOnlyRootFS.Status == Escalated
110130
}
111131

112132
func (e *EscalationReport) ReadOnlyRootFSReduced() bool {
113-
return e.ReadOnlyRootFS == Reduced
133+
return e.ReadOnlyRootFS.Status == Reduced
114134
}
115135

116136
func (e *EscalationReport) ReadOnlyRootFSNoChange() bool {
117-
return e.ReadOnlyRootFS == NoChange
137+
return e.ReadOnlyRootFS.Status == NoChange
118138
}
119139

120140
func (e *EscalationReport) RunAsUserStrategyEscalated() bool {
@@ -176,19 +196,19 @@ func (e *EscalationReport) Reduced() bool {
176196
}
177197

178198
func (e *EscalationReport) NoChanges() bool {
179-
if e.Privileged != NoChange {
199+
if e.Privileged.Status != NoChange {
180200
return false
181201
}
182202

183-
if e.HostIPC != NoChange {
203+
if e.HostIPC.Status != NoChange {
184204
return false
185205
}
186206

187-
if e.HostPID != NoChange {
207+
if e.HostPID.Status != NoChange {
188208
return false
189209
}
190210

191-
if e.HostNetwork != NoChange {
211+
if e.HostNetwork.Status != NoChange {
192212
return false
193213
}
194214

@@ -200,7 +220,7 @@ func (e *EscalationReport) NoChanges() bool {
200220
return false
201221
}
202222

203-
if e.ReadOnlyRootFS != NoChange {
223+
if e.ReadOnlyRootFS.Status != NoChange {
204224
return false
205225
}
206226

@@ -233,30 +253,38 @@ func (e *EscalationReport) GenerateEscalationReport(psp1, psp2 *v1beta1.PodSecur
233253

234254
// privileged mode
235255
if !spec1.Privileged && spec2.Privileged {
236-
e.Privileged = Escalated
256+
e.Privileged.Status = Escalated
257+
e.Privileged.StatusMessage = GetEscalatedStatus(Escalated)
237258
} else if spec1.Privileged && !spec2.Privileged {
238-
e.Privileged = Reduced
259+
e.Privileged.Status = Reduced
260+
e.Privileged.StatusMessage = GetEscalatedStatus(Reduced)
239261
}
240262

241263
// hostNetwork
242264
if !spec1.HostNetwork && spec2.HostNetwork {
243-
e.HostNetwork = Escalated
265+
e.HostNetwork.Status = Escalated
266+
e.HostNetwork.StatusMessage = GetEscalatedStatus(Escalated)
244267
} else if spec1.HostNetwork && !spec2.HostNetwork {
245-
e.HostNetwork = Reduced
268+
e.HostNetwork.Status = Reduced
269+
e.HostNetwork.StatusMessage = GetEscalatedStatus(Reduced)
246270
}
247271

248272
// hostPID
249273
if !spec1.HostPID && spec2.HostPID {
250-
e.HostPID = Escalated
274+
e.HostPID.Status = Escalated
275+
e.HostPID.StatusMessage = GetEscalatedStatus(Escalated)
251276
} else if spec1.HostPID && !spec2.HostPID {
252-
e.HostPID = Reduced
277+
e.HostPID.Status = Reduced
278+
e.HostPID.StatusMessage = GetEscalatedStatus(Reduced)
253279
}
254280

255281
// hostIPC
256282
if !spec1.HostIPC && spec2.HostIPC {
257-
e.HostIPC = Escalated
283+
e.HostIPC.Status = Escalated
284+
e.HostIPC.StatusMessage = GetEscalatedStatus(Escalated)
258285
} else if spec1.HostIPC && !spec2.HostIPC {
259-
e.HostIPC = Reduced
286+
e.HostIPC.Status = Reduced
287+
e.HostIPC.StatusMessage = GetEscalatedStatus(Reduced)
260288
}
261289

262290
//TODO: host paths
@@ -416,9 +444,11 @@ func (e *EscalationReport) GenerateEscalationReport(psp1, psp2 *v1beta1.PodSecur
416444

417445
// readOnlyFS
418446
if spec1.ReadOnlyRootFilesystem && !spec2.ReadOnlyRootFilesystem {
419-
e.ReadOnlyRootFS = Escalated
447+
e.ReadOnlyRootFS.Status = Escalated
448+
e.ReadOnlyRootFS.StatusMessage = GetEscalatedStatus(Escalated)
420449
} else if !spec1.ReadOnlyRootFilesystem && spec2.ReadOnlyRootFilesystem {
421-
e.ReadOnlyRootFS = Reduced
450+
e.ReadOnlyRootFS.Status = Reduced
451+
e.ReadOnlyRootFS.StatusMessage = GetEscalatedStatus(Reduced)
422452
}
423453

424454
if e.Escalated() {
@@ -432,6 +462,141 @@ func (e *EscalationReport) GenerateEscalationReport(psp1, psp2 *v1beta1.PodSecur
432462
return nil
433463
}
434464

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+
435600
func GetEscalatedStatus(status int) string {
436601
return m[status]
437602
}

advisor/types/securityspec.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ type PodSecuritySpec struct {
7272
}
7373

7474
type Metadata struct {
75-
Name string `json:"name"`
76-
Kind string `json:"kind"`
75+
Name string `json:"name"`
76+
Kind string `json:"kind"`
77+
Namespace string `json:"namespace"`
78+
YamlFile string `json:"file"`
7779
}

0 commit comments

Comments
 (0)