/* Copyright 2014 The Kubernetes Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package kubectl import ( "fmt" "net" "net/http" "net/http/httputil" "net/url" "os" "regexp" "strings" "time" "github.com/golang/glog" restclient "k8s.io/client-go/rest" "k8s.io/kubernetes/pkg/util" ) const ( DefaultHostAcceptRE = "^localhost$,^127\\.0\\.0\\.1$,^\\[::1\\]$" DefaultPathAcceptRE = "^.*" DefaultPathRejectRE = "^/api/.*/pods/.*/exec,^/api/.*/pods/.*/attach" DefaultMethodRejectRE = "POST,PUT,PATCH" ) var ( // The reverse proxy will periodically flush the io writer at this frequency. // Only matters for long poll connections like the one used to watch. With an // interval of 0 the reverse proxy will buffer content sent on any connection // with transfer-encoding=chunked. // TODO: Flush after each chunk so the client doesn't suffer a 100ms latency per // watch event. ReverseProxyFlushInterval = 100 * time.Millisecond ) // FilterServer rejects requests which don't match one of the specified regular expressions type FilterServer struct { // Only paths that match this regexp will be accepted AcceptPaths []*regexp.Regexp // Paths that match this regexp will be rejected, even if they match the above RejectPaths []*regexp.Regexp // Hosts are required to match this list of regexp AcceptHosts []*regexp.Regexp // Methods that match this regexp are rejected RejectMethods []*regexp.Regexp // The delegate to call to handle accepted requests. delegate http.Handler } // Splits a comma separated list of regexps into an array of Regexp objects. func MakeRegexpArray(str string) ([]*regexp.Regexp, error) { parts := strings.Split(str, ",") result := make([]*regexp.Regexp, len(parts)) for ix := range parts { re, err := regexp.Compile(parts[ix]) if err != nil { return nil, err } result[ix] = re } return result, nil } func MakeRegexpArrayOrDie(str string) []*regexp.Regexp { result, err := MakeRegexpArray(str) if err != nil { glog.Fatalf("Error compiling re: %v", err) } return result } func matchesRegexp(str string, regexps []*regexp.Regexp) bool { for _, re := range regexps { if re.MatchString(str) { glog.V(6).Infof("%v matched %s", str, re) return true } } return false } func (f *FilterServer) accept(method, path, host string) bool { if matchesRegexp(path, f.RejectPaths) { return false } if matchesRegexp(method, f.RejectMethods) { return false } if matchesRegexp(path, f.AcceptPaths) && matchesRegexp(host, f.AcceptHosts) { return true } return false } // Make a copy of f which passes requests along to the new delegate. func (f *FilterServer) HandlerFor(delegate http.Handler) *FilterServer { f2 := *f f2.delegate = delegate return &f2 } // Get host from a host header value like "localhost" or "localhost:8080" func extractHost(header string) (host string) { host, _, err := net.SplitHostPort(header) if err != nil { host = header } return host } func (f *FilterServer) ServeHTTP(rw http.ResponseWriter, req *http.Request) { host := extractHost(req.Host) if f.accept(req.Method, req.URL.Path, host) { glog.V(3).Infof("Filter accepting %v %v %v", req.Method, req.URL.Path, host) f.delegate.ServeHTTP(rw, req) return } glog.V(3).Infof("Filter rejecting %v %v %v", req.Method, req.URL.Path, host) rw.WriteHeader(http.StatusForbidden) rw.Write([]byte("