| 1 |
/* |
| 2 |
Copyright 2019 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package handlers |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"fmt" |
| 22 |
"io" |
| 23 |
"net/http" |
| 24 |
"net/http/httptest" |
| 25 |
"strings" |
| 26 |
"sync/atomic" |
| 27 |
"testing" |
| 28 |
|
| 29 |
"k8s.io/apimachinery/pkg/api/errors" |
| 30 |
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion" |
| 31 |
metainternalversionscheme "k8s.io/apimachinery/pkg/apis/meta/internalversion/scheme" |
| 32 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 33 |
"k8s.io/apimachinery/pkg/runtime" |
| 34 |
"k8s.io/apimachinery/pkg/runtime/schema" |
| 35 |
"k8s.io/apimachinery/pkg/runtime/serializer" |
| 36 |
"k8s.io/apiserver/pkg/admission" |
| 37 |
auditapis "k8s.io/apiserver/pkg/apis/audit" |
| 38 |
"k8s.io/apiserver/pkg/audit" |
| 39 |
"k8s.io/apiserver/pkg/authentication/user" |
| 40 |
"k8s.io/apiserver/pkg/authorization/authorizer" |
| 41 |
"k8s.io/apiserver/pkg/endpoints/handlers/negotiation" |
| 42 |
"k8s.io/apiserver/pkg/endpoints/request" |
| 43 |
"k8s.io/apiserver/pkg/registry/rest" |
| 44 |
|
| 45 |
"k8s.io/utils/pointer" |
| 46 |
"k8s.io/utils/ptr" |
| 47 |
) |
| 48 |
|
| 49 |
type mockCodecs struct { |
| 50 |
serializer.CodecFactory |
| 51 |
err error |
| 52 |
} |
| 53 |
|
| 54 |
type mockCodec struct { |
| 55 |
runtime.Codec |
| 56 |
codecs *mockCodecs |
| 57 |
} |
| 58 |
|
| 59 |
func (p mockCodec) Encode(obj runtime.Object, w io.Writer) error { |
| 60 |
err := p.Codec.Encode(obj, w) |
| 61 |
p.codecs.err = err |
| 62 |
return err |
| 63 |
} |
| 64 |
|
| 65 |
func (s *mockCodecs) EncoderForVersion(encoder runtime.Encoder, gv runtime.GroupVersioner) runtime.Encoder { |
| 66 |
out := s.CodecFactory.CodecForVersions(encoder, nil, gv, nil) |
| 67 |
return &mockCodec{ |
| 68 |
Codec: out, |
| 69 |
codecs: s, |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
func TestDeleteResourceAuditLogRequestObject(t *testing.T) { |
| 74 |
|
| 75 |
ctx := audit.WithAuditContext(context.TODO()) |
| 76 |
ac := audit.AuditContextFrom(ctx) |
| 77 |
ac.Event.Level = auditapis.LevelRequestResponse |
| 78 |
|
| 79 |
policy := metav1.DeletePropagationBackground |
| 80 |
deleteOption := &metav1.DeleteOptions{ |
| 81 |
GracePeriodSeconds: pointer.Int64Ptr(30), |
| 82 |
PropagationPolicy: &policy, |
| 83 |
} |
| 84 |
|
| 85 |
fakeCorev1GroupVersion := schema.GroupVersion{ |
| 86 |
Group: "", |
| 87 |
Version: "v1", |
| 88 |
} |
| 89 |
testScheme := runtime.NewScheme() |
| 90 |
metav1.AddToGroupVersion(testScheme, fakeCorev1GroupVersion) |
| 91 |
testCodec := serializer.NewCodecFactory(testScheme) |
| 92 |
|
| 93 |
tests := []struct { |
| 94 |
name string |
| 95 |
object runtime.Object |
| 96 |
gv schema.GroupVersion |
| 97 |
serializer serializer.CodecFactory |
| 98 |
ok bool |
| 99 |
}{ |
| 100 |
{ |
| 101 |
name: "meta built-in Codec encode v1.DeleteOptions", |
| 102 |
object: &metav1.DeleteOptions{ |
| 103 |
GracePeriodSeconds: pointer.Int64Ptr(30), |
| 104 |
PropagationPolicy: &policy, |
| 105 |
}, |
| 106 |
gv: metav1.SchemeGroupVersion, |
| 107 |
serializer: metainternalversionscheme.Codecs, |
| 108 |
ok: true, |
| 109 |
}, |
| 110 |
{ |
| 111 |
name: "fake corev1 registered codec encode v1 DeleteOptions", |
| 112 |
object: &metav1.DeleteOptions{ |
| 113 |
GracePeriodSeconds: pointer.Int64Ptr(30), |
| 114 |
PropagationPolicy: &policy, |
| 115 |
}, |
| 116 |
gv: metav1.SchemeGroupVersion, |
| 117 |
serializer: testCodec, |
| 118 |
ok: false, |
| 119 |
}, |
| 120 |
} |
| 121 |
|
| 122 |
for _, test := range tests { |
| 123 |
t.Run(test.name, func(t *testing.T) { |
| 124 |
|
| 125 |
codecs := &mockCodecs{} |
| 126 |
codecs.CodecFactory = test.serializer |
| 127 |
|
| 128 |
audit.LogRequestObject(ctx, deleteOption, test.gv, schema.GroupVersionResource{ |
| 129 |
Group: "", |
| 130 |
Version: "v1", |
| 131 |
Resource: "pods", |
| 132 |
}, "", codecs) |
| 133 |
|
| 134 |
err := codecs.err |
| 135 |
if err != nil { |
| 136 |
if test.ok { |
| 137 |
t.Errorf("expect nil but got %#v", err) |
| 138 |
} |
| 139 |
t.Logf("encode object: %#v", err) |
| 140 |
} else { |
| 141 |
if !test.ok { |
| 142 |
t.Errorf("expect err but got nil") |
| 143 |
} |
| 144 |
} |
| 145 |
}) |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
func TestDeleteCollection(t *testing.T) { |
| 150 |
req := &http.Request{ |
| 151 |
Header: http.Header{}, |
| 152 |
} |
| 153 |
req.Header.Set("Content-Type", "application/json") |
| 154 |
|
| 155 |
fakeCorev1GroupVersion := schema.GroupVersion{ |
| 156 |
Group: "", |
| 157 |
Version: "v1", |
| 158 |
} |
| 159 |
fakeCorev1Scheme := runtime.NewScheme() |
| 160 |
fakeCorev1Scheme.AddKnownTypes(fakeCorev1GroupVersion, &metav1.DeleteOptions{}) |
| 161 |
fakeCorev1Codec := serializer.NewCodecFactory(fakeCorev1Scheme) |
| 162 |
|
| 163 |
tests := []struct { |
| 164 |
name string |
| 165 |
codecFactory serializer.CodecFactory |
| 166 |
data []byte |
| 167 |
expectErr string |
| 168 |
}{ |
| 169 |
// for issue: https://github.com/kubernetes/kubernetes/issues/111985 |
| 170 |
{ |
| 171 |
name: "decode '{}' to metav1.DeleteOptions with fakeCorev1Codecs", |
| 172 |
codecFactory: fakeCorev1Codec, |
| 173 |
data: []byte("{}"), |
| 174 |
expectErr: "no kind \"DeleteOptions\" is registered", |
| 175 |
}, |
| 176 |
{ |
| 177 |
name: "decode '{}' to metav1.DeleteOptions with metainternalversionscheme.Codecs", |
| 178 |
codecFactory: metainternalversionscheme.Codecs, |
| 179 |
data: []byte("{}"), |
| 180 |
expectErr: "", |
| 181 |
}, |
| 182 |
{ |
| 183 |
name: "decode versioned (corev1) DeleteOptions with metainternalversionscheme.Codecs", |
| 184 |
codecFactory: metainternalversionscheme.Codecs, |
| 185 |
data: []byte(`{"apiVersion":"v1","kind":"DeleteOptions","gracePeriodSeconds":123}`), |
| 186 |
expectErr: "", |
| 187 |
}, |
| 188 |
{ |
| 189 |
name: "decode versioned (foo) DeleteOptions with metainternalversionscheme.Codecs", |
| 190 |
codecFactory: metainternalversionscheme.Codecs, |
| 191 |
data: []byte(`{"apiVersion":"foo/v1","kind":"DeleteOptions","gracePeriodSeconds":123}`), |
| 192 |
expectErr: "", |
| 193 |
}, |
| 194 |
} |
| 195 |
|
| 196 |
defaultGVK := metav1.SchemeGroupVersion.WithKind("DeleteOptions") |
| 197 |
for _, test := range tests { |
| 198 |
t.Run(test.name, func(t *testing.T) { |
| 199 |
s, err := negotiation.NegotiateInputSerializer(req, false, test.codecFactory) |
| 200 |
if err != nil { |
| 201 |
t.Fatal(err) |
| 202 |
} |
| 203 |
|
| 204 |
options := &metav1.DeleteOptions{} |
| 205 |
_, _, err = metainternalversionscheme.Codecs.DecoderToVersion(s.Serializer, defaultGVK.GroupVersion()).Decode(test.data, &defaultGVK, options) |
| 206 |
if test.expectErr != "" { |
| 207 |
if err == nil { |
| 208 |
t.Fatalf("expect %s but got nil", test.expectErr) |
| 209 |
} |
| 210 |
if !strings.Contains(err.Error(), test.expectErr) { |
| 211 |
t.Fatalf("expect %s but got %s", test.expectErr, err.Error()) |
| 212 |
} |
| 213 |
} |
| 214 |
}) |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
func TestDeleteCollectionWithNoContextDeadlineEnforced(t *testing.T) { |
| 219 |
var invokedGot, hasDeadlineGot int32 |
| 220 |
fakeDeleterFn := func(ctx context.Context, _ rest.ValidateObjectFunc, _ *metav1.DeleteOptions, _ *metainternalversion.ListOptions) (runtime.Object, error) { |
| 221 |
// we expect CollectionDeleter to be executed once |
| 222 |
atomic.AddInt32(&invokedGot, 1) |
| 223 |
|
| 224 |
// we don't expect any context deadline to be set |
| 225 |
if _, hasDeadline := ctx.Deadline(); hasDeadline { |
| 226 |
atomic.AddInt32(&hasDeadlineGot, 1) |
| 227 |
} |
| 228 |
return nil, nil |
| 229 |
} |
| 230 |
|
| 231 |
// do the minimum setup to ensure that it gets as far as CollectionDeleter |
| 232 |
scope := &RequestScope{ |
| 233 |
Namer: &mockNamer{}, |
| 234 |
Serializer: &fakeSerializer{ |
| 235 |
serializer: runtime.NewCodec(runtime.NoopEncoder{}, runtime.NoopDecoder{}), |
| 236 |
}, |
| 237 |
} |
| 238 |
handler := DeleteCollection(fakeCollectionDeleterFunc(fakeDeleterFn), false, scope, nil) |
| 239 |
|
| 240 |
request, err := http.NewRequest("GET", "/test", nil) |
| 241 |
if err != nil { |
| 242 |
t.Fatalf("unexpected error: %v", err) |
| 243 |
} |
| 244 |
// the request context should not have any deadline by default |
| 245 |
if _, hasDeadline := request.Context().Deadline(); hasDeadline { |
| 246 |
t.Fatalf("expected request context to not have any deadline") |
| 247 |
} |
| 248 |
|
| 249 |
recorder := httptest.NewRecorder() |
| 250 |
handler.ServeHTTP(recorder, request) |
| 251 |
if atomic.LoadInt32(&invokedGot) != 1 { |
| 252 |
t.Errorf("expected collection deleter to be invoked") |
| 253 |
} |
| 254 |
if atomic.LoadInt32(&hasDeadlineGot) > 0 { |
| 255 |
t.Errorf("expected context to not have any deadline") |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
type fakeCollectionDeleterFunc func(ctx context.Context, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions, listOptions *metainternalversion.ListOptions) (runtime.Object, error) |
| 260 |
|
| 261 |
func (f fakeCollectionDeleterFunc) DeleteCollection(ctx context.Context, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions, listOptions *metainternalversion.ListOptions) (runtime.Object, error) { |
| 262 |
return f(ctx, deleteValidation, options, listOptions) |
| 263 |
} |
| 264 |
|
| 265 |
type fakeSerializer struct { |
| 266 |
serializer runtime.Serializer |
| 267 |
} |
| 268 |
|
| 269 |
func (n *fakeSerializer) SupportedMediaTypes() []runtime.SerializerInfo { |
| 270 |
return []runtime.SerializerInfo{ |
| 271 |
{ |
| 272 |
MediaType: "application/json", |
| 273 |
MediaTypeType: "application", |
| 274 |
MediaTypeSubType: "json", |
| 275 |
}, |
| 276 |
} |
| 277 |
} |
| 278 |
func (n *fakeSerializer) EncoderForVersion(serializer runtime.Encoder, gv runtime.GroupVersioner) runtime.Encoder { |
| 279 |
return n.serializer |
| 280 |
} |
| 281 |
func (n *fakeSerializer) DecoderToVersion(serializer runtime.Decoder, gv runtime.GroupVersioner) runtime.Decoder { |
| 282 |
return n.serializer |
| 283 |
} |
| 284 |
|
| 285 |
func TestAuthorizeUnsafeDelete(t *testing.T) { |
| 286 |
const verbWant = "unsafe-delete-ignore-read-errors" |
| 287 |
tests := []struct { |
| 288 |
name string |
| 289 |
reqInfo *request.RequestInfo |
| 290 |
attr admission.Attributes |
| 291 |
authz authorizer.Authorizer |
| 292 |
err func(admission.Attributes) error |
| 293 |
}{ |
| 294 |
{ |
| 295 |
name: "operation is not delete, admit", |
| 296 |
attr: newAttributes(attributes{operation: admission.Update}), |
| 297 |
authz: nil, // Authorize should not be invoked |
| 298 |
}, |
| 299 |
{ |
| 300 |
name: "feature enabled, delete, operation option is nil, admit", |
| 301 |
attr: newAttributes(attributes{ |
| 302 |
operation: admission.Delete, |
| 303 |
operationOptions: nil, |
| 304 |
}), |
| 305 |
authz: nil, // Authorize should not be invoked |
| 306 |
}, |
| 307 |
{ |
| 308 |
name: "delete, operation option is not a match, forbid", |
| 309 |
attr: newAttributes(attributes{ |
| 310 |
operation: admission.Delete, |
| 311 |
operationOptions: &metav1.PatchOptions{}, |
| 312 |
}), |
| 313 |
authz: nil, // Authorize should not be invoked |
| 314 |
err: func(admission.Attributes) error { |
| 315 |
return errors.NewInternalError(fmt.Errorf("expected an option of type: %T, but got: %T", &metav1.DeleteOptions{}, &metav1.PatchOptions{})) |
| 316 |
}, |
| 317 |
}, |
| 318 |
{ |
| 319 |
name: "delete, IgnoreStoreReadErrorWithClusterBreakingPotential is nil, admit", |
| 320 |
attr: newAttributes(attributes{ |
| 321 |
operation: admission.Delete, |
| 322 |
operationOptions: &metav1.DeleteOptions{ |
| 323 |
IgnoreStoreReadErrorWithClusterBreakingPotential: nil, |
| 324 |
}, |
| 325 |
}), |
| 326 |
authz: nil, // Authorize should not be invoked |
| 327 |
}, |
| 328 |
{ |
| 329 |
name: "delete, IgnoreStoreReadErrorWithClusterBreakingPotential is false, admit", |
| 330 |
attr: newAttributes(attributes{ |
| 331 |
operation: admission.Delete, |
| 332 |
operationOptions: &metav1.DeleteOptions{ |
| 333 |
IgnoreStoreReadErrorWithClusterBreakingPotential: ptr.To[bool](false), |
| 334 |
}, |
| 335 |
}), |
| 336 |
authz: nil, // Authorize should not be invoked |
| 337 |
}, |
| 338 |
{ |
| 339 |
name: "feature enabled, delete, IgnoreStoreReadErrorWithClusterBreakingPotential is true, no RequestInfo in request context, forbid", |
| 340 |
reqInfo: nil, |
| 341 |
attr: newAttributes(attributes{ |
| 342 |
operation: admission.Delete, |
| 343 |
operationOptions: &metav1.DeleteOptions{ |
| 344 |
IgnoreStoreReadErrorWithClusterBreakingPotential: ptr.To[bool](true), |
| 345 |
}, |
| 346 |
}), |
| 347 |
authz: nil, |
| 348 |
err: func(attr admission.Attributes) error { |
| 349 |
return admission.NewForbidden(attr, fmt.Errorf("no RequestInfo found in the context")) |
| 350 |
}, |
| 351 |
}, |
| 352 |
{ |
| 353 |
name: "delete, IgnoreStoreReadErrorWithClusterBreakingPotential is true, subresource request, forbid", |
| 354 |
reqInfo: &request.RequestInfo{IsResourceRequest: true}, |
| 355 |
attr: newAttributes(attributes{ |
| 356 |
operation: admission.Delete, |
| 357 |
subresource: "foo", |
| 358 |
operationOptions: &metav1.DeleteOptions{ |
| 359 |
IgnoreStoreReadErrorWithClusterBreakingPotential: ptr.To[bool](true), |
| 360 |
}, |
| 361 |
}), |
| 362 |
authz: nil, |
| 363 |
err: func(attr admission.Attributes) error { |
| 364 |
return admission.NewForbidden(attr, fmt.Errorf("ignoreStoreReadErrorWithClusterBreakingPotential delete option is not allowed on a subresource or non-resource request")) |
| 365 |
}, |
| 366 |
}, |
| 367 |
{ |
| 368 |
name: "delete, IgnoreStoreReadErrorWithClusterBreakingPotential is true, subresource request, forbid", |
| 369 |
reqInfo: &request.RequestInfo{IsResourceRequest: false}, |
| 370 |
attr: newAttributes(attributes{ |
| 371 |
operation: admission.Delete, |
| 372 |
subresource: "", |
| 373 |
operationOptions: &metav1.DeleteOptions{ |
| 374 |
IgnoreStoreReadErrorWithClusterBreakingPotential: ptr.To[bool](true), |
| 375 |
}, |
| 376 |
}), |
| 377 |
authz: nil, |
| 378 |
err: func(attr admission.Attributes) error { |
| 379 |
return admission.NewForbidden(attr, fmt.Errorf("ignoreStoreReadErrorWithClusterBreakingPotential delete option is not allowed on a subresource or non-resource request")) |
| 380 |
}, |
| 381 |
}, |
| 382 |
{ |
| 383 |
name: "delete, IgnoreStoreReadErrorWithClusterBreakingPotential is true, authorizer returns error, forbid", |
| 384 |
reqInfo: &request.RequestInfo{IsResourceRequest: true}, |
| 385 |
attr: newAttributes(attributes{ |
| 386 |
subresource: "", |
| 387 |
operation: admission.Delete, |
| 388 |
operationOptions: &metav1.DeleteOptions{ |
| 389 |
IgnoreStoreReadErrorWithClusterBreakingPotential: ptr.To[bool](true), |
| 390 |
}, |
| 391 |
}), |
| 392 |
authz: &fakeAuthorizer{err: fmt.Errorf("unexpected error")}, |
| 393 |
err: func(attr admission.Attributes) error { |
| 394 |
return admission.NewForbidden(attr, fmt.Errorf("error while checking permission for %q, %w", verbWant, fmt.Errorf("unexpected error"))) |
| 395 |
}, |
| 396 |
}, |
| 397 |
{ |
| 398 |
name: "delete, IgnoreStoreReadErrorWithClusterBreakingPotential is true, user does not have permission, forbid", |
| 399 |
reqInfo: &request.RequestInfo{IsResourceRequest: true}, |
| 400 |
attr: newAttributes(attributes{ |
| 401 |
operation: admission.Delete, |
| 402 |
subresource: "", |
| 403 |
operationOptions: &metav1.DeleteOptions{ |
| 404 |
IgnoreStoreReadErrorWithClusterBreakingPotential: ptr.To[bool](true), |
| 405 |
}, |
| 406 |
}), |
| 407 |
authz: &fakeAuthorizer{ |
| 408 |
decision: authorizer.DecisionDeny, |
| 409 |
reason: "does not have permission", |
| 410 |
}, |
| 411 |
err: func(attr admission.Attributes) error { |
| 412 |
return admission.NewForbidden(attr, fmt.Errorf("not permitted to do %q, reason: %s", verbWant, "does not have permission")) |
| 413 |
}, |
| 414 |
}, |
| 415 |
{ |
| 416 |
name: "delete, IgnoreStoreReadErrorWithClusterBreakingPotential is true, authorizer gives no opinion, forbid", |
| 417 |
reqInfo: &request.RequestInfo{IsResourceRequest: true}, |
| 418 |
attr: newAttributes(attributes{ |
| 419 |
operation: admission.Delete, |
| 420 |
subresource: "", |
| 421 |
operationOptions: &metav1.DeleteOptions{ |
| 422 |
IgnoreStoreReadErrorWithClusterBreakingPotential: ptr.To[bool](true), |
| 423 |
}, |
| 424 |
}), |
| 425 |
authz: &fakeAuthorizer{ |
| 426 |
decision: authorizer.DecisionNoOpinion, |
| 427 |
reason: "no opinion", |
| 428 |
}, |
| 429 |
err: func(attr admission.Attributes) error { |
| 430 |
return admission.NewForbidden(attr, fmt.Errorf("not permitted to do %q, reason: %s", verbWant, "no opinion")) |
| 431 |
}, |
| 432 |
}, |
| 433 |
{ |
| 434 |
name: "delete, IgnoreStoreReadErrorWithClusterBreakingPotential is true, user has permission, admit", |
| 435 |
reqInfo: &request.RequestInfo{IsResourceRequest: true}, |
| 436 |
attr: newAttributes(attributes{ |
| 437 |
operation: admission.Delete, |
| 438 |
subresource: "", |
| 439 |
operationOptions: &metav1.DeleteOptions{ |
| 440 |
IgnoreStoreReadErrorWithClusterBreakingPotential: ptr.To[bool](true), |
| 441 |
}, |
| 442 |
userInfo: &user.DefaultInfo{Name: "foo"}, |
| 443 |
}), |
| 444 |
authz: &fakeAuthorizer{ |
| 445 |
decision: authorizer.DecisionAllow, |
| 446 |
reason: "permitted", |
| 447 |
}, |
| 448 |
}, |
| 449 |
} |
| 450 |
|
| 451 |
for _, test := range tests { |
| 452 |
t.Run(test.name, func(t *testing.T) { |
| 453 |
var want error |
| 454 |
if test.err != nil { |
| 455 |
want = test.err(test.attr) |
| 456 |
} |
| 457 |
|
| 458 |
ctx := context.Background() |
| 459 |
if test.reqInfo != nil { |
| 460 |
ctx = request.WithRequestInfo(ctx, test.reqInfo) |
| 461 |
} |
| 462 |
|
| 463 |
// wrap the attributes so we can access the annotations set during admission |
| 464 |
attrs := &fakeAttributes{Attributes: test.attr} |
| 465 |
got := authorizeUnsafeDelete(ctx, attrs, test.authz) |
| 466 |
switch { |
| 467 |
case want != nil: |
| 468 |
if got == nil || want.Error() != got.Error() { |
| 469 |
t.Errorf("expected error: %v, but got: %v", want, got) |
| 470 |
} |
| 471 |
default: |
| 472 |
if got != nil { |
| 473 |
t.Errorf("expected no error, but got: %v", got) |
| 474 |
} |
| 475 |
} |
| 476 |
}) |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
// attributes of interest for this test |
| 481 |
type attributes struct { |
| 482 |
operation admission.Operation |
| 483 |
operationOptions runtime.Object |
| 484 |
userInfo user.Info |
| 485 |
subresource string |
| 486 |
} |
| 487 |
|
| 488 |
func newAttributes(attr attributes) admission.Attributes { |
| 489 |
return admission.NewAttributesRecord( |
| 490 |
nil, // this plugin should never inspect the object |
| 491 |
nil, // old object, this plugin should never inspect it |
| 492 |
schema.GroupVersionKind{}, // this plugin should never inspect kind |
| 493 |
"", // namespace, leave it empty, this plugin only passes it along to the authorizer |
| 494 |
"", // name, leave it empty, this plugin only passes it along to the authorizer |
| 495 |
schema.GroupVersionResource{}, // resource, leave it empty, this plugin only passes it along to the authorizer |
| 496 |
attr.subresource, |
| 497 |
attr.operation, |
| 498 |
attr.operationOptions, |
| 499 |
false, // dryRun, this plugin should never inspect this attribute |
| 500 |
attr.userInfo) |
| 501 |
} |
| 502 |
|
| 503 |
type fakeAttributes struct { |
| 504 |
admission.Attributes |
| 505 |
annotations map[string]string |
| 506 |
} |
| 507 |
|
| 508 |
func (f *fakeAttributes) AddAnnotation(key, value string) error { |
| 509 |
if err := f.Attributes.AddAnnotation(key, value); err != nil { |
| 510 |
return err |
| 511 |
} |
| 512 |
|
| 513 |
if len(f.annotations) == 0 { |
| 514 |
f.annotations = map[string]string{} |
| 515 |
} |
| 516 |
f.annotations[key] = value |
| 517 |
return nil |
| 518 |
} |
| 519 |
|
| 520 |
type fakeAuthorizer struct { |
| 521 |
decision authorizer.Decision |
| 522 |
reason string |
| 523 |
err error |
| 524 |
} |
| 525 |
|
| 526 |
func (authorizer fakeAuthorizer) Authorize(ctx context.Context, a authorizer.Attributes) (authorized authorizer.Decision, reason string, err error) { |
| 527 |
return authorizer.decision, authorizer.reason, authorizer.err |
| 528 |
} |