Skip to content

Commit

Permalink
Fix MS Teams integration #607
Browse files Browse the repository at this point in the history
##### ISSUE TYPE
 - Bug fix Pull Request

##### SUMMARY

~⚠️ **NOTE:** This PR depends on #603 and needs to be rebased when #603 is merged. Actual changes: da0a7befa39b9e3b68f81734e428d584b6ec9b8f~

This PR fixes Teams and possibly also Lark integrations. Since v0.12.4 MS Teams HTTP server didn't handle requests properly because of 404 errors. It was caused by using default HTTP handlers configuration, which was changed in #603. However, #603 still didn't solve the issue fully, as the servers still didn't handle proper prefixes. This PR introduces https://github.com/gorilla/mux which can handle paths by prefix.

Please see the details: #606 (comment)

Fixes #606

##### TESTING

Checkout this PR and install BotKube:

```bash
cat > /tmp/values.yaml << ENDOFFILE
communications:
  teams:
    enabled: true
    appID: 'dbf68a92-26a0-4305-8e9b-8616500abf2f'
    appPassword: 'abc'
config:
  settings:
    clustername: sample
    kubectl:
      enabled: true
image:
  registry: docker.io
  repository: pkosiec/botkube
  tag: fix-bot-srvs # this image was repushed after rebase
ENDOFFILE

helm install botkube --namespace botkube ./helm/botkube -f /tmp/values.yaml --wait
```

Forward port:
```bash
kubectl port-forward -n botkube svc/botkube 3978
```

And test these calls:
```bash
curl http://localhost:3978/metrics # not anymore, metrics are now accessible only under 2112 port as they should
curl http://localhost:3978/bots/teams # it works
curl http://localhost:3978/bots/teams/v1/messages # it works
```

You can also edit service to check the metrics server (you can also do it from Helm chart configuration, but the ServiceMonitor CRD needs to be available in the cluster):

```bash
kubectl edit svc -n botkube botkube
```

```yaml
spec:
   # (...)
   ports:
   # (...)
    - name: metrics
       port: 2112
       targetPort: 2112
```

Forward port:

```bash
kubectl port-forward -n botkube svc/botkube 2112
```

And test these calls:
```bash
curl http://localhost:2112/metrics # not anymore, metrics are now accessible only under 2112 port as they should
curl http://localhost:2112/bots/teams # doesn't work anymore
```

Unfortunately, I couldn't test Lark integration as there's no instruction how to set it up, and I couldn't start the server with invalid/empty credentials.
  • Loading branch information
pkosiec committed Jun 6, 2022
1 parent cced2fc commit 98c1123
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 15 deletions.
8 changes: 4 additions & 4 deletions cmd/botkube/main.go
Expand Up @@ -28,6 +28,7 @@ import (
"time"

"github.com/google/go-github/v44/github"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
"github.com/vrischmann/envconfig"
Expand Down Expand Up @@ -214,10 +215,9 @@ func newLogger(logLevelStr string) *logrus.Logger {

func newMetricsServer(log logrus.FieldLogger, metricsPort string) *httpsrv.Server {
addr := fmt.Sprintf(":%s", metricsPort)
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())

return httpsrv.New(log, addr, mux)
router := mux.NewRouter()
router.Handle("/metrics", promhttp.Handler())
return httpsrv.New(log, addr, router)
}

func exitOnError(err error, context string) {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -5,6 +5,7 @@ require (
github.com/bwmarrin/discordgo v0.25.0
github.com/fsnotify/fsnotify v1.5.4
github.com/google/go-github/v44 v44.1.0
github.com/gorilla/mux v1.8.0
github.com/hashicorp/go-multierror v1.1.1
github.com/infracloudio/msbotbuilder-go v0.2.5
github.com/larksuite/oapi-sdk-go v1.1.44
Expand Down
1 change: 1 addition & 0 deletions go.sum
Expand Up @@ -493,6 +493,7 @@ github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/schema v1.2.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
Expand Down
11 changes: 5 additions & 6 deletions pkg/bot/lark.go
Expand Up @@ -25,14 +25,14 @@ import (
"net/http"
"strings"

"github.com/gorilla/mux"
"github.com/larksuite/oapi-sdk-go/core"
"github.com/larksuite/oapi-sdk-go/event"
eventhttpserver "github.com/larksuite/oapi-sdk-go/event/http"
"github.com/sirupsen/logrus"

"github.com/infracloudio/botkube/pkg/httpsrv"

"github.com/infracloudio/botkube/pkg/config"
"github.com/infracloudio/botkube/pkg/httpsrv"
"github.com/infracloudio/botkube/pkg/utils"
)

Expand Down Expand Up @@ -181,12 +181,11 @@ func (l *LarkBot) Start(ctx context.Context) error {
event.SetTypeCallback(larkConf, larkAddUserToChat, helloCallbackFn)

addr := fmt.Sprintf(":%d", l.Port)
mux := http.NewServeMux()
mux.HandleFunc(l.MessagePath, func(responseWriter http.ResponseWriter, request *http.Request) {
router := mux.NewRouter()
router.PathPrefix(l.MessagePath).HandlerFunc(func(responseWriter http.ResponseWriter, request *http.Request) {
eventhttpserver.Handle(larkConf, request, responseWriter)
})

srv := httpsrv.New(l.log, addr, mux)
srv := httpsrv.New(l.log, addr, router)
err := srv.Serve(ctx)
if err != nil {
return fmt.Errorf("while running Lark server: %w", err)
Expand Down
8 changes: 5 additions & 3 deletions pkg/bot/teams.go
Expand Up @@ -28,6 +28,7 @@ import (
"net/url"
"strings"

"github.com/gorilla/mux"
"github.com/hashicorp/go-multierror"
"github.com/infracloudio/msbotbuilder-go/core"
coreActivity "github.com/infracloudio/msbotbuilder-go/core/activity"
Expand Down Expand Up @@ -120,10 +121,11 @@ func (b *Teams) Start(ctx context.Context) error {
}

addr := fmt.Sprintf(":%s", b.Port)
mux := http.NewServeMux()
mux.HandleFunc(b.MessagePath, b.processActivity)

srv := httpsrv.New(b.log, addr, mux)
router := mux.NewRouter()
router.PathPrefix(b.MessagePath).HandlerFunc(b.processActivity)

srv := httpsrv.New(b.log, addr, router)
err = srv.Serve(ctx)
if err != nil {
return fmt.Errorf("while running MS Teams server: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/httpsrv/server.go
Expand Up @@ -15,9 +15,9 @@ type Server struct {
}

// New creates a new HTTP server.
func New(log logrus.FieldLogger, addr string, mux *http.ServeMux) *Server {
func New(log logrus.FieldLogger, addr string, handler http.Handler) *Server {
return &Server{
srv: &http.Server{Addr: addr, Handler: mux},
srv: &http.Server{Addr: addr, Handler: handler},
log: log,
}
}
Expand Down

0 comments on commit 98c1123

Please sign in to comment.