mirror of
https://github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin.git
synced 2026-07-21 11:38:59 +02:00
🍱 fix loging
This commit is contained in:
+9
-9
@@ -154,7 +154,7 @@ func (bouncer *Bouncer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
|||||||
// Here we check for the trusted IPs in the customHeader
|
// Here we check for the trusted IPs in the customHeader
|
||||||
remoteIP, err := ip.GetRemoteIP(req, bouncer.serverPoolStrategy, bouncer.customHeader)
|
remoteIP, err := ip.GetRemoteIP(req, bouncer.serverPoolStrategy, bouncer.customHeader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error(fmt.Sprintf("ServeHTTP ip:%s %s", remoteIP, err.Error()))
|
logger.Error(fmt.Sprintf("ServeHTTP ip:%s %w", remoteIP, err))
|
||||||
rw.WriteHeader(http.StatusForbidden)
|
rw.WriteHeader(http.StatusForbidden)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -275,7 +275,7 @@ func handleNoStreamCache(bouncer *Bouncer, rw http.ResponseWriter, req *http.Req
|
|||||||
var decisions []Decision
|
var decisions []Decision
|
||||||
err = json.Unmarshal(body, &decisions)
|
err = json.Unmarshal(body, &decisions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error(fmt.Sprintf("handleNoStreamCache:parseBody: %s", err))
|
logger.Error(fmt.Sprintf("handleNoStreamCache:parseBody: %w", err))
|
||||||
rw.WriteHeader(http.StatusForbidden)
|
rw.WriteHeader(http.StatusForbidden)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -289,7 +289,7 @@ func handleNoStreamCache(bouncer *Bouncer, rw http.ResponseWriter, req *http.Req
|
|||||||
rw.WriteHeader(http.StatusForbidden)
|
rw.WriteHeader(http.StatusForbidden)
|
||||||
duration, err := time.ParseDuration(decisions[0].Duration)
|
duration, err := time.ParseDuration(decisions[0].Duration)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error(fmt.Sprintf("handleNoStreamCache:parseDuration %s", err))
|
logger.Error(fmt.Sprintf("handleNoStreamCache:parseDuration %w", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if bouncer.crowdsecMode == liveMode {
|
if bouncer.crowdsecMode == liveMode {
|
||||||
@@ -323,7 +323,7 @@ func handleStreamCache(bouncer *Bouncer) {
|
|||||||
var stream Stream
|
var stream Stream
|
||||||
err = json.Unmarshal(body, &stream)
|
err = json.Unmarshal(body, &stream)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error(fmt.Sprintf("handleStreamCache:parsingBody %s", err))
|
logger.Error(fmt.Sprintf("handleStreamCache:parsingBody %w", err))
|
||||||
crowdsecStreamHealthy = false
|
crowdsecStreamHealthy = false
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -345,20 +345,20 @@ func crowdsecQuery(bouncer *Bouncer, stringURL string) ([]byte, error) {
|
|||||||
req.Header.Add(crowdsecLapiHeader, bouncer.crowdsecKey)
|
req.Header.Add(crowdsecLapiHeader, bouncer.crowdsecKey)
|
||||||
res, err := bouncer.client.Do(req)
|
res, err := bouncer.client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("crowdsecQuery url:%s %s", stringURL, err.Error())
|
return nil, fmt.Errorf("crowdsecQuery url:%s %w", stringURL, err)
|
||||||
}
|
}
|
||||||
if res.StatusCode != http.StatusOK {
|
if res.StatusCode != http.StatusOK {
|
||||||
return nil, fmt.Errorf("crowdsecQuery url:%s, statusCode:%d", stringURL, res.StatusCode)
|
return nil, fmt.Errorf("crowdsecQuery url:%s, statusCode:%d", stringURL, res.StatusCode)
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
if err = res.Body.Close(); err != nil {
|
if err = res.Body.Close(); err != nil {
|
||||||
logger.Error(fmt.Sprintf("crowdsecQuery:closeBody %s", err.Error()))
|
logger.Error(fmt.Sprintf("crowdsecQuery:closeBody %w", err))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
body, err := io.ReadAll(res.Body)
|
body, err := io.ReadAll(res.Body)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("crowdsecQuery:readBody %s", err.Error())
|
return nil, fmt.Errorf("crowdsecQuery:readBody %w", err)
|
||||||
}
|
}
|
||||||
return body, nil
|
return body, nil
|
||||||
}
|
}
|
||||||
@@ -401,7 +401,7 @@ func validateParams(config *Config) error {
|
|||||||
if len(config.ForwardedHeadersTrustedIPs) > 0 {
|
if len(config.ForwardedHeadersTrustedIPs) > 0 {
|
||||||
_, err = ip.NewChecker(config.ForwardedHeadersTrustedIPs)
|
_, err = ip.NewChecker(config.ForwardedHeadersTrustedIPs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("ForwardedHeadersTrustedIPs must be a list of IP/CIDR :%s", err.Error())
|
return fmt.Errorf("ForwardedHeadersTrustedIPs must be a list of IP/CIDR :%w", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
logger.Debug("No IP provided for ForwardedHeadersTrustedIPs")
|
logger.Debug("No IP provided for ForwardedHeadersTrustedIPs")
|
||||||
@@ -409,7 +409,7 @@ func validateParams(config *Config) error {
|
|||||||
if len(config.ClientTrustedIPs) > 0 {
|
if len(config.ClientTrustedIPs) > 0 {
|
||||||
_, err = ip.NewChecker(config.ClientTrustedIPs)
|
_, err = ip.NewChecker(config.ClientTrustedIPs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("TrustedIPs must be a list of IP/CIDR :%s", err.Error())
|
return fmt.Errorf("TrustedIPs must be a list of IP/CIDR :%w", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
logger.Debug("No IP provided for TrustedIPs")
|
logger.Debug("No IP provided for TrustedIPs")
|
||||||
|
|||||||
+3
-3
@@ -32,7 +32,7 @@ func NewChecker(trustedIPs []string) (*Checker, error) {
|
|||||||
|
|
||||||
_, ipAddr, err := net.ParseCIDR(ipMask)
|
_, ipAddr, err := net.ParseCIDR(ipMask)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("parsing CIDR trusted IPs %s: %s", ipAddr, err.Error())
|
return nil, fmt.Errorf("parsing CIDR trusted IPs %s: %w", ipAddr, err)
|
||||||
}
|
}
|
||||||
checker.authorizedIPsNet = append(checker.authorizedIPsNet, ipAddr)
|
checker.authorizedIPsNet = append(checker.authorizedIPsNet, ipAddr)
|
||||||
logger.Debug(fmt.Sprintf("IP network %v is trusted", ipAddr))
|
logger.Debug(fmt.Sprintf("IP network %v is trusted", ipAddr))
|
||||||
@@ -49,7 +49,7 @@ func (ip *Checker) Contains(addr string) (bool, error) {
|
|||||||
|
|
||||||
ipAddr, err := parseIP(addr)
|
ipAddr, err := parseIP(addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, fmt.Errorf("Contains:parseAddress addr:%s %s", addr, err.Error())
|
return false, fmt.Errorf("Contains:parseAddress addr:%s %w", addr, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return ip.ContainsIP(ipAddr), nil
|
return ip.ContainsIP(ipAddr), nil
|
||||||
@@ -122,7 +122,7 @@ func GetRemoteIP(req *http.Request, strategy *PoolStrategy, customHeader string)
|
|||||||
}
|
}
|
||||||
remoteIP, _, err := net.SplitHostPort(req.RemoteAddr)
|
remoteIP, _, err := net.SplitHostPort(req.RemoteAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("GetRemoteIP:extractIP: %s", err.Error())
|
return "", fmt.Errorf("GetRemoteIP:extractIP: %w", err)
|
||||||
}
|
}
|
||||||
return remoteIP, nil
|
return remoteIP, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user