Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix/reload config structs #7

Merged
merged 12 commits into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions banjax.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ package main
import (
"encoding/json"
"flag"
"github.com/equalitie/banjax/internal"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"os"
Expand All @@ -19,6 +17,9 @@ import (
"sync"
"syscall"
"time"

"github.com/equalitie/banjax/internal"
"gopkg.in/yaml.v2"
)

func load_config(config *internal.Config, standaloneTestingPtr *bool, configFilenamePtr *string, restartTime int) {
Expand Down Expand Up @@ -68,6 +69,18 @@ func load_config(config *internal.Config, standaloneTestingPtr *bool, configFile
}

func main() {
// XXX protects ipToRegexStates and failedChallengeStates
// (why both? because there are too many parameters already?)
var rateLimitMutex sync.Mutex
ipToRegexStates := internal.IpToRegexStates{}
failedChallengeStates := internal.FailedChallengeStates{}

var passwordProtectedPaths internal.PasswordProtectedPaths

// XXX protects decisionLists
var decisionListsMutex sync.Mutex
var decisionLists internal.DecisionLists

standaloneTestingPtr := flag.Bool("standalone-testing", false, "makes it easy to test standalone")
configFilenamePtr := flag.String("config-file", "/etc/banjax/banjax-config.yaml", "config file")
flag.Parse()
Expand All @@ -88,7 +101,10 @@ func main() {
go func() {
for _ = range sighup_channel {
log.Println("got SIGHUP; reloading config")
rateLimitMutex.Lock()
load_config(&config, standaloneTestingPtr, configFilenamePtr, restartTime)
rateLimitMutex.Unlock()
configToStructs(&config, &passwordProtectedPaths, &decisionLists)
}
}()

Expand All @@ -113,17 +129,7 @@ func main() {
}
log.Println(config.KafkaBrokers)

// XXX protects decisionLists
var decisionListsMutex sync.Mutex
decisionLists := internal.ConfigToDecisionLists(&config)

passwordProtectedPaths := internal.ConfigToPasswordProtectedPaths(&config)

// XXX protects ipToRegexStates and failedChallengeStates
// (why both? because there are too many parameters already?)
var rateLimitMutex sync.Mutex
ipToRegexStates := internal.IpToRegexStates{}
failedChallengeStates := internal.FailedChallengeStates{}
configToStructs(&config, &passwordProtectedPaths, &decisionLists)

// XXX this interface exists to make mocking out the iptables stuff
// in testing easier. there might be a better way to do it.
Expand Down Expand Up @@ -228,3 +234,17 @@ func main() {

wg.Wait()
}

var configToStructsMutex sync.Mutex

func configToStructs(
config *internal.Config,
passwordProtectedPaths *internal.PasswordProtectedPaths,
decisionLists *internal.DecisionLists,
) {
configToStructsMutex.Lock()
defer configToStructsMutex.Unlock()

*passwordProtectedPaths = internal.ConfigToPasswordProtectedPaths(config)
*decisionLists = internal.ConfigToDecisionLists(config)
}
54 changes: 40 additions & 14 deletions banjax_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
package main

import (
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -115,6 +117,15 @@ func httpTester(t *testing.T, resources []TestResource) {
}
}

func httpStress(resources []TestResource, repeat int) {
client := http.Client{}
for _, resource := range resources {
for i := 0; i <= repeat; i++ {
httpRequest(client, resource)
}
}
}

func httpRequest(client http.Client, resource TestResource) *http.Response {
req, err := http.NewRequest(resource.method, endpoint+resource.name, nil)
if err != nil {
Expand All @@ -133,20 +144,25 @@ func httpRequest(client http.Client, resource TestResource) *http.Response {
return resp
}

func randomXClientIP() http.Header {
ip := fmt.Sprintf("10.2.0.%d", rand.Intn(252))
return http.Header{"X-Client-IP": {ip}}
}

func TestBanjaxEndpoint(t *testing.T) {
banjax_resources := []TestResource{
{"GET", "/auth_request", 200, nil, nil},
{"POST", "/auth_request", 200, nil, nil},
{"PUT", "/auth_request", 200, nil, nil},
{"PATCH", "/auth_request", 200, nil, nil},
{"HEAD", "/auth_request", 200, nil, nil},
{"OPTIONS", "/auth_request", 200, nil, nil},
{"DELETE", "/auth_request", 200, nil, nil},
{"CONNECT", "/auth_request", 200, nil, nil},
{"TRACE", "/auth_request", 200, nil, nil},
{"GET", "/info", 200, nil, nil},
{"GET", "/decision_lists", 200, nil, nil},
{"GET", "/rate_limit_states", 200, nil, nil},
{"GET", "/auth_request", 200, randomXClientIP(), nil},
{"POST", "/auth_request", 200, randomXClientIP(), nil},
{"PUT", "/auth_request", 200, randomXClientIP(), nil},
{"PATCH", "/auth_request", 200, randomXClientIP(), nil},
{"HEAD", "/auth_request", 200, randomXClientIP(), nil},
{"OPTIONS", "/auth_request", 200, randomXClientIP(), nil},
{"DELETE", "/auth_request", 200, randomXClientIP(), nil},
{"CONNECT", "/auth_request", 200, randomXClientIP(), nil},
{"TRACE", "/auth_request", 200, randomXClientIP(), nil},
{"GET", "/info", 200, randomXClientIP(), nil},
{"GET", "/decision_lists", 200, randomXClientIP(), nil},
{"GET", "/rate_limit_states", 200, randomXClientIP(), nil},
}
httpTester(t, banjax_resources)
}
Expand All @@ -157,19 +173,29 @@ func TestProtectedResources(t *testing.T) {
protected_res := "wp-admin"
httpTester(t, []TestResource{
{"GET", "/info", 200, nil, []string{"2022-01-02"}},
{"GET", "/auth_request?path=" + protected_res, 401, nil, nil},
{"GET", "/auth_request?path=" + protected_res, 401, randomXClientIP(), nil},
})

protected_res = "wp-admin2"
reloadConfig(fixtureConfigTestReload)
httpTester(t, []TestResource{
{"GET", "/info", 200, nil, []string{"2022-02-03"}},
{"GET", "/auth_request?path=" + protected_res, 401, nil, nil},
{"GET", "/auth_request?path=" + protected_res, 401, randomXClientIP(), nil},
})
}

func reloadConfig(path string) {
done := make(chan bool)
// Simulate activity of http requests when the config is reloaded
go func() {
httpStress(
[]TestResource{{"GET", "/auth_request", 200, randomXClientIP(), nil}},
50)
done <- true
}()

copyConfigFile(path)
syscall.Kill(syscall.Getpid(), syscall.SIGHUP)
time.Sleep(1 * time.Second)
<-done
}
7 changes: 4 additions & 3 deletions internal/regex_rate_limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ package internal
import (
"bytes"
"encoding/json"
"github.com/hpcloud/tail"
"log"
"net/url"
"strconv"
"strings"
"sync"
"time"

"github.com/hpcloud/tail"
)

func RunLogTailer(
Expand Down Expand Up @@ -152,6 +153,7 @@ func consumeLine(
return
}

rateLimitMutex.Lock()
// log.Println(line.Text[secondSpace+firstSpace+2:])
for _, regex_with_rate := range config.RegexesWithRates {
ruleResult := RuleResult{}
Expand All @@ -174,7 +176,6 @@ func consumeLine(
}
ruleResult.SkipHost = false

rateLimitMutex.Lock()
states, ok := (*ipToRegexStates)[ipString]
if !ok {
// log.Println("we haven't seen this IP before")
Expand Down Expand Up @@ -212,9 +213,9 @@ func consumeLine(
(*(*ipToRegexStates)[ipString])[regex_with_rate.Rule].NumHits = 0 // XXX should it be 1?...
}

rateLimitMutex.Unlock()
consumeLineResult.RuleResults = append(consumeLineResult.RuleResults, ruleResult)
}

rateLimitMutex.Unlock()
return
}