/
admission.go
261 lines (235 loc) · 9.33 KB
/
admission.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package podnodeconstraints
import (
"fmt"
"io"
"reflect"
"k8s.io/klog"
kapierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apiserver/pkg/admission"
"k8s.io/apiserver/pkg/admission/initializer"
"k8s.io/apiserver/pkg/authorization/authorizer"
"k8s.io/kubernetes/pkg/apis/apps"
"k8s.io/kubernetes/pkg/apis/batch"
kapi "k8s.io/kubernetes/pkg/apis/core"
"k8s.io/kubernetes/pkg/apis/extensions"
"k8s.io/kubernetes/pkg/auth/nodeidentifier"
oapps "github.com/openshift/api/apps"
"github.com/openshift/api/security"
"github.com/openshift/origin/pkg/api/imagereferencemutators"
"github.com/openshift/origin/pkg/api/legacy"
configlatest "github.com/openshift/origin/pkg/cmd/server/apis/config/latest"
"github.com/openshift/origin/pkg/scheduler/admission/apis/podnodeconstraints"
)
const PluginName = "scheduling.openshift.io/PodNodeConstraints"
// kindsToIgnore is a list of kinds that contain a PodSpec that
// we choose not to handle in this plugin
var kindsToIgnore = []schema.GroupKind{
extensions.Kind("DaemonSet"),
}
func Register(plugins *admission.Plugins) {
plugins.Register(PluginName,
func(config io.Reader) (admission.Interface, error) {
pluginConfig, err := readConfig(config)
if err != nil {
return nil, err
}
if pluginConfig == nil {
klog.Infof("Admission plugin %q is not configured so it will be disabled.", PluginName)
return nil, nil
}
return NewPodNodeConstraints(pluginConfig, nodeidentifier.NewDefaultNodeIdentifier()), nil
})
}
// NewPodNodeConstraints creates a new admission plugin to prevent objects that contain pod templates
// from containing node bindings by name or selector based on role permissions.
func NewPodNodeConstraints(config *podnodeconstraints.PodNodeConstraintsConfig, nodeIdentifier nodeidentifier.NodeIdentifier) admission.Interface {
plugin := podNodeConstraints{
config: config,
Handler: admission.NewHandler(admission.Create, admission.Update),
nodeIdentifier: nodeIdentifier,
}
if config != nil {
plugin.selectorLabelBlacklist = sets.NewString(config.NodeSelectorLabelBlacklist...)
}
return &plugin
}
type podNodeConstraints struct {
*admission.Handler
selectorLabelBlacklist sets.String
config *podnodeconstraints.PodNodeConstraintsConfig
authorizer authorizer.Authorizer
nodeIdentifier nodeidentifier.NodeIdentifier
}
var _ = initializer.WantsAuthorizer(&podNodeConstraints{})
var _ = admission.ValidationInterface(&podNodeConstraints{})
func shouldCheckResource(resource schema.GroupResource, kind schema.GroupKind) (bool, error) {
expectedKind, shouldCheck := resourcesToCheck[resource]
if !shouldCheck {
return false, nil
}
for _, ignore := range kindsToIgnore {
if ignore == expectedKind {
return false, nil
}
}
if expectedKind != kind {
return false, fmt.Errorf("Unexpected resource kind %v for resource %v", &kind, &resource)
}
return true, nil
}
// resourcesToCheck is a map of resources and corresponding kinds of things that we want handled in this plugin
var resourcesToCheck = map[schema.GroupResource]schema.GroupKind{
kapi.Resource("pods"): kapi.Kind("Pod"),
kapi.Resource("podtemplates"): kapi.Kind("PodTemplate"),
kapi.Resource("replicationcontrollers"): kapi.Kind("ReplicationController"),
batch.Resource("jobs"): batch.Kind("Job"),
batch.Resource("jobtemplates"): batch.Kind("JobTemplate"),
batch.Resource("cronjobs"): batch.Kind("CronJob"),
extensions.Resource("deployments"): extensions.Kind("Deployment"),
extensions.Resource("replicasets"): extensions.Kind("ReplicaSet"),
apps.Resource("deployments"): apps.Kind("Deployment"),
apps.Resource("replicasets"): apps.Kind("ReplicaSet"),
apps.Resource("statefulsets"): apps.Kind("StatefulSet"),
legacy.Resource("deploymentconfigs"): legacy.Kind("DeploymentConfig"),
legacy.Resource("podsecuritypolicysubjectreviews"): legacy.Kind("PodSecurityPolicySubjectReview"),
legacy.Resource("podsecuritypolicyselfsubjectreviews"): legacy.Kind("PodSecurityPolicySelfSubjectReview"),
legacy.Resource("podsecuritypolicyreviews"): legacy.Kind("PodSecurityPolicyReview"),
oapps.Resource("deploymentconfigs"): oapps.Kind("DeploymentConfig"),
security.Resource("podsecuritypolicysubjectreviews"): security.Kind("PodSecurityPolicySubjectReview"),
security.Resource("podsecuritypolicyselfsubjectreviews"): security.Kind("PodSecurityPolicySelfSubjectReview"),
security.Resource("podsecuritypolicyreviews"): security.Kind("PodSecurityPolicyReview"),
}
func readConfig(reader io.Reader) (*podnodeconstraints.PodNodeConstraintsConfig, error) {
if reader == nil || reflect.ValueOf(reader).IsNil() {
return nil, nil
}
obj, err := configlatest.ReadYAML(reader)
if err != nil {
return nil, err
}
if obj == nil {
return nil, nil
}
config, ok := obj.(*podnodeconstraints.PodNodeConstraintsConfig)
if !ok {
return nil, fmt.Errorf("unexpected config object: %#v", obj)
}
// No validation needed since config is just list of strings
return config, nil
}
func (o *podNodeConstraints) Validate(attr admission.Attributes) error {
switch {
case o.config == nil,
attr.GetSubresource() != "":
return nil
}
shouldCheck, err := shouldCheckResource(attr.GetResource().GroupResource(), attr.GetKind().GroupKind())
if err != nil {
return err
}
if !shouldCheck {
return nil
}
// Only check Create operation on pods
if attr.GetResource().GroupResource() == kapi.Resource("pods") && attr.GetOperation() != admission.Create {
return nil
}
ps, err := o.getPodSpec(attr)
if err != nil {
return err
}
return o.validatePodSpec(attr, ps)
}
// extract the PodSpec from the pod templates for each object we care about
func (o *podNodeConstraints) getPodSpec(attr admission.Attributes) (kapi.PodSpec, error) {
spec, _, err := imagereferencemutators.GetPodSpec(attr.GetObject())
if err != nil {
return kapi.PodSpec{}, kapierrors.NewInternalError(err)
}
return *spec, nil
}
// validate PodSpec if NodeName or NodeSelector are specified
func (o *podNodeConstraints) validatePodSpec(attr admission.Attributes, ps kapi.PodSpec) error {
// a node creating a mirror pod that targets itself is allowed
// see the NodeRestriction plugin for further details
if o.isNodeSelfTargetWithMirrorPod(attr, ps.NodeName) {
return nil
}
matchingLabels := []string{}
// nodeSelector blacklist filter
for nodeSelectorLabel := range ps.NodeSelector {
if o.selectorLabelBlacklist.Has(nodeSelectorLabel) {
matchingLabels = append(matchingLabels, nodeSelectorLabel)
}
}
// nodeName constraint
if len(ps.NodeName) > 0 || len(matchingLabels) > 0 {
allow, err := o.checkPodsBindAccess(attr)
if err != nil {
return err
}
if !allow {
switch {
case len(ps.NodeName) > 0 && len(matchingLabels) == 0:
return admission.NewForbidden(attr, fmt.Errorf("node selection by nodeName is prohibited by policy for your role"))
case len(ps.NodeName) == 0 && len(matchingLabels) > 0:
return admission.NewForbidden(attr, fmt.Errorf("node selection by label(s) %v is prohibited by policy for your role", matchingLabels))
case len(ps.NodeName) > 0 && len(matchingLabels) > 0:
return admission.NewForbidden(attr, fmt.Errorf("node selection by nodeName and label(s) %v is prohibited by policy for your role", matchingLabels))
}
}
}
return nil
}
func (o *podNodeConstraints) SetAuthorizer(a authorizer.Authorizer) {
o.authorizer = a
}
func (o *podNodeConstraints) ValidateInitialization() error {
if o.authorizer == nil {
return fmt.Errorf("%s requires an authorizer", PluginName)
}
if o.nodeIdentifier == nil {
return fmt.Errorf("%s requires a node identifier", PluginName)
}
return nil
}
// build LocalSubjectAccessReview struct to validate role via checkAccess
func (o *podNodeConstraints) checkPodsBindAccess(attr admission.Attributes) (bool, error) {
authzAttr := authorizer.AttributesRecord{
User: attr.GetUserInfo(),
Verb: "create",
Namespace: attr.GetNamespace(),
Resource: "pods",
Subresource: "binding",
APIGroup: kapi.GroupName,
ResourceRequest: true,
}
if attr.GetResource().GroupResource() == kapi.Resource("pods") {
authzAttr.Name = attr.GetName()
}
authorized, _, err := o.authorizer.Authorize(authzAttr)
return authorized == authorizer.DecisionAllow, err
}
func (o *podNodeConstraints) isNodeSelfTargetWithMirrorPod(attr admission.Attributes, nodeName string) bool {
// make sure we are actually trying to target a node
if len(nodeName) == 0 {
return false
}
// this check specifically requires the object to be pod (unlike the other checks where we want any pod spec)
pod, ok := attr.GetObject().(*kapi.Pod)
if !ok {
return false
}
// note that anyone can create a mirror pod, but they are not privileged in any way
// they are actually highly constrained since they cannot reference secrets
// nodes can only create and delete them, and they will delete any "orphaned" mirror pods
if _, isMirrorPod := pod.Annotations[kapi.MirrorPodAnnotationKey]; !isMirrorPod {
return false
}
// we are targeting a node with a mirror pod
// confirm the user is a node that is targeting itself
actualNodeName, isNode := o.nodeIdentifier.NodeIdentity(attr.GetUserInfo())
return isNode && actualNodeName == nodeName
}