From a42fd00de025d307835f9f049a1c44353a7e5811 Mon Sep 17 00:00:00 2001 From: Brice Colucci Date: Thu, 6 Jul 2017 20:08:52 +0200 Subject: [PATCH 1/2] - Implement the HTTP basic auth. - Remove some else statements using return. Just to make the code clearer. --- beeping.go | 68 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 30 deletions(-) diff --git a/beeping.go b/beeping.go index fae4085..60d9408 100644 --- a/beeping.go +++ b/beeping.go @@ -2,6 +2,7 @@ package main import ( "crypto/tls" + "encoding/base64" "flag" "fmt" "io/ioutil" @@ -43,6 +44,7 @@ type Check struct { Header string `json:"header"` Insecure bool `json:"insecure"` Timeout time.Duration `json:"timeout"` + Auth string `json:"auth"` } type Timeline struct { @@ -163,35 +165,39 @@ func handlerDefault(c *gin.Context) { func handlerCheck(c *gin.Context) { var check = NewCheck() - if c.BindJSON(&check) == nil { - if *validatetarget { - if err := check.validateTarget(); err != nil { - log.Println("[WARN] Invalid target:", err.Error()) - c.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()}) - } else { - response, err := CheckHTTP(check) - if err != nil { - log.Println("[WARN] Check failed:", err.Error()) - c.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()}) - } else { - log.Println("[INFO] Successful check:", check.URL, "-", response.HTTPRequestTime, "ms") - c.JSON(http.StatusOK, response) - } - } - } else { - response, err := CheckHTTP(check) - if err != nil { - log.Println("[WARN] Check failed:", err.Error()) - c.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()}) - } else { - log.Println("[INFO] Successful check:", check.URL, "-", response.HTTPRequestTime, "ms") - c.JSON(http.StatusOK, response) - } - } - } else { + if c.BindJSON(&check) != nil { log.Println("[WARN] Invalid JSON sent") c.JSON(http.StatusBadRequest, gin.H{"message": "invalid json sent"}) + return } + + // with security checks + if *validatetarget { + if err := check.validateTarget(); err != nil { + log.Println("[WARN] Invalid target:", err.Error()) + c.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()}) + return + } + response, err := CheckHTTP(check) + if err != nil { + log.Println("[WARN] Check failed:", err.Error()) + c.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()}) + return + } + log.Println("[INFO] Successful check:", check.URL, "-", response.HTTPRequestTime, "ms") + c.JSON(http.StatusOK, response) + return + } + + // without security checks + response, err := CheckHTTP(check) + if err != nil { + log.Println("[WARN] Check failed:", err.Error()) + c.JSON(http.StatusInternalServerError, gin.H{"message": err.Error()}) + return + } + log.Println("[INFO] Successful check:", check.URL, "-", response.HTTPRequestTime, "ms") + c.JSON(http.StatusOK, response) } // CheckHTTP do HTTP check and return a beeping response @@ -205,6 +211,11 @@ func CheckHTTP(check *Check) (*Response, error) { } req.Header.Set("User-Agent", USERAGENT) + + if len(check.Auth) > 0 { + req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(check.Auth))) + } + // Create go-httpstat powered context and pass it to http.Request var result httpstat.Result ctx := httpstat.WithHTTPStat(req.Context(), &result) @@ -254,10 +265,7 @@ func CheckHTTP(check *Check) (*Response, error) { tr.CloseIdleConnections() - pattern := true - if !strings.Contains(string(body), check.Pattern) { - pattern = false - } + pattern := strings.Contains(string(body), check.Pattern) header := true if check.Header != "" { From 3f84f1e221d8783ef73c8f068e118a04c5d580de Mon Sep 17 00:00:00 2001 From: Brice Colucci Date: Thu, 6 Jul 2017 20:13:45 +0200 Subject: [PATCH 2/2] Update the readme file for the auth support --- README.md | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f4a557f..74845d1 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,9 @@ _previously named pingmeback_ > It forage the servers and brings the metrics back to the hive - + ![beeping](http://oi68.tinypic.com/2yngw9h.jpg) - + BeePing is a distant http check as a Service. Call the very simple API, BeePing will measure website for you. @@ -150,6 +150,14 @@ beeping returns HTTP 500 when check fail. The body contains the reason of the fa } ``` +## HTTP Basic Auth + +Just add the 'auth' option in your JSON. + +``` +$ curl -XPOST http://localhost:8080/check -d '{"url":"http://127.0.0.1:3000","auth":"john:secret"}' +``` + ## Changelog ### 0.6.0 - UNRELEASED @@ -181,7 +189,7 @@ beeping returns HTTP 500 when check fail. The body contains the reason of the fa ## To Do -- [ ] Add HTTP Auth +- [x] Add HTTP Auth - [ ] Add tests - [ ] More metrics - [ ] Packaging