Skip to content

Commit

Permalink
feat: HTTP basic auth for webhooks (#9332)
Browse files Browse the repository at this point in the history
Co-authored-by: Sebastian Spaink <[email protected]>
  • Loading branch information
akrantz01 and sspaink authored Mar 4, 2022
1 parent 7e2f22c commit f76729c
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 8 deletions.
23 changes: 23 additions & 0 deletions plugins/common/auth/basic_auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package auth

import (
"crypto/subtle"
"net/http"
)

type BasicAuth struct {
Username string `toml:"username"`
Password string `toml:"password"`
}

func (b *BasicAuth) Verify(r *http.Request) bool {
if b.Username == "" && b.Password == "" {
return true
}

username, password, ok := r.BasicAuth()

usernameComparison := subtle.ConstantTimeCompare([]byte(username), []byte(b.Username)) == 1
passwordComparison := subtle.ConstantTimeCompare([]byte(password), []byte(b.Password)) == 1
return ok && usernameComparison && passwordComparison
}
33 changes: 33 additions & 0 deletions plugins/common/auth/basic_auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package auth

import (
"github.com/stretchr/testify/require"
"net/http/httptest"
"testing"
)

func TestBasicAuth_VerifyWithCredentials(t *testing.T) {
auth := BasicAuth{"username", "password"}

r := httptest.NewRequest("GET", "/github", nil)
r.SetBasicAuth(auth.Username, auth.Password)

require.True(t, auth.Verify(r))
}

func TestBasicAuth_VerifyWithoutCredentials(t *testing.T) {
auth := BasicAuth{}

r := httptest.NewRequest("GET", "/github", nil)

require.True(t, auth.Verify(r))
}

func TestBasicAuth_VerifyWithInvalidCredentials(t *testing.T) {
auth := BasicAuth{"username", "password"}

r := httptest.NewRequest("GET", "/github", nil)
r.SetBasicAuth("wrong-username", "wrong-password")

require.False(t, auth.Verify(r))
}
24 changes: 24 additions & 0 deletions plugins/inputs/webhooks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,45 @@ sudo service telegraf start
[inputs.webhooks.filestack]
path = "/filestack"

## HTTP basic auth
#username = ""
#password = ""

[inputs.webhooks.github]
path = "/github"
# secret = ""

## HTTP basic auth
#username = ""
#password = ""

[inputs.webhooks.mandrill]
path = "/mandrill"

## HTTP basic auth
#username = ""
#password = ""

[inputs.webhooks.rollbar]
path = "/rollbar"

## HTTP basic auth
#username = ""
#password = ""

[inputs.webhooks.papertrail]
path = "/papertrail"

## HTTP basic auth
#username = ""
#password = ""

[inputs.webhooks.particle]
path = "/particle"

## HTTP basic auth
#username = ""
#password = ""
```

## Available webhooks
Expand Down
12 changes: 10 additions & 2 deletions plugins/inputs/webhooks/filestack/filestack_webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ package filestack

import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"time"

"github.com/gorilla/mux"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/common/auth"
)

type FilestackWebhook struct {
Path string
acc telegraf.Accumulator
log telegraf.Logger
auth.BasicAuth
}

func (fs *FilestackWebhook) Register(router *mux.Router, acc telegraf.Accumulator, log telegraf.Logger) {
Expand All @@ -27,7 +29,13 @@ func (fs *FilestackWebhook) Register(router *mux.Router, acc telegraf.Accumulato

func (fs *FilestackWebhook) eventHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
body, err := io.ReadAll(r.Body)

if !fs.Verify(r) {
w.WriteHeader(http.StatusUnauthorized)
return
}

body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
Expand Down
8 changes: 8 additions & 0 deletions plugins/inputs/webhooks/github/github_webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import (
"github.com/gorilla/mux"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/common/auth"
)

type GithubWebhook struct {
Path string
Secret string
acc telegraf.Accumulator
log telegraf.Logger
auth.BasicAuth
}

func (gh *GithubWebhook) Register(router *mux.Router, acc telegraf.Accumulator, log telegraf.Logger) {
Expand All @@ -30,6 +32,12 @@ func (gh *GithubWebhook) Register(router *mux.Router, acc telegraf.Accumulator,

func (gh *GithubWebhook) eventHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()

if !gh.Verify(r) {
w.WriteHeader(http.StatusUnauthorized)
return
}

eventType := r.Header.Get("X-Github-Event")
data, err := io.ReadAll(r.Body)
if err != nil {
Expand Down
16 changes: 12 additions & 4 deletions plugins/inputs/webhooks/mandrill/mandrill_webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ package mandrill

import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/url"
"time"

"github.com/influxdata/telegraf"

"github.com/gorilla/mux"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/common/auth"
)

type MandrillWebhook struct {
Path string
acc telegraf.Accumulator
log telegraf.Logger
auth.BasicAuth
}

func (md *MandrillWebhook) Register(router *mux.Router, acc telegraf.Accumulator, log telegraf.Logger) {
Expand All @@ -33,7 +35,13 @@ func (md *MandrillWebhook) returnOK(w http.ResponseWriter, _ *http.Request) {

func (md *MandrillWebhook) eventHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
body, err := io.ReadAll(r.Body)

if !md.Verify(r) {
w.WriteHeader(http.StatusUnauthorized)
return
}

body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
Expand Down
7 changes: 7 additions & 0 deletions plugins/inputs/webhooks/papertrail/papertrail_webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import (
"github.com/gorilla/mux"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/common/auth"
)

type PapertrailWebhook struct {
Path string
acc telegraf.Accumulator
log telegraf.Logger
auth.BasicAuth
}

func (pt *PapertrailWebhook) Register(router *mux.Router, acc telegraf.Accumulator, log telegraf.Logger) {
Expand All @@ -30,6 +32,11 @@ func (pt *PapertrailWebhook) eventHandler(w http.ResponseWriter, r *http.Request
return
}

if !pt.Verify(r) {
w.WriteHeader(http.StatusUnauthorized)
return
}

data := r.PostFormValue("payload")
if data == "" {
http.Error(w, "Bad Request", http.StatusBadRequest)
Expand Down
8 changes: 8 additions & 0 deletions plugins/inputs/webhooks/particle/particle_webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/gorilla/mux"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/common/auth"
)

type event struct {
Expand Down Expand Up @@ -40,6 +41,7 @@ type ParticleWebhook struct {
Path string
acc telegraf.Accumulator
log telegraf.Logger
auth.BasicAuth
}

func (rb *ParticleWebhook) Register(router *mux.Router, acc telegraf.Accumulator, log telegraf.Logger) {
Expand All @@ -51,6 +53,12 @@ func (rb *ParticleWebhook) Register(router *mux.Router, acc telegraf.Accumulator

func (rb *ParticleWebhook) eventHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()

if !rb.Verify(r) {
w.WriteHeader(http.StatusUnauthorized)
return
}

e := newEvent()
if err := json.NewDecoder(r.Body).Decode(e); err != nil {
rb.acc.AddError(err)
Expand Down
12 changes: 10 additions & 2 deletions plugins/inputs/webhooks/rollbar/rollbar_webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ package rollbar
import (
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
"time"

"github.com/gorilla/mux"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/common/auth"
)

type RollbarWebhook struct {
Path string
acc telegraf.Accumulator
log telegraf.Logger
auth.BasicAuth
}

func (rb *RollbarWebhook) Register(router *mux.Router, acc telegraf.Accumulator, log telegraf.Logger) {
Expand All @@ -27,7 +29,13 @@ func (rb *RollbarWebhook) Register(router *mux.Router, acc telegraf.Accumulator,

func (rb *RollbarWebhook) eventHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
data, err := io.ReadAll(r.Body)

if !rb.Verify(r) {
w.WriteHeader(http.StatusUnauthorized)
return
}

data, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
Expand Down
24 changes: 24 additions & 0 deletions plugins/inputs/webhooks/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,45 @@ func (wb *Webhooks) SampleConfig() string {
[inputs.webhooks.filestack]
path = "/filestack"
## HTTP basic auth
#username = ""
#password = ""
[inputs.webhooks.github]
path = "/github"
# secret = ""
## HTTP basic auth
#username = ""
#password = ""
[inputs.webhooks.mandrill]
path = "/mandrill"
## HTTP basic auth
#username = ""
#password = ""
[inputs.webhooks.rollbar]
path = "/rollbar"
## HTTP basic auth
#username = ""
#password = ""
[inputs.webhooks.papertrail]
path = "/papertrail"
## HTTP basic auth
#username = ""
#password = ""
[inputs.webhooks.particle]
path = "/particle"
## HTTP basic auth
#username = ""
#password = ""
`
}

Expand Down

0 comments on commit f76729c

Please sign in to comment.