2018-11-14 09:18:03 +00:00
|
|
|
package ipwhitelist
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-04-01 13:30:07 +00:00
|
|
|
"errors"
|
2018-11-14 09:18:03 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/opentracing/opentracing-go/ext"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/config/dynamic"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/ip"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/log"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/middlewares"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/tracing"
|
2018-11-14 09:18:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
typeName = "IPWhiteLister"
|
|
|
|
)
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// ipWhiteLister is a middleware that provides Checks of the Requesting IP against a set of Whitelists.
|
2018-11-14 09:18:03 +00:00
|
|
|
type ipWhiteLister struct {
|
|
|
|
next http.Handler
|
|
|
|
whiteLister *ip.Checker
|
|
|
|
strategy ip.Strategy
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// New builds a new IPWhiteLister given a list of CIDR-Strings to whitelist.
|
2019-07-10 07:26:04 +00:00
|
|
|
func New(ctx context.Context, next http.Handler, config dynamic.IPWhiteList, name string) (http.Handler, error) {
|
2019-09-13 17:28:04 +00:00
|
|
|
logger := log.FromContext(middlewares.GetLoggerCtx(ctx, name, typeName))
|
2018-11-14 09:18:03 +00:00
|
|
|
logger.Debug("Creating middleware")
|
|
|
|
|
|
|
|
if len(config.SourceRange) == 0 {
|
|
|
|
return nil, errors.New("sourceRange is empty, IPWhiteLister not created")
|
|
|
|
}
|
|
|
|
|
|
|
|
checker, err := ip.NewChecker(config.SourceRange)
|
|
|
|
if err != nil {
|
2020-05-11 10:06:07 +00:00
|
|
|
return nil, fmt.Errorf("cannot parse CIDR whitelist %s: %w", config.SourceRange, err)
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
strategy, err := config.IPStrategy.Get()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Debugf("Setting up IPWhiteLister with sourceRange: %s", config.SourceRange)
|
2019-09-13 17:28:04 +00:00
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
return &ipWhiteLister{
|
|
|
|
strategy: strategy,
|
|
|
|
whiteLister: checker,
|
|
|
|
next: next,
|
|
|
|
name: name,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wl *ipWhiteLister) GetTracingInformation() (string, ext.SpanKindEnum) {
|
|
|
|
return wl.name, tracing.SpanKindNoneEnum
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wl *ipWhiteLister) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
2019-09-13 17:28:04 +00:00
|
|
|
ctx := middlewares.GetLoggerCtx(req.Context(), wl.name, typeName)
|
|
|
|
logger := log.FromContext(ctx)
|
2018-11-14 09:18:03 +00:00
|
|
|
|
|
|
|
err := wl.whiteLister.IsAuthorized(wl.strategy.GetIP(req))
|
|
|
|
if err != nil {
|
|
|
|
logMessage := fmt.Sprintf("rejecting request %+v: %v", req, err)
|
|
|
|
logger.Debug(logMessage)
|
|
|
|
tracing.SetErrorWithEvent(req, logMessage)
|
2019-09-13 17:28:04 +00:00
|
|
|
reject(ctx, rw)
|
2018-11-14 09:18:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
logger.Debugf("Accept %s: %+v", wl.strategy.GetIP(req), req)
|
|
|
|
|
|
|
|
wl.next.ServeHTTP(rw, req)
|
|
|
|
}
|
|
|
|
|
2019-09-13 17:28:04 +00:00
|
|
|
func reject(ctx context.Context, rw http.ResponseWriter) {
|
2018-11-14 09:18:03 +00:00
|
|
|
statusCode := http.StatusForbidden
|
|
|
|
|
|
|
|
rw.WriteHeader(statusCode)
|
|
|
|
_, err := rw.Write([]byte(http.StatusText(statusCode)))
|
|
|
|
if err != nil {
|
2019-09-13 17:28:04 +00:00
|
|
|
log.FromContext(ctx).Error(err)
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
}
|