|
| 1 | +package generator |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/sysdiglabs/kube-psp-advisor/advisor/types" |
| 7 | +) |
| 8 | + |
| 9 | +var ( |
| 10 | + allowPrivilegeEscalation = true |
| 11 | + notRunAsNonRoot = false |
| 12 | + runAsNonRoot = true |
| 13 | + namespaceTest = "test" |
| 14 | + runAsUser int64 = 100 |
| 15 | + runAsGroup int64 = 1000 |
| 16 | + |
| 17 | + emptyCSSList = []types.ContainerSecuritySpec{} |
| 18 | + |
| 19 | + cssList = []types.ContainerSecuritySpec{ |
| 20 | + {Metadata: types.Metadata{ |
| 21 | + Kind: "Deployment", |
| 22 | + Name: "testDeploy", |
| 23 | + }, |
| 24 | + ContainerName: "containerA", |
| 25 | + ImageName: "imageA", |
| 26 | + Capabilities: []string{"SYS_ADMIN"}, |
| 27 | + Privileged: true, |
| 28 | + ReadOnlyRootFS: false, |
| 29 | + AllowPrivilegeEscalation: &allowPrivilegeEscalation, |
| 30 | + RunAsNonRoot: ¬RunAsNonRoot, |
| 31 | + ServiceAccount: "acctA", |
| 32 | + Namespace: namespaceTest, |
| 33 | + RunAsUser: &runAsUser, |
| 34 | + RunAsGroup: &runAsGroup, |
| 35 | + }, |
| 36 | + {Metadata: types.Metadata{ |
| 37 | + Kind: "Deployment", |
| 38 | + Name: "testDeploy", |
| 39 | + }, |
| 40 | + ContainerName: "containerB", |
| 41 | + ImageName: "imageA", |
| 42 | + Capabilities: []string{"SYS_ADMIN"}, |
| 43 | + Privileged: false, |
| 44 | + ReadOnlyRootFS: true, |
| 45 | + RunAsNonRoot: &runAsNonRoot, |
| 46 | + ServiceAccount: "acctB", |
| 47 | + Namespace: namespaceTest, |
| 48 | + RunAsUser: &runAsUser, |
| 49 | + RunAsGroup: &runAsGroup, |
| 50 | + }, |
| 51 | + } |
| 52 | + |
| 53 | + emptyPSSList = []types.PodSecuritySpec{} |
| 54 | + |
| 55 | + pssList = []types.PodSecuritySpec{ |
| 56 | + {Metadata: types.Metadata{ |
| 57 | + Kind: "Deployment", |
| 58 | + Name: "testDeploy", |
| 59 | + }, |
| 60 | + Namespace: namespaceTest, |
| 61 | + HostIPC: true, |
| 62 | + HostNetwork: true, |
| 63 | + HostPID: true, |
| 64 | + VolumeTypes: []string{"secret", "configMap"}, |
| 65 | + MountHostPaths: map[string]bool{ |
| 66 | + "/proc": true, |
| 67 | + }, |
| 68 | + }, |
| 69 | + {Metadata: types.Metadata{ |
| 70 | + Kind: "Deployment", |
| 71 | + Name: "testDeploy", |
| 72 | + }, |
| 73 | + Namespace: namespaceTest, |
| 74 | + HostIPC: false, |
| 75 | + HostNetwork: false, |
| 76 | + HostPID: false, |
| 77 | + VolumeTypes: []string{"secret", "emptyDir"}, |
| 78 | + MountHostPaths: map[string]bool{ |
| 79 | + "/etc": true, |
| 80 | + }, |
| 81 | + }, |
| 82 | + } |
| 83 | +) |
| 84 | + |
| 85 | +func TestCSS(t *testing.T) { |
| 86 | + gen, _ := NewGenerator() |
| 87 | + |
| 88 | + psp := gen.GeneratePSP(cssList, emptyPSSList, namespaceTest, "v1.12.1") |
| 89 | + |
| 90 | + if !psp.Spec.Privileged { |
| 91 | + t.Fatal("psp should be privileged") |
| 92 | + } |
| 93 | + |
| 94 | + hasSYSADMIN := false |
| 95 | + for _, cap := range psp.Spec.AllowedCapabilities { |
| 96 | + if string(cap) == "SYS_ADMIN" { |
| 97 | + hasSYSADMIN = true |
| 98 | + break |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if !hasSYSADMIN { |
| 103 | + t.Fatal("psp should have SYS_ADMIN in capabilities") |
| 104 | + } |
| 105 | + |
| 106 | + if !psp.Spec.ReadOnlyRootFilesystem { |
| 107 | + t.Fatal("psp should have readonlyrootsystem to false") |
| 108 | + } |
| 109 | + |
| 110 | + if psp.Spec.AllowPrivilegeEscalation != nil && !*psp.Spec.AllowPrivilegeEscalation { |
| 111 | + t.Fatal("psp should have allowPrivilegeEscalation to true") |
| 112 | + } |
| 113 | + |
| 114 | + if psp.Spec.RunAsUser.Ranges[0].Min != runAsUser && psp.Spec.RunAsUser.Ranges[0].Max != runAsUser { |
| 115 | + t.Fatal("psp should have set run as user to 100") |
| 116 | + } |
| 117 | + |
| 118 | + if psp.Spec.RunAsGroup.Ranges[0].Min != runAsGroup && psp.Spec.RunAsGroup.Ranges[0].Max != runAsGroup { |
| 119 | + t.Fatal("psp should have set run as group to 1000") |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func TestPSS(t *testing.T) { |
| 124 | + gen, _ := NewGenerator() |
| 125 | + |
| 126 | + psp := gen.GeneratePSP(emptyCSSList, pssList, namespaceTest, "v1.12.1") |
| 127 | + |
| 128 | + if !psp.Spec.HostPID { |
| 129 | + t.Fatal("psp should allow hostPID") |
| 130 | + } |
| 131 | + |
| 132 | + if !psp.Spec.HostNetwork { |
| 133 | + t.Fatal("psp should allow hostNetwork") |
| 134 | + } |
| 135 | + |
| 136 | + if !psp.Spec.HostIPC { |
| 137 | + t.Fatal("psp should allow hostIPC") |
| 138 | + } |
| 139 | + |
| 140 | + volMap := map[string]bool{} |
| 141 | + |
| 142 | + for _, fs := range psp.Spec.Volumes { |
| 143 | + volMap[string(fs)] = true |
| 144 | + } |
| 145 | + |
| 146 | + if _, exists := volMap["secret"]; !exists { |
| 147 | + t.Fatal("psp should allow mount secret") |
| 148 | + } |
| 149 | + |
| 150 | + if _, exists := volMap["configMap"]; !exists { |
| 151 | + t.Fatal("psp should allow mount configMap") |
| 152 | + } |
| 153 | + |
| 154 | + if _, exists := volMap["emptyDir"]; !exists { |
| 155 | + t.Fatal("psp should allow mount emptyDir") |
| 156 | + } |
| 157 | + |
| 158 | + if len(volMap) > 3 { |
| 159 | + t.Fatal("psp allow more volume types than needed") |
| 160 | + } |
| 161 | + |
| 162 | + hpMap := map[string]bool{} |
| 163 | + for _, hp := range psp.Spec.AllowedHostPaths { |
| 164 | + hpMap[hp.PathPrefix] = true |
| 165 | + } |
| 166 | + |
| 167 | + if _, exists := hpMap["/proc"]; !exists { |
| 168 | + t.Fatal("psp shoud allow mount on hostpath /proc") |
| 169 | + } |
| 170 | + |
| 171 | + if _, exists := hpMap["/etc"]; !exists { |
| 172 | + t.Fatal("psp shoud allow mount on hostpath /etc") |
| 173 | + } |
| 174 | + |
| 175 | + if len(hpMap) > 2 { |
| 176 | + t.Fatal("psp allow more host path mount than needed") |
| 177 | + } |
| 178 | +} |
0 commit comments