-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
byrestrictions_test.go
174 lines (160 loc) · 5.18 KB
/
byrestrictions_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package scc
import (
"testing"
securityapi "github.com/openshift/origin/pkg/security/api"
)
func TestPointValue(t *testing.T) {
newSCC := func(priv bool, seLinuxStrategy securityapi.SELinuxContextStrategyType, userStrategy securityapi.RunAsUserStrategyType) *securityapi.SecurityContextConstraints {
scc := &securityapi.SecurityContextConstraints{
SELinuxContext: securityapi.SELinuxContextStrategyOptions{
Type: seLinuxStrategy,
},
RunAsUser: securityapi.RunAsUserStrategyOptions{
Type: userStrategy,
},
}
if priv {
scc.AllowPrivilegedContainer = true
}
return scc
}
seLinuxStrategies := map[securityapi.SELinuxContextStrategyType]int{
securityapi.SELinuxStrategyRunAsAny: 4,
securityapi.SELinuxStrategyMustRunAs: 1,
}
userStrategies := map[securityapi.RunAsUserStrategyType]int{
securityapi.RunAsUserStrategyRunAsAny: 4,
securityapi.RunAsUserStrategyMustRunAsNonRoot: 3,
securityapi.RunAsUserStrategyMustRunAsRange: 2,
securityapi.RunAsUserStrategyMustRunAs: 1,
}
privilegedPoints := 20
// run through all combos of user strategy + seLinux strategy + priv
for userStrategy, userStrategyPoints := range userStrategies {
for seLinuxStrategy, seLinuxStrategyPoints := range seLinuxStrategies {
expectedPoints := privilegedPoints + userStrategyPoints + seLinuxStrategyPoints
scc := newSCC(true, seLinuxStrategy, userStrategy)
actualPoints := pointValue(scc)
if actualPoints != expectedPoints {
t.Errorf("privileged, user: %v, seLinux %v expected %d score but got %d", userStrategy, seLinuxStrategy, expectedPoints, actualPoints)
}
expectedPoints = userStrategyPoints + seLinuxStrategyPoints
scc = newSCC(false, seLinuxStrategy, userStrategy)
actualPoints = pointValue(scc)
if actualPoints != expectedPoints {
t.Errorf("non privileged, user: %v, seLinux %v expected %d score but got %d", userStrategy, seLinuxStrategy, expectedPoints, actualPoints)
}
}
}
// sanity check to ensure volume score is added (specific volumes scores are tested below
scc := newSCC(false, securityapi.SELinuxStrategyMustRunAs, securityapi.RunAsUserStrategyMustRunAs)
scc.Volumes = []securityapi.FSType{securityapi.FSTypeHostPath}
actualPoints := pointValue(scc)
if actualPoints != 12 { //1 (SELinux) + 1 (User) + 10 (host path volume)
t.Errorf("volume score was not added to the scc point value correctly!")
}
}
func TestVolumePointValue(t *testing.T) {
newSCC := func(host, nonTrivial, trivial bool) *securityapi.SecurityContextConstraints {
volumes := []securityapi.FSType{}
if host {
volumes = append(volumes, securityapi.FSTypeHostPath)
}
if nonTrivial {
volumes = append(volumes, securityapi.FSTypeAWSElasticBlockStore)
}
if trivial {
volumes = append(volumes, securityapi.FSTypeSecret)
}
return &securityapi.SecurityContextConstraints{
Volumes: volumes,
}
}
allowAllSCC := &securityapi.SecurityContextConstraints{
Volumes: []securityapi.FSType{securityapi.FSTypeAll},
}
nilVolumeSCC := &securityapi.SecurityContextConstraints{}
tests := map[string]struct {
scc *securityapi.SecurityContextConstraints
expectedPoints int
}{
"all volumes": {
scc: allowAllSCC,
expectedPoints: 10,
},
"host volume": {
scc: newSCC(true, false, false),
expectedPoints: 10,
},
"host volume and non trivial volumes": {
scc: newSCC(true, true, false),
expectedPoints: 10,
},
"host volume, non trivial, and trivial": {
scc: newSCC(true, true, true),
expectedPoints: 10,
},
"non trivial": {
scc: newSCC(false, true, false),
expectedPoints: 5,
},
"non trivial and trivial": {
scc: newSCC(false, true, true),
expectedPoints: 5,
},
"trivial": {
scc: newSCC(false, false, true),
expectedPoints: 0,
},
"trivial - secret": {
scc: &securityapi.SecurityContextConstraints{
Volumes: []securityapi.FSType{securityapi.FSTypeSecret},
},
expectedPoints: 0,
},
"trivial - configMap": {
scc: &securityapi.SecurityContextConstraints{
Volumes: []securityapi.FSType{securityapi.FSTypeConfigMap},
},
expectedPoints: 0,
},
"trivial - emptyDir": {
scc: &securityapi.SecurityContextConstraints{
Volumes: []securityapi.FSType{securityapi.FSTypeEmptyDir},
},
expectedPoints: 0,
},
"trivial - downwardAPI": {
scc: &securityapi.SecurityContextConstraints{
Volumes: []securityapi.FSType{securityapi.FSTypeDownwardAPI},
},
expectedPoints: 0,
},
"trivial - projected": {
scc: &securityapi.SecurityContextConstraints{
Volumes: []securityapi.FSType{securityapi.FSProjected},
},
expectedPoints: 0,
},
"trivial - none": {
scc: &securityapi.SecurityContextConstraints{
Volumes: []securityapi.FSType{securityapi.FSTypeNone},
},
expectedPoints: 0,
},
"no volumes allowed": {
scc: newSCC(false, false, false),
expectedPoints: 0,
},
"nil volumes": {
scc: nilVolumeSCC,
expectedPoints: 0,
},
}
for k, v := range tests {
actualPoints := volumePointValue(v.scc)
if actualPoints != v.expectedPoints {
t.Errorf("%s expected %d volume score but got %d", k, v.expectedPoints, actualPoints)
}
}
}