Skip to content

Commit ccfea04

Browse files
committed
SecurityContextConstraints: add AllowedFlexDrivers field.
1 parent 4443818 commit ccfea04

File tree

4 files changed

+48
-11
lines changed

4 files changed

+48
-11
lines changed

pkg/oc/cli/describe/describer.go

+8
Original file line numberDiff line numberDiff line change
@@ -1810,6 +1810,7 @@ func describeSecurityContextConstraints(scc *securityapi.SecurityContextConstrai
18101810
fmt.Fprintf(out, " Allowed Capabilities:\t%s\n", capsToString(scc.AllowedCapabilities))
18111811
fmt.Fprintf(out, " Allowed Seccomp Profiles:\t%s\n", stringOrNone(strings.Join(scc.SeccompProfiles, ",")))
18121812
fmt.Fprintf(out, " Allowed Volume Types:\t%s\n", fsTypeToString(scc.Volumes))
1813+
fmt.Fprintf(out, " Allowed Flexvolume Drivers:\t%s\n", stringOrDefaultValue(strings.Join(scc.AllowedFlexDrivers, ","), "<all>"))
18131814
fmt.Fprintf(out, " Allow Host Network:\t%t\n", scc.AllowHostNetwork)
18141815
fmt.Fprintf(out, " Allow Host Ports:\t%t\n", scc.AllowHostPorts)
18151816
fmt.Fprintf(out, " Allow Host PID:\t%t\n", scc.AllowHostPID)
@@ -1865,6 +1866,13 @@ func stringOrNone(s string) string {
18651866
return "<none>"
18661867
}
18671868

1869+
func stringOrDefaultValue(s, defaultValue string) string {
1870+
if len(s) > 0 {
1871+
return s
1872+
}
1873+
return defaultValue
1874+
}
1875+
18681876
func fsTypeToString(volumes []securityapi.FSType) string {
18691877
strVolumes := []string{}
18701878
for _, v := range volumes {

pkg/security/apis/security/types.go

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ type SecurityContextConstraints struct {
4242
// of a VolumeSource (azureFile, configMap, emptyDir). To allow all volumes you may use "*".
4343
// To allow no volumes, set to ["none"].
4444
Volumes []FSType
45+
// AllowedFlexDrivers is a whitelist of allowed Flexvolume drivers.
46+
// Empty or nil indicates that all drivers may be used.
47+
// +optional
48+
AllowedFlexDrivers []string
4549
// AllowHostNetwork determines if the policy allows the use of HostNetwork in the pod spec.
4650
AllowHostNetwork bool
4751
// AllowHostPorts determines if the policy allows host ports in the containers.

pkg/security/apis/security/v1/types.go

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ type SecurityContextConstraints struct {
4747
// of a VolumeSource (azureFile, configMap, emptyDir). To allow all volumes you may use "*".
4848
// To allow no volumes, set to ["none"].
4949
Volumes []FSType `json:"volumes" protobuf:"bytes,8,rep,name=volumes,casttype=FSType"`
50+
// AllowedFlexDrivers is a whitelist of allowed Flexvolume drivers.
51+
// Empty or nil indicates that all drivers may be used.
52+
AllowedFlexDrivers []string `json:"allowedFlexDrivers" protobuf:"bytes,21,opt,name=allowedFlexDrivers"`
5053
// AllowHostNetwork determines if the policy allows the use of HostNetwork in the pod spec.
5154
AllowHostNetwork bool `json:"allowHostNetwork" protobuf:"varint,9,opt,name=allowHostNetwork"`
5255
// AllowHostPorts determines if the policy allows host ports in the containers.

pkg/security/securitycontextconstraints/provider.go

+33-11
Original file line numberDiff line numberDiff line change
@@ -292,19 +292,41 @@ func (s *simpleProvider) ValidateContainerSecurityContext(pod *api.Pod, containe
292292

293293
allErrs = append(allErrs, s.capabilitiesStrategy.Validate(pod, container)...)
294294

295-
if len(pod.Spec.Volumes) > 0 && !sccutil.SCCAllowsAllVolumes(s.scc) {
296-
allowedVolumes := sccutil.FSTypeToStringSet(s.scc.Volumes)
297-
for i, v := range pod.Spec.Volumes {
298-
fsType, err := sccutil.GetVolumeFSType(v)
299-
if err != nil {
300-
allErrs = append(allErrs, field.Invalid(fldPath.Child("volumes").Index(i), string(fsType), err.Error()))
301-
continue
295+
if len(pod.Spec.Volumes) > 0 {
296+
if !sccutil.SCCAllowsAllVolumes(s.scc) {
297+
allowedVolumes := sccutil.FSTypeToStringSet(s.scc.Volumes)
298+
for i, v := range pod.Spec.Volumes {
299+
fsType, err := sccutil.GetVolumeFSType(v)
300+
if err != nil {
301+
allErrs = append(allErrs, field.Invalid(fldPath.Child("volumes").Index(i), string(fsType), err.Error()))
302+
continue
303+
}
304+
305+
if !allowedVolumes.Has(string(fsType)) {
306+
allErrs = append(allErrs, field.Invalid(
307+
fldPath.Child("volumes").Index(i), string(fsType),
308+
fmt.Sprintf("%s volumes are not allowed to be used", string(fsType))))
309+
}
302310
}
311+
}
303312

304-
if !allowedVolumes.Has(string(fsType)) {
305-
allErrs = append(allErrs, field.Invalid(
306-
fldPath.Child("volumes").Index(i), string(fsType),
307-
fmt.Sprintf("%s volumes are not allowed to be used", string(fsType))))
313+
if len(s.scc.AllowedFlexDrivers) > 0 && sccutil.SCCAllowsFSType(s.scc, securityapi.FSTypeFlexVolume) {
314+
for i, v := range pod.Spec.Volumes {
315+
if v.FlexVolume == nil {
316+
continue
317+
}
318+
319+
found := false
320+
driver := v.FlexVolume.Driver
321+
for _, allowedDriver := range s.scc.AllowedFlexDrivers {
322+
if driver == allowedDriver {
323+
found = true
324+
break
325+
}
326+
}
327+
if !found {
328+
allErrs = append(allErrs, field.Invalid(fldPath.Child("volumes").Index(i), driver, "Flexvolume driver is not allowed to be used"))
329+
}
308330
}
309331
}
310332
}

0 commit comments

Comments
 (0)