| 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 |
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)) |
| 44 |
|
| 45 |
var errMsg error |
| 46 |
if len(reason) == 0 { |
| 47 |
errMsg = fmt.Errorf("%s", msg) |
| 48 |
} else { |
| 49 |
errMsg = fmt.Errorf("%s: %s", msg, reason) |
| 50 |
} |
| 51 |
|
| 52 |
gr := schema.GroupResource{Group: attributes.GetAPIGroup(), Resource: attributes.GetResource()} |
| 53 |
|
| 54 |
return apierrors.NewForbidden(gr, attributes.GetName(), errMsg) |
| 55 |
} |
| 56 |
|
| 57 |
func forbiddenMessage(attributes authorizer.Attributes) string { |
| 58 |
username := "" |
| 59 |
if user := attributes.GetUser(); user != nil { |
| 60 |
username = user.GetName() |
| 61 |
} |
| 62 |
|
| 63 |
if !attributes.IsResourceRequest() { |
| 64 |
return fmt.Sprintf("User %q cannot %s path %q", username, attributes.GetVerb(), attributes.GetPath()) |
| 65 |
} |
| 66 |
|
| 67 |
resource := attributes.GetResource() |
| 68 |
if subresource := attributes.GetSubresource(); len(subresource) > 0 { |
| 69 |
resource = resource + "/" + subresource |
| 70 |
} |
| 71 |
|
| 72 |
if ns := attributes.GetNamespace(); len(ns) > 0 { |
| 73 |
return fmt.Sprintf("User %q cannot %s resource %q in API group %q in the namespace %q", username, attributes.GetVerb(), resource, attributes.GetAPIGroup(), ns) |
| 74 |
} |
| 75 |
|
| 76 |
return fmt.Sprintf("User %q cannot %s resource %q in API group %q at the cluster scope", username, attributes.GetVerb(), resource, attributes.GetAPIGroup()) |
| 77 |
} |
| 78 |
|
| 79 |
// InternalError renders a simple internal error |
| 80 |
func InternalError(w http.ResponseWriter, req *http.Request, err error) { |
| 81 |
http.Error(w, sanitizer.Replace(fmt.Sprintf("Internal Server Error: %q: %v", req.RequestURI, err)), |
| 82 |
http.StatusInternalServerError) |
| 83 |
utilruntime.HandleError(err) |
| 84 |
} |