|
| 1 | +package podsecuritypolicyreview |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "sort" |
| 6 | + |
| 7 | + "github.com/golang/glog" |
| 8 | + |
| 9 | + kapi "k8s.io/kubernetes/pkg/api" |
| 10 | + kapierrors "k8s.io/kubernetes/pkg/api/errors" |
| 11 | + clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset" |
| 12 | + "k8s.io/kubernetes/pkg/runtime" |
| 13 | + kscc "k8s.io/kubernetes/pkg/securitycontextconstraints" |
| 14 | + "k8s.io/kubernetes/pkg/serviceaccount" |
| 15 | + kerrors "k8s.io/kubernetes/pkg/util/errors" |
| 16 | + |
| 17 | + securityapi "github.com/openshift/origin/pkg/security/api" |
| 18 | + securityvalidation "github.com/openshift/origin/pkg/security/api/validation" |
| 19 | + "github.com/openshift/origin/pkg/security/registry/podsecuritypolicysubjectreview" |
| 20 | + oscc "github.com/openshift/origin/pkg/security/scc" |
| 21 | +) |
| 22 | + |
| 23 | +// REST implements the RESTStorage interface in terms of an Registry. |
| 24 | +type REST struct { |
| 25 | + sccMatcher oscc.SCCMatcher |
| 26 | + client clientset.Interface |
| 27 | +} |
| 28 | + |
| 29 | +// NewREST creates a new REST for policies.. |
| 30 | +func NewREST(m oscc.SCCMatcher, c clientset.Interface) *REST { |
| 31 | + return &REST{sccMatcher: m, client: c} |
| 32 | +} |
| 33 | + |
| 34 | +// New creates a new PodSecurityPolicyReview object |
| 35 | +func (r *REST) New() runtime.Object { |
| 36 | + return &securityapi.PodSecurityPolicyReview{} |
| 37 | +} |
| 38 | + |
| 39 | +// Create registers a given new PodSecurityPolicyReview instance to r.registry. |
| 40 | +func (r *REST) Create(ctx kapi.Context, obj runtime.Object) (runtime.Object, error) { |
| 41 | + pspr, ok := obj.(*securityapi.PodSecurityPolicyReview) |
| 42 | + if !ok { |
| 43 | + return nil, kapierrors.NewBadRequest(fmt.Sprintf("not a PodSecurityPolicyReview: %#v", obj)) |
| 44 | + } |
| 45 | + if errs := securityvalidation.ValidatePodSecurityPolicyReview(pspr); len(errs) > 0 { |
| 46 | + return nil, kapierrors.NewInvalid(kapi.Kind("PodSecurityPolicyReview"), "", errs) |
| 47 | + } |
| 48 | + ns, ok := kapi.NamespaceFrom(ctx) |
| 49 | + if !ok { |
| 50 | + return nil, kapierrors.NewBadRequest("namespace parameter required.") |
| 51 | + } |
| 52 | + serviceAccounts, err := getServiceAccounts(pspr.Spec, r.client, ns) |
| 53 | + if err != nil { |
| 54 | + return nil, kapierrors.NewBadRequest(err.Error()) |
| 55 | + } |
| 56 | + |
| 57 | + if len(serviceAccounts) == 0 { |
| 58 | + glog.Errorf("No service accounts for namespace %s", ns) |
| 59 | + return nil, kapierrors.NewBadRequest(fmt.Sprintf("unable to find ServiceAccount for namespace: %s", ns)) |
| 60 | + } |
| 61 | + |
| 62 | + errs := []error{} |
| 63 | + newStatus := securityapi.PodSecurityPolicyReviewStatus{} |
| 64 | + for _, sa := range serviceAccounts { |
| 65 | + userInfo := serviceaccount.UserInfo(ns, sa.Name, "") |
| 66 | + saConstraints, err := r.sccMatcher.FindApplicableSCCs(userInfo) |
| 67 | + if err != nil { |
| 68 | + errs = append(errs, fmt.Errorf("unable to find SecurityContextConstraints for ServiceAccount %s: %v", sa.Name, err)) |
| 69 | + continue |
| 70 | + } |
| 71 | + oscc.DeduplicateSecurityContextConstraints(saConstraints) |
| 72 | + sort.Sort(oscc.ByPriority(saConstraints)) |
| 73 | + var namespace *kapi.Namespace |
| 74 | + for _, constraint := range saConstraints { |
| 75 | + var ( |
| 76 | + provider kscc.SecurityContextConstraintsProvider |
| 77 | + err error |
| 78 | + ) |
| 79 | + pspsrs := securityapi.PodSecurityPolicySubjectReviewStatus{} |
| 80 | + if provider, namespace, err = oscc.CreateProviderFromConstraint(ns, namespace, constraint, r.client); err != nil { |
| 81 | + errs = append(errs, fmt.Errorf("unable to create provider for service account %s: %v", sa.Name, err)) |
| 82 | + continue |
| 83 | + } |
| 84 | + _, err = podsecuritypolicysubjectreview.FillPodSecurityPolicySubjectReviewStatus(&pspsrs, provider, pspr.Spec.Template.Spec, constraint) |
| 85 | + if err != nil { |
| 86 | + glog.Errorf("unable to fill PodSecurityPolicyReviewStatus from constraint %v", err) |
| 87 | + continue |
| 88 | + } |
| 89 | + sapsprs := securityapi.ServiceAccountPodSecurityPolicyReviewStatus{pspsrs, sa.Name} |
| 90 | + newStatus.AllowedServiceAccounts = append(newStatus.AllowedServiceAccounts, sapsprs) |
| 91 | + } |
| 92 | + } |
| 93 | + if len(errs) > 0 { |
| 94 | + return nil, kapierrors.NewBadRequest(fmt.Sprintf("%s", kerrors.NewAggregate(errs))) |
| 95 | + } |
| 96 | + pspr.Status = newStatus |
| 97 | + return pspr, nil |
| 98 | +} |
| 99 | + |
| 100 | +func getServiceAccounts(psprSpec securityapi.PodSecurityPolicyReviewSpec, client clientset.Interface, namespace string) ([]*kapi.ServiceAccount, error) { |
| 101 | + serviceAccounts := []*kapi.ServiceAccount{} |
| 102 | + // TODO: express 'all service accounts' |
| 103 | + //if serviceAccountList, err := client.Core().ServiceAccounts(namespace).List(kapi.ListOptions{}); err == nil { |
| 104 | + // serviceAccounts = serviceAccountList.Items |
| 105 | + // return serviceAccounts, fmt.Errorf("unable to retrieve service accounts: %v", err) |
| 106 | + //} |
| 107 | + |
| 108 | + if len(psprSpec.ServiceAccountNames) > 0 { |
| 109 | + errs := []error{} |
| 110 | + for _, saName := range psprSpec.ServiceAccountNames { |
| 111 | + // TODO: use cache as soon ServiceAccount informer is ready |
| 112 | + sa, err := client.Core().ServiceAccounts(namespace).Get(saName) |
| 113 | + if err != nil { |
| 114 | + errs = append(errs, fmt.Errorf("unable to retrieve ServiceAccount %s: %v", saName, err)) |
| 115 | + } |
| 116 | + serviceAccounts = append(serviceAccounts, sa) |
| 117 | + } |
| 118 | + return serviceAccounts, kerrors.NewAggregate(errs) |
| 119 | + } |
| 120 | + saName := "default" |
| 121 | + if len(psprSpec.Template.Spec.ServiceAccountName) > 0 { |
| 122 | + saName = psprSpec.Template.Spec.ServiceAccountName |
| 123 | + } |
| 124 | + sa, err := client.Core().ServiceAccounts(namespace).Get(saName) |
| 125 | + if err != nil { |
| 126 | + return serviceAccounts, fmt.Errorf("unable to retrieve ServiceAccount %s: %v", saName, err) |
| 127 | + } |
| 128 | + serviceAccounts = append(serviceAccounts, sa) |
| 129 | + return serviceAccounts, nil |
| 130 | +} |
0 commit comments