mirror of
				https://github.com/k3s-io/kubernetes.git
				synced 2025-11-03 23:40:03 +00:00 
			
		
		
		
	CreatePodSecurityContext: rename; modify its arguments instead of returning a copy.
This commit is contained in:
		@@ -64,17 +64,16 @@ func NewSimpleProvider(psp *extensions.PodSecurityPolicy, namespace string, stra
 | 
				
			|||||||
	}, nil
 | 
						}, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Create a PodSecurityContext based on the given constraints.  If a setting is already set
 | 
					// DefaultPodSecurityContext sets the default values of the required but not filled fields.
 | 
				
			||||||
// on the PodSecurityContext it will not be changed.  Validate should be used after the context
 | 
					// It modifies the SecurityContext and annotations of the provided pod. Validation should be
 | 
				
			||||||
// is created to ensure it complies with the required restrictions.
 | 
					// used after the context is defaulted to ensure it complies with the required restrictions.
 | 
				
			||||||
func (s *simpleProvider) CreatePodSecurityContext(pod *api.Pod) (*api.PodSecurityContext, map[string]string, error) {
 | 
					func (s *simpleProvider) DefaultPodSecurityContext(pod *api.Pod) error {
 | 
				
			||||||
	sc := securitycontext.NewPodSecurityContextMutator(pod.Spec.SecurityContext)
 | 
						sc := securitycontext.NewPodSecurityContextMutator(pod.Spec.SecurityContext)
 | 
				
			||||||
	annotations := maps.CopySS(pod.Annotations)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if sc.SupplementalGroups() == nil {
 | 
						if sc.SupplementalGroups() == nil {
 | 
				
			||||||
		supGroups, err := s.strategies.SupplementalGroupStrategy.Generate(pod)
 | 
							supGroups, err := s.strategies.SupplementalGroupStrategy.Generate(pod)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return nil, nil, err
 | 
								return err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		sc.SetSupplementalGroups(supGroups)
 | 
							sc.SetSupplementalGroups(supGroups)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -82,7 +81,7 @@ func (s *simpleProvider) CreatePodSecurityContext(pod *api.Pod) (*api.PodSecurit
 | 
				
			|||||||
	if sc.FSGroup() == nil {
 | 
						if sc.FSGroup() == nil {
 | 
				
			||||||
		fsGroup, err := s.strategies.FSGroupStrategy.GenerateSingle(pod)
 | 
							fsGroup, err := s.strategies.FSGroupStrategy.GenerateSingle(pod)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return nil, nil, err
 | 
								return err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		sc.SetFSGroup(fsGroup)
 | 
							sc.SetFSGroup(fsGroup)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -90,24 +89,27 @@ func (s *simpleProvider) CreatePodSecurityContext(pod *api.Pod) (*api.PodSecurit
 | 
				
			|||||||
	if sc.SELinuxOptions() == nil {
 | 
						if sc.SELinuxOptions() == nil {
 | 
				
			||||||
		seLinux, err := s.strategies.SELinuxStrategy.Generate(pod, nil)
 | 
							seLinux, err := s.strategies.SELinuxStrategy.Generate(pod, nil)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return nil, nil, err
 | 
								return err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		sc.SetSELinuxOptions(seLinux)
 | 
							sc.SetSELinuxOptions(seLinux)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// This is only generated on the pod level.  Containers inherit the pod's profile.  If the
 | 
						// This is only generated on the pod level.  Containers inherit the pod's profile.  If the
 | 
				
			||||||
	// container has a specific profile set then it will be caught in the validation step.
 | 
						// container has a specific profile set then it will be caught in the validation step.
 | 
				
			||||||
	seccompProfile, err := s.strategies.SeccompStrategy.Generate(annotations, pod)
 | 
						seccompProfile, err := s.strategies.SeccompStrategy.Generate(pod.Annotations, pod)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, nil, err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if seccompProfile != "" {
 | 
						if seccompProfile != "" {
 | 
				
			||||||
		if annotations == nil {
 | 
							if pod.Annotations == nil {
 | 
				
			||||||
			annotations = map[string]string{}
 | 
								pod.Annotations = map[string]string{}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		annotations[api.SeccompPodAnnotationKey] = seccompProfile
 | 
							pod.Annotations[api.SeccompPodAnnotationKey] = seccompProfile
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return sc.PodSecurityContext(), annotations, nil
 | 
					
 | 
				
			||||||
 | 
						pod.Spec.SecurityContext = sc.PodSecurityContext()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Create a SecurityContext based on the given constraints.  If a setting is already set on the
 | 
					// Create a SecurityContext based on the given constraints.  If a setting is already set on the
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -38,7 +38,7 @@ import (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
const defaultContainerName = "test-c"
 | 
					const defaultContainerName = "test-c"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestCreatePodSecurityContextNonmutating(t *testing.T) {
 | 
					func TestDefaultPodSecurityContextNonmutating(t *testing.T) {
 | 
				
			||||||
	// Create a pod with a security context that needs filling in
 | 
						// Create a pod with a security context that needs filling in
 | 
				
			||||||
	createPod := func() *api.Pod {
 | 
						createPod := func() *api.Pod {
 | 
				
			||||||
		return &api.Pod{
 | 
							return &api.Pod{
 | 
				
			||||||
@@ -82,7 +82,7 @@ func TestCreatePodSecurityContextNonmutating(t *testing.T) {
 | 
				
			|||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		t.Fatalf("unable to create provider %v", err)
 | 
							t.Fatalf("unable to create provider %v", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	_, _, err = provider.CreatePodSecurityContext(pod)
 | 
						err = provider.DefaultPodSecurityContext(pod)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		t.Fatalf("unable to create psc %v", err)
 | 
							t.Fatalf("unable to create psc %v", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -91,10 +91,10 @@ func TestCreatePodSecurityContextNonmutating(t *testing.T) {
 | 
				
			|||||||
	// since all the strategies were permissive
 | 
						// since all the strategies were permissive
 | 
				
			||||||
	if !reflect.DeepEqual(createPod(), pod) {
 | 
						if !reflect.DeepEqual(createPod(), pod) {
 | 
				
			||||||
		diffs := diff.ObjectDiff(createPod(), pod)
 | 
							diffs := diff.ObjectDiff(createPod(), pod)
 | 
				
			||||||
		t.Errorf("pod was mutated by CreatePodSecurityContext. diff:\n%s", diffs)
 | 
							t.Errorf("pod was mutated by DefaultPodSecurityContext. diff:\n%s", diffs)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if !reflect.DeepEqual(createPSP(), psp) {
 | 
						if !reflect.DeepEqual(createPSP(), psp) {
 | 
				
			||||||
		t.Error("psp was mutated by CreatePodSecurityContext")
 | 
							t.Error("psp was mutated by DefaultPodSecurityContext")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -32,9 +32,9 @@ import (
 | 
				
			|||||||
// Provider provides the implementation to generate a new security
 | 
					// Provider provides the implementation to generate a new security
 | 
				
			||||||
// context based on constraints or validate an existing security context against constraints.
 | 
					// context based on constraints or validate an existing security context against constraints.
 | 
				
			||||||
type Provider interface {
 | 
					type Provider interface {
 | 
				
			||||||
	// Create a PodSecurityContext based on the given constraints. Also returns an updated set
 | 
						// DefaultPodSecurityContext sets the default values of the required but not filled fields.
 | 
				
			||||||
	// of Pod annotations for alpha feature support.
 | 
						// It modifies the SecurityContext and annotations of the provided pod.
 | 
				
			||||||
	CreatePodSecurityContext(pod *api.Pod) (*api.PodSecurityContext, map[string]string, error)
 | 
						DefaultPodSecurityContext(pod *api.Pod) error
 | 
				
			||||||
	// Create a container SecurityContext based on the given constraints. Also returns an updated set
 | 
						// Create a container SecurityContext based on the given constraints. Also returns an updated set
 | 
				
			||||||
	// of Pod annotations for alpha feature support.
 | 
						// of Pod annotations for alpha feature support.
 | 
				
			||||||
	CreateContainerSecurityContext(pod *api.Pod, container *api.Container) (*api.SecurityContext, map[string]string, error)
 | 
						CreateContainerSecurityContext(pod *api.Pod, container *api.Container) (*api.SecurityContext, map[string]string, error)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -273,12 +273,10 @@ func (c *PodSecurityPolicyPlugin) computeSecurityContext(a admission.Attributes,
 | 
				
			|||||||
func assignSecurityContext(provider psp.Provider, pod *api.Pod, fldPath *field.Path) field.ErrorList {
 | 
					func assignSecurityContext(provider psp.Provider, pod *api.Pod, fldPath *field.Path) field.ErrorList {
 | 
				
			||||||
	errs := field.ErrorList{}
 | 
						errs := field.ErrorList{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	psc, pscAnnotations, err := provider.CreatePodSecurityContext(pod)
 | 
						err := provider.DefaultPodSecurityContext(pod)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		errs = append(errs, field.Invalid(field.NewPath("spec", "securityContext"), pod.Spec.SecurityContext, err.Error()))
 | 
							errs = append(errs, field.Invalid(field.NewPath("spec", "securityContext"), pod.Spec.SecurityContext, err.Error()))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	pod.Spec.SecurityContext = psc
 | 
					 | 
				
			||||||
	pod.Annotations = pscAnnotations
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	errs = append(errs, provider.ValidatePodSecurityContext(pod, field.NewPath("spec", "securityContext"))...)
 | 
						errs = append(errs, provider.ValidatePodSecurityContext(pod, field.NewPath("spec", "securityContext"))...)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user