Skip to content

Commit

Permalink
Merge pull request #16 from BetterCorp/testings
Browse files Browse the repository at this point in the history
Testings
  • Loading branch information
mrinc committed Sep 28, 2022
2 parents 2576a98 + 7c9568b commit 5e2ccb5
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 22 deletions.
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ linters-settings:
linters:
enable-all: true
disable:
- deadcode # deprecated
- varcheck # deprecated
- structcheck # deprecated
- nosnakecase # deprecated
- interfacer # deprecated
- maligned # deprecated
- scopelint # deprecated
Expand Down
17 changes: 15 additions & 2 deletions cloudflarewarp.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Config struct {

// TrustResult for Trust IP test result.
type TrustResult struct {
isFatal bool
isError bool
trusted bool
directIP string
Expand Down Expand Up @@ -86,8 +87,16 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h

func (r *RealIPOverWriter) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
trustResult := r.trust(req.RemoteAddr)
if trustResult.directIP == "" || trustResult.isError {
http.Error(rw, "Unknown source", 500)
if trustResult.isFatal {
http.Error(rw, "Unknown source", http.StatusInternalServerError)
return
}
if trustResult.isError {
http.Error(rw, "Unknown source", http.StatusBadRequest)
return
}
if trustResult.directIP == "" {
http.Error(rw, "Unknown source", http.StatusUnprocessableEntity)
return
}
if trustResult.trusted {
Expand Down Expand Up @@ -118,6 +127,7 @@ func (r *RealIPOverWriter) trust(s string) *TrustResult {
temp, _, err := net.SplitHostPort(s)
if err != nil {
return &TrustResult{
isFatal: true,
isError: true,
trusted: false,
directIP: "",
Expand All @@ -126,6 +136,7 @@ func (r *RealIPOverWriter) trust(s string) *TrustResult {
ip := net.ParseIP(temp)
if ip == nil {
return &TrustResult{
isFatal: false,
isError: true,
trusted: false,
directIP: "",
Expand All @@ -134,13 +145,15 @@ func (r *RealIPOverWriter) trust(s string) *TrustResult {
for _, network := range r.TrustIP {
if network.Contains(ip) {
return &TrustResult{
isFatal: false,
isError: false,
trusted: true,
directIP: ip.String(),
}
}
}
return &TrustResult{
isFatal: false,
isError: false,
trusted: false,
directIP: ip.String(),
Expand Down
51 changes: 44 additions & 7 deletions cloudflarewarp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"net/http"
"net/http/httptest"
"strconv"
"testing"

plugin "github.com/BetterCorp/cloudflarewarp"
Expand All @@ -20,7 +21,8 @@ func TestNew(t *testing.T) {
t.Fatal(err)
}
testCases := []struct {
expect500 bool
ipv6 bool
expect400 bool
trusted bool
remote string
desc string
Expand Down Expand Up @@ -56,14 +58,43 @@ func TestNew(t *testing.T) {
expectedScheme: "",
trusted: false,
},
{
remote: "10.0.1.20",
desc: "not trust ip4/6",
cfConnectingIP: "1001:3984:3989::1",
cfVisitor: "",
expected: "",
expectedScheme: "",
trusted: false,
},
{
remote: "1001:3984:3989::1",
ipv6: true,
desc: "not trust ip6/6",
cfConnectingIP: "1001:3984:3989::1",
cfVisitor: "",
expected: "",
expectedScheme: "",
trusted: false,
},
{
remote: "1001:3984:3989::1",
ipv6: true,
desc: "not trust ip6/4",
cfConnectingIP: "10.0.1.20",
cfVisitor: "",
expected: "",
expectedScheme: "",
trusted: false,
},
{
remote: "10.0.2",
desc: "wrong IP format",
cfConnectingIP: "10.0.0.1",
cfVisitor: "",
expected: "",
expectedScheme: "",
expect500: true,
expect400: true,
trusted: false,
},
{
Expand All @@ -73,7 +104,7 @@ func TestNew(t *testing.T) {
cfVisitor: "",
expected: "",
expectedScheme: "",
expect500: true,
expect400: true,
trusted: false,
},
{
Expand Down Expand Up @@ -104,18 +135,24 @@ func TestNew(t *testing.T) {
if err != nil {
t.Fatal(err)
}
req.RemoteAddr = test.remote + ":36001"
if test.ipv6 == true {
req.RemoteAddr = "[" + test.remote + "]:36001"
} else {
req.RemoteAddr = test.remote + ":36001"
}
req.Header.Set("X-Real-Ip", test.remote)
req.Header.Set("Cf-Connecting-IP", test.cfConnectingIP)
req.Header.Set("Cf-Visitor", test.cfVisitor)

handler.ServeHTTP(recorder, req)

if recorder.Result().StatusCode == 500 {
if test.expect500 == true {
if recorder.Result().StatusCode == http.StatusBadRequest {
if test.expect400 == true {
return
}
t.Errorf("invalid response: 500")
}
if recorder.Result().StatusCode != http.StatusOK {
t.Errorf("invalid response: " + strconv.Itoa(recorder.Result().StatusCode))
return
}

Expand Down
8 changes: 8 additions & 0 deletions test/config/invalid.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

[http]
[http.middlewares]
[http.middlewares.cloudflarewarp]
[http.middlewares.cloudflarewarp.plugin]
[http.middlewares.cloudflarewarp.plugin.cloudflarewarp]
trustip=[]

6 changes: 6 additions & 0 deletions test/config/invalid.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
http:
middlewares:
cloudflarewarp:
plugin:
cloudflarewarp:
disableDefault: false
31 changes: 25 additions & 6 deletions test/test-prod.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
TEST_IP="187.2.2.3"

rm -rf ./logs-success
rm -rf ./logs-success-toml
rm -rf ./logs-success-yml
rm -rf ./logs-fail
rm -rf ./logs-fail-toml
rm -rf ./logs-fail-yml
rm -rf ./logs-invalid
rm -rf ./logs-invalid-toml
rm -rf ./logs-invalid-yml

if [ "${1}" = "stack" ]; then
docker swarm init
Expand All @@ -27,7 +34,7 @@ if [ ! "${1}" = "stack" ]; then
cp docker-compose-prod.yml docker-compose.yml
fi

rm -rf ./logs-success-toml
sleep 1s

bash test-base.sh success toml "${1}" $TEST_IP

Expand All @@ -38,8 +45,6 @@ mv ./tempconfig ./logs-success-toml/config

sleep 1s

rm -rf ./logs-fail-toml

bash test-base.sh fail toml "${1}" $TEST_IP

sleep 1s
Expand All @@ -49,7 +54,14 @@ mv ./tempconfig ./logs-fail-toml/config

sleep 1s

rm -rf ./logs-success-yml
bash test-base.sh invalid toml "${1}" "1522.20.2"

sleep 1s

mv ./logs ./logs-invalid-toml
mv ./tempconfig ./logs-invalid-toml/config

sleep 1s

bash ./test-verify.sh toml $TEST_IP

Expand All @@ -64,8 +76,6 @@ mv ./tempconfig ./logs-success-yml/config

sleep 1s

rm -rf ./logs-fail-yml

bash test-base.sh fail yml "${1}" $TEST_IP

sleep 1s
Expand All @@ -75,6 +85,15 @@ mv ./tempconfig ./logs-fail-yml/config

sleep 1s

bash test-base.sh invalid yml "${1}" "1522.20.2"

sleep 1s

mv ./logs ./logs-invalid-yml
mv ./tempconfig ./logs-invalid-yml/config

sleep 1s

bash ./test-verify.sh yml $TEST_IP

if [ ! "${1}" = "stack" ]; then
Expand Down
6 changes: 6 additions & 0 deletions test/test-verify.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
SUCCESS_CONFIG_FILE="./logs-success-${1}/output.log"
FAIL_CONFIG_FILE="./logs-fail-${1}/output.log"
INVALID_CONFIG_FILE="./logs-invalid-${1}/output.log"

echo "RUNNING TESTS FOR ${1}"
echo " - Succ $SUCCESS_CONFIG_FILE"
echo " - Fail $FAIL_CONFIG_FILE"
echo " - Inva $INVALID_CONFIG_FILE"

sleep 1s

Expand All @@ -28,6 +30,10 @@ if ! grep -q "X-Is-Trusted: no" $FAIL_CONFIG_FILE; then
echo "'X-Is-Trusted: no' header was not added to the invalid request ($FAIL_CONFIG_FILE)"
exit 5
fi
if ! grep -q "X-Is-Trusted: no" $INVALID_CONFIG_FILE; then
echo "'X-Is-Trusted: no' header was not added to the invalid request ($INVALID_CONFIG_FILE)"
exit 5
fi
#if ! grep -q "X-Forwarded-For: 10.0.0.2" $FAIL_CONFIG_FILE; then
# echo "Forwarded header was not defined as the original IP"
# exit 5
Expand Down
31 changes: 24 additions & 7 deletions test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
TEST_IP="187.2.2.1"

rm -rf ./logs-success
rm -rf ./logs-success-toml
rm -rf ./logs-success-yml
rm -rf ./logs-fail
rm -rf ./logs-fail-toml
rm -rf ./logs-fail-yml
rm -rf ./logs-invalid
rm -rf ./logs-invalid-toml
rm -rf ./logs-invalid-yml

if [ "${1}" = "stack" ]; then
docker swarm init
Expand All @@ -15,8 +22,6 @@ docker pull traefik:2.8

sleep 1s

rm -rf ./logs-success-toml

bash test-base.sh success toml "${1}" $TEST_IP

sleep 1s
Expand All @@ -26,8 +31,6 @@ mv ./tempconfig ./logs-success-toml/config

sleep 1s

rm -rf ./logs-fail-toml

bash test-base.sh fail toml "${1}" $TEST_IP

sleep 1s
Expand All @@ -37,7 +40,14 @@ mv ./tempconfig ./logs-fail-toml/config

sleep 1s

rm -rf ./logs-success-yml
bash test-base.sh invalid toml "${1}" "1522.20.2"

sleep 1s

mv ./logs ./logs-invalid-toml
mv ./tempconfig ./logs-invalid-toml/config

sleep 1s

bash ./test-verify.sh toml $TEST_IP

Expand All @@ -52,8 +62,6 @@ mv ./tempconfig ./logs-success-yml/config

sleep 1s

rm -rf ./logs-fail-yml

bash test-base.sh fail yml "${1}" $TEST_IP

sleep 1s
Expand All @@ -63,4 +71,13 @@ mv ./tempconfig ./logs-fail-yml/config

sleep 1s

bash test-base.sh invalid yml "${1}" "1522.20.2"

sleep 1s

mv ./logs ./logs-invalid-yml
mv ./tempconfig ./logs-invalid-yml/config

sleep 1s

bash ./test-verify.sh yml $TEST_IP

0 comments on commit 5e2ccb5

Please sign in to comment.