Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TT-10070] Fix/sanitize error logging #6817

Merged
merged 12 commits into from
Jan 9, 2025
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ var (
Enabled: false,
AllowUnsafe: []string{},
},
PIDFileLocation: "/var/run/tyk/tyk-gateway.pid",
}
)

Expand Down
11 changes: 9 additions & 2 deletions gateway/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2236,13 +2236,16 @@ func (gw *Gateway) createOauthClient(w http.ResponseWriter, r *http.Request) {
storageManager := gw.getGlobalMDCBStorageHandler(prefix, false)
storageManager.Connect()

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

apiSpec.OAuthManager = &OAuthManager{
OsinServer: gw.TykOsinNewServer(
&osin.ServerConfig{},
&RedisOsinStorageInterface{
storageManager,
gw.GlobalSessionManager,
&storage.RedisCluster{KeyPrefix: prefix, HashKeys: false, ConnectionHandler: gw.StorageConnectionHandler},
storageDriver,
apiSpec.OrgID,
gw,
}),
Expand Down Expand Up @@ -2623,12 +2626,16 @@ func (gw *Gateway) getOauthClientDetails(keyName, apiID string) (interface{}, in
prefix := generateOAuthPrefix(apiSpec.APIID)
storageManager := gw.getGlobalMDCBStorageHandler(prefix, false)
storageManager.Connect()

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

apiSpec.OAuthManager = &OAuthManager{
OsinServer: gw.TykOsinNewServer(&osin.ServerConfig{},
&RedisOsinStorageInterface{
storageManager,
gw.GlobalSessionManager,
&storage.RedisCluster{KeyPrefix: prefix, HashKeys: false, ConnectionHandler: gw.StorageConnectionHandler},
storageDriver,
apiSpec.OrgID,
gw,
}),
Expand Down
11 changes: 9 additions & 2 deletions gateway/api_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,16 @@ type ChainObject struct {

func (gw *Gateway) prepareStorage() generalStores {
var gs generalStores

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

gs.redisOrgStore = &storage.RedisCluster{KeyPrefix: "orgkey.", ConnectionHandler: gw.StorageConnectionHandler}
gs.redisOrgStore.Connect()

gs.healthStore = &storage.RedisCluster{KeyPrefix: "apihealth.", ConnectionHandler: gw.StorageConnectionHandler}
gs.healthStore.Connect()

gs.rpcAuthStore = &RPCStorageHandler{KeyPrefix: "apikey-", HashKeys: gw.GetConfig().HashKeys, Gw: gw}
gs.rpcOrgStore = gw.getGlobalMDCBStorageHandler("orgkey.", false)

Expand All @@ -56,7 +63,6 @@ func (gw *Gateway) prepareStorage() generalStores {
}

func (gw *Gateway) skipSpecBecauseInvalid(spec *APISpec, logger *logrus.Entry) bool {

switch spec.Protocol {
case "", "http", "https":
if spec.Proxy.ListenPath == "" {
Expand Down Expand Up @@ -1064,7 +1070,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
6 changes: 4 additions & 2 deletions gateway/coprocess_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ import (
// CoProcessDefaultKeyPrefix is used as a key prefix for this CP.
const CoProcessDefaultKeyPrefix = "coprocess-data:"

func getStorageForPython(ctx context.Context) storage.RedisCluster {
func getStorageForPython(ctx context.Context) *storage.RedisCluster {
rc := storage.NewConnectionHandler(ctx)

go rc.Connect(ctx, nil, &config.Config{})
rc.WaitConnect(ctx)

return storage.RedisCluster{KeyPrefix: CoProcessDefaultKeyPrefix, ConnectionHandler: rc}
handler := &storage.RedisCluster{KeyPrefix: CoProcessDefaultKeyPrefix, ConnectionHandler: rc}
handler.Connect()
return handler
}

// TykStoreData is a CoProcess API function for storing data.
Expand Down
12 changes: 10 additions & 2 deletions gateway/coprocess_id_extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,27 @@ const (

func (ts *Test) createSpecTestFrom(tb testing.TB, def *apidef.APIDefinition) *APISpec {
tb.Helper()

loader := APIDefinitionLoader{Gw: ts.Gw}
spec, _ := loader.MakeSpec(&model.MergedAPI{APIDefinition: def}, nil)
tname := tb.Name()

redisStore := &storage.RedisCluster{KeyPrefix: tname + "-apikey.", ConnectionHandler: ts.Gw.StorageConnectionHandler}
redisStore.Connect()

healthStore := &storage.RedisCluster{KeyPrefix: tname + "-apihealth.", ConnectionHandler: ts.Gw.StorageConnectionHandler}
healthStore.Connect()

orgStore := &storage.RedisCluster{KeyPrefix: tname + "-orgKey.", ConnectionHandler: ts.Gw.StorageConnectionHandler}
orgStore.Connect()

spec.Init(redisStore, redisStore, healthStore, orgStore)
return spec
}

func (ts *Test) prepareExtractor(tb testing.TB, extractorSource apidef.IdExtractorSource, extractorType apidef.IdExtractorType,
config map[string]interface{}, disabled bool) (IdExtractor, *APISpec) {
func (ts *Test) prepareExtractor(tb testing.TB, extractorSource apidef.IdExtractorSource, extractorType apidef.IdExtractorType, config map[string]interface{}, disabled bool) (IdExtractor, *APISpec) {
tb.Helper()

def := &apidef.APIDefinition{
OrgID: MockOrgID,
CustomMiddleware: apidef.MiddlewareSection{
Expand Down
2 changes: 2 additions & 0 deletions gateway/delete_api_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ import (

func (gw *Gateway) invalidateAPICache(apiID string) bool {
store := storage.RedisCluster{IsCache: true, ConnectionHandler: gw.StorageConnectionHandler}
store.Connect()

return store.DeleteScanMatch(fmt.Sprintf("cache-%s*", apiID))
}
5 changes: 5 additions & 0 deletions gateway/event_handler_webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,14 @@ func (w *WebHookHandler) getRequestMethod(m string) WebHookRequestMethod {
}

func (w *WebHookHandler) checkURL(r string) bool {
if r == "" {
return false
}

log.WithFields(logrus.Fields{
"prefix": "webhooks",
}).Debug("Checking URL: ", r)

if _, err := url.ParseRequestURI(r); err != nil {
log.WithFields(logrus.Fields{
"prefix": "webhooks",
Expand Down
26 changes: 26 additions & 0 deletions gateway/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,10 @@ func TestCacheAllSafeRequests(t *testing.T) {
t.Skip() // DeleteScanMatch interferes with other tests.

ts := StartTest(nil)

cache := storage.RedisCluster{KeyPrefix: "cache-", ConnectionHandler: ts.Gw.StorageConnectionHandler}
cache.Connect()

t.Cleanup(func() {
ts.Close()
cache.DeleteScanMatch("*")
Expand Down Expand Up @@ -919,7 +922,10 @@ func TestCacheAllSafeRequestsWithCachedHeaders(t *testing.T) {
t.Skip() // DeleteScanMatch interferes with other tests.

ts := StartTest(nil)

cache := storage.RedisCluster{KeyPrefix: "cache-", ConnectionHandler: ts.Gw.StorageConnectionHandler}
cache.Connect()

t.Cleanup(func() {
ts.Close()
cache.DeleteScanMatch("*")
Expand Down Expand Up @@ -965,7 +971,10 @@ func TestCacheWithAdvanceUrlRewrite(t *testing.T) {
t.Skip() // DeleteScanMatch interferes with other tests.

ts := StartTest(nil)

cache := storage.RedisCluster{KeyPrefix: "cache-", ConnectionHandler: ts.Gw.StorageConnectionHandler}
cache.Connect()

t.Cleanup(func() {
ts.Close()
cache.DeleteScanMatch("*")
Expand Down Expand Up @@ -1024,7 +1033,10 @@ func TestCachePostRequest(t *testing.T) {
t.Skip() // DeleteScanMatch interferes with other tests.

ts := StartTest(nil)

cache := storage.RedisCluster{KeyPrefix: "cache-", ConnectionHandler: ts.Gw.StorageConnectionHandler}
cache.Connect()

t.Cleanup(func() {
ts.Close()
cache.DeleteScanMatch("*")
Expand Down Expand Up @@ -1071,7 +1083,10 @@ func TestAdvanceCachePutRequest(t *testing.T) {
t.Skip() // DeleteScanMatch interferes with other tests.

ts := StartTest(nil)

cache := storage.RedisCluster{KeyPrefix: "cache-", ConnectionHandler: ts.Gw.StorageConnectionHandler}
cache.Connect()

t.Cleanup(func() {
ts.Close()
cache.DeleteScanMatch("*")
Expand Down Expand Up @@ -1164,7 +1179,10 @@ func TestCacheAllSafeRequestsWithAdvancedCacheEndpoint(t *testing.T) {
t.Skip() // DeleteScanMatch interferes with other tests.

ts := StartTest(nil)

cache := storage.RedisCluster{KeyPrefix: "cache-", ConnectionHandler: ts.Gw.StorageConnectionHandler}
cache.Connect()

t.Cleanup(func() {
ts.Close()
cache.DeleteScanMatch("*")
Expand Down Expand Up @@ -1203,7 +1221,9 @@ func TestCacheEtag(t *testing.T) {
t.Skip() // DeleteScanMatch interferes with other tests.

ts := StartTest(nil)

cache := storage.RedisCluster{KeyPrefix: "cache-", ConnectionHandler: ts.Gw.StorageConnectionHandler}
cache.Connect()

t.Cleanup(func() {
ts.Close()
Expand Down Expand Up @@ -1258,7 +1278,10 @@ func TestOldCachePlugin(t *testing.T) {
t.Helper()

ts := StartTest(nil)

cache := storage.RedisCluster{KeyPrefix: "cache-", ConnectionHandler: ts.Gw.StorageConnectionHandler}
cache.Connect()

t.Cleanup(func() {
ts.Close()
cache.DeleteScanMatch("*")
Expand Down Expand Up @@ -1287,7 +1310,10 @@ func TestAdvanceCacheTimeoutPerEndpoint(t *testing.T) {
t.Skip() // DeleteScanMatch interferes with other tests.

ts := StartTest(nil)

cache := storage.RedisCluster{KeyPrefix: "cache-", ConnectionHandler: ts.Gw.StorageConnectionHandler}
cache.Connect()

t.Cleanup(func() {
ts.Close()
cache.DeleteScanMatch("*")
Expand Down
1 change: 1 addition & 0 deletions gateway/health_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func (gw *Gateway) gatherHealthChecks() {
allInfos := SafeHealthCheck{info: make(map[string]HealthCheckItem, 3)}

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

key := "tyk-liveness-probe"

Expand Down
4 changes: 4 additions & 0 deletions gateway/host_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,10 @@ func eraseSyncMap(m *sync.Map) {
}

func (h *HostUptimeChecker) Stop() {
if h == nil {
return
}

was := atomic.SwapInt32(&h.isClosed, CLOSED)
if was == OPEN {
eraseSyncMap(h.samples)
Expand Down
17 changes: 12 additions & 5 deletions gateway/host_checker_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (hc *HostCheckerManager) Init(store storage.Handler) {

func (hc *HostCheckerManager) Start(ctx context.Context) {
// Start loop to check if we are active instance
if hc.Id != "" {
if hc != nil {
go hc.CheckActivePollerLoop(ctx)
}
}
Expand Down Expand Up @@ -181,7 +181,6 @@ func (hc *HostCheckerManager) AmIPolling() bool {
}

func (hc *HostCheckerManager) StartPoller(ctx context.Context) {

log.WithFields(logrus.Fields{
"prefix": "host-check-mgr",
}).Debug("---> Initialising checker")
Expand All @@ -192,7 +191,8 @@ func (hc *HostCheckerManager) StartPoller(ctx context.Context) {
hc.checker = &HostUptimeChecker{Gw: hc.Gw}
}

hc.checker.Init(hc.Gw.GetConfig().UptimeTests.Config.CheckerPoolSize,
hc.checker.Init(
hc.Gw.GetConfig().UptimeTests.Config.CheckerPoolSize,
hc.Gw.GetConfig().UptimeTests.Config.FailureTriggerSampleSize,
hc.Gw.GetConfig().UptimeTests.Config.TimeWait,
hc.currentHostList,
Expand All @@ -207,14 +207,21 @@ func (hc *HostCheckerManager) StartPoller(ctx context.Context) {
log.WithFields(logrus.Fields{
"prefix": "host-check-mgr",
}).Debug("---> Starting checker")

hc.checker.Start(ctx)

log.WithFields(logrus.Fields{
"prefix": "host-check-mgr",
}).Debug("---> Checker started.")

hc.checkerMu.Unlock()
}

func (hc *HostCheckerManager) StopPoller() {
if hc == nil {
return
}

hc.checkerMu.Lock()
hc.checker.Stop()
hc.checkerMu.Unlock()
Expand Down Expand Up @@ -536,11 +543,11 @@ func (hc *HostCheckerManager) RecordUptimeAnalytics(report HostHealthReport) err

func (gw *Gateway) InitHostCheckManager(ctx context.Context, store storage.Handler) {
// Already initialized
if gw.GlobalHostChecker.Id != "" {
if gw.GlobalHostChecker != nil {
return
}

gw.GlobalHostChecker = HostCheckerManager{Gw: gw}
gw.GlobalHostChecker = &HostCheckerManager{Gw: gw}
gw.GlobalHostChecker.Init(store)
gw.GlobalHostChecker.Start(ctx)
}
Expand Down
17 changes: 17 additions & 0 deletions gateway/host_checker_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ func TestHostCheckerManagerInit(t *testing.T) {
defer ts.Close()

hc := HostCheckerManager{Gw: ts.Gw}

redisStorage := &storage.RedisCluster{KeyPrefix: "host-checker-test:", ConnectionHandler: ts.Gw.StorageConnectionHandler}
redisStorage.Connect()

hc.Init(redisStorage)

if hc.Id == "" {
Expand Down Expand Up @@ -46,6 +49,8 @@ func TestAmIPolling(t *testing.T) {
ts.Gw.SetConfig(globalConf)

redisStorage := &storage.RedisCluster{KeyPrefix: "host-checker-test:", ConnectionHandler: ts.Gw.StorageConnectionHandler}
redisStorage.Connect()

hc.Init(redisStorage)
hc2 := HostCheckerManager{Gw: ts.Gw}
hc2.Init(redisStorage)
Expand Down Expand Up @@ -74,7 +79,10 @@ func TestAmIPolling(t *testing.T) {

//Testing if the PollerCacheKey doesn't contains the poller_group by default
hc = HostCheckerManager{Gw: ts.Gw}

redisStorage = &storage.RedisCluster{KeyPrefix: "host-checker-test:", ConnectionHandler: ts.Gw.StorageConnectionHandler}
redisStorage.Connect()

hc.Init(redisStorage)
hc.AmIPolling()

Expand Down Expand Up @@ -106,7 +114,10 @@ func TestCheckActivePollerLoop(t *testing.T) {
defer ts.Close()

hc := &HostCheckerManager{Gw: ts.Gw}

redisStorage := &storage.RedisCluster{KeyPrefix: "host-checker-test-1:", ConnectionHandler: ts.Gw.StorageConnectionHandler}
redisStorage.Connect()

hc.Init(redisStorage)

go hc.CheckActivePollerLoop(ts.Gw.ctx)
Expand All @@ -122,7 +133,10 @@ func TestStartPoller(t *testing.T) {
defer ts.Close()

hc := HostCheckerManager{Gw: ts.Gw}

redisStorage := &storage.RedisCluster{KeyPrefix: "host-checker-TestStartPoller:", ConnectionHandler: ts.Gw.StorageConnectionHandler}
redisStorage.Connect()

hc.Init(redisStorage)

hc.StartPoller(ts.Gw.ctx)
Expand All @@ -135,9 +149,12 @@ func TestStartPoller(t *testing.T) {
func TestRecordUptimeAnalytics(t *testing.T) {
ts := StartTest(nil)
defer ts.Close()

hc := &HostCheckerManager{Gw: ts.Gw}

redisStorage := &storage.RedisCluster{KeyPrefix: "host-checker-test-analytics:", ConnectionHandler: ts.Gw.StorageConnectionHandler}
redisStorage.Connect()

hc.Init(redisStorage)

spec := &APISpec{}
Expand Down
Loading
Loading