-
Notifications
You must be signed in to change notification settings - Fork 4.4k
vpa/admission-controller: limit request payload size to 5MB #9690
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /* | ||
| Copyright The Kubernetes Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package logic | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
|
||
| "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/admission-controller/resource" | ||
| ) | ||
|
|
||
| // InfiniteReader simulates an endless stream of bytes to verify LimitReader behavior. | ||
| type InfiniteReader struct{} | ||
|
|
||
| func (r *InfiniteReader) Read(p []byte) (n int, err error) { | ||
| for i := range p { | ||
| p[i] = 'a' | ||
| } | ||
| return len(p), nil | ||
| } | ||
|
|
||
| func (r *InfiniteReader) Close() error { | ||
| return nil | ||
| } | ||
|
|
||
| func TestServePayloadLimit(t *testing.T) { | ||
| tests := []struct { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add more tests here for my above comment? |
||
| name string | ||
| requestBody *bytes.Buffer | ||
| isEndless bool | ||
| expectedStatus int | ||
| }{ | ||
| { | ||
| name: "Small valid JSON payload", | ||
| requestBody: bytes.NewBufferString(`{"kind":"AdmissionReview","apiVersion":"admission.k8s.io/v1","request":{"uid":"123","resource":{"group":"autoscaling.k8s.io","version":"v1","resource":"verticalpodautoscalers"},"requestKind":{"group":"autoscaling.k8s.io","version":"v1","kind":"VerticalPodAutoscaler"}}}`), | ||
| expectedStatus: http.StatusOK, | ||
| }, | ||
| { | ||
| name: "Oversized payload exceeding 5MB limit", | ||
| isEndless: true, | ||
| expectedStatus: http.StatusOK, // Fails open, returning 200 OK with unmarshal error rather than crashing | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we rather be expecting a 413 from the server here?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question! I chose to let the read error "fail open" (proceeding with a 200 OK rather than a 413) for two reasons:
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure that's true. This changes the logic to respond with a 200, without the expected payload, see https://kubernetes.io/docs/reference/access-authn-authz/extensible-admission-controllers/#response |
||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| server := &AdmissionServer{ | ||
| resourceHandlers: make(map[metav1.GroupResource]resource.Handler), // Unused in basic HTTP parse step | ||
| } | ||
|
|
||
| var req *http.Request | ||
| if tc.isEndless { | ||
| req = httptest.NewRequest(http.MethodPost, "/admit", &InfiniteReader{}) | ||
| } else { | ||
| req = httptest.NewRequest(http.MethodPost, "/admit", tc.requestBody) | ||
| } | ||
| req.Header.Set("Content-Type", "application/json") | ||
|
|
||
| w := httptest.NewRecorder() | ||
| server.Serve(w, req) | ||
|
|
||
| assert.Equal(t, tc.expectedStatus, w.Code) | ||
| }) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How can we be sure that the error here is because of that reason?
Also, printing out 5MB without a reference to the const value can cause errors.
I would expect something like this: