Review Code, add comments

This commit is contained in:
MathieuHa
2022-09-28 19:09:11 +02:00
parent 279c496fb3
commit 188aea2446
+99 -88
View File
@@ -17,13 +17,11 @@ import (
) )
const ( const (
realIpHeader = "X-Real-Ip" crowdsecAuthHeader = "X-Api-Key"
forwardHeader = "X-Forwarded-For" crowdsecRoute = "v1/decisions"
crowdsecAuthHeader = "X-Api-Key" crowdsecStreamRoute = "v1/decisions/stream"
crowdsecRoute = "v1/decisions" cacheBannedValue = "t"
crowdsecStreamRoute = "v1/decisions/stream" cacheNoBannedValue = "f"
cacheBannedValue = "t"
cacheNoBannedValue = "f"
) )
var cache = ttl_map.New() var cache = ttl_map.New()
@@ -66,13 +64,13 @@ type Bouncer struct {
client *http.Client client *http.Client
} }
// New created a new Demo plugin. // New creates the crowdsec bouncer plugin.
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) { func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
requiredStrings := map[string]string{ requiredStrings := map[string]string{
"CrowdsecLapiScheme": config.CrowdsecLapiScheme, "CrowdsecLapiScheme": config.CrowdsecLapiScheme,
"CrowdsecLapiHost": config.CrowdsecLapiHost, "CrowdsecLapiHost": config.CrowdsecLapiHost,
"CrowdsecLapiKey": config.CrowdsecLapiKey, "CrowdsecLapiKey": config.CrowdsecLapiKey,
"CrowdsecMode": config.CrowdsecMode, "CrowdsecMode": config.CrowdsecMode,
} }
for key, val := range requiredStrings { for key, val := range requiredStrings {
if len(val) == 0 { if len(val) == 0 {
@@ -88,6 +86,16 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
return nil, fmt.Errorf("%v cannot be less than 1", key) return nil, fmt.Errorf("%v cannot be less than 1", key)
} }
} }
// none -> If the client IP is on ban list, it will get a http code 403 response.
// Otherwise, request will continue as usual. All request call the Crowdsec LAPI
// live -> If the client IP is on ban list, it will get a http code 403 response.
// Otherwise, request will continue as usual.
// The bouncer can leverage use of a local cache in order to reduce the number
// of requests made to the Crowdsec LAPI. It will keep in cache the status for
// each IP that makes queries.
// stream -> Stream Streaming mode allows you to keep in the local cache only the Banned IPs,
// every requests that does not hit the cache is authorized.
// Every minute, the cache is updated with news from the Crowdsec LAPI.
if !contains([]string{"none", "live", "stream"}, config.CrowdsecMode) { if !contains([]string{"none", "live", "stream"}, config.CrowdsecMode) {
return nil, fmt.Errorf("CrowdsecMode must be one of: none, live or stream") return nil, fmt.Errorf("CrowdsecMode must be one of: none, live or stream")
} }
@@ -95,9 +103,9 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
return nil, fmt.Errorf("CrowdsecLapiScheme must be one of: http, https") return nil, fmt.Errorf("CrowdsecLapiScheme must be one of: http, https")
} }
testUrl := url.URL{ testUrl := url.URL{
Scheme: config.CrowdsecLapiScheme, Scheme: config.CrowdsecLapiScheme,
Host: config.CrowdsecLapiHost, Host: config.CrowdsecLapiHost,
Path: crowdsecRoute, Path: crowdsecRoute,
} }
_, err := http.NewRequest(http.MethodGet, testUrl.String(), nil) _, err := http.NewRequest(http.MethodGet, testUrl.String(), nil)
if err != nil { if err != nil {
@@ -109,13 +117,13 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
name: name, name: name,
template: template.New("CrowdsecBouncer").Delims("[[", "]]"), template: template.New("CrowdsecBouncer").Delims("[[", "]]"),
enabled: config.Enabled, enabled: config.Enabled,
crowdsecStreamHealthy: false, crowdsecStreamHealthy: false,
crowdsecMode: config.CrowdsecMode, crowdsecMode: config.CrowdsecMode,
crowdsecScheme: config.CrowdsecLapiScheme, crowdsecScheme: config.CrowdsecLapiScheme,
crowdsecHost: config.CrowdsecLapiHost, crowdsecHost: config.CrowdsecLapiHost,
crowdsecKey: config.CrowdsecLapiKey, crowdsecKey: config.CrowdsecLapiKey,
updateInterval: config.UpdateIntervalSeconds, updateInterval: config.UpdateIntervalSeconds,
defaultDecisionTimeout: config.DefaultDecisionSeconds, defaultDecisionTimeout: config.DefaultDecisionSeconds,
client: &http.Client{ client: &http.Client{
Transport: &http.Transport{ Transport: &http.Transport{
@@ -125,18 +133,21 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
Timeout: 5 * time.Second, Timeout: 5 * time.Second,
}, },
} }
go handleStreamCache(bouncer, "true") // if we are on a stream mode, we fetch in a go routine every minute the new decisions
if config.CrowdsecMode == "stream" {
go handleStreamCache(bouncer, true)
}
return bouncer, nil return bouncer, nil
} }
func (a *Bouncer) ServeHTTP(rw http.ResponseWriter, req *http.Request) { func (a *Bouncer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
log.Printf("not enabled, %v", a.enabled )
if !a.enabled { if !a.enabled {
log.Printf("not enabled") log.Printf("Crowdsec Bouncer not enabled")
a.next.ServeHTTP(rw, req) a.next.ServeHTTP(rw, req)
return return
} }
// TODO Make sur remote address does not include the port
remoteHost, _, err := net.SplitHostPort(req.RemoteAddr) remoteHost, _, err := net.SplitHostPort(req.RemoteAddr)
if err != nil { if err != nil {
log.Printf("failed to extract ip from remote address: %v", err) log.Printf("failed to extract ip from remote address: %v", err)
@@ -156,6 +167,7 @@ func (a *Bouncer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
} }
} }
// Right here if we cannot join the stream we forbid the request to go on
if a.crowdsecMode == "stream" { if a.crowdsecMode == "stream" {
if a.crowdsecStreamHealthy { if a.crowdsecStreamHealthy {
a.next.ServeHTTP(rw, req) a.next.ServeHTTP(rw, req)
@@ -165,70 +177,66 @@ func (a *Bouncer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
return return
} }
if a.crowdsecMode == "none" || a.crowdsecMode == "live" { // We are now in none or live mode
noneUrl := url.URL{ noneUrl := url.URL{
Scheme: a.crowdsecScheme, Scheme: a.crowdsecScheme,
Host: a.crowdsecHost, Host: a.crowdsecHost,
Path: crowdsecRoute, Path: crowdsecRoute,
RawQuery: fmt.Sprintf("ip=%v&banned=true", remoteHost), RawQuery: fmt.Sprintf("ip=%v&banned=true", remoteHost),
} }
request, _ := http.NewRequest(http.MethodGet, noneUrl.String(), nil) request, _ := http.NewRequest(http.MethodGet, noneUrl.String(), nil)
request.Header.Add(crowdsecAuthHeader, a.crowdsecKey) request.Header.Add(crowdsecAuthHeader, a.crowdsecKey)
res, err := a.client.Do(request) res, err := a.client.Do(request)
if err != nil { if err != nil {
log.Printf("failed to get decision: %s", err) log.Printf("failed to get decision: %s", err)
rw.WriteHeader(http.StatusForbidden) rw.WriteHeader(http.StatusForbidden)
return
}
defer res.Body.Close()
if res.StatusCode != 200 {
log.Printf("failed to get decision, status code: %d", res.StatusCode)
rw.WriteHeader(http.StatusForbidden)
return
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Printf("failed to read body: %s", err)
rw.WriteHeader(http.StatusForbidden)
return
}
if !bytes.Equal(body, []byte("null")) {
var decisions []Decision
err = json.Unmarshal(body, &decisions)
if err != nil {
log.Printf("failed to parse body: %s", err)
rw.WriteHeader(http.StatusForbidden)
return
}
if len(decisions) == 0 {
if a.crowdsecMode == "live" {
setDecision(remoteHost, false, a.defaultDecisionTimeout)
}
a.next.ServeHTTP(rw, req)
return
}
duration, err := time.ParseDuration(decisions[0].Duration)
if err != nil {
log.Printf("failed to parse duration: %s", err)
rw.WriteHeader(http.StatusForbidden)
return
}
rw.WriteHeader(http.StatusForbidden)
setDecision(remoteHost, true, int64(duration.Seconds()))
return
}
if a.crowdsecMode == "live" {
setDecision(remoteHost, false, a.defaultDecisionTimeout)
}
a.next.ServeHTTP(rw, req)
return return
} }
defer res.Body.Close()
if res.StatusCode != 200 {
log.Printf("failed to get decision, status code: %d", res.StatusCode)
rw.WriteHeader(http.StatusForbidden)
return
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Printf("failed to read body: %s", err)
rw.WriteHeader(http.StatusForbidden)
return
}
if !bytes.Equal(body, []byte("null")) {
var decisions []Decision
err = json.Unmarshal(body, &decisions)
if err != nil {
log.Printf("failed to parse body: %s", err)
rw.WriteHeader(http.StatusForbidden)
return
}
if len(decisions) == 0 {
if a.crowdsecMode == "live" {
setDecision(remoteHost, false, a.defaultDecisionTimeout)
}
a.next.ServeHTTP(rw, req)
return
}
duration, err := time.ParseDuration(decisions[0].Duration)
if err != nil {
log.Printf("failed to parse duration: %s", err)
rw.WriteHeader(http.StatusForbidden)
return
}
rw.WriteHeader(http.StatusForbidden)
setDecision(remoteHost, true, int64(duration.Seconds()))
return
}
if a.crowdsecMode == "live" {
setDecision(remoteHost, false, a.defaultDecisionTimeout)
}
a.next.ServeHTTP(rw, req) a.next.ServeHTTP(rw, req)
} }
// CUSTOM CODE // CUSTOM CODE
// TODO place in another file
type Decision struct { type Decision struct {
Id int `json:"id"` Id int `json:"id"`
Origin string `json:"origin"` Origin string `json:"origin"`
@@ -241,8 +249,8 @@ type Decision struct {
} }
type Stream struct { type Stream struct {
Deleted []Decision `json:"deleted"` Deleted []Decision `json:"deleted"`
New []Decision `json:"new"` New []Decision `json:"new"`
} }
func contains(source []string, target string) bool { func contains(source []string, target string) bool {
@@ -254,6 +262,8 @@ func contains(source []string, target string) bool {
return false return false
} }
// Get Decision check in the cache if the IP has the banned / not banned value
// Otherwise return with an error to add the IP in cache if we are on
func getDecision(clientIP string) (bool, error) { func getDecision(clientIP string) (bool, error) {
isBanned, ok := cache.Get(clientIP) isBanned, ok := cache.Get(clientIP)
if ok && len(isBanned.(string)) > 0 { if ok && len(isBanned.(string)) > 0 {
@@ -274,15 +284,16 @@ func setDecision(clientIP string, isBanned bool, duration int64) {
} }
} }
func handleStreamCache(a *Bouncer, initialized string) { func handleStreamCache(a *Bouncer, initialized bool) {
time.AfterFunc(time.Duration(a.updateInterval) * time.Second, func () { // TODO clean properly on exit
handleStreamCache(a, "false") time.AfterFunc(time.Duration(a.updateInterval)*time.Second, func() {
handleStreamCache(a, false)
}) })
streamUrl := url.URL{ streamUrl := url.URL{
Scheme: a.crowdsecScheme, Scheme: a.crowdsecScheme,
Host: a.crowdsecHost, Host: a.crowdsecHost,
Path: crowdsecStreamRoute, Path: crowdsecStreamRoute,
RawQuery: fmt.Sprintf("startup=%s", initialized), RawQuery: fmt.Sprintf("startup=%t", initialized),
} }
req, _ := http.NewRequest(http.MethodGet, streamUrl.String(), nil) req, _ := http.NewRequest(http.MethodGet, streamUrl.String(), nil)
req.Header.Add(crowdsecAuthHeader, a.crowdsecKey) req.Header.Add(crowdsecAuthHeader, a.crowdsecKey)