mirror of
https://github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin.git
synced 2026-07-21 03:28:59 +02:00
First implementation of the bypass feature for trusted IP
This commit is contained in:
+28
-3
@@ -40,10 +40,11 @@ type Config struct {
|
|||||||
CrowdsecLapiScheme string `json:"crowdsecLapiScheme,omitempty"`
|
CrowdsecLapiScheme string `json:"crowdsecLapiScheme,omitempty"`
|
||||||
CrowdsecLapiHost string `json:"crowdsecLapiHost,omitempty"`
|
CrowdsecLapiHost string `json:"crowdsecLapiHost,omitempty"`
|
||||||
CrowdsecLapiKey string `json:"crowdsecLapiKey,omitempty"`
|
CrowdsecLapiKey string `json:"crowdsecLapiKey,omitempty"`
|
||||||
ForwardedHeadersCustomName string `json:"forwardedheaderscustomheader,omitempty"`
|
|
||||||
UpdateIntervalSeconds int64 `json:"updateIntervalSeconds,omitempty"`
|
UpdateIntervalSeconds int64 `json:"updateIntervalSeconds,omitempty"`
|
||||||
DefaultDecisionSeconds int64 `json:"defaultDecisionSeconds,omitempty"`
|
DefaultDecisionSeconds int64 `json:"defaultDecisionSeconds,omitempty"`
|
||||||
|
ForwardedHeadersCustomName string `json:"forwardedheaderscustomheader,omitempty"`
|
||||||
ForwardedHeadersTrustedIPs []string `json:"forwardedHeadersTrustedIps,omitempty"`
|
ForwardedHeadersTrustedIPs []string `json:"forwardedHeadersTrustedIps,omitempty"`
|
||||||
|
TrustedIPs []string `json:"TrustedIps,omitempty"`
|
||||||
RedisCacheEnabled bool `json:"redisCacheEnabled,omitempty"`
|
RedisCacheEnabled bool `json:"redisCacheEnabled,omitempty"`
|
||||||
RedisCacheHost string `json:"redisCacheHost,omitempty"`
|
RedisCacheHost string `json:"redisCacheHost,omitempty"`
|
||||||
}
|
}
|
||||||
@@ -60,6 +61,7 @@ func CreateConfig() *Config {
|
|||||||
UpdateIntervalSeconds: 60,
|
UpdateIntervalSeconds: 60,
|
||||||
DefaultDecisionSeconds: 60,
|
DefaultDecisionSeconds: 60,
|
||||||
ForwardedHeadersTrustedIPs: []string{},
|
ForwardedHeadersTrustedIPs: []string{},
|
||||||
|
TrustedIPs: []string{},
|
||||||
ForwardedHeadersCustomName: "X-Forwarded-For",
|
ForwardedHeadersCustomName: "X-Forwarded-For",
|
||||||
RedisCacheEnabled: false,
|
RedisCacheEnabled: false,
|
||||||
RedisCacheHost: "redis:6379",
|
RedisCacheHost: "redis:6379",
|
||||||
@@ -81,6 +83,8 @@ type Bouncer struct {
|
|||||||
defaultDecisionTimeout int64
|
defaultDecisionTimeout int64
|
||||||
customHeader string
|
customHeader string
|
||||||
poolStrategy *ip.PoolStrategy
|
poolStrategy *ip.PoolStrategy
|
||||||
|
poolStrategyTrusted *ip.PoolStrategy
|
||||||
|
CheckerTrusted *ip.Checker
|
||||||
client *http.Client
|
client *http.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,6 +98,7 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
|
|||||||
}
|
}
|
||||||
|
|
||||||
checker, _ := ip.NewChecker(config.ForwardedHeadersTrustedIPs)
|
checker, _ := ip.NewChecker(config.ForwardedHeadersTrustedIPs)
|
||||||
|
checkerTrusted, _ := ip.NewChecker(config.TrustedIPs)
|
||||||
|
|
||||||
bouncer := &Bouncer{
|
bouncer := &Bouncer{
|
||||||
next: next,
|
next: next,
|
||||||
@@ -111,6 +116,10 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
|
|||||||
poolStrategy: &ip.PoolStrategy{
|
poolStrategy: &ip.PoolStrategy{
|
||||||
Checker: checker,
|
Checker: checker,
|
||||||
},
|
},
|
||||||
|
poolStrategyTrusted: &ip.PoolStrategy{
|
||||||
|
Checker: checkerTrusted,
|
||||||
|
},
|
||||||
|
CheckerTrusted: checkerTrusted,
|
||||||
client: &http.Client{
|
client: &http.Client{
|
||||||
Transport: &http.Transport{
|
Transport: &http.Transport{
|
||||||
MaxIdleConns: 10,
|
MaxIdleConns: 10,
|
||||||
@@ -138,8 +147,24 @@ func (bouncer *Bouncer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
|||||||
bouncer.next.ServeHTTP(rw, req)
|
bouncer.next.ServeHTTP(rw, req)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// Here we check for the trusted IPs in the customHeader
|
||||||
remoteHost, err := ip.GetRemoteIP(req, bouncer.poolStrategy, bouncer.customHeader)
|
remoteHost, err := ip.GetRemoteIP(req, bouncer.poolStrategyTrusted, bouncer.customHeader)
|
||||||
|
if err != nil {
|
||||||
|
logger.Info(fmt.Sprintf("%w", err))
|
||||||
|
bouncer.next.ServeHTTP(rw, req)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
trusted, err := bouncer.CheckerTrusted.Contains(remoteHost)
|
||||||
|
if err != nil {
|
||||||
|
logger.Info(fmt.Sprintf("%w", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// if our IP is in the trusted list we bypass the next checks
|
||||||
|
if trusted {
|
||||||
|
bouncer.next.ServeHTTP(rw, req)
|
||||||
|
}
|
||||||
|
// Here we get the IP from the trusted header customHeader
|
||||||
|
remoteHost, err = ip.GetRemoteIP(req, bouncer.poolStrategy, bouncer.customHeader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Info(fmt.Sprintf("%w", err))
|
logger.Info(fmt.Sprintf("%w", err))
|
||||||
bouncer.next.ServeHTTP(rw, req)
|
bouncer.next.ServeHTTP(rw, req)
|
||||||
|
|||||||
Reference in New Issue
Block a user