Skip to content

Commit a17acbc

Browse files
committed
clean up code
Signed-off-by: kaizhe <[email protected]>
1 parent 16a5392 commit a17acbc

File tree

11 files changed

+146
-133
lines changed

11 files changed

+146
-133
lines changed

advisor/types/escalation.go

Lines changed: 60 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ const (
2626
type 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
6568
func 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
7680
func (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+
91110
func (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
111131
func 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
154178
func (er *EscalationReport) HostIPCEscalated() bool {
155179
return er.NewHostIPC.IsEscalated()
156180
}
157181

182+
// HostIPC
158183
func (er *EscalationReport) HostIPCReduced() bool {
159184
return er.RemovedHostIPC.IsReduced()
160185
}
161186

187+
// HostIPC
162188
func (er *EscalationReport) HostIPCNoChange() bool {
163189
return !er.HostIPCEscalated() && !er.HostIPCReduced()
164190
}
165191

192+
// HostNetwork
166193
func (er *EscalationReport) HostNetworkEscalated() bool {
167194
return er.NewHostNetwork.IsEscalated()
168195
}
169196

197+
// HostNetwork
170198
func (er *EscalationReport) HostNetworkReduced() bool {
171199
return er.RemovedHostNetwork.IsReduced()
172200
}
173201

202+
// HostNetwork
174203
func (er *EscalationReport) HostNetworkNoChange() bool {
175204
return !er.HostNetworkEscalated() && !er.HostNetworkReduced()
176205
}
177206

207+
// HostPID
178208
func (er *EscalationReport) HostPIDEscalated() bool {
179209
return er.NewHostPID.IsEscalated()
180210
}
181211

212+
// HostPID
182213
func (er *EscalationReport) HostPIDReduced() bool {
183214
return er.RemovedHostPID.IsReduced()
184215
}
185216

217+
// HostPID
186218
func (er *EscalationReport) HostPIDNoChange() bool {
187219
return !er.HostPIDEscalated() && !er.HostPIDReduced()
188220
}
189221

222+
// ReadOnlyRootFileSystem
190223
func (er *EscalationReport) ReadOnlyRootFSEscalated() bool {
191224
return er.RemovedReadOnlyRootFS.IsEscalated()
192225
}
193226

227+
// ReadOnlyRootFileSystem
194228
func (er *EscalationReport) ReadOnlyRootFSReduced() bool {
195229
return er.NewReadOnlyRootFS.IsReduced()
196230
}
197231

232+
// ReadOnlyRootFileSystem
198233
func (er *EscalationReport) ReadOnlyRootFSNoChange() bool {
199234
return !er.ReadOnlyRootFSEscalated() && !er.ReadOnlyRootFSReduced()
200235
}
201236

237+
// runAsUser (non root -> root)
202238
func (er *EscalationReport) RunUserAsRootEscalated() bool {
203239
return er.NewRunUserAsRoot.IsEscalated()
204240
}
205241

242+
// runAsUser (root -> non root)
206243
func (er *EscalationReport) RunUserAsRootReduced() bool {
207244
return er.RemovedRunUserAsRoot.IsReduced()
208245
}
209246

247+
// runAsUser
210248
func (er *EscalationReport) RunUserAsRootNoChange() bool {
211249
return !er.RunUserAsRootEscalated() && !er.RunUserAsRootReduced()
212250
}
213251

252+
// runAsGroup (non root -> root)
214253
func (er *EscalationReport) RunGroupAsRootEscalated() bool {
215254
return er.NewRunGroupAsRoot.IsEscalated()
216255
}
217256

257+
// runAsGroup (root -> non root)
218258
func (er *EscalationReport) RunGroupAsRootReduced() bool {
219259
return er.RemovedRunGroupAsRoot.IsReduced()
220260
}
221261

262+
// runAsGroup
222263
func (er *EscalationReport) RunGroupAsRootNoChange() bool {
223264
return er.NewRunGroupAsRoot.NoChanges()
224265
}
225266

267+
// newly added volume types
226268
func (er *EscalationReport) AddedVolumes() bool {
227269
return len(er.NewVolumeTypes) > 0
228270
}
229271

272+
// removed volume types
230273
func (er *EscalationReport) RemovedVolumes() bool {
231274
return len(er.RemovedVolumeTypes) > 0
232275
}
233276

277+
// added capabilities
234278
func (er *EscalationReport) AddedCapabilities() bool {
235279
return len(er.NewCapabilities) > 0
236280
}
237281

282+
// dropped capabilities
238283
func (er *EscalationReport) DroppedCapabilities() bool {
239284
return len(er.RemovedCapabilities) > 0
240285
}
241286

242287
func (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

251296
func (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
308306
func (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

560544
func getEscalatedStatus(status int) string {

advisor/types/escalation_test.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7-
"testing"
87

98
"k8s.io/api/policy/v1beta1"
109

@@ -58,14 +57,6 @@ spec:
5857
`
5958
)
6059

61-
func TestNoChanges(t *testing.T) {
62-
r := NewEscalationReport()
63-
64-
if !r.NoChanges() {
65-
t.Fatal("new report should not contain an changes.")
66-
}
67-
}
68-
6960
func readPSPYaml(pspInput string) (*v1beta1.PodSecurityPolicy, error) {
7061
var psp v1beta1.PodSecurityPolicy
7162

advisor/types/securityspec.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package types
22

3-
import "github.com/sysdiglabs/kube-psp-advisor/utils"
4-
53
var (
64
DefaultCaps = []string{
75
"SETPCAP",
@@ -77,13 +75,30 @@ type Metadata struct {
7775
Name string `json:"name"`
7876
Kind string `json:"kind"`
7977
Namespace string `json:"namespace"`
80-
YamlFile string `json:"file"`
78+
YamlFile string `json:"file,omitempty"`
79+
Image string `json:"image,omitempty"`
80+
}
81+
82+
type PodSecuritySpecMap map[Metadata]PodSecuritySpec
83+
84+
func NewPodSecuritySpecMap(pssList []PodSecuritySpec) PodSecuritySpecMap {
85+
pssMap := PodSecuritySpecMap{}
86+
87+
for _, pss := range pssList {
88+
pssMap[pss.Metadata] = pss
89+
}
90+
91+
return pssMap
8192
}
8293

83-
func (css ContainerSecuritySpec) ContainCapability(cap string) bool {
84-
m := utils.ArrayToMap(css.Capabilities)
94+
type ContainerSecuritySpecMap map[Metadata]ContainerSecuritySpec
8595

86-
_, exsits := m[cap]
96+
func NewContainerSecuritySpecMap(cssList []ContainerSecuritySpec) ContainerSecuritySpecMap {
97+
cssMap := ContainerSecuritySpecMap{}
98+
for _, css := range cssList {
99+
css.Metadata.Image = css.ImageName
100+
cssMap[css.Metadata] = css
101+
}
87102

88-
return exsits
103+
return cssMap
89104
}

0 commit comments

Comments
 (0)