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:
+16
-17
@@ -175,7 +175,7 @@ func (bouncer *Bouncer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
|||||||
if bouncer.crowdsecMode != noneMode {
|
if bouncer.crowdsecMode != noneMode {
|
||||||
isBanned, err := cache.GetDecision(remoteIP)
|
isBanned, err := cache.GetDecision(remoteIP)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error(err.Error())
|
logger.Debug(err.Error())
|
||||||
if err.Error() == simpleredis.RedisUnreachable {
|
if err.Error() == simpleredis.RedisUnreachable {
|
||||||
healthy = false
|
healthy = false
|
||||||
}
|
}
|
||||||
@@ -259,7 +259,7 @@ func handleNoStreamCache(bouncer *Bouncer, rw http.ResponseWriter, req *http.Req
|
|||||||
}
|
}
|
||||||
body, err := crowdsecQuery(bouncer, routeURL.String())
|
body, err := crowdsecQuery(bouncer, routeURL.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Info(err.Error())
|
logger.Error(err.Error())
|
||||||
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.Info(fmt.Sprintf("failed to parse body: %s", err))
|
logger.Error(fmt.Sprintf("handleNoStreamCache:parseBody: %s", 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.Info(fmt.Sprintf("failed to parse duration: %s", err))
|
logger.Error(fmt.Sprintf("handleNoStreamCache:parseDuration %s", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if bouncer.crowdsecMode == liveMode {
|
if bouncer.crowdsecMode == liveMode {
|
||||||
@@ -302,9 +302,9 @@ func handleStreamCache(bouncer *Bouncer) {
|
|||||||
// Instead of blocking the goroutine interval for all the secondary node,
|
// Instead of blocking the goroutine interval for all the secondary node,
|
||||||
// if the master service is shut down, other goroutine can take the lead
|
// if the master service is shut down, other goroutine can take the lead
|
||||||
// because updated routine information is in the cache
|
// because updated routine information is in the cache
|
||||||
logger.Debug("handleStreamCache")
|
|
||||||
_, err := cache.GetDecision(cacheTimeoutKey)
|
_, err := cache.GetDecision(cacheTimeoutKey)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
logger.Debug("handleStreamCache:alreadyUpdated")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
cache.SetDecision(cacheTimeoutKey, false, bouncer.updateInterval-1)
|
cache.SetDecision(cacheTimeoutKey, false, bouncer.updateInterval-1)
|
||||||
@@ -316,14 +316,14 @@ func handleStreamCache(bouncer *Bouncer) {
|
|||||||
}
|
}
|
||||||
body, err := crowdsecQuery(bouncer, streamRouteURL.String())
|
body, err := crowdsecQuery(bouncer, streamRouteURL.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Info(err.Error())
|
logger.Error(err.Error())
|
||||||
crowdsecStreamHealthy = false
|
crowdsecStreamHealthy = false
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var stream Stream
|
var stream Stream
|
||||||
err = json.Unmarshal(body, &stream)
|
err = json.Unmarshal(body, &stream)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Info(fmt.Sprintf("error while parsing body: %s", err))
|
logger.Error(fmt.Sprintf("handleStreamCache:parsingBody %s", err))
|
||||||
crowdsecStreamHealthy = false
|
crowdsecStreamHealthy = false
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -345,21 +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("error while fetching %v: %w", stringURL, err)
|
return nil, fmt.Errorf("crowdsecQuery url:%s %s", stringURL, err.Error())
|
||||||
}
|
}
|
||||||
if res.StatusCode != http.StatusOK {
|
if res.StatusCode != http.StatusOK {
|
||||||
return nil, fmt.Errorf("error while fetching %v, status code: %d", stringURL, res.StatusCode)
|
return nil, fmt.Errorf("crowdsecQuery url:%s, statusCode:%d", stringURL, res.StatusCode)
|
||||||
}
|
}
|
||||||
defer func(body io.ReadCloser) {
|
defer func() {
|
||||||
err = body.Close()
|
if err = res.Body.Close(); err != nil {
|
||||||
if err != nil {
|
logger.Error(fmt.Sprintf("crowdsecQuery:closeBody %s", err.Error()))
|
||||||
logger.Error(fmt.Sprintf("failed to close body reader: %s", err.Error()))
|
|
||||||
}
|
}
|
||||||
}(res.Body)
|
}()
|
||||||
body, err := io.ReadAll(res.Body)
|
body, err := io.ReadAll(res.Body)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error while reading body: %w", err)
|
return nil, fmt.Errorf("crowdsecQuery:readBody %s", err.Error())
|
||||||
}
|
}
|
||||||
return body, nil
|
return body, nil
|
||||||
}
|
}
|
||||||
@@ -402,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 :%w", err)
|
return fmt.Errorf("ForwardedHeadersTrustedIPs must be a list of IP/CIDR :%s", err.Error())
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
logger.Debug("No IP provided for ForwardedHeadersTrustedIPs")
|
logger.Debug("No IP provided for ForwardedHeadersTrustedIPs")
|
||||||
@@ -410,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 :%w", err)
|
return fmt.Errorf("TrustedIPs must be a list of IP/CIDR :%s", err.Error())
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
logger.Debug("No IP provided for TrustedIPs")
|
logger.Debug("No IP provided for TrustedIPs")
|
||||||
|
|||||||
+5
-5
@@ -33,7 +33,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: %w", ipAddr, err)
|
return nil, fmt.Errorf("parsing CIDR trusted IPs %s: %s", ipAddr, err.Error())
|
||||||
}
|
}
|
||||||
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))
|
||||||
@@ -45,12 +45,12 @@ func NewChecker(trustedIPs []string) (*Checker, error) {
|
|||||||
// Contains checks if provided address is in the trusted IPs.
|
// Contains checks if provided address is in the trusted IPs.
|
||||||
func (ip *Checker) Contains(addr string) (bool, error) {
|
func (ip *Checker) Contains(addr string) (bool, error) {
|
||||||
if len(addr) == 0 {
|
if len(addr) == 0 {
|
||||||
return false, errors.New("empty IP address")
|
return false, errors.New("Contains:noAddress")
|
||||||
}
|
}
|
||||||
|
|
||||||
ipAddr, err := parseIP(addr)
|
ipAddr, err := parseIP(addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, fmt.Errorf("unable to parse address: %s: %w", addr, err)
|
return false, fmt.Errorf("Contains:parseAddress addr:%s %s", addr, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
return ip.ContainsIP(ipAddr), nil
|
return ip.ContainsIP(ipAddr), nil
|
||||||
@@ -76,7 +76,7 @@ func (ip *Checker) ContainsIP(addr net.IP) bool {
|
|||||||
func parseIP(addr string) (net.IP, error) {
|
func parseIP(addr string) (net.IP, error) {
|
||||||
userIP := net.ParseIP(addr)
|
userIP := net.ParseIP(addr)
|
||||||
if userIP == nil {
|
if userIP == nil {
|
||||||
return nil, fmt.Errorf("can't parse IP from address %s", addr)
|
return nil, fmt.Errorf("parseIP:parseAddress %s", addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
return userIP, nil
|
return userIP, nil
|
||||||
@@ -123,7 +123,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("failed to extract ip from remote address: %w", err)
|
return "", fmt.Errorf("GetRemoteIP:extractIP: %s", err.Error())
|
||||||
}
|
}
|
||||||
return remoteIP, nil
|
return remoteIP, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user