k8s.io/apiserver@v0.31.1/pkg/endpoints/handlers/responsewriters/errors.go (about) 1 /* 2 Copyright 2014 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 responsewriters 18 19 import ( 20 "context" 21 "fmt" 22 "net/http" 23 "strings" 24 25 apierrors "k8s.io/apimachinery/pkg/api/errors" 26 "k8s.io/apimachinery/pkg/runtime" 27 "k8s.io/apimachinery/pkg/runtime/schema" 28 utilruntime "k8s.io/apimachinery/pkg/util/runtime" 29 "k8s.io/apiserver/pkg/authorization/authorizer" 30 ) 31 32 // Avoid emitting errors that look like valid HTML. Quotes are okay. 33 var sanitizer = strings.NewReplacer(`&`, "&", `<`, "<", `>`, ">") 34 35 // Forbidden renders a simple forbidden error 36 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)) 38 w.Header().Set("X-Content-Type-Options", "nosniff") 39 40 var errMsg string 41 if len(reason) == 0 { 42 errMsg = fmt.Sprintf("%s", msg) 43 } else { 44 errMsg = fmt.Sprintf("%s: %s", msg, reason) 45 } 46 gv := schema.GroupVersion{Group: attributes.GetAPIGroup(), Version: attributes.GetAPIVersion()} 47 gr := schema.GroupResource{Group: attributes.GetAPIGroup(), Resource: attributes.GetResource()} 48 ErrorNegotiated(apierrors.NewForbidden(gr, attributes.GetName(), fmt.Errorf(errMsg)), s, gv, w, req) 49 } 50 51 func forbiddenMessage(attributes authorizer.Attributes) string { 52 username := "" 53 if user := attributes.GetUser(); user != nil { 54 username = user.GetName() 55 } 56 57 if !attributes.IsResourceRequest() { 58 return fmt.Sprintf("User %q cannot %s path %q", username, attributes.GetVerb(), attributes.GetPath()) 59 } 60 61 resource := attributes.GetResource() 62 if subresource := attributes.GetSubresource(); len(subresource) > 0 { 63 resource = resource + "/" + subresource 64 } 65 66 if ns := attributes.GetNamespace(); len(ns) > 0 { 67 return fmt.Sprintf("User %q cannot %s resource %q in API group %q in the namespace %q", username, attributes.GetVerb(), resource, attributes.GetAPIGroup(), ns) 68 } 69 70 return fmt.Sprintf("User %q cannot %s resource %q in API group %q at the cluster scope", username, attributes.GetVerb(), resource, attributes.GetAPIGroup()) 71 } 72 73 // InternalError renders a simple internal error 74 func InternalError(w http.ResponseWriter, req *http.Request, err error) { 75 http.Error(w, sanitizer.Replace(fmt.Sprintf("Internal Server Error: %q: %v", req.RequestURI, err)), 76 http.StatusInternalServerError) 77 utilruntime.HandleError(err) 78 }