traefik/pkg/middlewares/ipallowlist/ip_allowlist.go

97 lines
2.7 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"
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"
2024-01-08 08:10:06 +00:00
"go.opentelemetry.io/otel/trace"
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 {
next http.Handler
allowLister *ip.Checker
strategy ip.Strategy
name string
rejectStatusCode int
2018-11-14 09:18:03 +00:00
}
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
}
rejectStatusCode := config.RejectStatusCode
// If RejectStatusCode is not given, default to Forbidden (403).
if rejectStatusCode == 0 {
rejectStatusCode = http.StatusForbidden
} else if http.StatusText(rejectStatusCode) == "" {
return nil, fmt.Errorf("invalid HTTP status code %d", rejectStatusCode)
}
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{
strategy: strategy,
allowLister: checker,
next: next,
name: name,
rejectStatusCode: rejectStatusCode,
2018-11-14 09:18:03 +00:00
}, nil
}
2024-01-08 08:10:06 +00:00
func (al *ipAllowLister) GetTracingInformation() (string, string, trace.SpanKind) {
return al.name, typeName, trace.SpanKindInternal
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)
2024-01-08 08:10:06 +00:00
tracing.SetStatusErrorf(req.Context(), msg)
reject(ctx, al.rejectStatusCode, 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
}
func reject(ctx context.Context, statusCode int, rw http.ResponseWriter) {
2018-11-14 09:18:03 +00:00
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
}
}