-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Merging to release-5.3: [TT-13155] Explicitly copy BaseMiddleware for each middleware that takes it (#6744) #6764
Merging to release-5.3: [TT-13155] Explicitly copy BaseMiddleware for each middleware that takes it (#6744) #6764
Conversation
API Changes --- prev.txt 2024-12-12 13:20:10.010374331 +0000
+++ current.txt 2024-12-12 13:20:07.100380329 +0000
@@ -7591,18 +7591,27 @@
type BaseMiddleware struct {
Spec *APISpec
Proxy ReturningHttpHandler
+ Gw *Gateway `json:"-"`
- Gw *Gateway `json:"-"`
// Has unexported fields.
}
BaseMiddleware wraps up the ApiSpec and Proxy objects to be included in a
middleware handler, this can probably be handled better.
+func NewBaseMiddleware(gw *Gateway, spec *APISpec, proxy ReturningHttpHandler, logger *logrus.Entry) *BaseMiddleware
+ NewBaseMiddleware creates a new *BaseMiddleware. The passed logrus.Entry
+ is duplicated. BaseMiddleware keeps the pointer to *Gateway and *APISpec,
+ as well as Proxy. The logger duplication is used so that basemiddleware
+ copies can be created for different middleware.
+
func (t *BaseMiddleware) ApplyPolicies(session *user.SessionState) error
ApplyPolicies will check if any policies are loaded. If any are, it will
overwrite the session state to use the policy values.
-func (t BaseMiddleware) Base() *BaseMiddleware
+func (t *BaseMiddleware) Base() *BaseMiddleware
+ Base serves to provide the full BaseMiddleware API. It's part of
+ the TykMiddleware interface. It escapes to a wider API surface than
+ TykMiddleware, used by middlewares, etc.
func (t *BaseMiddleware) CheckSessionAndIdentityForValidKey(originalKey string, r *http.Request) (user.SessionState, bool)
CheckSessionAndIdentityForValidKey will check first the Session store for a
@@ -7611,6 +7620,10 @@
func (t *BaseMiddleware) Config() (interface{}, error)
+func (m *BaseMiddleware) Copy() *BaseMiddleware
+ Copy provides a new BaseMiddleware with it's own logger scope (copy).
+ The Spec, Proxy and Gw values are not copied.
+
func (t *BaseMiddleware) EnabledForSpec() bool
func (t *BaseMiddleware) FireEvent(name apidef.TykEvent, meta interface{})
@@ -7620,6 +7633,7 @@
func (t *BaseMiddleware) Init()
func (t *BaseMiddleware) Logger() (logger *logrus.Entry)
+ Logger is used by middleware process functions.
func (t *BaseMiddleware) OrgSession(orgID string) (user.SessionState, bool)
@@ -7629,7 +7643,7 @@
func (t *BaseMiddleware) SetOrgExpiry(orgid string, expiry int64)
-func (t *BaseMiddleware) SetRequestLogger(r *http.Request)
+func (t *BaseMiddleware) SetRequestLogger(r *http.Request) *logrus.Entry
func (t *BaseMiddleware) UpdateRequestSession(r *http.Request) bool
@@ -10106,11 +10120,9 @@
}
type TykMiddleware interface {
- Init()
Base() *BaseMiddleware
- SetName(string)
- SetRequestLogger(*http.Request)
+ Init()
Logger() *logrus.Entry
Config() (interface{}, error)
ProcessRequest(w http.ResponseWriter, r *http.Request, conf interface{}) (error, int) // Handles request |
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
PR Code Suggestions ✨Explore these optional code suggestions:
|
…kes it (#6744) <details open> <summary><a href="https://tyktech.atlassian.net/browse/TT-13155" title="TT-13155" target="_blank">TT-13155</a></summary> <br /> <table> <tr> <th>Summary</th> <td>[Regression] Gateway Debug logs starting v5.3 only logs AccessRightsCheck for most of the middlewares</td> </tr> <tr> <th>Type</th> <td> <img alt="Bug" src="https://tyktech.atlassian.net/rest/api/2/universal_avatar/view/type/issuetype/avatar/10303?size=medium" /> Bug </td> </tr> <tr> <th>Status</th> <td>In Dev</td> </tr> <tr> <th>Points</th> <td>N/A</td> </tr> <tr> <th>Labels</th> <td><a href="https://tyktech.atlassian.net/issues?jql=project%20%3D%20TT%20AND%20labels%20%3D%20'24Bugsmash%20ORDER%20BY%20created%20DESC" title="'24Bugsmash">'24Bugsmash</a>, <a href="https://tyktech.atlassian.net/issues?jql=project%20%3D%20TT%20AND%20labels%20%3D%202025lts%20ORDER%20BY%20created%20DESC" title="2025lts">2025lts</a>, <a href="https://tyktech.atlassian.net/issues?jql=project%20%3D%20TT%20AND%20labels%20%3D%20SESAP%20ORDER%20BY%20created%20DESC" title="SESAP">SESAP</a>, <a href="https://tyktech.atlassian.net/issues?jql=project%20%3D%20TT%20AND%20labels%20%3D%20customer_bug%20ORDER%20BY%20created%20DESC" title="customer_bug">customer_bug</a>, <a href="https://tyktech.atlassian.net/issues?jql=project%20%3D%20TT%20AND%20labels%20%3D%20jira_escalated%20ORDER%20BY%20created%20DESC" title="jira_escalated">jira_escalated</a></td> </tr> </table> </details> <!-- do not remove this marker as it will break jira-lint's functionality. added_by_jira_lint --> --- Enhancement PR: - implements per-middleware basemiddleware copy behaviour - reverts the logger+mutex on base middleware - touches coprocess/ to better handle grpc server startup, shutdown, conflicts on static port number - not to swallow errors when net.Listener fails ___ - Refactored the `BaseMiddleware` initialization process by introducing a `NewBaseMiddleware` function, encapsulating the creation logic. - Added a `Copy` method to `BaseMiddleware` to create scoped copies with a duplicated logger, ensuring middleware-specific logging. - Updated all middleware initialization in `gateway/api_loader.go` to use `baseMid.Copy()` for better isolation and logging scope. - Enhanced code readability and maintainability by centralizing `BaseMiddleware` creation logic and ensuring proper separation of concerns. --------- Co-authored-by: Tit Petric <[email protected]> (cherry picked from commit 9916296)
af9d5ae
to
2bd3b8e
Compare
Quality Gate failedFailed conditions See analysis details on SonarQube Cloud Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE |
User description
TT-13155 Explicitly copy BaseMiddleware for each middleware that takes it (#6744)
TT-13155
PR Type
Enhancement
PR:
conflicts on static port number - not to swallow errors when
net.Listener fails
Description
BaseMiddleware
initialization process by introducinga
NewBaseMiddleware
function, encapsulating the creation logic.Copy
method toBaseMiddleware
to create scoped copies witha duplicated logger, ensuring middleware-specific logging.
gateway/api_loader.go
touse
baseMid.Copy()
for better isolation and logging scope.BaseMiddleware
creation logic and ensuring proper separation ofconcerns.
Co-authored-by: Tit Petric [email protected]
PR Type
Bug fix, Enhancement
Description
BaseMiddleware
initialization by introducingNewBaseMiddleware
andCopy
methods for better middleware isolation and logging scope.gateway/api_loader.go
to usebaseMid.Copy()
for improved isolation and maintainability.startTestServices
andstopTestServices
helpers, improving test isolation and reliability.dnscache/storage_test.go
andgateway_test.go
..taskfiles/test.yml
andcoprocess/Taskfile.yml
to improve test reporting and execution.Changes walkthrough 📝
5 files
coprocess_grpc_test.go
Refactor gRPC test setup and improve test isolation
coprocess/grpc/coprocess_grpc_test.go
startTestServices
for better testisolation.
dispatcher_test.go
Add dedicated dispatcher test file and improve coverage
coprocess/grpc/dispatcher_test.go
services_test.go
Add helpers for gRPC service test setup and teardown
coprocess/grpc/services_test.go
startTestServices
andstopTestServices
helpers for testsetup and teardown.
storage_test.go
Skip slow test with bad practices in storage tests
dnscache/storage_test.go
time.Sleep
.testutil.go
Improve test utility reliability and error handling
gateway/testutil.go
utilities.
resources.
4 files
api_loader.go
Refactor middleware initialization for better isolation
gateway/api_loader.go
BaseMiddleware
initialization to useNewBaseMiddleware
.baseMid.Copy()
for betterisolation.
unique
BaseMiddleware
instance.coprocess.go
Enhance CoProcess middleware creation with better isolation
gateway/coprocess.go
CreateCoProcessMiddleware
to useBaseMiddleware.Copy()
forbetter isolation.
middleware.go
Add `NewBaseMiddleware` and `Copy` for better middleware management
gateway/middleware.go
NewBaseMiddleware
to encapsulateBaseMiddleware
creationlogic.
Copy
method toBaseMiddleware
for creating scoped copies withunique loggers.
BaseMiddleware
.mw_go_plugin.go
Enhance Go plugin middleware with isolated BaseMiddleware instances
gateway/mw_go_plugin.go
BaseMiddleware.Copy()
for betterisolation.
2 files
test.yml
Improve test task to generate and consolidate JSON coverage reports
.taskfiles/test.yml
report.
Taskfile.yml
Enhance test task with `gotestsum` and sequential execution
coprocess/Taskfile.yml
gotestsum
for better test output formatting.-p 1
.