Skip to content

Commit 857127f

Browse files
committed
SSA: improve create authz error message
Signed-off-by: Monis Khan <[email protected]>
1 parent d76eae4 commit 857127f

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

staging/src/k8s.io/apiserver/pkg/endpoints/handlers/responsewriters/errors.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,24 @@ var sanitizer = strings.NewReplacer(`&`, "&amp;", `<`, "&lt;", `>`, "&gt;")
3434

3535
// Forbidden renders a simple forbidden error
3636
func Forbidden(ctx context.Context, attributes authorizer.Attributes, w http.ResponseWriter, req *http.Request, reason string, s runtime.NegotiatedSerializer) {
37-
msg := sanitizer.Replace(forbiddenMessage(attributes))
3837
w.Header().Set("X-Content-Type-Options", "nosniff")
38+
gv := schema.GroupVersion{Group: attributes.GetAPIGroup(), Version: attributes.GetAPIVersion()}
39+
ErrorNegotiated(ForbiddenStatusError(attributes, reason), s, gv, w, req)
40+
}
41+
42+
func ForbiddenStatusError(attributes authorizer.Attributes, reason string) *apierrors.StatusError {
43+
msg := sanitizer.Replace(forbiddenMessage(attributes))
3944

4045
var errMsg string
4146
if len(reason) == 0 {
4247
errMsg = fmt.Sprintf("%s", msg)
4348
} else {
4449
errMsg = fmt.Sprintf("%s: %s", msg, reason)
4550
}
46-
gv := schema.GroupVersion{Group: attributes.GetAPIGroup(), Version: attributes.GetAPIVersion()}
51+
4752
gr := schema.GroupResource{Group: attributes.GetAPIGroup(), Resource: attributes.GetResource()}
48-
ErrorNegotiated(apierrors.NewForbidden(gr, attributes.GetName(), fmt.Errorf(errMsg)), s, gv, w, req)
53+
54+
return apierrors.NewForbidden(gr, attributes.GetName(), fmt.Errorf(errMsg))
4955
}
5056

5157
func forbiddenMessage(attributes authorizer.Attributes) string {

staging/src/k8s.io/apiserver/pkg/endpoints/handlers/update.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"k8s.io/apiserver/pkg/endpoints/handlers/finisher"
4040
requestmetrics "k8s.io/apiserver/pkg/endpoints/handlers/metrics"
4141
"k8s.io/apiserver/pkg/endpoints/handlers/negotiation"
42+
"k8s.io/apiserver/pkg/endpoints/handlers/responsewriters"
4243
"k8s.io/apiserver/pkg/endpoints/request"
4344
"k8s.io/apiserver/pkg/registry/rest"
4445
"k8s.io/apiserver/pkg/util/dryrun"
@@ -275,13 +276,7 @@ func withAuthorization(validate rest.ValidateObjectFunc, a authorizer.Authorizer
275276
}
276277

277278
// The user is not authorized to perform this action, so we need to build the error response
278-
gr := schema.GroupResource{
279-
Group: attributes.GetAPIGroup(),
280-
Resource: attributes.GetResource(),
281-
}
282-
name := attributes.GetName()
283-
err := fmt.Errorf("%v", authorizerReason)
284-
return errors.NewForbidden(gr, name, err)
279+
return responsewriters.ForbiddenStatusError(attributes, authorizerReason)
285280
}
286281
}
287282

test/integration/apiserver/apply/apply_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4805,7 +4805,7 @@ func TestCreateOnApplyFailsWithForbidden(t *testing.T) {
48054805

48064806
errCreate := ssaPod(pandaClient)
48074807

4808-
requireForbiddenPodErr(t, errCreate, `pods "test-pod" is forbidden: `) // TODO make this error better
4808+
requireForbiddenPodErr(t, errCreate, `pods "test-pod" is forbidden: User "panda" cannot create resource "pods" in API group "" in the namespace "default"`)
48094809

48104810
createPodRBACAndWait(t, adminClient, "create")
48114811

0 commit comments

Comments
 (0)