2017-04-30 09:22:07 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2018-08-24 14:20:03 +00:00
|
|
|
"github.com/containous/traefik/ip"
|
2017-04-30 09:22:07 +00:00
|
|
|
"github.com/containous/traefik/log"
|
2018-01-10 16:48:04 +00:00
|
|
|
"github.com/containous/traefik/middlewares/tracing"
|
2017-05-26 15:03:14 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-07-19 10:02:51 +00:00
|
|
|
"github.com/urfave/negroni"
|
2017-04-30 09:22:07 +00:00
|
|
|
)
|
|
|
|
|
2017-10-10 12:50:03 +00:00
|
|
|
// IPWhiteLister is a middleware that provides Checks of the Requesting IP against a set of Whitelists
|
|
|
|
type IPWhiteLister struct {
|
|
|
|
handler negroni.Handler
|
2018-08-24 14:20:03 +00:00
|
|
|
whiteLister *ip.Checker
|
|
|
|
strategy ip.Strategy
|
2017-04-30 09:22:07 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 16:40:04 +00:00
|
|
|
// NewIPWhiteLister builds a new IPWhiteLister given a list of CIDR-Strings to whitelist
|
2018-08-24 14:20:03 +00:00
|
|
|
func NewIPWhiteLister(whiteList []string, strategy ip.Strategy) (*IPWhiteLister, error) {
|
2018-03-23 16:40:04 +00:00
|
|
|
if len(whiteList) == 0 {
|
|
|
|
return nil, errors.New("no white list provided")
|
2017-04-30 09:22:07 +00:00
|
|
|
}
|
|
|
|
|
2018-08-24 14:20:03 +00:00
|
|
|
checker, err := ip.NewChecker(whiteList)
|
2017-10-10 12:50:03 +00:00
|
|
|
if err != nil {
|
2018-03-23 16:40:04 +00:00
|
|
|
return nil, fmt.Errorf("parsing CIDR whitelist %s: %v", whiteList, err)
|
2017-04-30 09:22:07 +00:00
|
|
|
}
|
2018-08-24 14:20:03 +00:00
|
|
|
|
|
|
|
whiteLister := IPWhiteLister{
|
|
|
|
strategy: strategy,
|
|
|
|
whiteLister: checker,
|
|
|
|
}
|
2017-04-30 09:22:07 +00:00
|
|
|
|
2017-10-10 12:50:03 +00:00
|
|
|
whiteLister.handler = negroni.HandlerFunc(whiteLister.handle)
|
2018-04-03 16:36:03 +00:00
|
|
|
log.Debugf("configured IP white list: %s", whiteList)
|
2017-04-30 09:22:07 +00:00
|
|
|
|
2017-10-10 12:50:03 +00:00
|
|
|
return &whiteLister, nil
|
2017-04-30 09:22:07 +00:00
|
|
|
}
|
|
|
|
|
2017-10-10 12:50:03 +00:00
|
|
|
func (wl *IPWhiteLister) handle(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
2018-08-24 14:20:03 +00:00
|
|
|
err := wl.whiteLister.IsAuthorized(wl.strategy.GetIP(r))
|
2017-10-10 12:50:03 +00:00
|
|
|
if err != nil {
|
2018-04-23 14:20:05 +00:00
|
|
|
tracing.SetErrorAndDebugLog(r, "request %+v - rejecting: %v", r, err)
|
2017-10-10 12:50:03 +00:00
|
|
|
reject(w)
|
|
|
|
return
|
2017-04-30 09:22:07 +00:00
|
|
|
}
|
2018-08-24 14:20:03 +00:00
|
|
|
log.Debugf("Accept %s: %+v", wl.strategy.GetIP(r), r)
|
2018-09-07 07:40:03 +00:00
|
|
|
tracing.SetErrorAndDebugLog(r, "request %+v matched white list %v - passing", r, wl.whiteLister)
|
2018-04-23 14:20:05 +00:00
|
|
|
next.ServeHTTP(w, r)
|
2017-04-30 09:22:07 +00:00
|
|
|
}
|
|
|
|
|
2017-10-10 12:50:03 +00:00
|
|
|
func (wl *IPWhiteLister) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
|
|
|
wl.handler.ServeHTTP(rw, r, next)
|
|
|
|
}
|
|
|
|
|
2017-04-30 09:22:07 +00:00
|
|
|
func reject(w http.ResponseWriter) {
|
|
|
|
statusCode := http.StatusForbidden
|
|
|
|
|
|
|
|
w.WriteHeader(statusCode)
|
2018-04-23 14:20:05 +00:00
|
|
|
_, err := w.Write([]byte(http.StatusText(statusCode)))
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
2017-04-30 09:22:07 +00:00
|
|
|
}
|