khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 1 | // Copyright 2015 The etcd Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package v2http |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "net/http" |
| 20 | |
| 21 | "go.etcd.io/etcd/etcdserver/api" |
| 22 | "go.etcd.io/etcd/etcdserver/api/v2http/httptypes" |
| 23 | ) |
| 24 | |
| 25 | func authCapabilityHandler(fn func(http.ResponseWriter, *http.Request)) http.HandlerFunc { |
| 26 | return func(w http.ResponseWriter, r *http.Request) { |
| 27 | if !api.IsCapabilityEnabled(api.AuthCapability) { |
| 28 | notCapable(w, r, api.AuthCapability) |
| 29 | return |
| 30 | } |
| 31 | fn(w, r) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func notCapable(w http.ResponseWriter, r *http.Request, c api.Capability) { |
| 36 | herr := httptypes.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Not capable of accessing %s feature during rolling upgrades.", c)) |
| 37 | if err := herr.WriteTo(w); err != nil { |
| 38 | plog.Debugf("error writing HTTPError (%v) to %s", err, r.RemoteAddr) |
| 39 | } |
| 40 | } |