Read webhook token from viper config.

This commit is contained in:
Patrick Connolly
2018-10-30 00:42:29 +08:00
parent 3d6f6ebe61
commit d47553c28c
4 changed files with 19 additions and 10 deletions

View File

@@ -171,6 +171,7 @@ type ConfigValues struct {
General Protocol
Gateway []Gateway
SameChannelGateway []SameChannelGateway
ConfigWebhookToken string
}
type Config struct {

View File

@@ -4,6 +4,7 @@ import (
"net/http"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/spf13/viper"
)
func Serve() {
@@ -13,12 +14,9 @@ func Serve() {
e.Logger.Fatal(e.Start(":1323"))
}
var (
secretToken = "1234567890"
)
func reloadConfig(c echo.Context) error {
providedToken := c.QueryParam("token")
secretToken := viper.GetString("ConfigWebhookToken")
if providedToken != secretToken {
return c.String(http.StatusUnauthorized, "Unauthorized")
}

View File

@@ -10,6 +10,7 @@ import (
"github.com/42wim/matterbridge/gateway"
"github.com/42wim/matterbridge/gateway/webhook"
"github.com/google/gops/agent"
"github.com/spf13/viper"
prefixed "github.com/matterbridge/logrus-prefixed-formatter"
log "github.com/sirupsen/logrus"
)
@@ -41,18 +42,22 @@ func main() {
flog.Info("Enabling debug")
log.SetLevel(log.DebugLevel)
}
if *flagWebhook {
fmt.Printf("Starting webhook for reloading remote config...")
fmt.Printf("Serving at: POST /webhook")
webhook.Serve()
return
}
flog.Printf("Running version %s %s", version, githash)
if strings.Contains(version, "-dev") {
flog.Println("WARNING: THIS IS A DEVELOPMENT VERSION. Things may break.")
}
cfg := config.NewConfig(*flagConfig)
cfg.General.Debug = *flagDebug
if *flagWebhook {
// TODO: Find out why this reverts after config load
log.SetFormatter(&prefixed.TextFormatter{PrefixPadding: 13, DisableColors: true, FullTimestamp: true})
flog.Println("Starting webhook for reloading remote config...")
if viper.GetString("ConfigWebhookToken") == "" {
flog.Fatalf("Must set config webhook's auth token to use.")
}
flog.Println("Serving at: POST /webhook")
webhook.Serve()
}
r, err := gateway.NewRouter(cfg)
if err != nil {
flog.Fatalf("Starting gateway failed: %s", err)

View File

@@ -1,3 +1,8 @@
# If you are serving the config reload webhook, you will need to set this
# security token the will be needed to trigger reload.
# OPTIONAL (default "")
ConfigWebhookToken=1234567890
#This is configuration for matterbridge.
#WARNING: as this file contains credentials, be sure to set correct file permissions
###################################################################