forked from vikunja/vikunja
Compare commits
2 Commits
2cf73b07be
...
b018fa4f43
Author | SHA1 | Date |
---|---|---|
Ubuntu | b018fa4f43 | |
Ubuntu | 64b2bb4f78 |
|
@ -378,7 +378,7 @@ func InitDefaultConfig() {
|
|||
// Metrics
|
||||
MetricsEnabled.setDefault(false)
|
||||
|
||||
WebhooksConf.setDefault( []int{} )
|
||||
WebhooksConf.setDefault([]int{})
|
||||
}
|
||||
|
||||
// InitConfig initializes the config, sets defaults etc.
|
||||
|
|
|
@ -16,20 +16,17 @@
|
|||
|
||||
package webhooks
|
||||
|
||||
|
||||
import (
|
||||
|
||||
"code.vikunja.io/api/pkg/log"
|
||||
|
||||
"github.com/ThreeDotsLabs/watermill/message"
|
||||
)
|
||||
|
||||
|
||||
func GenerateMiddleware() message.HandlerMiddleware {
|
||||
runtimeConfig := ProcessConfig()
|
||||
return func(hf message.HandlerFunc) message.HandlerFunc {
|
||||
return func(msg *message.Message) ([]*message.Message, error) {
|
||||
topic := message.SubscribeTopicFromCtx(msg.Context() )
|
||||
topic := message.SubscribeTopicFromCtx(msg.Context())
|
||||
handleOutgoingWebhook(runtimeConfig, topic, msg)
|
||||
newMsg, err := hf(msg)
|
||||
if err != nil {
|
||||
|
@ -40,8 +37,6 @@ func GenerateMiddleware() message.HandlerMiddleware {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
func handleOutgoingWebhook(cfg []WebhookRuntimeConfig, topic string, msg *message.Message) {
|
||||
log.Debugf("Webhook handler for '%s'", topic)
|
||||
for i, entry := range cfg {
|
||||
|
@ -52,4 +47,3 @@ func handleOutgoingWebhook(cfg []WebhookRuntimeConfig, topic string, msg *messag
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -17,18 +17,18 @@
|
|||
package webhooks
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"net/http"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
|
||||
"code.vikunja.io/api/pkg/log"
|
||||
"code.vikunja.io/api/pkg/config"
|
||||
"code.vikunja.io/api/pkg/log"
|
||||
|
||||
"github.com/ThreeDotsLabs/watermill/message"
|
||||
)
|
||||
|
@ -40,9 +40,8 @@ const (
|
|||
hmacHeader = "X-Signature"
|
||||
)
|
||||
|
||||
type FilteringFunction func(string) (bool)
|
||||
type WebhookCallFunction func(string, *message.Message) (error)
|
||||
|
||||
type FilteringFunction func(string) bool
|
||||
type WebhookCallFunction func(string, *message.Message) error
|
||||
|
||||
// Single configuration entry
|
||||
type SingleConfEntry struct {
|
||||
|
@ -52,35 +51,31 @@ type SingleConfEntry struct {
|
|||
Timeout int `json:"timeout"`
|
||||
}
|
||||
|
||||
|
||||
type WebhookRuntimeConfig struct {
|
||||
FilterFunc FilteringFunction
|
||||
ExecuteFunc WebhookCallFunction
|
||||
}
|
||||
|
||||
|
||||
|
||||
func getWebhookFilterFunc(cfg SingleConfEntry) FilteringFunction {
|
||||
return func(topic string) (is_interesting bool) {
|
||||
for _, filter := range cfg.Events {
|
||||
log.Debugf("Match pattern:'%s' agains:'%s'", filter, topic)
|
||||
log.Debugf("Match pattern:'%s' topic:'%s'", filter, topic)
|
||||
if filter == "*" {
|
||||
log.Debugf(" '*' == Always match ")
|
||||
return true
|
||||
} else {
|
||||
}
|
||||
if strings.HasPrefix(topic, filter) {
|
||||
log.Debugf("Positive match [%s] -> [%s]", filter, topic)
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
log.Debugf("No match for [%s]", topic)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func getWebhookCallFunc(cfg SingleConfEntry) WebhookCallFunction {
|
||||
return func(topic string, msg *message.Message) (error) {
|
||||
return func(topic string, msg *message.Message) error {
|
||||
endpointURL := cfg.URL
|
||||
hmacKey := cfg.Secret
|
||||
timeout := defaultTimeout
|
||||
|
@ -89,11 +84,11 @@ func getWebhookCallFunc(cfg SingleConfEntry) WebhookCallFunction {
|
|||
}
|
||||
|
||||
log.Debugf("Webhook Call : %s (key=%s)", endpointURL, hmacKey)
|
||||
webhookUrl := fmt.Sprintf("%s%s", endpointURL, topic)
|
||||
webhookURL := fmt.Sprintf("%s%s", endpointURL, topic)
|
||||
|
||||
rawData := msg.Payload
|
||||
|
||||
req, err := http.NewRequest( http.MethodPost, webhookUrl, bytes.NewBuffer(rawData))
|
||||
req, err := http.NewRequest(http.MethodPost, webhookURL, bytes.NewBuffer(rawData))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -110,30 +105,26 @@ func getWebhookCallFunc(cfg SingleConfEntry) WebhookCallFunction {
|
|||
req.Header.Set(hmacHeader, signature)
|
||||
}
|
||||
|
||||
_, err = client.Do(req)
|
||||
|
||||
resp, err = client.Do(req)
|
||||
defer resp.Body.Close()
|
||||
if err != nil {
|
||||
log.Debugf("Webhook failed : %s , +%v", webhookUrl , err)
|
||||
log.Debugf("Webhook failed : %s , +%v", webhookURL, err)
|
||||
return err
|
||||
}
|
||||
|
||||
log.Debugf("Webhook success : %s ", webhookUrl )
|
||||
log.Debugf("Webhook success : %s ", webhookURL)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
func GenerateHMAC(data []byte, key string) string {
|
||||
h := hmac.New(sha256.New, []byte(key))
|
||||
h.Write(data)
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Process config and prepare mapping
|
||||
func ProcessConfig() ([]WebhookRuntimeConfig) {
|
||||
func ProcessConfig() []WebhookRuntimeConfig {
|
||||
|
||||
var items []SingleConfEntry
|
||||
|
||||
|
|
Loading…
Reference in New Issue