Skip to content
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

Return appropriate status codes for rejected requests #97

Merged
merged 1 commit into from
Apr 24, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions pkg/webhook/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"context"
"flag"
"fmt"
"net/http"
"strings"

opa "github.com/open-policy-agent/frameworks/constraint/pkg/client"
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
authenticationv1 "k8s.io/api/authentication/v1"
k8sruntime "k8s.io/apimachinery/pkg/runtime"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/serializer"
apitypes "k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/manager"
Expand Down Expand Up @@ -115,15 +117,25 @@ func (h *validationHandler) Handle(ctx context.Context, req atypes.Request) atyp
resp, err := h.opa.Review(ctx, req.AdmissionRequest)
if err != nil {
log.Error(err, "error executing query")
return admission.ValidationResponse(false, err.Error())
vResp := admission.ValidationResponse(false, err.Error())
if vResp.Response.Result == nil {
vResp.Response.Result = &metav1.Status{}
}
vResp.Response.Result.Code = http.StatusInternalServerError
return vResp
}
res := resp.Results()
if len(res) != 0 {
var msgs []string
for _, r := range res {
msgs = append(msgs, r.Msg)
}
return admission.ValidationResponse(false, strings.Join(msgs, "\n"))
vResp := admission.ValidationResponse(false, strings.Join(msgs, "\n"))
if vResp.Response.Result == nil {
vResp.Response.Result = &metav1.Status{}
}
vResp.Response.Result.Code = http.StatusForbidden
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maxsmythe

Don't you want to send back 422 (Unprocessable entity) instead of 403 (Forbidden)

403 is what is sent back in an RBAC/AUTH failure and this is going to confuse the living daylights out of users.

We use 422 in a number of other places to indicate validation failures:

e.g. https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/generated.proto#L728

return vResp
}
return admission.ValidationResponse(true, "")
}
Expand Down