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

Response Modifications Toxic #1

Merged
merged 19 commits into from
Jul 15, 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
55 changes: 55 additions & 0 deletions .github/workflows/push.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---

name: push-release-pipeline
on:
push:
branches:
- master
tags:
- "*"

jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
# Install golang
- uses: actions/setup-go@v2
with:
go-version: 1.17

# Checkout to the latest commit
# On specific directory/path
- name: Checkout
uses: actions/checkout@v2

- name: gofmt check
run: |
if [ "$(gofmt -s -l . | wc -l)" -ne 0 ]
then
echo "The following files were found to be not go formatted:"
gofmt -s -l .
exit 1
fi

- name: golangci-lint
uses: reviewdog/action-golangci-lint@v1

- name: unused-package check
run: |
make unused-package-check


- name: Building toxiproxy
run: |
make prod-build

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1

- name: Copy binaries to the toxiproxy s3 bucket
run: |
aws s3 sync dist s3://${{ secrets.AWS_S3_BUCKET }}
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
fail-fast: true
matrix:
go: ["1.18", "1.17", "1.16", "1.15", "1.14"]
go: ["1.18", "1.17"]
name: Go ${{ matrix.go }}
steps:
- name: Checkout
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ dist/
vendor/

tmp/

.vscode/
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ lint:
shfmt -l -s -d -kp -i 2 scripts/test-*
yamllint .

.PHONY: prod-build
prod-build: dist clean
@bash scripts/build.sh ./cmd/server server
@bash scripts/build.sh ./cmd/cli cli

.PHONY: build
build: dist clean
go build -ldflags="-s -w" -o ./dist/toxiproxy-server ./cmd/server
Expand All @@ -64,3 +69,13 @@ dist:
.PHONY: clean
clean:
rm -fr dist/*

.PHONY: unused-package-check
unused-package-check:
@echo "------------------"
@echo "--> Check unused packages for the litmusctl"
@echo "------------------"
@tidy=$$(go mod tidy); \
if [ -n "$${tidy}" ]; then \
echo "go mod tidy checking failed!"; echo "$${tidy}"; echo; \
fi
2 changes: 1 addition & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (server *ApiServer) PopulateConfig(filename string) {
func StopBrowsersMiddleware(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.UserAgent(), "Mozilla/") {
http.Error(w, "User agent not allowed", 403)
http.Error(w, "User agent not allowed", http.StatusForbidden)
} else {
h.ServeHTTP(w, r)
}
Expand Down
8 changes: 4 additions & 4 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func TestPopulateExistingProxy(t *testing.T) {
t.Fatal("Unable to create proxy:", err)
}

// Create a toxic so we can make sure the proxy wasn't replaced
// Create a toxic, so we can make sure the proxy wasn't replaced
_, err = testProxy.AddToxic("", "latency", "downstream", 1, nil)
if err != nil {
t.Fatal("Unable to create toxic:", err)
Expand Down Expand Up @@ -476,7 +476,7 @@ func TestCreateProxyWithSave(t *testing.T) {

proxy, err := client.Proxy("mysql_master")
if err != nil {
t.Fatal("Unable to retriecve proxy:", err)
t.Fatal("Unable to retrieve proxy:", err)
}

if proxy.Name != "mysql_master" || proxy.Listen != "127.0.0.1:3310" ||
Expand Down Expand Up @@ -509,7 +509,7 @@ func TestCreateDisabledProxy(t *testing.T) {

proxy, err := client.Proxy("mysql_master")
if err != nil {
t.Fatal("Unable to retriecve proxy:", err)
t.Fatal("Unable to retrieve proxy:", err)
}

if proxy.Name != "mysql_master" || proxy.Listen != "localhost:3310" ||
Expand Down Expand Up @@ -542,7 +542,7 @@ func TestCreateDisabledProxyAndEnable(t *testing.T) {

proxy, err := client.Proxy("mysql_master")
if err != nil {
t.Fatal("Unable to retriecve proxy:", err)
t.Fatal("Unable to retrieve proxy:", err)
}

if proxy.Name != "mysql_master" || proxy.Listen != "localhost:3310" ||
Expand Down
8 changes: 6 additions & 2 deletions cmd/cli/cli.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"fmt"
"os"
"sort"
Expand Down Expand Up @@ -574,14 +575,17 @@ func parseAddToxicParams(c *cli.Context) (*toxiproxy.ToxicOptions, error) {
func parseAttributes(c *cli.Context, name string) toxiproxy.Attributes {
parsed := map[string]interface{}{}
args := c.StringSlice(name)

var t map[string]interface{}
for _, raw := range args {
kv := strings.SplitN(raw, "=", 2)
if len(kv) < 2 {
continue
}
if float, err := strconv.ParseFloat(kv[1], 64); err == nil {
parsed[kv[0]] = float
} else if err := json.Unmarshal([]byte(kv[1]), &t); err == nil {
parsed[kv[0]] = t
continue
} else {
parsed[kv[0]] = kv[1]
}
Expand Down Expand Up @@ -619,7 +623,7 @@ func (a attributeList) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func sortedAttributes(attrs toxiproxy.Attributes) attributeList {
li := make(attributeList, 0, len(attrs))
for k, v := range attrs {
li = append(li, attribute{k, v.(float64)})
li = append(li, attribute{k, v})
}
sort.Sort(li)
return li
Expand Down
10 changes: 0 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RX
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.1.0 h1:ksErzDEI1khOiGPgpwuI7x2ebx/uXQNw7xJpn9Eq1+I=
github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
Expand All @@ -53,8 +51,6 @@ github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5P
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cpuguy83/go-md2man/v2 v2.0.1 h1:r/myEWzV9lfsM1tFLgDyu0atFtJ1fXn261LKYj/3DxU=
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -203,8 +199,6 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/urfave/cli/v2 v2.4.0 h1:m2pxjjDFgDxSPtO8WSdbndj17Wu2y8vOT86wE/tjr+I=
github.com/urfave/cli/v2 v2.4.0/go.mod h1:NX9W0zmTvedE5oDoOMs2RTC8RvdK98NTYZE5LbaEYPg=
github.com/urfave/cli/v2 v2.11.0 h1:c6bD90aLd2iEsokxhxkY5Er0zA2V9fId2aJfwmrF+do=
github.com/urfave/cli/v2 v2.11.0/go.mod h1:f8iq5LtQ/bLxafbdBSLPPNsgaW0l/2fYYEHhAyPlwvo=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
Expand Down Expand Up @@ -342,7 +336,6 @@ golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220708085239-5a0f0661e09d h1:/m5NbqQelATgoSPVC2Z23sR4kVNokFwDDyWh/3rGY+I=
golang.org/x/sys v0.0.0-20220708085239-5a0f0661e09d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467 h1:CBpWXWQpIRjzmkkA+M7q9Fqnwd2mZr3AFqexg8YTfoM=
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Expand Down Expand Up @@ -489,12 +482,9 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
37 changes: 21 additions & 16 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package toxiproxy
import (
"errors"
"net"
"strings"
"sync"

"github.com/Shopify/toxiproxy/v2/stream"
Expand Down Expand Up @@ -102,9 +103,9 @@ func (proxy *Proxy) listen() error {
proxy.started <- nil

logrus.WithFields(logrus.Fields{
"name": proxy.Name,
"proxy": proxy.Listen,
"upstream": proxy.Upstream,
"name": cleanUserInput(proxy.Name),
"proxy": cleanUserInput(proxy.Listen),
"upstream": cleanUserInput(proxy.Upstream),
}).Info("Started proxy")

return nil
Expand All @@ -115,8 +116,8 @@ func (proxy *Proxy) close() {
err := proxy.listener.Close()
if err != nil {
logrus.WithFields(logrus.Fields{
"proxy": proxy.Name,
"listen": proxy.Listen,
"proxy": cleanUserInput(proxy.Name),
"listen": cleanUserInput(proxy.Listen),
"err": err,
}).Warn("Attempted to close an already closed proxy server")
}
Expand Down Expand Up @@ -164,28 +165,28 @@ func (proxy *Proxy) server() {
case <-acceptTomb.Dying():
default:
logrus.WithFields(logrus.Fields{
"proxy": proxy.Name,
"listen": proxy.Listen,
"proxy": cleanUserInput(proxy.Name),
"listen": cleanUserInput(proxy.Listen),
"err": err,
}).Warn("Error while accepting client")
}
return
}

logrus.WithFields(logrus.Fields{
"name": proxy.Name,
"name": cleanUserInput(proxy.Name),
"client": client.RemoteAddr(),
"proxy": proxy.Listen,
"upstream": proxy.Upstream,
"proxy": cleanUserInput(proxy.Listen),
"upstream": cleanUserInput(proxy.Upstream),
}).Info("Accepted client")

upstream, err := net.Dial("tcp", proxy.Upstream)
if err != nil {
logrus.WithFields(logrus.Fields{
"name": proxy.Name,
"name": cleanUserInput(proxy.Name),
"client": client.RemoteAddr(),
"proxy": proxy.Listen,
"upstream": proxy.Upstream,
"proxy": cleanUserInput(proxy.Listen),
"upstream": cleanUserInput(proxy.Upstream),
}).Error("Unable to open connection to upstream: " + err.Error())
client.Close()
continue
Expand Down Expand Up @@ -238,8 +239,12 @@ func stop(proxy *Proxy) {
}

logrus.WithFields(logrus.Fields{
"name": proxy.Name,
"proxy": proxy.Listen,
"upstream": proxy.Upstream,
"name": cleanUserInput(proxy.Name),
"proxy": cleanUserInput(proxy.Listen),
"upstream": cleanUserInput(proxy.Upstream),
}).Info("Terminated proxy")
}

func cleanUserInput(input string) string {
return strings.ReplaceAll(strings.ReplaceAll(input, "\n", ""), "\r", "")
}
44 changes: 44 additions & 0 deletions scripts/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env bash

package_path=$1
if [[ -z "$package_path" ]]; then
echo "usage: $0 <package-path> $1 <package-name>"
exit 1
fi

package=$2

platforms=(
"darwin/amd64"
"darwin/arm64"
"linux/386"
"linux/amd64"
"linux/arm"
"linux/arm64"
"windows/386"
"windows/amd64"
"windows/arm"
)

rm -rf dist/$package/*
for platform in "${platforms[@]}"
do
platform_split=(${platform//\// })
GOOS=${platform_split[0]}``
GOARCH=${platform_split[1]}
echo 'Building' $GOOS-$GOARCH
output_name=toxiproxy-$package-$GOOS-$GOARCH

env GOOS=$GOOS GOARCH=$GOARCH go build -v -o dist/$package/$output_name $package_path > /dev/null

if [ $? -ne 0 ]; then
echo 'An error has occurred! Aborting the script execution...'
exit 1
fi

cd dist/$package
mv $output_name toxiproxy-$package
tar -czvf $output_name.tar.gz toxiproxy-$package
rm -rf toxiproxy-$package
cd ../..
done
Loading