-
Notifications
You must be signed in to change notification settings - Fork 38.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
use PodSecurityPolicySpec of policy/v1beta1 instead of extensions/v1beta1 #67495
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,6 @@ import ( | |
"fmt" | ||
|
||
"k8s.io/api/core/v1" | ||
extensionsv1beta1 "k8s.io/api/extensions/v1beta1" | ||
policy "k8s.io/api/policy/v1beta1" | ||
rbacv1beta1 "k8s.io/api/rbac/v1beta1" | ||
apierrs "k8s.io/apimachinery/pkg/api/errors" | ||
|
@@ -79,27 +78,9 @@ var _ = SIGDescribe("PodSecurityPolicy", func() { | |
expectForbidden(err) | ||
}) | ||
|
||
// TODO: merge tests for extensions/policy API groups when PSP will be completely moved out of the extensions | ||
|
||
It("should enforce the restricted extensions.PodSecurityPolicy", func() { | ||
By("Creating & Binding a restricted policy for the test service account") | ||
_, cleanup := createAndBindPSP(f, restrictedPSP("restrictive")) | ||
defer cleanup() | ||
|
||
By("Running a restricted pod") | ||
pod, err := c.CoreV1().Pods(ns).Create(restrictedPod("allowed")) | ||
framework.ExpectNoError(err) | ||
framework.ExpectNoError(framework.WaitForPodNameRunningInNamespace(c, pod.Name, pod.Namespace)) | ||
|
||
testPrivilegedPods(func(pod *v1.Pod) { | ||
_, err := c.CoreV1().Pods(ns).Create(pod) | ||
expectForbidden(err) | ||
}) | ||
}) | ||
|
||
It("should enforce the restricted policy.PodSecurityPolicy", func() { | ||
By("Creating & Binding a restricted policy for the test service account") | ||
_, cleanup := createAndBindPSPInPolicy(f, restrictedPSPInPolicy("restrictive")) | ||
_, cleanup := createAndBindPSP(f, restrictedPSP("restrictive")) | ||
defer cleanup() | ||
|
||
By("Running a restricted pod") | ||
|
@@ -113,34 +94,12 @@ var _ = SIGDescribe("PodSecurityPolicy", func() { | |
}) | ||
}) | ||
|
||
It("should allow pods under the privileged extensions.PodSecurityPolicy", func() { | ||
By("Creating & Binding a privileged policy for the test service account") | ||
// Ensure that the permissive policy is used even in the presence of the restricted policy. | ||
_, cleanup := createAndBindPSP(f, restrictedPSP("restrictive")) | ||
defer cleanup() | ||
expectedPSP, cleanup := createAndBindPSP(f, framework.PrivilegedPSP("permissive")) | ||
defer cleanup() | ||
|
||
testPrivilegedPods(func(pod *v1.Pod) { | ||
p, err := c.CoreV1().Pods(ns).Create(pod) | ||
framework.ExpectNoError(err) | ||
framework.ExpectNoError(framework.WaitForPodNameRunningInNamespace(c, p.Name, p.Namespace)) | ||
|
||
// Verify expected PSP was used. | ||
p, err = c.CoreV1().Pods(ns).Get(p.Name, metav1.GetOptions{}) | ||
framework.ExpectNoError(err) | ||
validated, found := p.Annotations[psputil.ValidatedPSPAnnotation] | ||
Expect(found).To(BeTrue(), "PSP annotation not found") | ||
Expect(validated).To(Equal(expectedPSP.Name), "Unexpected validated PSP") | ||
}) | ||
}) | ||
|
||
It("should allow pods under the privileged policy.PodSecurityPolicy", func() { | ||
By("Creating & Binding a privileged policy for the test service account") | ||
// Ensure that the permissive policy is used even in the presence of the restricted policy. | ||
_, cleanup := createAndBindPSPInPolicy(f, restrictedPSPInPolicy("restrictive")) | ||
_, cleanup := createAndBindPSP(f, restrictedPSP("restrictive")) | ||
defer cleanup() | ||
expectedPSP, cleanup := createAndBindPSPInPolicy(f, privilegedPSPInPolicy("permissive")) | ||
expectedPSP, cleanup := createAndBindPSP(f, privilegedPSP("permissive")) | ||
defer cleanup() | ||
|
||
testPrivilegedPods(func(pod *v1.Pod) { | ||
|
@@ -229,49 +188,8 @@ func testPrivilegedPods(tester func(pod *v1.Pod)) { | |
}) | ||
} | ||
|
||
func createAndBindPSP(f *framework.Framework, pspTemplate *extensionsv1beta1.PodSecurityPolicy) (psp *extensionsv1beta1.PodSecurityPolicy, cleanup func()) { | ||
// Create the PodSecurityPolicy object. | ||
psp = pspTemplate.DeepCopy() | ||
// Add the namespace to the name to ensure uniqueness and tie it to the namespace. | ||
ns := f.Namespace.Name | ||
name := fmt.Sprintf("%s-%s", ns, psp.Name) | ||
psp.Name = name | ||
psp, err := f.ClientSet.ExtensionsV1beta1().PodSecurityPolicies().Create(psp) | ||
framework.ExpectNoError(err, "Failed to create PSP") | ||
|
||
// Create the Role to bind it to the namespace. | ||
_, err = f.ClientSet.RbacV1beta1().Roles(ns).Create(&rbacv1beta1.Role{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: name, | ||
}, | ||
Rules: []rbacv1beta1.PolicyRule{{ | ||
APIGroups: []string{"extensions"}, | ||
Resources: []string{"podsecuritypolicies"}, | ||
ResourceNames: []string{name}, | ||
Verbs: []string{"use"}, | ||
}}, | ||
}) | ||
framework.ExpectNoError(err, "Failed to create PSP role") | ||
|
||
// Bind the role to the namespace. | ||
framework.BindRoleInNamespace(f.ClientSet.RbacV1beta1(), name, ns, rbacv1beta1.Subject{ | ||
Kind: rbacv1beta1.ServiceAccountKind, | ||
Namespace: ns, | ||
Name: "default", | ||
}) | ||
framework.ExpectNoError(framework.WaitForNamedAuthorizationUpdate(f.ClientSet.AuthorizationV1beta1(), | ||
serviceaccount.MakeUsername(ns, "default"), ns, "use", name, | ||
schema.GroupResource{Group: "extensions", Resource: "podsecuritypolicies"}, true)) | ||
|
||
return psp, func() { | ||
// Cleanup non-namespaced PSP object. | ||
f.ClientSet.ExtensionsV1beta1().PodSecurityPolicies().Delete(name, &metav1.DeleteOptions{}) | ||
} | ||
} | ||
|
||
// createAndBindPSPInPolicy creates a PSP in the policy API group (unlike createAndBindPSP()). | ||
// TODO: merge these functions when PSP will be completely moved out of the extensions | ||
func createAndBindPSPInPolicy(f *framework.Framework, pspTemplate *policy.PodSecurityPolicy) (psp *policy.PodSecurityPolicy, cleanup func()) { | ||
// createAndBindPSP creates a PSP in the policy API group. | ||
func createAndBindPSP(f *framework.Framework, pspTemplate *policy.PodSecurityPolicy) (psp *policy.PodSecurityPolicy, cleanup func()) { | ||
// Create the PodSecurityPolicy object. | ||
psp = pspTemplate.DeepCopy() | ||
// Add the namespace to the name to ensure uniqueness and tie it to the namespace. | ||
|
@@ -334,8 +252,7 @@ func restrictedPod(name string) *v1.Pod { | |
} | ||
|
||
// privilegedPSPInPolicy creates a PodSecurityPolicy (in the "policy" API Group) that allows everything. | ||
// TODO: replace by PrivilegedPSP when PSP will be completely moved out of the extensions | ||
func privilegedPSPInPolicy(name string) *policy.PodSecurityPolicy { | ||
func privilegedPSP(name string) *policy.PodSecurityPolicy { | ||
return &policy.PodSecurityPolicy{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: name, | ||
|
@@ -368,8 +285,7 @@ func privilegedPSPInPolicy(name string) *policy.PodSecurityPolicy { | |
} | ||
|
||
// restrictedPSPInPolicy creates a PodSecurityPolicy (in the "policy" API Group) that is most strict. | ||
// TODO: replace by restrictedPSP when PSP will be completely moved out of the extensions | ||
func restrictedPSPInPolicy(name string) *policy.PodSecurityPolicy { | ||
func restrictedPSP(name string) *policy.PodSecurityPolicy { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @php-coder Thanks for your review! I removed the old func, e2e tests and renamed func |
||
return &policy.PodSecurityPolicy{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: name, | ||
|
@@ -423,61 +339,6 @@ func restrictedPSPInPolicy(name string) *policy.PodSecurityPolicy { | |
} | ||
} | ||
|
||
// restrictedPSP creates a PodSecurityPolicy that is most strict. | ||
func restrictedPSP(name string) *extensionsv1beta1.PodSecurityPolicy { | ||
return &extensionsv1beta1.PodSecurityPolicy{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: name, | ||
Annotations: map[string]string{ | ||
seccomp.AllowedProfilesAnnotationKey: v1.SeccompProfileRuntimeDefault, | ||
seccomp.DefaultProfileAnnotationKey: v1.SeccompProfileRuntimeDefault, | ||
apparmor.AllowedProfilesAnnotationKey: apparmor.ProfileRuntimeDefault, | ||
apparmor.DefaultProfileAnnotationKey: apparmor.ProfileRuntimeDefault, | ||
}, | ||
}, | ||
Spec: extensionsv1beta1.PodSecurityPolicySpec{ | ||
Privileged: false, | ||
AllowPrivilegeEscalation: utilpointer.BoolPtr(false), | ||
RequiredDropCapabilities: []v1.Capability{ | ||
"AUDIT_WRITE", | ||
"CHOWN", | ||
"DAC_OVERRIDE", | ||
"FOWNER", | ||
"FSETID", | ||
"KILL", | ||
"MKNOD", | ||
"NET_RAW", | ||
"SETGID", | ||
"SETUID", | ||
"SYS_CHROOT", | ||
}, | ||
Volumes: []extensionsv1beta1.FSType{ | ||
extensionsv1beta1.ConfigMap, | ||
extensionsv1beta1.EmptyDir, | ||
extensionsv1beta1.PersistentVolumeClaim, | ||
"projected", | ||
extensionsv1beta1.Secret, | ||
}, | ||
HostNetwork: false, | ||
HostIPC: false, | ||
HostPID: false, | ||
RunAsUser: extensionsv1beta1.RunAsUserStrategyOptions{ | ||
Rule: extensionsv1beta1.RunAsUserStrategyMustRunAsNonRoot, | ||
}, | ||
SELinux: extensionsv1beta1.SELinuxStrategyOptions{ | ||
Rule: extensionsv1beta1.SELinuxStrategyRunAsAny, | ||
}, | ||
SupplementalGroups: extensionsv1beta1.SupplementalGroupsStrategyOptions{ | ||
Rule: extensionsv1beta1.SupplementalGroupsStrategyRunAsAny, | ||
}, | ||
FSGroup: extensionsv1beta1.FSGroupStrategyOptions{ | ||
Rule: extensionsv1beta1.FSGroupStrategyRunAsAny, | ||
}, | ||
ReadOnlyRootFilesystem: false, | ||
}, | ||
} | ||
} | ||
|
||
func boolPtr(b bool) *bool { | ||
return &b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is
createAndBindPSPInPolicy()
function (see below) that do the same. Just use it and remove this function.