Skip to content
This repository was archived by the owner on Apr 11, 2021. It is now read-only.
Merged

Auth #23

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
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
68 changes: 38 additions & 30 deletions beeping.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"crypto/tls"
"encoding/base64"
"flag"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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 != "" {
Expand Down