|  | 
|  | 1 | +package podsecurity | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"context" | 
|  | 5 | +	"fmt" | 
|  | 6 | + | 
|  | 7 | +	utilruntime "k8s.io/apimachinery/pkg/util/runtime" | 
|  | 8 | + | 
|  | 9 | +	corev1 "k8s.io/api/core/v1" | 
|  | 10 | +	"k8s.io/apimachinery/pkg/api/meta" | 
|  | 11 | +	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | 
|  | 12 | +	"k8s.io/apimachinery/pkg/runtime" | 
|  | 13 | +	"k8s.io/apimachinery/pkg/runtime/schema" | 
|  | 14 | +	"k8s.io/apiserver/pkg/admission" | 
|  | 15 | +	"k8s.io/apiserver/pkg/authentication/serviceaccount" | 
|  | 16 | +	"k8s.io/apiserver/pkg/authentication/user" | 
|  | 17 | +	"k8s.io/klog/v2" | 
|  | 18 | +	"k8s.io/kubernetes/pkg/apis/core" | 
|  | 19 | +	v1 "k8s.io/kubernetes/pkg/apis/core/v1" | 
|  | 20 | +	saadmission "k8s.io/kubernetes/plugin/pkg/admission/serviceaccount" | 
|  | 21 | +	podsecurityadmission "k8s.io/pod-security-admission/admission" | 
|  | 22 | +) | 
|  | 23 | + | 
|  | 24 | +type SCCMutatingPodSpecExtractor struct { | 
|  | 25 | +	sccAdmission admission.MutationInterface | 
|  | 26 | +	delegate     podsecurityadmission.PodSpecExtractor | 
|  | 27 | +} | 
|  | 28 | + | 
|  | 29 | +var SCCMutatingPodSpecExtractorInstance = &SCCMutatingPodSpecExtractor{ | 
|  | 30 | +	delegate: podsecurityadmission.DefaultPodSpecExtractor{}, | 
|  | 31 | +} | 
|  | 32 | + | 
|  | 33 | +func (s *SCCMutatingPodSpecExtractor) SetSCCAdmission(sccAdmission admission.MutationInterface) { | 
|  | 34 | +	s.sccAdmission = sccAdmission | 
|  | 35 | +} | 
|  | 36 | + | 
|  | 37 | +func (s *SCCMutatingPodSpecExtractor) HasPodSpec(gr schema.GroupResource) bool { | 
|  | 38 | +	return s.delegate.HasPodSpec(gr) | 
|  | 39 | +} | 
|  | 40 | + | 
|  | 41 | +func (s *SCCMutatingPodSpecExtractor) ExtractPodSpec(obj runtime.Object) (*metav1.ObjectMeta, *corev1.PodSpec, error) { | 
|  | 42 | +	if s.sccAdmission == nil { | 
|  | 43 | +		return s.delegate.ExtractPodSpec(obj) | 
|  | 44 | +	} | 
|  | 45 | + | 
|  | 46 | +	switch obj := obj.(type) { | 
|  | 47 | +	case *corev1.Pod: | 
|  | 48 | +		return s.delegate.ExtractPodSpec(obj) | 
|  | 49 | +	} | 
|  | 50 | + | 
|  | 51 | +	podTemplateMeta, originalPodSpec, err := s.delegate.ExtractPodSpec(obj) | 
|  | 52 | +	if err != nil { | 
|  | 53 | +		return podTemplateMeta, originalPodSpec, err | 
|  | 54 | +	} | 
|  | 55 | +	if originalPodSpec == nil { | 
|  | 56 | +		return nil, nil, nil | 
|  | 57 | +	} | 
|  | 58 | +	objectMeta, err := meta.Accessor(obj) | 
|  | 59 | +	if err != nil { | 
|  | 60 | +		return podTemplateMeta, originalPodSpec, fmt.Errorf("unable to get metadata for SCC mutation: %w", err) | 
|  | 61 | +	} | 
|  | 62 | + | 
|  | 63 | +	pod := &corev1.Pod{ | 
|  | 64 | +		ObjectMeta: *podTemplateMeta.DeepCopy(), | 
|  | 65 | +		Spec:       *originalPodSpec.DeepCopy(), | 
|  | 66 | +	} | 
|  | 67 | +	if len(pod.Namespace) == 0 { | 
|  | 68 | +		pod.Namespace = objectMeta.GetNamespace() | 
|  | 69 | +	} | 
|  | 70 | +	if len(pod.Name) == 0 { | 
|  | 71 | +		pod.Name = "pod-for-container-named-" + objectMeta.GetName() | 
|  | 72 | +	} | 
|  | 73 | +	if len(pod.Spec.ServiceAccountName) == 0 { | 
|  | 74 | +		pod.Spec.ServiceAccountName = saadmission.DefaultServiceAccountName | 
|  | 75 | +	} | 
|  | 76 | +	internalPod := &core.Pod{} | 
|  | 77 | +	if err := v1.Convert_v1_Pod_To_core_Pod(pod, internalPod, nil); err != nil { | 
|  | 78 | +		return nil, nil, err | 
|  | 79 | +	} | 
|  | 80 | + | 
|  | 81 | +	admissionAttributes := admission.NewAttributesRecord( | 
|  | 82 | +		internalPod, | 
|  | 83 | +		nil, | 
|  | 84 | +		corev1.SchemeGroupVersion.WithKind("Pod"), | 
|  | 85 | +		pod.Namespace, | 
|  | 86 | +		pod.Name, | 
|  | 87 | +		corev1.SchemeGroupVersion.WithResource("pods"), | 
|  | 88 | +		"", | 
|  | 89 | +		admission.Create, | 
|  | 90 | +		nil, | 
|  | 91 | +		false, | 
|  | 92 | +		&user.DefaultInfo{ | 
|  | 93 | +			Name:   serviceaccount.MakeUsername(pod.Namespace, pod.Spec.ServiceAccountName), | 
|  | 94 | +			UID:    "", | 
|  | 95 | +			Groups: append([]string{user.AllAuthenticated}, serviceaccount.MakeGroupNames(pod.Namespace)...), | 
|  | 96 | +			Extra:  nil, | 
|  | 97 | +		}) | 
|  | 98 | +	if err := s.sccAdmission.Admit(context.Background(), admissionAttributes, nil); err != nil { | 
|  | 99 | +		// don't fail the request, just warn if SCC will fail | 
|  | 100 | +		klog.ErrorS(err, "failed to mutate object for PSA using SCC") | 
|  | 101 | +		utilruntime.HandleError(fmt.Errorf("failed to mutate object for PSA using SCC: %w", err)) | 
|  | 102 | +		// TODO remove this failure we're causing when SCC fails, but for now we actually need to see our test fail because that was almost really bad. | 
|  | 103 | +		return podTemplateMeta, originalPodSpec, nil | 
|  | 104 | +	} | 
|  | 105 | + | 
|  | 106 | +	if err := v1.Convert_core_Pod_To_v1_Pod(internalPod, pod, nil); err != nil { | 
|  | 107 | +		return nil, nil, err | 
|  | 108 | +	} | 
|  | 109 | + | 
|  | 110 | +	return podTemplateMeta, &pod.Spec, nil | 
|  | 111 | +} | 
0 commit comments