Skip to content

Commit

Permalink
refactor (build/gql-middleware): Introduce config as a yml file (bigb…
Browse files Browse the repository at this point in the history
…luebutton#20992)

* Introduce Gql-Middleware config as a yml file

* use path /usr/share/bbb-graphql-middleware/ instead of /usr/local/bigbluebutton/bbb-graphql-middleware

* remove /etc/default/bbb-graphql-middleware file
  • Loading branch information
gustavotrott authored Aug 29, 2024
1 parent 9cee20c commit e140714
Show file tree
Hide file tree
Showing 19 changed files with 211 additions and 117 deletions.
39 changes: 9 additions & 30 deletions bbb-graphql-middleware/cmd/bbb-graphql-middleware/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bbb-graphql-middleware/config"
"bbb-graphql-middleware/internal/common"
"bbb-graphql-middleware/internal/websrv"
"context"
Expand All @@ -9,15 +10,15 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
"net/http"
"os"
"strconv"
"time"
)

func main() {
cfg := config.GetConfig()

// Configure logger
if logLevelFromEnvVar, err := log.ParseLevel(os.Getenv("BBB_GRAPHQL_MIDDLEWARE_LOG_LEVEL")); err == nil {
log.SetLevel(logLevelFromEnvVar)
if logLevelFromConfig, err := log.ParseLevel(cfg.LogLevel); err == nil {
log.SetLevel(logLevelFromConfig)
} else {
log.SetLevel(log.InfoLevel)
}
Expand All @@ -33,35 +34,13 @@ func main() {
// Listen msgs from akka (for example to invalidate connection)
go websrv.StartRedisListener()

if jsonPatchDisabled := os.Getenv("BBB_GRAPHQL_MIDDLEWARE_JSON_PATCH_DISABLED"); jsonPatchDisabled != "" {
if cfg.Server.JsonPatchDisabled {
log.Infof("Json Patch Disabled!")
}

// Websocket listener

//Define IP to listen
listenIp := "127.0.0.1"
if envListenIp := os.Getenv("BBB_GRAPHQL_MIDDLEWARE_LISTEN_IP"); envListenIp != "" {
listenIp = envListenIp
}

// Define port to listen on
listenPort := 8378
if envListenPort := os.Getenv("BBB_GRAPHQL_MIDDLEWARE_LISTEN_PORT"); envListenPort != "" {
if envListenPortAsInt, err := strconv.Atoi(envListenPort); err == nil {
listenPort = envListenPortAsInt
}
}

//Define new Connections Rate Limit
maxConnPerSecond := 10
if envMaxConnPerSecond := os.Getenv("BBB_GRAPHQL_MIDDLEWARE_MAX_CONN_PER_SECOND"); envMaxConnPerSecond != "" {
if envMaxConnPerSecondAsInt, err := strconv.Atoi(envMaxConnPerSecond); err == nil {
maxConnPerSecond = envMaxConnPerSecondAsInt
}
}
rateLimiter := common.NewCustomRateLimiter(maxConnPerSecond)

rateLimiter := common.NewCustomRateLimiter(cfg.Server.MaxConnectionsPerSecond)
http.HandleFunc("/graphql", func(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 120*time.Second)
defer cancel()
Expand All @@ -84,6 +63,6 @@ func main() {
// Add Prometheus metrics endpoint
http.Handle("/metrics", promhttp.Handler())

log.Infof("listening on %v:%v", listenIp, listenPort)
log.Fatal(http.ListenAndServe(fmt.Sprintf("%v:%v", listenIp, listenPort), nil))
log.Infof("listening on %v:%v", cfg.Server.Host, cfg.Server.Port)
log.Fatal(http.ListenAndServe(fmt.Sprintf("%v:%v", cfg.Server.Host, cfg.Server.Port), nil))
}
102 changes: 102 additions & 0 deletions bbb-graphql-middleware/config/Config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package config

import (
"dario.cat/mergo"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
"io/ioutil"
"os"
"path/filepath"
"sync"
)

var (
instance *Config
once sync.Once
)

var DefaultConfigPath = "/usr/share/bbb-graphql-middleware/config.yml"
var OverrideConfigPath = "/etc/bigbluebutton/bbb-graphql-middleware.yml"

type Config struct {
Server struct {
Host string `yaml:"listen_host"`
Port int `yaml:"listen_port"`
MaxConnections int `yaml:"max_connections"`
MaxConnectionsPerSecond int `yaml:"max_connections_per_session_token"`
MaxConnectionsPerSessionToken int `yaml:"max_connections_per_second"`
AuthorizedCrossOrigin string `yaml:"authorized_cross_origin"`
JsonPatchDisabled bool `yaml:"json_patch_disabled"`
SubscriptionAllowedList string `yaml:"subscriptions_allowed_list"`
SubscriptionsDeniedList string `yaml:"subscriptions_denied_list"`
} `yaml:"server"`
Redis struct {
Host string `yaml:"host"`
Port int32 `yaml:"port"`
Password string `yaml:"password"`
} `yaml:"redis"`
Hasura struct {
Url string `yaml:"url"`
} `yaml:"hasura"`
GraphqlActions struct {
Url string `yaml:"url"`
} `yaml:"graphql-actions"`
AuthHook struct {
Url string `yaml:"url"`
} `yaml:"auth_hook"`
SessionVarsHook struct {
Url string `yaml:"url"`
} `yaml:"session_vars_hook"`
LogLevel string `yaml:"log_level"`
PrometheusAdvancedMetricsEnabled bool `yaml:"prometheus_advanced_metrics_enabled"`
}

func GetConfig() *Config {
once.Do(func() {
instance = &Config{}
instance.loadConfigs()
})
return instance
}

func (c *Config) loadConfigs() {
// Load default config file
configDefault, err := loadConfigFile(DefaultConfigPath)
if err != nil {
log.Fatalf("Error while loading config file (%s): %v", DefaultConfigPath, err)
}

// Load override config file if exists
if _, err := os.Stat(OverrideConfigPath); err == nil {
configOverride, err := loadConfigFile(OverrideConfigPath)
if err != nil {
log.Fatalf("Error while loading override config file (%s): %v", OverrideConfigPath, err)
}

log.Info("Override config found at " + OverrideConfigPath)

// Use mergo to merge configs
err = mergo.Merge(&configDefault, configOverride, mergo.WithOverride)
if err != nil {
log.Fatalf("Erro ao mesclar as configurações: %v", err)
}
}

// Update the singleton instance with the merged config
*instance = configDefault
}

func loadConfigFile(path string) (Config, error) {
var config Config
data, err := ioutil.ReadFile(filepath.Clean(path))
if err != nil {
return config, err
}

err = yaml.Unmarshal(data, &config)
if err != nil {
return config, err
}

return config, nil
}
28 changes: 28 additions & 0 deletions bbb-graphql-middleware/config/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
server:
listen_host: 127.0.0.1
listen_port: 8378
#max of concurrent connections
max_connections: 500
max_connections_per_session_token: 3
#rate limit
max_connections_per_second: 100
# If you are running a cluster proxy setup, you need to allow the url of the Frontend
# Add an Authorized Cross Origin. See https://docs.bigbluebutton.org/administration/cluster-proxy
#authorized_cross_origin: 'bbb-proxy.example.com'
json_patch_disabled: false
subscriptions_allowed_list:
subscriptions_denied_list:
redis:
host: 127.0.0.1
port: 6379
password: ""
hasura:
url: ws://127.0.0.1:8085/v1/graphql
graphql-actions:
url: http://127.0.0.1:8093
auth_hook:
url: http://127.0.0.1:8090/bigbluebutton/connection/checkGraphqlAuthorization
session_vars_hook:
url: http://127.0.0.1:8901/userInfo
prometheus_advanced_metrics_enabled: false
log_level: INFO
7 changes: 4 additions & 3 deletions bbb-graphql-middleware/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,27 @@ module bbb-graphql-middleware
go 1.22.5

require (
dario.cat/mergo v1.0.1
github.com/evanphx/json-patch v0.5.2
github.com/google/uuid v1.6.0
github.com/mattbaird/jsonpatch v0.0.0-20240118010651-0ba75a80ca38
github.com/prometheus/client_golang v1.19.1
github.com/redis/go-redis/v9 v9.6.1
github.com/sirupsen/logrus v1.9.3
golang.org/x/xerrors v0.0.0-20240716161551-93cc26a95ae9
gopkg.in/yaml.v3 v3.0.1
nhooyr.io/websocket v1.8.11
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/go-redis/redis v6.15.9+incompatible // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/redis/go-redis v6.15.9+incompatible // indirect
github.com/redis/go-redis/v9 v9.6.1 // indirect
golang.org/x/sys v0.17.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
)
22 changes: 17 additions & 5 deletions bbb-graphql-middleware/go.sum
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
dario.cat/mergo v1.0.1 h1:Ra4+bf83h2ztPIQYNP99R6m+Y7KfnARDfID+a+vLl4s=
dario.cat/mergo v1.0.1/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/evanphx/json-patch v0.5.2 h1:xVCHIVMUu1wtM/VkR9jVZ45N3FhZfYMMYGorLCR8P3k=
github.com/evanphx/json-patch v0.5.2/go.mod h1:ZWS5hhDbVDyob71nXKNL0+PWn6ToqBHMikGIFbs31qQ=
github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg=
github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mattbaird/jsonpatch v0.0.0-20240118010651-0ba75a80ca38 h1:hQWBtNqRYrI7CWIaUSXXtNKR90KzcUA5uiuxFVWw7sU=
github.com/mattbaird/jsonpatch v0.0.0-20240118010651-0ba75a80ca38/go.mod h1:M1qoD/MqPgTZIk0EWKB38wE28ACRfVcn+cU08jyArI0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand All @@ -30,10 +39,10 @@ github.com/prometheus/common v0.48.0 h1:QO8U2CdOzSn1BBsmXJXduaaW+dY/5QLjfB8svtSz
github.com/prometheus/common v0.48.0/go.mod h1:0/KsvlIEfPQCQ5I2iNSAWKPZziNCvRs5EC6ILDTlAPc=
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
github.com/redis/go-redis v6.15.9+incompatible h1:F+tnlesQSl3h9V8DdmtcYFdvkHLhbb7AgcLW6UJxnC4=
github.com/redis/go-redis v6.15.9+incompatible/go.mod h1:ic6dLmR0d9rkHSzaa0Ab3QVRZcjopJ9hSSPCrecj/+s=
github.com/redis/go-redis/v9 v9.6.1 h1:HHDteefn6ZkTtY5fGUE8tj8uy85AHk6zP7CpzIAM0y4=
github.com/redis/go-redis/v9 v9.6.1/go.mod h1:0C0c6ycQsdpVNQpxb1njEQIqkx5UcsM8FJCQLgE9+RA=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All @@ -47,7 +56,10 @@ golang.org/x/xerrors v0.0.0-20240716161551-93cc26a95ae9/go.mod h1:NDW/Ps6MPRej6f
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
nhooyr.io/websocket v1.8.11 h1:f/qXNc2/3DpoSZkHt1DQu6rj4zGC8JmkkLkWss0MgN0=
nhooyr.io/websocket v1.8.11/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c=
4 changes: 2 additions & 2 deletions bbb-graphql-middleware/internal/akka_apps/client.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package akka_apps

import (
"bbb-graphql-middleware/config"
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
"os"
"strings"
)

// sessionVarsHookUrl is the authentication hook URL obtained from an environment variable.
var sessionVarsHookUrl = os.Getenv("BBB_GRAPHQL_MIDDLEWARE_SESSION_VARS_HOOK_URL")
var sessionVarsHookUrl = config.GetConfig().SessionVarsHook.Url

func AkkaAppsGetSessionVariablesFrom(browserConnectionId string, sessionToken string) (map[string]string, error) {
logger := log.WithField("_routine", "AkkaAppsClient").WithField("browserConnectionId", browserConnectionId)
Expand Down
4 changes: 2 additions & 2 deletions bbb-graphql-middleware/internal/bbb_web/client.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package bbb_web

import (
"bbb-graphql-middleware/config"
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"os"
"strings"
)

// authHookUrl is the authentication hook URL obtained from an environment variable.
var authHookUrl = os.Getenv("BBB_GRAPHQL_MIDDLEWARE_AUTH_HOOK_URL")
var authHookUrl = config.GetConfig().AuthHook.Url

func BBBWebCheckAuthorization(browserConnectionId string, sessionToken string, cookies []*http.Cookie) (string, string, error) {
logger := log.WithField("_routine", "BBBWebClient").WithField("browserConnectionId", browserConnectionId)
Expand Down
21 changes: 3 additions & 18 deletions bbb-graphql-middleware/internal/common/GlobalState.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package common

import (
"bbb-graphql-middleware/config"
"github.com/google/uuid"
"os"
"strconv"
"sync"
"time"
)
Expand Down Expand Up @@ -109,22 +108,8 @@ func RemoveStreamCursorValueCache(cacheKey uint32, delayInSecs time.Duration) {
delete(StreamCursorValueCache, cacheKey)
}

var MaxConnPerSessionToken = 3
var MaxConnGlobal = 500

func init() {
if envMaxConnPerSessionToken := os.Getenv("BBB_GRAPHQL_MIDDLEWARE_MAX_CONN_PER_SESSION_TOKEN"); envMaxConnPerSessionToken != "" {
if envMaxConnPerSessionTokenAsInt, err := strconv.Atoi(envMaxConnPerSessionToken); err == nil {
MaxConnPerSessionToken = envMaxConnPerSessionTokenAsInt
}
}

if envMaxConnGlobal := os.Getenv("BBB_GRAPHQL_MIDDLEWARE_MAX_CONN"); envMaxConnGlobal != "" {
if envMaxConnGlobalAsInt, err := strconv.Atoi(envMaxConnGlobal); err == nil {
MaxConnGlobal = envMaxConnGlobalAsInt
}
}
}
var MaxConnPerSessionToken = config.GetConfig().Server.MaxConnectionsPerSessionToken
var MaxConnGlobal = config.GetConfig().Server.MaxConnections

func GetMaxConnectionsPerSessionToken() int {
return MaxConnPerSessionToken
Expand Down
10 changes: 2 additions & 8 deletions bbb-graphql-middleware/internal/common/PrometheusMetrics.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
package common

import (
"bbb-graphql-middleware/config"
"github.com/prometheus/client_golang/prometheus"
"os"
)

var PrometheusAdvancedMetricsEnabled = false

func init() {
if prometheusAdvancedMetricsEnabled := os.Getenv("BBB_GRAPHQL_MIDDLEWARE_PROMETHEUS_ADVANCED_METRICS_ENABLED"); prometheusAdvancedMetricsEnabled == "true" {
PrometheusAdvancedMetricsEnabled = true
}
}
var PrometheusAdvancedMetricsEnabled = config.GetConfig().PrometheusAdvancedMetricsEnabled

var (
HttpConnectionGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Expand Down
Loading

0 comments on commit e140714

Please sign in to comment.