-
Notifications
You must be signed in to change notification settings - Fork 40k
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
Ensure 4xx+ response codes from webhook rejections #77022
Conversation
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: liggitt The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
/cc @brendandburns |
/cc @caesarxuchao |
staging/src/k8s.io/apiserver/pkg/admission/plugin/webhook/errors/statuserror.go
Show resolved
Hide resolved
9978a34
to
5007643
Compare
staging/src/k8s.io/apiserver/pkg/admission/plugin/webhook/errors/statuserror.go
Show resolved
Hide resolved
@@ -32,6 +33,15 @@ func ToStatusErr(webhookName string, result *metav1.Status) *apierrors.StatusErr | |||
result = &metav1.Status{Status: metav1.StatusFailure} | |||
} | |||
|
|||
// Make sure we don't return < 400 status codes along with a rejection | |||
if result.Code < http.StatusBadRequest { | |||
result.Code = http.StatusBadRequest |
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.
We talked about switching this to a 500 in the api-machinery call today, but the more I consider this, I think that would be misleading to the end user. The reason their request was rejected was because their request was judged to be bad, not because the server had some error.
I see this as akin to what we do here:
kubernetes/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/responsewriters/writers.go
Lines 119 to 126 in 7289c1e
_, serializer, err := negotiation.NegotiateOutputMediaType(req, s, restrictions) | |
if err != nil { | |
// if original statusCode was not successful we need to return the original error | |
// we cannot hide it behind negotiation problems | |
if statusCode < http.StatusOK || statusCode >= http.StatusBadRequest { | |
WriteRawJSON(int(statusCode), object, w) | |
return | |
} |
if we encounter a negotiation error in the process of trying to write an error response, we don't switch to a 500, we preserve the original error 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.
I personally find that argument compelling.
/lgtm |
What type of PR is this?
/kind bug
What this PR does / why we need it:
Admission webhooks can unintentionally reject requests while still returning a 200 status to the user.
This causes things like
kubectl create
to return a 0 status when their request was actually rejected.Webhook dispatch should force rejections to have >= 400 http status codes.
Special notes for your reviewer:
xref #76984
Does this PR introduce a user-facing change?: