forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
securitycontextconstraints.go
290 lines (277 loc) · 14.3 KB
/
securitycontextconstraints.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package bootstrappolicy
import (
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/controller/serviceaccount"
)
const (
// SecurityContextConstraintPrivileged is used as the name for the system default privileged scc.
SecurityContextConstraintPrivileged = "privileged"
SecurityContextConstraintPrivilegedDesc = "privileged allows access to all privileged and host features and the ability to run as any user, any group, any fsGroup, and with any SELinux context. WARNING: this is the most relaxed SCC and should be used only for cluster administration. Grant with caution."
// SecurityContextConstraintRestricted is used as the name for the system default restricted scc.
SecurityContextConstraintRestricted = "restricted"
SecurityContextConstraintRestrictedDesc = "restricted denys access to all host features and requires pods to be run with a UID, SELinux context, fs group, and supplemental groups that are allocated to the namespace. This is the most restrictive SCC."
// SecurityContextConstraintNonRoot is used as the name for the system default non-root scc.
SecurityContextConstraintNonRoot = "nonroot"
SecurityContextConstraintNonRootDesc = "nonroot provides all features of the restricted SCC but allows users to run with any non-root UID. The user must specify the UID or it must be specified on the by the manifest of the container runtime."
// SecurityContextConstraintHostMount is used as the name for the system default host mount only scc.
SecurityContextConstraintHostMount = "hostmount"
SecurityContextConstraintHostMountDesc = "hostmount provides all the features of the restricted SCC but allows host mounts by a pod. This is primarily used by the persistent volume recycler. WARNING: this SCC allows host file system access. Grant with caution."
// SecurityContextConstraintHostNS is used as the name for the system default scc
// that grants access to all host ns features.
SecurityContextConstraintHostNS = "hostaccess"
SecurityContextConstraintHostNSDesc = "hostaccess allows access to all host namespaces but still requires pods to be run with a UID, SELinux context, fs group, and supplemental groups that are allocated to the namespace. WARNING: this SCC allows host access to namespaces, file systems, and PIDS. It should only be used by trusted pods. Grant with caution."
// SecurityContextConstraintsAnyUID is used as the name for the system default scc that
// grants access to run as any uid but is still restricted to specific SELinux contexts.
SecurityContextConstraintsAnyUID = "anyuid"
SecurityContextConstraintsAnyUIDDesc = "anyuid provides all features of the restricted SCC but allows users to run with any UID and any GID. This is the default SCC for authenticated users."
// DescriptionAnnotation is the annotation used for attaching descriptions.
DescriptionAnnotation = "kubernetes.io/description"
)
// GetBootstrapSecurityContextConstraints returns the slice of default SecurityContextConstraints
// for system bootstrapping. This method takes additional users and groups that should be added
// to the strategies. Use GetBoostrapSCCAccess to produce the default set of mappings.
func GetBootstrapSecurityContextConstraints(sccNameToAdditionalGroups map[string][]string, sccNameToAdditionalUsers map[string][]string) []kapi.SecurityContextConstraints {
// define priorities here and reference them below so it is easy to see, at a glance
// what we're setting
var (
// this is set to 10 to allow wiggle room for admins to set other priorities without
// having to adjust anyUID.
securityContextConstraintsAnyUIDPriority = 10
)
constraints := []kapi.SecurityContextConstraints{
// SecurityContextConstraintPrivileged allows all access for every field
{
ObjectMeta: kapi.ObjectMeta{
Name: SecurityContextConstraintPrivileged,
Annotations: map[string]string{
DescriptionAnnotation: SecurityContextConstraintPrivilegedDesc,
},
},
AllowPrivilegedContainer: true,
AllowHostDirVolumePlugin: true,
AllowHostNetwork: true,
AllowHostPorts: true,
AllowHostPID: true,
AllowHostIPC: true,
SELinuxContext: kapi.SELinuxContextStrategyOptions{
Type: kapi.SELinuxStrategyRunAsAny,
},
RunAsUser: kapi.RunAsUserStrategyOptions{
Type: kapi.RunAsUserStrategyRunAsAny,
},
FSGroup: kapi.FSGroupStrategyOptions{
Type: kapi.FSGroupStrategyRunAsAny,
},
SupplementalGroups: kapi.SupplementalGroupsStrategyOptions{
Type: kapi.SupplementalGroupsStrategyRunAsAny,
},
},
// SecurityContextConstraintNonRoot does not allow host access, allocates SELinux labels
// and allows the user to request a specific UID or provide the default in the dockerfile.
{
ObjectMeta: kapi.ObjectMeta{
Name: SecurityContextConstraintNonRoot,
Annotations: map[string]string{
DescriptionAnnotation: SecurityContextConstraintNonRootDesc,
},
},
SELinuxContext: kapi.SELinuxContextStrategyOptions{
// This strategy requires that annotations on the namespace which will be populated
// by the admission controller. If namespaces are not annotated creating the strategy
// will fail.
Type: kapi.SELinuxStrategyMustRunAs,
},
RunAsUser: kapi.RunAsUserStrategyOptions{
// This strategy requires that the user request to run as a specific UID or that
// the docker file contain a USER directive.
Type: kapi.RunAsUserStrategyMustRunAsNonRoot,
},
FSGroup: kapi.FSGroupStrategyOptions{
// This strategy requires that annotations on the namespace which will be populated
// by the admission controller. Admission will first look for the SupplementalGroupsAnnotation
// on the namespace and if it is unable to find that annotation it will attempt
// to use the UIDRangeAnnotation. If neither annotation exists then creation
// of the strategy will fail.
Type: kapi.FSGroupStrategyMustRunAs,
},
SupplementalGroups: kapi.SupplementalGroupsStrategyOptions{
// This strategy requires that annotations on the namespace which will be populated
// by the admission controller. Admission will first look for the SupplementalGroupsAnnotation
// on the namespace and if it is unable to find that annotation it will attempt
// to use the UIDRangeAnnotation. If neither annotation exists then creation
// of the strategy will fail.
Type: kapi.SupplementalGroupsStrategyMustRunAs,
},
},
// SecurityContextConstraintHostMount is the same as the restricted scc but allows host mounts.
// Used by the PV recycler.
{
ObjectMeta: kapi.ObjectMeta{
Name: SecurityContextConstraintHostMount,
Annotations: map[string]string{
DescriptionAnnotation: SecurityContextConstraintHostMountDesc,
},
},
AllowHostDirVolumePlugin: true,
SELinuxContext: kapi.SELinuxContextStrategyOptions{
// This strategy requires that annotations on the namespace which will be populated
// by the admission controller. If namespaces are not annotated creating the strategy
// will fail.
Type: kapi.SELinuxStrategyMustRunAs,
},
RunAsUser: kapi.RunAsUserStrategyOptions{
// This strategy requires that annotations on the namespace which will be populated
// by the admission controller. If namespaces are not annotated creating the strategy
// will fail.
Type: kapi.RunAsUserStrategyMustRunAsRange,
},
FSGroup: kapi.FSGroupStrategyOptions{
// This strategy requires that annotations on the namespace which will be populated
// by the admission controller. Admission will first look for the SupplementalGroupsAnnotation
// on the namespace and if it is unable to find that annotation it will attempt
// to use the UIDRangeAnnotation. If neither annotation exists then creation
// of the strategy will fail.
Type: kapi.FSGroupStrategyMustRunAs,
},
SupplementalGroups: kapi.SupplementalGroupsStrategyOptions{
// This strategy requires that annotations on the namespace which will be populated
// by the admission controller. Admission will first look for the SupplementalGroupsAnnotation
// on the namespace and if it is unable to find that annotation it will attempt
// to use the UIDRangeAnnotation. If neither annotation exists then creation
// of the strategy will fail.
Type: kapi.SupplementalGroupsStrategyMustRunAs,
},
},
// SecurityContextConstraintHostNS allows access to everything except privileged on the host
// but still allocates UIDs and SELinux.
{
ObjectMeta: kapi.ObjectMeta{
Name: SecurityContextConstraintHostNS,
Annotations: map[string]string{
DescriptionAnnotation: SecurityContextConstraintHostNSDesc,
},
},
AllowHostDirVolumePlugin: true,
AllowHostNetwork: true,
AllowHostPorts: true,
AllowHostPID: true,
AllowHostIPC: true,
SELinuxContext: kapi.SELinuxContextStrategyOptions{
// This strategy requires that annotations on the namespace which will be populated
// by the admission controller. If namespaces are not annotated creating the strategy
// will fail.
Type: kapi.SELinuxStrategyMustRunAs,
},
RunAsUser: kapi.RunAsUserStrategyOptions{
// This strategy requires that annotations on the namespace which will be populated
// by the admission controller. If namespaces are not annotated creating the strategy
// will fail.
Type: kapi.RunAsUserStrategyMustRunAsRange,
},
FSGroup: kapi.FSGroupStrategyOptions{
// This strategy requires that annotations on the namespace which will be populated
// by the admission controller. Admission will first look for the SupplementalGroupsAnnotation
// on the namespace and if it is unable to find that annotation it will attempt
// to use the UIDRangeAnnotation. If neither annotation exists then creation
// of the strategy will fail.
Type: kapi.FSGroupStrategyMustRunAs,
},
SupplementalGroups: kapi.SupplementalGroupsStrategyOptions{
// This strategy requires that annotations on the namespace which will be populated
// by the admission controller. Admission will first look for the SupplementalGroupsAnnotation
// on the namespace and if it is unable to find that annotation it will attempt
// to use the UIDRangeAnnotation. If neither annotation exists then creation
// of the strategy will fail.
Type: kapi.SupplementalGroupsStrategyMustRunAs,
},
},
// SecurityContextConstraintRestricted allows no host access and allocates UIDs and SELinux.
{
ObjectMeta: kapi.ObjectMeta{
Name: SecurityContextConstraintRestricted,
Annotations: map[string]string{
DescriptionAnnotation: SecurityContextConstraintRestrictedDesc,
},
},
SELinuxContext: kapi.SELinuxContextStrategyOptions{
// This strategy requires that annotations on the namespace which will be populated
// by the admission controller. If namespaces are not annotated creating the strategy
// will fail.
Type: kapi.SELinuxStrategyMustRunAs,
},
RunAsUser: kapi.RunAsUserStrategyOptions{
// This strategy requires that annotations on the namespace which will be populated
// by the admission controller. If namespaces are not annotated creating the strategy
// will fail.
Type: kapi.RunAsUserStrategyMustRunAsRange,
},
FSGroup: kapi.FSGroupStrategyOptions{
// This strategy requires that annotations on the namespace which will be populated
// by the admission controller. Admission will first look for the SupplementalGroupsAnnotation
// on the namespace and if it is unable to find that annotation it will attempt
// to use the UIDRangeAnnotation. If neither annotation exists then creation
// of the strategy will fail.
Type: kapi.FSGroupStrategyMustRunAs,
},
SupplementalGroups: kapi.SupplementalGroupsStrategyOptions{
// This strategy requires that annotations on the namespace which will be populated
// by the admission controller. Admission will first look for the SupplementalGroupsAnnotation
// on the namespace and if it is unable to find that annotation it will attempt
// to use the UIDRangeAnnotation. If neither annotation exists then creation
// of the strategy will fail.
Type: kapi.SupplementalGroupsStrategyMustRunAs,
},
},
// SecurityContextConstraintsAnyUID allows no host access and allocates SELinux.
{
ObjectMeta: kapi.ObjectMeta{
Name: SecurityContextConstraintsAnyUID,
Annotations: map[string]string{
DescriptionAnnotation: SecurityContextConstraintsAnyUIDDesc,
},
},
SELinuxContext: kapi.SELinuxContextStrategyOptions{
// This strategy requires that annotations on the namespace which will be populated
// by the admission controller. If namespaces are not annotated creating the strategy
// will fail.
Type: kapi.SELinuxStrategyMustRunAs,
},
RunAsUser: kapi.RunAsUserStrategyOptions{
Type: kapi.RunAsUserStrategyRunAsAny,
},
FSGroup: kapi.FSGroupStrategyOptions{
Type: kapi.FSGroupStrategyRunAsAny,
},
SupplementalGroups: kapi.SupplementalGroupsStrategyOptions{
Type: kapi.SupplementalGroupsStrategyRunAsAny,
},
// prefer the anyuid SCC over ones that force a uid
Priority: &securityContextConstraintsAnyUIDPriority,
},
}
// add default access
for i, constraint := range constraints {
if usersToAdd, ok := sccNameToAdditionalUsers[constraint.Name]; ok {
constraints[i].Users = append(constraints[i].Users, usersToAdd...)
}
if groupsToAdd, ok := sccNameToAdditionalGroups[constraint.Name]; ok {
constraints[i].Groups = append(constraints[i].Groups, groupsToAdd...)
}
}
return constraints
}
// GetBoostrapSCCAccess provides the default set of access that should be passed to GetBootstrapSecurityContextConstraints.
func GetBoostrapSCCAccess(infraNamespace string) (map[string][]string, map[string][]string) {
groups := map[string][]string{
SecurityContextConstraintPrivileged: {ClusterAdminGroup, NodesGroup},
SecurityContextConstraintsAnyUID: {ClusterAdminGroup},
SecurityContextConstraintRestricted: {AuthenticatedGroup},
}
buildControllerUsername := serviceaccount.MakeUsername(infraNamespace, InfraBuildControllerServiceAccountName)
pvControllerUsername := serviceaccount.MakeUsername(infraNamespace, InfraPersistentVolumeControllerServiceAccountName)
users := map[string][]string{
SecurityContextConstraintPrivileged: {buildControllerUsername},
SecurityContextConstraintHostMount: {pvControllerUsername},
}
return groups, users
}