2022-10-26 15:16:05 +00:00
|
|
|
package ipallowlist
|
2018-11-14 09:18:03 +00:00
|
|
|
|
|
|
|
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 (
|
2022-10-26 15:16:05 +00:00
|
|
|
typeName = "IPAllowLister"
|
2018-11-14 09:18:03 +00:00
|
|
|
)
|
|
|
|
|
2022-10-26 15:16:05 +00:00
|
|
|
// ipAllowLister is a middleware that provides Checks of the Requesting IP against a set of Allowlists.
|
|
|
|
type ipAllowLister struct {
|
2018-11-14 09:18:03 +00:00
|
|
|
next http.Handler
|
2022-10-26 15:16:05 +00:00
|
|
|
allowLister *ip.Checker
|
2018-11-14 09:18:03 +00:00
|
|
|
strategy ip.Strategy
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
2022-10-26 15:16:05 +00:00
|
|
|
// New builds a new IPAllowLister given a list of CIDR-Strings to allow.
|
|
|
|
func New(ctx context.Context, next http.Handler, config dynamic.IPAllowList, 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 {
|
2022-10-26 15:16:05 +00:00
|
|
|
return nil, errors.New("sourceRange is empty, IPAllowLister not created")
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
checker, err := ip.NewChecker(config.SourceRange)
|
|
|
|
if err != nil {
|
2022-10-26 15:16:05 +00:00
|
|
|
return nil, fmt.Errorf("cannot parse CIDRs %s: %w", config.SourceRange, err)
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
strategy, err := config.IPStrategy.Get()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-26 15:16:05 +00:00
|
|
|
logger.Debugf("Setting up IPAllowLister with sourceRange: %s", config.SourceRange)
|
2019-09-13 17:28:04 +00:00
|
|
|
|
2022-10-26 15:16:05 +00:00
|
|
|
return &ipAllowLister{
|
2018-11-14 09:18:03 +00:00
|
|
|
strategy: strategy,
|
2022-10-26 15:16:05 +00:00
|
|
|
allowLister: checker,
|
2018-11-14 09:18:03 +00:00
|
|
|
next: next,
|
|
|
|
name: name,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-10-26 15:16:05 +00:00
|
|
|
func (al *ipAllowLister) GetTracingInformation() (string, ext.SpanKindEnum) {
|
|
|
|
return al.name, tracing.SpanKindNoneEnum
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 15:16:05 +00:00
|
|
|
func (al *ipAllowLister) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
ctx := middlewares.GetLoggerCtx(req.Context(), al.name, typeName)
|
2019-09-13 17:28:04 +00:00
|
|
|
logger := log.FromContext(ctx)
|
2018-11-14 09:18:03 +00:00
|
|
|
|
2022-10-26 15:16:05 +00:00
|
|
|
clientIP := al.strategy.GetIP(req)
|
|
|
|
err := al.allowLister.IsAuthorized(clientIP)
|
2018-11-14 09:18:03 +00:00
|
|
|
if err != nil {
|
2022-08-11 14:20:14 +00:00
|
|
|
msg := fmt.Sprintf("Rejecting IP %s: %v", clientIP, err)
|
|
|
|
logger.Debug(msg)
|
|
|
|
tracing.SetErrorWithEvent(req, msg)
|
2019-09-13 17:28:04 +00:00
|
|
|
reject(ctx, rw)
|
2018-11-14 09:18:03 +00:00
|
|
|
return
|
|
|
|
}
|
2022-08-11 14:20:14 +00:00
|
|
|
logger.Debugf("Accepting IP %s", clientIP)
|
2018-11-14 09:18:03 +00:00
|
|
|
|
2022-10-26 15:16:05 +00:00
|
|
|
al.next.ServeHTTP(rw, req)
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|