misc updates
This commit is contained in:
parent
fe5cf5a911
commit
865045c8ba
2
Makefile
2
Makefile
|
@ -9,7 +9,7 @@ linters-install:
|
|||
|
||||
lint: linters-install
|
||||
@gofmt -l . >gofmt.test 2>&1 && if [ -s gofmt.test ]; then echo "Fix formatting using 'gofmt -s -w .' for:"; cat gofmt.test; exit 1; fi && rm gofmt.test
|
||||
gometalinter --vendor --disable-all --enable=vet --enable=vetshadow --enable=golint --enable=maligned --enable=megacheck --enable=ineffassign --enable=misspell --enable=errcheck --enable=goconst ./...
|
||||
gometalinter --vendor --disable-all --enable=vet --enable=vetshadow --enable=golint --enable=megacheck --enable=ineffassign --enable=misspell --enable=errcheck --enable=goconst ./...
|
||||
|
||||
test:
|
||||
$(GOCMD) test -cover -race ./...
|
||||
|
|
|
@ -152,7 +152,7 @@ func (hook Webhook) Parse(r *http.Request, events ...Event) (interface{}, error)
|
|||
return nil, ErrMissingHubSignatureHeader
|
||||
}
|
||||
mac := hmac.New(sha1.New, []byte(hook.secret))
|
||||
mac.Write(payload)
|
||||
_, _ = mac.Write(payload)
|
||||
expectedMAC := hex.EncodeToString(mac.Sum(nil))
|
||||
|
||||
if !hmac.Equal([]byte(signature[5:]), []byte(expectedMAC)) {
|
||||
|
|
|
@ -120,7 +120,7 @@ func (hook Webhook) Parse(r *http.Request, events ...Event) (interface{}, error)
|
|||
}
|
||||
|
||||
mac := hmac.New(sha256.New, []byte(hook.secret))
|
||||
mac.Write(payload)
|
||||
_, _ = mac.Write(payload)
|
||||
|
||||
expectedMAC := hex.EncodeToString(mac.Sum(nil))
|
||||
|
||||
|
|
119
webhooks.go
119
webhooks.go
|
@ -1,119 +0,0 @@
|
|||
package webhooks
|
||||
|
||||
// Header provides http.Header to minimize imports
|
||||
// type Header http.Header
|
||||
|
||||
// // Provider defines the type of webhook
|
||||
// type Provider int
|
||||
|
||||
// func (p Provider) String() string {
|
||||
// switch p {
|
||||
// case GitHub:
|
||||
// return "GitHub"
|
||||
// case Bitbucket:
|
||||
// return "Bitbucket"
|
||||
// case GitLab:
|
||||
// return "GitLab"
|
||||
// case Gogs:
|
||||
// return "Gogs"
|
||||
// default:
|
||||
// return "Unknown"
|
||||
// }
|
||||
// }
|
||||
|
||||
// // webhooks available providers
|
||||
// const (
|
||||
// GitHub Provider = iota
|
||||
// Bitbucket
|
||||
// GitLab
|
||||
// Gogs
|
||||
// )
|
||||
|
||||
// // Webhook interface defines a webhook to receive events
|
||||
// type Webhook interface {
|
||||
// Provider() Provider
|
||||
// ParsePayload(w http.ResponseWriter, r *http.Request)
|
||||
// }
|
||||
|
||||
// type server struct {
|
||||
// hook Webhook
|
||||
// path string
|
||||
// includePathCheck bool
|
||||
// }
|
||||
|
||||
// ProcessPayloadFunc is a common function for payload return values
|
||||
// type ProcessPayloadFunc func(payload interface{}, header Header) error
|
||||
|
||||
// // Handler returns the webhook http.Handler for use in your own Mux implementation
|
||||
// func Handler(hook Webhook) http.Handler {
|
||||
// return &server{
|
||||
// hook: hook,
|
||||
// }
|
||||
// }
|
||||
|
||||
// // Run runs a server
|
||||
// func Run(hook Webhook, addr string, path string) error {
|
||||
// srv := &server{
|
||||
// hook: hook,
|
||||
// path: path,
|
||||
// includePathCheck: true,
|
||||
// }
|
||||
// s := &http.Server{Addr: addr, Handler: srv}
|
||||
|
||||
// DefaultLog.Info(fmt.Sprintf("Listening on addr: %s path: %s", addr, path))
|
||||
// return s.ListenAndServe()
|
||||
// }
|
||||
|
||||
// // RunServer runs a custom server.
|
||||
// func RunServer(s *http.Server, hook Webhook, path string) error {
|
||||
|
||||
// srv := &server{
|
||||
// hook: hook,
|
||||
// path: path,
|
||||
// includePathCheck: true,
|
||||
// }
|
||||
|
||||
// s.Handler = srv
|
||||
// DefaultLog.Info(fmt.Sprintf("Listening on addr: %s path: %s", s.Addr, path))
|
||||
// return s.ListenAndServe()
|
||||
// }
|
||||
|
||||
// // RunTLSServer runs a custom server with TLS configuration.
|
||||
// // NOTE: http.Server Handler will be overridden by this library, just set it to nil.
|
||||
// // Setting the Certificates can be done in the http.Server.TLSConfig.Certificates
|
||||
// // see example here: https://github.com/go-playground/webhooks/blob/v2/webhooks_test.go#L178
|
||||
// func RunTLSServer(s *http.Server, hook Webhook, path string) error {
|
||||
|
||||
// srv := &server{
|
||||
// hook: hook,
|
||||
// path: path,
|
||||
// includePathCheck: true,
|
||||
// }
|
||||
|
||||
// s.Handler = srv
|
||||
// DefaultLog.Info(fmt.Sprintf("Listening on addr: %s path: %s", s.Addr, path))
|
||||
// return s.ListenAndServeTLS("", "")
|
||||
// }
|
||||
|
||||
// // ServeHTTP is the Handler for every posted WebHook Event
|
||||
// func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
// defer r.Body.Close()
|
||||
// DefaultLog.Info("Webhook received")
|
||||
|
||||
// if r.Method != "POST" {
|
||||
// DefaultLog.Error(fmt.Sprintf("405 Method not allowed, attempt made using Method: %s", r.Method))
|
||||
// http.Error(w, "405 Method not allowed", http.StatusMethodNotAllowed)
|
||||
// return
|
||||
// }
|
||||
|
||||
// DefaultLog.Debug(fmt.Sprintf("Include path check: %t", s.includePathCheck))
|
||||
// if s.includePathCheck {
|
||||
// if r.URL.Path != s.path {
|
||||
// DefaultLog.Error(fmt.Sprintf("404 Not found, POST made using path: %s, but expected %s", r.URL.Path, s.path))
|
||||
// http.Error(w, "404 Not found", http.StatusNotFound)
|
||||
// return
|
||||
// }
|
||||
// }
|
||||
|
||||
// s.hook.ParsePayload(w, r)
|
||||
// }
|
281
webhooks_test.go
281
webhooks_test.go
|
@ -1,281 +0,0 @@
|
|||
package webhooks
|
||||
|
||||
//
|
||||
//import (
|
||||
// "bytes"
|
||||
// "crypto/tls"
|
||||
// "net/http"
|
||||
// "os"
|
||||
// "testing"
|
||||
// "time"
|
||||
//
|
||||
// "net/http/httptest"
|
||||
//
|
||||
// . "gopkg.in/go-playground/assert.v1"
|
||||
//)
|
||||
//
|
||||
//// NOTES:
|
||||
//// - Run "go test" to run tests
|
||||
//// - Run "gocov test | gocov report" to report on test converage by file
|
||||
//// - Run "gocov test | gocov annotate -" to report on all code and functions, those ,marked with "MISS" were never called
|
||||
////
|
||||
//// or
|
||||
////
|
||||
//// -- may be a good idea to change to output path to somewherelike /tmp
|
||||
//// go test -coverprofile cover.out && go tool cover -html=cover.out -o cover.html
|
||||
////
|
||||
//
|
||||
//type FakeWebhook struct {
|
||||
// provider Provider
|
||||
//}
|
||||
//
|
||||
//func (fhook FakeWebhook) Provider() Provider {
|
||||
// return fhook.provider
|
||||
//}
|
||||
//
|
||||
//func (fhook FakeWebhook) ParsePayload(w http.ResponseWriter, r *http.Request) {
|
||||
//}
|
||||
//
|
||||
//var fakeHook Webhook
|
||||
//
|
||||
//func TestMain(m *testing.M) {
|
||||
//
|
||||
// // setup
|
||||
// fakeHook = &FakeWebhook{
|
||||
// provider: GitHub,
|
||||
// }
|
||||
//
|
||||
// os.Exit(m.Run())
|
||||
//
|
||||
// // teardown
|
||||
//}
|
||||
//
|
||||
//func TestHandler(t *testing.T) {
|
||||
//
|
||||
// mux := http.NewServeMux()
|
||||
// mux.Handle("/webhooks", Handler(fakeHook))
|
||||
//
|
||||
// s := httptest.NewServer(Handler(fakeHook))
|
||||
// defer s.Close()
|
||||
//
|
||||
// payload := "{}"
|
||||
//
|
||||
// req, err := http.NewRequest("POST", s.URL+"/webhooks", bytes.NewBuffer([]byte(payload)))
|
||||
// req.Header.Set("Content-Type", "application/json")
|
||||
//
|
||||
// Equal(t, err, nil)
|
||||
//
|
||||
// client := &http.Client{}
|
||||
// resp, err := client.Do(req)
|
||||
// Equal(t, err, nil)
|
||||
//
|
||||
// defer resp.Body.Close()
|
||||
//
|
||||
// Equal(t, resp.StatusCode, http.StatusOK)
|
||||
//
|
||||
// // Test BAD METHOD
|
||||
// req, err = http.NewRequest("GET", s.URL+"/webhooks", bytes.NewBuffer([]byte(payload)))
|
||||
// req.Header.Set("Content-Type", "application/json")
|
||||
//
|
||||
// Equal(t, err, nil)
|
||||
//
|
||||
// resp, err = client.Do(req)
|
||||
// Equal(t, err, nil)
|
||||
//
|
||||
// defer resp.Body.Close()
|
||||
//
|
||||
// Equal(t, resp.StatusCode, http.StatusMethodNotAllowed)
|
||||
//}
|
||||
//
|
||||
//func TestRun(t *testing.T) {
|
||||
//
|
||||
// go Run(fakeHook, "127.0.0.1:3006", "/webhooks")
|
||||
// time.Sleep(5000)
|
||||
//
|
||||
// payload := "{}"
|
||||
//
|
||||
// req, err := http.NewRequest("POST", "http://127.0.0.1:3006/webhooks", bytes.NewBuffer([]byte(payload)))
|
||||
// req.Header.Set("Content-Type", "application/json")
|
||||
//
|
||||
// Equal(t, err, nil)
|
||||
//
|
||||
// client := &http.Client{}
|
||||
// resp, err := client.Do(req)
|
||||
// Equal(t, err, nil)
|
||||
//
|
||||
// defer resp.Body.Close()
|
||||
//
|
||||
// Equal(t, resp.StatusCode, http.StatusOK)
|
||||
//
|
||||
// // While HTTP Server is running test some bad input
|
||||
//
|
||||
// // Test BAD URL
|
||||
// req, err = http.NewRequest("POST", "http://127.0.0.1:3006", bytes.NewBuffer([]byte(payload)))
|
||||
// req.Header.Set("Content-Type", "application/json")
|
||||
//
|
||||
// Equal(t, err, nil)
|
||||
//
|
||||
// resp, err = client.Do(req)
|
||||
// Equal(t, err, nil)
|
||||
//
|
||||
// defer resp.Body.Close()
|
||||
//
|
||||
// Equal(t, resp.StatusCode, http.StatusNotFound)
|
||||
//
|
||||
// // Test BAD METHOD
|
||||
// req, err = http.NewRequest("GET", "http://127.0.0.1:3006/webhooks", bytes.NewBuffer([]byte(payload)))
|
||||
// req.Header.Set("Content-Type", "application/json")
|
||||
//
|
||||
// Equal(t, err, nil)
|
||||
//
|
||||
// resp, err = client.Do(req)
|
||||
// Equal(t, err, nil)
|
||||
//
|
||||
// defer resp.Body.Close()
|
||||
//
|
||||
// Equal(t, resp.StatusCode, http.StatusMethodNotAllowed)
|
||||
//}
|
||||
//
|
||||
//func TestRunServer(t *testing.T) {
|
||||
// DefaultLog = NewLogger(true)
|
||||
// server := &http.Server{Addr: "127.0.0.1:3007", Handler: nil}
|
||||
// go RunServer(server, fakeHook, "/webhooks")
|
||||
// time.Sleep(5000)
|
||||
//
|
||||
// payload := "{}"
|
||||
//
|
||||
// req, err := http.NewRequest("POST", "http://127.0.0.1:3007/webhooks", bytes.NewBuffer([]byte(payload)))
|
||||
// req.Header.Set("Content-Type", "application/json")
|
||||
//
|
||||
// Equal(t, err, nil)
|
||||
//
|
||||
// client := &http.Client{}
|
||||
// resp, err := client.Do(req)
|
||||
// Equal(t, err, nil)
|
||||
//
|
||||
// defer resp.Body.Close()
|
||||
//
|
||||
// Equal(t, resp.StatusCode, http.StatusOK)
|
||||
//
|
||||
// // While HTTP Server is running test some bad input
|
||||
//
|
||||
// // Test BAD URL
|
||||
// req, err = http.NewRequest("POST", "http://127.0.0.1:3007", bytes.NewBuffer([]byte(payload)))
|
||||
// req.Header.Set("Content-Type", "application/json")
|
||||
//
|
||||
// Equal(t, err, nil)
|
||||
//
|
||||
// resp, err = client.Do(req)
|
||||
// Equal(t, err, nil)
|
||||
//
|
||||
// defer resp.Body.Close()
|
||||
//
|
||||
// Equal(t, resp.StatusCode, http.StatusNotFound)
|
||||
//
|
||||
// // Test BAD METHOD
|
||||
// req, err = http.NewRequest("GET", "http://127.0.0.1:3007/webhooks", bytes.NewBuffer([]byte(payload)))
|
||||
// req.Header.Set("Content-Type", "application/json")
|
||||
//
|
||||
// Equal(t, err, nil)
|
||||
//
|
||||
// resp, err = client.Do(req)
|
||||
// Equal(t, err, nil)
|
||||
//
|
||||
// defer resp.Body.Close()
|
||||
//
|
||||
// Equal(t, resp.StatusCode, http.StatusMethodNotAllowed)
|
||||
//}
|
||||
//
|
||||
//func TestRunTLSServer(t *testing.T) {
|
||||
//
|
||||
// var err error
|
||||
//
|
||||
// // can have certificates in static variables or load from disk
|
||||
// cert := `-----BEGIN CERTIFICATE-----
|
||||
//MIIB0zCCAX2gAwIBAgIJAI/M7BYjwB+uMA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNV
|
||||
//BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX
|
||||
//aWRnaXRzIFB0eSBMdGQwHhcNMTIwOTEyMjE1MjAyWhcNMTUwOTEyMjE1MjAyWjBF
|
||||
//MQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50
|
||||
//ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANLJ
|
||||
//hPHhITqQbPklG3ibCVxwGMRfp/v4XqhfdQHdcVfHap6NQ5Wok/4xIA+ui35/MmNa
|
||||
//rtNuC+BdZ1tMuVCPFZcCAwEAAaNQME4wHQYDVR0OBBYEFJvKs8RfJaXTH08W+SGv
|
||||
//zQyKn0H8MB8GA1UdIwQYMBaAFJvKs8RfJaXTH08W+SGvzQyKn0H8MAwGA1UdEwQF
|
||||
//MAMBAf8wDQYJKoZIhvcNAQEFBQADQQBJlffJHybjDGxRMqaRmDhX0+6v02TUKZsW
|
||||
//r5QuVbpQhH6u+0UgcW0jp9QwpxoPTLTWGXEWBBBurxFwiCBhkQ+V
|
||||
//-----END CERTIFICATE-----
|
||||
// `
|
||||
// key := `-----BEGIN RSA PRIVATE KEY-----
|
||||
//MIIBOwIBAAJBANLJhPHhITqQbPklG3ibCVxwGMRfp/v4XqhfdQHdcVfHap6NQ5Wo
|
||||
//k/4xIA+ui35/MmNartNuC+BdZ1tMuVCPFZcCAwEAAQJAEJ2N+zsR0Xn8/Q6twa4G
|
||||
//6OB1M1WO+k+ztnX/1SvNeWu8D6GImtupLTYgjZcHufykj09jiHmjHx8u8ZZB/o1N
|
||||
//MQIhAPW+eyZo7ay3lMz1V01WVjNKK9QSn1MJlb06h/LuYv9FAiEA25WPedKgVyCW
|
||||
//SmUwbPw8fnTcpqDWE3yTO3vKcebqMSsCIBF3UmVue8YU3jybC3NxuXq3wNm34R8T
|
||||
//xVLHwDXh/6NJAiEAl2oHGGLz64BuAfjKrqwz7qMYr9HCLIe/YsoWq/olzScCIQDi
|
||||
//D2lWusoe2/nEqfDVVWGWlyJ7yOmqaVm/iNUN9B2N2g==
|
||||
//-----END RSA PRIVATE KEY-----
|
||||
//`
|
||||
//
|
||||
// server := &http.Server{Addr: "127.0.0.1:3008", Handler: nil, TLSConfig: &tls.Config{}}
|
||||
// server.TLSConfig.Certificates = make([]tls.Certificate, 1)
|
||||
//
|
||||
// server.TLSConfig.Certificates[0], err = tls.X509KeyPair([]byte(cert), []byte(key))
|
||||
// Equal(t, err, nil)
|
||||
//
|
||||
// go RunTLSServer(server, fakeHook, "/webhooks")
|
||||
// time.Sleep(5000)
|
||||
//
|
||||
// payload := "{}"
|
||||
//
|
||||
// req, err := http.NewRequest("POST", "https://127.0.0.1:3008/webhooks", bytes.NewBuffer([]byte(payload)))
|
||||
// req.Header.Set("Content-Type", "application/json")
|
||||
//
|
||||
// Equal(t, err, nil)
|
||||
//
|
||||
// tr := &http.Transport{
|
||||
// TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
// }
|
||||
//
|
||||
// client := &http.Client{Transport: tr}
|
||||
// resp, err := client.Do(req)
|
||||
// Equal(t, err, nil)
|
||||
//
|
||||
// defer resp.Body.Close()
|
||||
//
|
||||
// Equal(t, resp.StatusCode, http.StatusOK)
|
||||
//
|
||||
// // While HTTP Server is running test some bad input
|
||||
//
|
||||
// // Test BAD URL
|
||||
// req, err = http.NewRequest("POST", "https://127.0.0.1:3008", bytes.NewBuffer([]byte(payload)))
|
||||
// req.Header.Set("Content-Type", "application/json")
|
||||
//
|
||||
// Equal(t, err, nil)
|
||||
//
|
||||
// resp, err = client.Do(req)
|
||||
// Equal(t, err, nil)
|
||||
//
|
||||
// defer resp.Body.Close()
|
||||
//
|
||||
// Equal(t, resp.StatusCode, http.StatusNotFound)
|
||||
//
|
||||
// // Test BAD METHOD
|
||||
// req, err = http.NewRequest("GET", "https://127.0.0.1:3008/webhooks", bytes.NewBuffer([]byte(payload)))
|
||||
// req.Header.Set("Content-Type", "application/json")
|
||||
//
|
||||
// Equal(t, err, nil)
|
||||
//
|
||||
// resp, err = client.Do(req)
|
||||
// Equal(t, err, nil)
|
||||
//
|
||||
// defer resp.Body.Close()
|
||||
//
|
||||
// Equal(t, resp.StatusCode, http.StatusMethodNotAllowed)
|
||||
//}
|
||||
//
|
||||
//func TestProviderString(t *testing.T) {
|
||||
//
|
||||
// Equal(t, GitHub.String(), "GitHub")
|
||||
// Equal(t, Bitbucket.String(), "Bitbucket")
|
||||
// Equal(t, GitLab.String(), "GitLab")
|
||||
// Equal(t, Provider(999999).String(), "Unknown")
|
||||
//}
|
Loading…
Reference in New Issue