Skip to content

Commit f0e337c

Browse files
author
Kubernetes Submit Queue
authored
Merge pull request kubernetes#53179 from wanghaoran1988/psp_flexvolume
Automatic merge from submit-queue (batch tested with PRs 55824, 53179). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. Allow Pod Security Policy to manage access to the Flexvolumes **What this PR does / why we need it**: For proposal: https://github.com/kubernetes/community/blob/a1b9495e1b722699196ccec88d831fc850100827/contributors/design-proposals/auth/flex-volumes-drivers-psp.md (kubernetes/community#723) **Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes # **Special notes for your reviewer**: **Release note**: ```release-note Pod Security Policy can now manage access to specific FlexVolume drivers ```
2 parents 6a1d336 + e297a81 commit f0e337c

File tree

16 files changed

+793
-278
lines changed

16 files changed

+793
-278
lines changed

api/openapi-spec/swagger.json

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/swagger-spec/extensions_v1beta1.json

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/api-reference/extensions/v1beta1/definitions.html

+41
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/extensions/types.go

+11
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,11 @@ type PodSecurityPolicySpec struct {
860860
// AllowedHostPaths is a white list of allowed host paths. Empty indicates that all host paths may be used.
861861
// +optional
862862
AllowedHostPaths []AllowedHostPath
863+
// AllowedFlexVolumes is a whitelist of allowed Flexvolumes. Empty or nil indicates that all
864+
// Flexvolumes may be used. This parameter is effective only when the usage of the Flexvolumes
865+
// is allowed in the "Volumes" field.
866+
// +optional
867+
AllowedFlexVolumes []AllowedFlexVolume
863868
}
864869

865870
// AllowedHostPath defines the host volume conditions that will be enabled by a policy
@@ -923,6 +928,12 @@ var (
923928
All FSType = "*"
924929
)
925930

931+
// AllowedFlexVolume represents a single Flexvolume that is allowed to be used.
932+
type AllowedFlexVolume struct {
933+
// Driver is the name of the Flexvolume driver.
934+
Driver string
935+
}
936+
926937
// SELinuxStrategyOptions defines the strategy type and any options used to create the strategy.
927938
type SELinuxStrategyOptions struct {
928939
// Rule is the strategy that will dictate the allowable labels that may be set.

pkg/apis/extensions/v1beta1/zz_generated.conversion.go

+24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/extensions/validation/validation.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,7 @@ func ValidatePodSecurityPolicySpec(spec *extensions.PodSecurityPolicySpec, fldPa
655655
allErrs = append(allErrs, validatePSPCapsAgainstDrops(spec.RequiredDropCapabilities, spec.AllowedCapabilities, field.NewPath("allowedCapabilities"))...)
656656
allErrs = append(allErrs, validatePSPDefaultAllowPrivilegeEscalation(fldPath.Child("defaultAllowPrivilegeEscalation"), spec.DefaultAllowPrivilegeEscalation, spec.AllowPrivilegeEscalation)...)
657657
allErrs = append(allErrs, validatePSPAllowedHostPaths(fldPath.Child("allowedHostPaths"), spec.AllowedHostPaths)...)
658+
allErrs = append(allErrs, validatePSPAllowedFlexVolumes(fldPath.Child("allowedFlexVolumes"), spec.AllowedFlexVolumes)...)
658659

659660
return allErrs
660661
}
@@ -721,6 +722,20 @@ func validatePSPAllowedHostPaths(fldPath *field.Path, allowedHostPaths []extensi
721722
return allErrs
722723
}
723724

725+
// validatePSPAllowedFlexVolumes
726+
func validatePSPAllowedFlexVolumes(fldPath *field.Path, flexVolumes []extensions.AllowedFlexVolume) field.ErrorList {
727+
allErrs := field.ErrorList{}
728+
if len(flexVolumes) > 0 {
729+
for idx, fv := range flexVolumes {
730+
if len(fv.Driver) == 0 {
731+
allErrs = append(allErrs, field.Required(fldPath.Child("allowedFlexVolumes").Index(idx).Child("driver"),
732+
"must specify a driver"))
733+
}
734+
}
735+
}
736+
return allErrs
737+
}
738+
724739
// validatePSPSELinux validates the SELinux fields of PodSecurityPolicy.
725740
func validatePSPSELinux(fldPath *field.Path, seLinux *extensions.SELinuxStrategyOptions) field.ErrorList {
726741
allErrs := field.ErrorList{}
@@ -802,7 +817,6 @@ func validatePodSecurityPolicyVolumes(fldPath *field.Path, volumes []extensions.
802817
allErrs = append(allErrs, field.NotSupported(fldPath.Child("volumes"), v, allowed.List()))
803818
}
804819
}
805-
806820
return allErrs
807821
}
808822

pkg/apis/extensions/validation/validation_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -2450,6 +2450,13 @@ func TestValidatePodSecurityPolicy(t *testing.T) {
24502450
pe := true
24512451
invalidDefaultAllowPrivilegeEscalation.Spec.DefaultAllowPrivilegeEscalation = &pe
24522452

2453+
emptyFlexDriver := validPSP()
2454+
emptyFlexDriver.Spec.Volumes = []extensions.FSType{extensions.FlexVolume}
2455+
emptyFlexDriver.Spec.AllowedFlexVolumes = []extensions.AllowedFlexVolume{{}}
2456+
2457+
nonEmptyFlexVolumes := validPSP()
2458+
nonEmptyFlexVolumes.Spec.AllowedFlexVolumes = []extensions.AllowedFlexVolume{{Driver: "example/driver"}}
2459+
24532460
type testCase struct {
24542461
psp *extensions.PodSecurityPolicy
24552462
errorType field.ErrorType
@@ -2581,6 +2588,11 @@ func TestValidatePodSecurityPolicy(t *testing.T) {
25812588
errorType: field.ErrorTypeInvalid,
25822589
errorDetail: "must not contain '..'",
25832590
},
2591+
"empty flex volume driver": {
2592+
psp: emptyFlexDriver,
2593+
errorType: field.ErrorTypeRequired,
2594+
errorDetail: "must specify a driver",
2595+
},
25842596
}
25852597

25862598
for k, v := range errorCases {
@@ -2660,6 +2672,17 @@ func TestValidatePodSecurityPolicy(t *testing.T) {
26602672
validDefaultAllowPrivilegeEscalation.Spec.DefaultAllowPrivilegeEscalation = &pe
26612673
validDefaultAllowPrivilegeEscalation.Spec.AllowPrivilegeEscalation = true
26622674

2675+
flexvolumeWhenFlexVolumesAllowed := validPSP()
2676+
flexvolumeWhenFlexVolumesAllowed.Spec.Volumes = []extensions.FSType{extensions.FlexVolume}
2677+
flexvolumeWhenFlexVolumesAllowed.Spec.AllowedFlexVolumes = []extensions.AllowedFlexVolume{
2678+
{Driver: "example/driver1"},
2679+
}
2680+
2681+
flexvolumeWhenAllVolumesAllowed := validPSP()
2682+
flexvolumeWhenAllVolumesAllowed.Spec.Volumes = []extensions.FSType{extensions.All}
2683+
flexvolumeWhenAllVolumesAllowed.Spec.AllowedFlexVolumes = []extensions.AllowedFlexVolume{
2684+
{Driver: "example/driver2"},
2685+
}
26632686
successCases := map[string]struct {
26642687
psp *extensions.PodSecurityPolicy
26652688
}{
@@ -2690,6 +2713,12 @@ func TestValidatePodSecurityPolicy(t *testing.T) {
26902713
"valid defaultAllowPrivilegeEscalation as true": {
26912714
psp: validDefaultAllowPrivilegeEscalation,
26922715
},
2716+
"allow white-listed flexVolume when flex volumes are allowed": {
2717+
psp: flexvolumeWhenFlexVolumesAllowed,
2718+
},
2719+
"allow white-listed flexVolume when all volumes are allowed": {
2720+
psp: flexvolumeWhenAllVolumesAllowed,
2721+
},
26932722
}
26942723

26952724
for k, v := range successCases {

pkg/apis/extensions/zz_generated.deepcopy.go

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/printers/internalversion/describe.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -3386,6 +3386,9 @@ func describePodSecurityPolicy(psp *extensions.PodSecurityPolicy) (string, error
33863386
w.Write(LEVEL_1, "Allowed Capabilities:\t%s\n", capsToString(psp.Spec.AllowedCapabilities))
33873387
w.Write(LEVEL_1, "Allowed Volume Types:\t%s\n", fsTypeToString(psp.Spec.Volumes))
33883388

3389+
if len(psp.Spec.AllowedFlexVolumes) > 0 {
3390+
w.Write(LEVEL_1, "Allowed FlexVolume Types:\t%s\n", flexVolumesToString(psp.Spec.AllowedFlexVolumes))
3391+
}
33893392
w.Write(LEVEL_1, "Allow Host Network:\t%t\n", psp.Spec.HostNetwork)
33903393
w.Write(LEVEL_1, "Allow Host Ports:\t%s\n", hostPortRangeToString(psp.Spec.HostPorts))
33913394
w.Write(LEVEL_1, "Allow Host PID:\t%t\n", psp.Spec.HostPID)
@@ -3419,10 +3422,14 @@ func describePodSecurityPolicy(psp *extensions.PodSecurityPolicy) (string, error
34193422
}
34203423

34213424
func stringOrNone(s string) string {
3425+
return stringOrDefaultValue(s, "<none>")
3426+
}
3427+
3428+
func stringOrDefaultValue(s, defaultValue string) string {
34223429
if len(s) > 0 {
34233430
return s
34243431
}
3425-
return "<none>"
3432+
return defaultValue
34263433
}
34273434

34283435
func fsTypeToString(volumes []extensions.FSType) string {
@@ -3433,6 +3440,14 @@ func fsTypeToString(volumes []extensions.FSType) string {
34333440
return stringOrNone(strings.Join(strVolumes, ","))
34343441
}
34353442

3443+
func flexVolumesToString(flexVolumes []extensions.AllowedFlexVolume) string {
3444+
volumes := []string{}
3445+
for _, flexVolume := range flexVolumes {
3446+
volumes = append(volumes, "driver="+flexVolume.Driver)
3447+
}
3448+
return stringOrDefaultValue(strings.Join(volumes, ","), "<all>")
3449+
}
3450+
34363451
func hostPortRangeToString(ranges []extensions.HostPortRange) string {
34373452
formattedString := ""
34383453
if ranges != nil {

pkg/security/podsecuritypolicy/provider.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,24 @@ func (s *simpleProvider) ValidatePodSecurityContext(pod *api.Pod, fldPath *field
233233
fmt.Sprintf("is not allowed to be used")))
234234
}
235235
}
236+
237+
if fsType == extensions.FlexVolume && len(s.psp.Spec.AllowedFlexVolumes) > 0 {
238+
found := false
239+
driver := v.FlexVolume.Driver
240+
for _, allowedFlexVolume := range s.psp.Spec.AllowedFlexVolumes {
241+
if driver == allowedFlexVolume.Driver {
242+
found = true
243+
break
244+
}
245+
}
246+
if !found {
247+
allErrs = append(allErrs,
248+
field.Invalid(fldPath.Child("volumes").Index(i).Child("driver"), driver,
249+
"Flexvolume driver is not allowed to be used"))
250+
}
251+
}
236252
}
237253
}
238-
239254
return allErrs
240255
}
241256

0 commit comments

Comments
 (0)