|
| 1 | +package policy |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + |
| 7 | + "github.com/spf13/cobra" |
| 8 | + |
| 9 | + kapi "k8s.io/kubernetes/pkg/api" |
| 10 | + "k8s.io/kubernetes/pkg/api/meta" |
| 11 | + "k8s.io/kubernetes/pkg/apis/apps" |
| 12 | + "k8s.io/kubernetes/pkg/kubectl" |
| 13 | + kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" |
| 14 | + "k8s.io/kubernetes/pkg/kubectl/resource" |
| 15 | + "k8s.io/kubernetes/pkg/runtime" |
| 16 | + utilerrors "k8s.io/kubernetes/pkg/util/errors" |
| 17 | + |
| 18 | + ometa "github.com/openshift/origin/pkg/api/meta" |
| 19 | + "github.com/openshift/origin/pkg/client" |
| 20 | + "github.com/openshift/origin/pkg/cmd/cli/describe" |
| 21 | + "github.com/openshift/origin/pkg/cmd/templates" |
| 22 | + "github.com/openshift/origin/pkg/cmd/util/clientcmd" |
| 23 | + securityapi "github.com/openshift/origin/pkg/security/api" |
| 24 | +) |
| 25 | + |
| 26 | +var ( |
| 27 | + reviewLong = templates.LongDesc(`Checks which Service Account can create a Pod. |
| 28 | + The Pod is inferred from the PodTemplateSpec in the provided resource. |
| 29 | + If no Service Account is provided the one specified in podTemplateSpec.spec.serviceAccountName is used, |
| 30 | + unless it is empty, in which case "default" is used. |
| 31 | + If Service Accounts are provided the podTemplateSpec.spec.serviceAccountName is ignored. |
| 32 | + `) |
| 33 | + reviewExamples = templates.Examples(`# Check whether Service Accounts sa1 and sa2 can admit a Pod with TemplatePodSpec specified in my_resource.yaml |
| 34 | + # Service Account specified in myresource.yaml file is ignored |
| 35 | + $ %[1]s -s sa1,sa2 -f my_resource.yaml |
| 36 | + |
| 37 | + # Check whether Service Account specified in my_resource_with_sa.yaml can admit the Pod |
| 38 | + $ %[1]s -f my_resource_with_sa.yaml |
| 39 | + |
| 40 | + # Check whether default Service Account can admit the Pod, default is taken since no Service Account is defined in myresource_with_no_sa.yaml |
| 41 | + $ %[1]s -f myresource_with_no_sa.yaml |
| 42 | + `) |
| 43 | +) |
| 44 | + |
| 45 | +const ReviewRecommendedName = "scc-review" |
| 46 | + |
| 47 | +type sccReviewOptions struct { |
| 48 | + client client.PodSecurityPolicyReviewsNamespacer |
| 49 | + namespace string |
| 50 | + enforceNamespace bool |
| 51 | + out io.Writer |
| 52 | + mapper meta.RESTMapper |
| 53 | + typer runtime.ObjectTyper |
| 54 | + RESTClientFactory func(mapping *meta.RESTMapping) (resource.RESTClient, error) |
| 55 | + printer kubectl.ResourcePrinter |
| 56 | + FilenameOptions resource.FilenameOptions |
| 57 | + ServiceAccountNames []string |
| 58 | +} |
| 59 | + |
| 60 | +func NewCmdSccReview(name, fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command { |
| 61 | + o := &sccReviewOptions{} |
| 62 | + cmd := &cobra.Command{ |
| 63 | + Use: name, |
| 64 | + Short: "Checks which ServiceAccount can create a Pod", |
| 65 | + Long: reviewLong, |
| 66 | + Example: fmt.Sprintf(reviewExamples, fullName), |
| 67 | + Run: func(cmd *cobra.Command, args []string) { |
| 68 | + kcmdutil.CheckErr(o.Complete(f, args, cmd, out)) |
| 69 | + kcmdutil.CheckErr(o.Run(args)) |
| 70 | + }, |
| 71 | + } |
| 72 | + |
| 73 | + cmd.Flags().StringSliceVarP(&o.ServiceAccountNames, "serviceaccounts", "s", o.ServiceAccountNames, "List of ServiceAccount names, comma separated") |
| 74 | + kcmdutil.AddFilenameOptionFlags(cmd, &o.FilenameOptions, "Filename, directory, or URL to a file identifying the resource to get from a server.") |
| 75 | + |
| 76 | + kcmdutil.AddPrinterFlags(cmd) |
| 77 | + |
| 78 | + return cmd |
| 79 | +} |
| 80 | + |
| 81 | +func (o *sccReviewOptions) Complete(f *clientcmd.Factory, args []string, cmd *cobra.Command, out io.Writer) error { |
| 82 | + if len(args) == 0 && len(o.FilenameOptions.Filenames) == 0 { |
| 83 | + return kcmdutil.UsageError(cmd, cmd.Use) |
| 84 | + } |
| 85 | + var err error |
| 86 | + o.namespace, o.enforceNamespace, err = f.DefaultNamespace() |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + o.client, _, err = f.Clients() |
| 91 | + if err != nil { |
| 92 | + return fmt.Errorf("unable to obtain client: %v", err) |
| 93 | + } |
| 94 | + o.mapper, o.typer = f.Object() |
| 95 | + o.RESTClientFactory = f.ClientForMapping |
| 96 | + |
| 97 | + if len(kcmdutil.GetFlagString(cmd, "output")) != 0 { |
| 98 | + clientConfig, err := f.ClientConfig() |
| 99 | + if err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + version, err := kcmdutil.OutputVersion(cmd, clientConfig.GroupVersion) |
| 103 | + if err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + p, _, err := kcmdutil.PrinterForCommand(cmd) |
| 107 | + if err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + o.printer = kubectl.NewVersionedPrinter(p, kapi.Scheme, version) |
| 111 | + } else { |
| 112 | + o.printer = describe.NewHumanReadablePrinter(kubectl.PrintOptions{NoHeaders: kcmdutil.GetFlagBool(cmd, "no-headers")}) |
| 113 | + } |
| 114 | + |
| 115 | + o.out = out |
| 116 | + |
| 117 | + return nil |
| 118 | +} |
| 119 | + |
| 120 | +func (o *sccReviewOptions) Run(args []string) error { |
| 121 | + |
| 122 | + r := resource.NewBuilder(o.mapper, o.typer, resource.ClientMapperFunc(o.RESTClientFactory), kapi.Codecs.UniversalDecoder()). |
| 123 | + NamespaceParam(o.namespace). |
| 124 | + FilenameParam(o.enforceNamespace, &o.FilenameOptions). |
| 125 | + ResourceTypeOrNameArgs(true, args...). |
| 126 | + ContinueOnError(). |
| 127 | + Flatten(). |
| 128 | + Do() |
| 129 | + err := r.Err() |
| 130 | + if err != nil { |
| 131 | + return err |
| 132 | + } |
| 133 | + |
| 134 | + allErrs := []error{} |
| 135 | + reviews := []*securityapi.PodSecurityPolicyReview{} |
| 136 | + err = r.Visit(func(info *resource.Info, err error) error { |
| 137 | + if err != nil { |
| 138 | + return err |
| 139 | + } |
| 140 | + objectName := info.Name |
| 141 | + podTemplateSpec, err := GetPodTemplateForObject(info.Object) |
| 142 | + if err != nil { |
| 143 | + return fmt.Errorf(" %q cannot create pod: %v", objectName, err) |
| 144 | + } |
| 145 | + err = CheckStatefulSetWithWolumeClaimTemplates(info.Object) |
| 146 | + if err != nil { |
| 147 | + return err |
| 148 | + } |
| 149 | + review := &securityapi.PodSecurityPolicyReview{ |
| 150 | + Spec: securityapi.PodSecurityPolicyReviewSpec{ |
| 151 | + Template: *podTemplateSpec, |
| 152 | + ServiceAccountNames: o.ServiceAccountNames, |
| 153 | + }, |
| 154 | + } |
| 155 | + response, err := o.client.PodSecurityPolicyReviews(o.namespace).Create(review) |
| 156 | + if err != nil { |
| 157 | + return fmt.Errorf("unable to compute Pod Security Policy Review for %q: %v", objectName, err) |
| 158 | + } |
| 159 | + reviews = append(reviews, response) |
| 160 | + return nil |
| 161 | + }) |
| 162 | + allErrs = append(allErrs, err) |
| 163 | + for i := range reviews { |
| 164 | + if err := o.printer.PrintObj(reviews[i], o.out); err != nil { |
| 165 | + allErrs = append(allErrs, err) |
| 166 | + } |
| 167 | + } |
| 168 | + return utilerrors.NewAggregate(allErrs) |
| 169 | +} |
| 170 | + |
| 171 | +// CheckStatefulSetWithWolumeClaimTemplates checks whether a supplied object is a statefulSet with volumeClaimTemplates |
| 172 | +// Currently scc-review and scc-subject-review commands cannot handle correctly this case since validation is not based |
| 173 | +// only on podTemplateSpec. |
| 174 | +func CheckStatefulSetWithWolumeClaimTemplates(obj runtime.Object) error { |
| 175 | + // TODO remove this as soon upstream statefulSet validation for podSpec is fixed. |
| 176 | + // Currently podTemplateSpec for a statefulSet is not fully validated |
| 177 | + // spec.volumeClaimTemplates info should be propagated down to |
| 178 | + // spec.template.spec validateContainers to validate volumeMounts |
| 179 | + //https://github.com/openshift/origin/blob/master/vendor/k8s.io/kubernetes/pkg/apis/apps/validation/validation.go#L57 |
| 180 | + switch r := obj.(type) { |
| 181 | + case *apps.StatefulSet: |
| 182 | + if len(r.Spec.VolumeClaimTemplates) > 0 { |
| 183 | + return fmt.Errorf("StatefulSet %q with spec.volumeClaimTemplates currently not supported.", r.GetName()) |
| 184 | + } |
| 185 | + } |
| 186 | + return nil |
| 187 | +} |
| 188 | + |
| 189 | +func GetPodTemplateForObject(obj runtime.Object) (*kapi.PodTemplateSpec, error) { |
| 190 | + podSpec, _, err := ometa.GetPodSpec(obj) |
| 191 | + if err != nil { |
| 192 | + return nil, err |
| 193 | + } |
| 194 | + return &kapi.PodTemplateSpec{Spec: *podSpec}, nil |
| 195 | +} |
0 commit comments