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

Dashboard for Toxiproxy #218

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ cover*.out
coverage.html
*.deb
tmp/
statik/*
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ COMBINED_GOPATH=$(GODEP_PATH):$(ORIGINAL_PATH)
.PHONY: packages deb test linux darwin windows

build:
statik -src=./dashboard
GOPATH=$(COMBINED_GOPATH) go build -ldflags="-X github.com/Shopify/toxiproxy.Version=git-$(shell git rev-parse --short HEAD)" -o $(SERVER_NAME) ./cmd
GOPATH=$(COMBINED_GOPATH) go build -ldflags="-X github.com/Shopify/toxiproxy.Version=git-$(shell git rev-parse --short HEAD)" -o $(CLI_NAME) ./cli

Expand All @@ -26,6 +27,7 @@ clean:
rm -f $(SERVER_NAME)
rm -f $(CLI_NAME)
rm -f *.deb
rm -f statik/*

test:
echo "Testing with" `go version`
Expand Down
38 changes: 32 additions & 6 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,40 @@ import (
"net"
"net/http"
"os"
"strings"

_ "github.com/Shopify/toxiproxy/statik"
"github.com/Shopify/toxiproxy/toxics"
"github.com/gorilla/mux"
"github.com/rakyll/statik/fs"
"github.com/sirupsen/logrus"
)

type ApiServer struct {
Collection *ProxyCollection
Dashboard http.File
}

func NewServer() *ApiServer {
return &ApiServer{
Collection: NewProxyCollection(),
Dashboard: LoadDashboard(),
}
}

func LoadDashboard() http.File {
statikFS, e := fs.New()
if e != nil {
log.Fatal(e)
}

file, e := statikFS.Open("/index.html")
if e != nil {
log.Fatal(e)
}

return file
}

func (server *ApiServer) PopulateConfig(filename string) {
file, err := os.Open(filename)
if err != nil {
Expand All @@ -49,11 +66,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)
} else {
h.ServeHTTP(w, r)
}
h.ServeHTTP(w, r)
})
}

Expand All @@ -73,6 +86,7 @@ func (server *ApiServer) Listen(host string, port string) {
r.HandleFunc("/proxies/{proxy}/toxics/{toxic}", server.ToxicDelete).Methods("DELETE")

r.HandleFunc("/version", server.Version).Methods("GET")
r.HandleFunc("/", server.ServeDashboard).Methods("GET")

http.Handle("/", StopBrowsersMiddleware(r))

Expand Down Expand Up @@ -389,6 +403,18 @@ func (server *ApiServer) Version(response http.ResponseWriter, request *http.Req
}
}

func (server *ApiServer) ServeDashboard(response http.ResponseWriter, request *http.Request) {
f := server.Dashboard

info, e := f.Stat()
if e != nil {
log.Fatal(e)
}

response.Header().Set("Content-Type", "text/html;charset=utf-8")
http.ServeContent(response, request, info.Name(), info.ModTime(), f)
}

type ApiError struct {
Message string `json:"error"`
StatusCode int `json:"status"`
Expand Down
25 changes: 4 additions & 21 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,15 @@ func WithServer(t *testing.T, f func(string)) {
f("http://localhost:8475")
}

func TestBrowserGets403(t *testing.T) {
func TestDashboardIsAccessible(t *testing.T) {
WithServer(t, func(addr string) {
client := http.Client{}

req, _ := http.NewRequest("GET", "http://localhost:8475/proxies", nil)
req.Header.Add("User-Agent", "Mozilla/5.0 (Linux; Android 4.4.2); Nexus 5 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Mobile Safari/537.36 OPR/20.0.1396.72047")

resp, _ := client.Do(req)

if resp.StatusCode != 403 {
t.Fatal("Browser-like UserAgent was not denied access to Toxiproxy")
}
})
}

func TestNonBrowserGets200(t *testing.T) {
WithServer(t, func(addr string) {
client := http.Client{}

req, _ := http.NewRequest("GET", "http://localhost:8475/proxies", nil)
req.Header.Add("User-Agent", "Wget/2.1")

req, _ := http.NewRequest("GET", "http://localhost:8475/", nil)
resp, _ := client.Do(req)

if resp.StatusCode == 403 {
t.Fatal("Non-Browser-like UserAgent was denied access to Toxiproxy")
if resp.StatusCode != 200 {
t.Fatal("Dashboard is not accessible at /")
}
})
}
Expand Down
Loading