traefik/pkg/middlewares/ipallowlist/ip_allowlist.go

89 lines
2.3 KiB
Go
Raw Normal View History

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"
2022-11-21 17:36:05 +00:00
"github.com/rs/zerolog/log"
2023-02-03 14:24:05 +00:00
"github.com/traefik/traefik/v3/pkg/config/dynamic"
"github.com/traefik/traefik/v3/pkg/ip"
"github.com/traefik/traefik/v3/pkg/middlewares"
"github.com/traefik/traefik/v3/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) {
2022-11-21 17:36:05 +00:00
logger := middlewares.GetLogger(ctx, name, typeName)
logger.Debug().Msg("Creating middleware")
2018-11-14 09:18:03 +00:00
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-11-21 17:36:05 +00:00
logger.Debug().Msgf("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) {
2022-11-21 17:36:05 +00:00
logger := middlewares.GetLogger(req.Context(), al.name, typeName)
ctx := logger.WithContext(req.Context())
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 {
msg := fmt.Sprintf("Rejecting IP %s: %v", clientIP, err)
2022-11-21 17:36:05 +00:00
logger.Debug().Msg(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-11-21 17:36:05 +00:00
logger.Debug().Msgf("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 {
2022-11-21 17:36:05 +00:00
log.Ctx(ctx).Error().Err(err).Send()
2018-11-14 09:18:03 +00:00
}
}