Skip to content

Commit

Permalink
Connect to redis before usage, usage after connect
Browse files Browse the repository at this point in the history
  • Loading branch information
Tit Petric committed Jan 3, 2025
1 parent 993fd84 commit 77614a4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
3 changes: 2 additions & 1 deletion gateway/api_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,8 @@ func (gw *Gateway) loadApps(specs []*APISpec) {

gwListenPort := gw.GetConfig().ListenPort
controlApiIsConfigured := (gw.GetConfig().ControlAPIPort != 0 && gw.GetConfig().ControlAPIPort != gwListenPort) || gw.GetConfig().ControlAPIHostname != ""
if gw.allApisAreMTLS() && !gw.GetConfig().Security.ControlAPIUseMutualTLS && !controlApiIsConfigured {

if !gw.isRunningTests() && gw.allApisAreMTLS() && !gw.GetConfig().Security.ControlAPIUseMutualTLS && !controlApiIsConfigured {
mainLog.Warning("All APIs are protected with mTLS, except for the control API. " +
"We recommend configuring the control API port or control hostname to ensure consistent security measures")
}
Expand Down
2 changes: 1 addition & 1 deletion gateway/event_handler_webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (w *WebHookHandler) Init(handlerConf interface{}) error {
return err
}

if w.conf.Disabled {
if w.conf.Disabled || w.conf.TargetPath == "" {
log.WithFields(logrus.Fields{
"prefix": "webhooks",
}).Infof("skipping disabled webhook %s", w.conf.Name)
Expand Down
41 changes: 35 additions & 6 deletions gateway/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,16 +348,21 @@ func (gw *Gateway) setupGlobals() {
}

healthCheckStore := storage.RedisCluster{KeyPrefix: "host-checker:", IsAnalytics: true, ConnectionHandler: gw.StorageConnectionHandler}
healthCheckStore.Connect()

gw.InitHostCheckManager(gw.ctx, &healthCheckStore)
}

gw.initHealthCheck(gw.ctx)

redisStore := storage.RedisCluster{KeyPrefix: "apikey-", HashKeys: gwConfig.HashKeys, ConnectionHandler: gw.StorageConnectionHandler}
redisStore.Connect()

gw.GlobalSessionManager.Init(&redisStore)

versionStore := storage.RedisCluster{KeyPrefix: "version-check-", ConnectionHandler: gw.StorageConnectionHandler}
versionStore.Connect()

err := versionStore.SetKey("gateway", VERSION, 0)

if err != nil {
Expand All @@ -371,10 +376,14 @@ func (gw *Gateway) setupGlobals() {
mainLog.Debug("Setting up analytics DB connection")

analyticsStore := storage.RedisCluster{KeyPrefix: "analytics-", IsAnalytics: true, ConnectionHandler: gw.StorageConnectionHandler}
analyticsStore.Connect()

gw.Analytics.Store = &analyticsStore
gw.Analytics.Init()

store := storage.RedisCluster{KeyPrefix: "analytics-", IsAnalytics: true, ConnectionHandler: gw.StorageConnectionHandler}
store.Connect()

redisPurger := RedisPurger{Store: &store, Gw: gw}
go redisPurger.PurgeLoop(gw.ctx)

Expand All @@ -385,6 +394,8 @@ func (gw *Gateway) setupGlobals() {
mainLog.Debug("Using RPC cache purge")

store := storage.RedisCluster{KeyPrefix: "analytics-", IsAnalytics: true, ConnectionHandler: gw.StorageConnectionHandler}
store.Connect()

purger := rpc.Purger{
Store: &store,
}
Expand Down Expand Up @@ -429,6 +440,7 @@ func (gw *Gateway) setupGlobals() {
}

storeCert := &storage.RedisCluster{KeyPrefix: "cert-", HashKeys: false, ConnectionHandler: gw.StorageConnectionHandler}
storeCert.Connect()
gw.CertificateManager = certs.NewCertificateManager(storeCert, certificateSecret, log, !gw.GetConfig().Cloud)
if gw.GetConfig().SlaveOptions.UseRPC {
rpcStore := &RPCStorageHandler{
Expand Down Expand Up @@ -747,10 +759,14 @@ func (gw *Gateway) addOAuthHandlers(spec *APISpec, muxer *mux.Router) *OAuthMana
prefix := generateOAuthPrefix(spec.APIID)
storageManager := gw.getGlobalMDCBStorageHandler(prefix, false)
storageManager.Connect()

storage := &storage.RedisCluster{KeyPrefix: prefix, HashKeys: false, ConnectionHandler: gw.StorageConnectionHandler}

Check failure on line 763 in gateway/server.go

View workflow job for this annotation

GitHub Actions / golangci-lint

import-shadowing: The name 'storage' shadows an import name (revive)
storage.Connect()

osinStorage := &RedisOsinStorageInterface{
storageManager,
gw.GlobalSessionManager,
&storage.RedisCluster{KeyPrefix: prefix, HashKeys: false, ConnectionHandler: gw.StorageConnectionHandler},
storage,
spec.OrgID,
gw,
}
Expand Down Expand Up @@ -1258,11 +1274,26 @@ func (gw *Gateway) initSystem() error {
mainLog.Fatal("Redis connection details not set, please ensure that the storage type is set to Redis and that the connection parameters are correct.")
}

go gw.StorageConnectionHandler.Connect(gw.ctx, func() {
gw.reloadURLStructure(func() {})
}, &gwConfig)

timeout, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

connected := gw.StorageConnectionHandler.WaitConnect(timeout)
if !connected {
mainLog.Error("storage: timeout connecting")
} else {
mainLog.Info("storage: connected to " + gwConfig.Storage.Type)
}

// suply rpc client globals to join it main loging and instrumentation sub systems
rpc.Log = log
rpc.Instrument = instrument

gw.setupGlobals()

gwConfig = gw.GetConfig()
if *cli.Port != "" {
portNum, err := strconv.Atoi(*cli.Port)
Expand Down Expand Up @@ -1566,6 +1597,8 @@ func (gw *Gateway) getHostDetails(file string) {

func (gw *Gateway) getGlobalMDCBStorageHandler(keyPrefix string, hashKeys bool) storage.Handler {
localStorage := &storage.RedisCluster{KeyPrefix: keyPrefix, HashKeys: hashKeys, ConnectionHandler: gw.StorageConnectionHandler}
localStorage.Connect()

logger := logrus.New().WithFields(logrus.Fields{"prefix": "mdcb-storage-handler"})

if gw.GetConfig().SlaveOptions.UseRPC {
Expand Down Expand Up @@ -1623,7 +1656,7 @@ func Start() {
}

gwConfig = gw.GetConfig()
if gwConfig.ControlAPIPort == 0 {
if !gw.isRunningTests() && gwConfig.ControlAPIPort == 0 {
mainLog.Warn("The control_api_port should be changed for production")
}

Expand Down Expand Up @@ -1671,10 +1704,6 @@ func Start() {
gw.GetConfig().DBAppConfOptions.Tags)

gw.start()
configs := gw.GetConfig()
go gw.StorageConnectionHandler.Connect(gw.ctx, func() {
gw.reloadURLStructure(func() {})
}, &configs)

unix := time.Now().Unix()

Expand Down

0 comments on commit 77614a4

Please sign in to comment.