[TT-14494] improve error logging for JWKS URL handling#7593
[TT-14494] improve error logging for JWKS URL handling#7593NurayAhmadova merged 11 commits intomasterfrom
Conversation
|
This pull request introduces a centralized helper function, Files Changed AnalysisThe core of this change is the new Architecture & Impact Assessment
graph TD
A[Error during JWKS fetch/parse] --> B{logJWKError};
B --> C{Is it a Content/JSON error?};
C -- Yes --> D[Log \"Invalid JWKS retrieved from endpoint\"];
C -- No --> E{Is it a Network error?};
E -- Yes --> F[Log \"JWKS endpoint resolution failed\"];
E -- No --> G[Log \"Failed to fetch or decode JWKs\"];
Scope Discovery & Context ExpansionThe changes are well-contained within the Metadata
Powered by Visor from Probelabs Last updated: 2025-12-17T05:43:11.608Z | Triggered by: pr_updated | Commit: 4246b5b 💡 TIP: You can chat with Visor using |
Security Issues (4)
Architecture Issues (1)
✅ Performance Check PassedNo performance issues found – changes LGTM. Quality Issues (1)
Powered by Visor from Probelabs Last updated: 2025-12-17T05:43:14.251Z | Triggered by: pr_updated | Commit: 4246b5b 💡 TIP: You can chat with Visor using |
|
API Changes no api changes detected |
02561f7 to
06185f3
Compare
10deb6b to
8e6dc6c
Compare
|
Notes for the reviewers:
|
shults
left a comment
There was a problem hiding this comment.
Looks good to me.
Except if statements in tests. They seem to be redundant.
Tests look like to be very similar, maybe you can extract common code?
323d2e7 to
9cf5d3a
Compare
9cf5d3a to
b88fb57
Compare
b88fb57 to
4246b5b
Compare
|
|
|
… handling (#7593) (#7650) ### **User description** Cherry-pick of `f4689e7d4b518bf43aaca3e4882f509200de97a1` from `master` to `release-5.8` requires manual resolution. **Conflicts detected:** 9 - gateway/mw_external_oauth.go - gateway/mw_jwt.go - gateway/mw_jwt_test.go Tips: - Check out this branch locally and run: `git cherry-pick -x f4689e7` - Resolve conflicts (including submodules if any), then push back to this branch. Original commit: f4689e7 ___ ### **PR Type** Enhancement, Tests ___ ### **Description** - Add granular JWKS error logging helper - Integrate helper into JWT/OAuth flows - Log base64 decode failures for JWT source - Add comprehensive tests for logging paths ___ ### Diagram Walkthrough ```mermaid flowchart LR A["logJWKError helper (gateway/log_helpers.go)"] -- "used by" --> B["JWTMiddleware.getSecretFromURL/legacy"] A -- "used by" --> C["ExternalOAuth.getSecretFromJWKURL"] D["JWT source base64 decode logging"] -- "in" --> E["JWTMiddleware.getSecretToVerifySignature"] F["Unit tests"] -- "validate" --> A F -- "validate" --> B F -- "validate" --> C F -- "validate" --> E ``` <details> <summary><h3> File Walkthrough</h3></summary> <table><thead><tr><th></th><th align="left">Relevant files</th></tr></thead><tbody><tr><td><strong>Enhancement</strong></td><td><table> <tr> <td> <details> <summary><strong>log_helpers.go</strong><dd><code>Centralized JWKS error classification and logging</code> </dd></summary> <hr> gateway/log_helpers.go <ul><li>Add <code>logJWKError</code> helper with typed/string checks<br> <li> Handle JSON, base64, EOF, and network errors<br> <li> Fallback generic JWK fetch/decode error message<br> <li> Import required stdlib packages</ul> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/7650/files#diff-09ae6fbbd4356171d32de9c73885565376af155e3d75d3e9fa78c4d3abad7fdb">+49/-0</a> </td> </tr> <tr> <td> <details> <summary><strong>mw_external_oauth.go</strong><dd><code>OAuth JWK fetch uses centralized logging</code> </dd></summary> <hr> gateway/mw_external_oauth.go - Use `logJWKError` on JWK fetch failure - Minor import reordering </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/7650/files#diff-49758921227a3506a0c29936c58d02fbc8829d140acb5730de55f6621823a82c">+2/-2</a> </td> </tr> <tr> <td> <details> <summary><strong>mw_jwt.go</strong><dd><code>JWT middleware adopts centralized JWKS logging</code> </dd></summary> <hr> gateway/mw_jwt.go <ul><li>Use <code>logJWKError</code> in legacy and modern JWK paths<br> <li> Log explicit error on invalid base64 JWTSource<br> <li> Minor import reordering</ul> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/7650/files#diff-e8bce0f6790c8c56b30e24dbeebb0fc4aa0879ab5ea5f6ef6dbe68931410e237">+6/-4</a> </td> </tr> </table></td></tr><tr><td><strong>Tests</strong></td><td><table> <tr> <td> <details> <summary><strong>log_helpers_test.go</strong><dd><code>Tests for JWKS logging helper</code> </dd></summary> <hr> gateway/log_helpers_test.go <ul><li>Add tests for <code>logJWKError</code> scenarios<br> <li> Cover JSON, base64, EOF, URL/net/syscall errors<br> <li> Verify no-log on nil error and generic fallback</ul> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/7650/files#diff-592434f2495b588f61da231f73c67b9f8682d74e718b814bb46a5ded0aabf32b">+113/-0</a> </td> </tr> <tr> <td> <details> <summary><strong>mw_external_oauth_test.go</strong><dd><code>Test OAuth JWK error logging path</code> </dd></summary> <hr> gateway/mw_external_oauth_test.go <ul><li>Add test to assert logging on invalid JWKS<br> <li> Use test logger hook to capture errors</ul> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/7650/files#diff-3bde461a83fa23b503cf80349641129e4c69e6ae1afb7b628a882fada3a54566">+53/-3</a> </td> </tr> <tr> <td> <details> <summary><strong>mw_jwt_test.go</strong><dd><code>Tests for JWT middleware error logging</code> </dd></summary> <hr> gateway/mw_jwt_test.go <ul><li>Add tests for legacy and network error logging<br> <li> Test invalid base64 JWTSource logging<br> <li> Utilize test server and logger hooks</ul> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/7650/files#diff-406bf8fdb6c0cc77f04c6245c70abfc592ddb1525aa843200d850e14d135ebfc">+103/-9</a> </td> </tr> </table></td></tr></tr></tbody></table> </details> ___ <!---TykTechnologies/jira-linter starts here--> ### Ticket Details <details> <summary> <a href="https://tyktech.atlassian.net/browse/TT-14494" title="TT-14494" target="_blank">TT-14494</a> </summary> | | | |---------|----| | Status | In Dev | | Summary | Unclear Gateway error logs when handling JWKS URLs | Generated at: 2025-12-18 09:57:48 </details> <!---TykTechnologies/jira-linter ends here--> --------- Co-authored-by: Tyk Bot <bot@tyk.io> Co-authored-by: Nuray Ahmadova <nuray.akhmedova@gmail.com>
… handling (#7593) (#7650) ### **User description** Cherry-pick of `f4689e7d4b518bf43aaca3e4882f509200de97a1` from `master` to `release-5.8` requires manual resolution. **Conflicts detected:** 9 - gateway/mw_external_oauth.go - gateway/mw_jwt.go - gateway/mw_jwt_test.go Tips: - Check out this branch locally and run: `git cherry-pick -x f4689e7` - Resolve conflicts (including submodules if any), then push back to this branch. Original commit: f4689e7 ___ ### **PR Type** Enhancement, Tests ___ ### **Description** - Add granular JWKS error logging helper - Integrate helper into JWT/OAuth flows - Log base64 decode failures for JWT source - Add comprehensive tests for logging paths ___ ### Diagram Walkthrough ```mermaid flowchart LR A["logJWKError helper (gateway/log_helpers.go)"] -- "used by" --> B["JWTMiddleware.getSecretFromURL/legacy"] A -- "used by" --> C["ExternalOAuth.getSecretFromJWKURL"] D["JWT source base64 decode logging"] -- "in" --> E["JWTMiddleware.getSecretToVerifySignature"] F["Unit tests"] -- "validate" --> A F -- "validate" --> B F -- "validate" --> C F -- "validate" --> E ``` <details> <summary><h3> File Walkthrough</h3></summary> <table><thead><tr><th></th><th align="left">Relevant files</th></tr></thead><tbody><tr><td><strong>Enhancement</strong></td><td><table> <tr> <td> <details> <summary><strong>log_helpers.go</strong><dd><code>Centralized JWKS error classification and logging</code> </dd></summary> <hr> gateway/log_helpers.go <ul><li>Add <code>logJWKError</code> helper with typed/string checks<br> <li> Handle JSON, base64, EOF, and network errors<br> <li> Fallback generic JWK fetch/decode error message<br> <li> Import required stdlib packages</ul> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/7650/files#diff-09ae6fbbd4356171d32de9c73885565376af155e3d75d3e9fa78c4d3abad7fdb">+49/-0</a> </td> </tr> <tr> <td> <details> <summary><strong>mw_external_oauth.go</strong><dd><code>OAuth JWK fetch uses centralized logging</code> </dd></summary> <hr> gateway/mw_external_oauth.go - Use `logJWKError` on JWK fetch failure - Minor import reordering </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/7650/files#diff-49758921227a3506a0c29936c58d02fbc8829d140acb5730de55f6621823a82c">+2/-2</a> </td> </tr> <tr> <td> <details> <summary><strong>mw_jwt.go</strong><dd><code>JWT middleware adopts centralized JWKS logging</code> </dd></summary> <hr> gateway/mw_jwt.go <ul><li>Use <code>logJWKError</code> in legacy and modern JWK paths<br> <li> Log explicit error on invalid base64 JWTSource<br> <li> Minor import reordering</ul> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/7650/files#diff-e8bce0f6790c8c56b30e24dbeebb0fc4aa0879ab5ea5f6ef6dbe68931410e237">+6/-4</a> </td> </tr> </table></td></tr><tr><td><strong>Tests</strong></td><td><table> <tr> <td> <details> <summary><strong>log_helpers_test.go</strong><dd><code>Tests for JWKS logging helper</code> </dd></summary> <hr> gateway/log_helpers_test.go <ul><li>Add tests for <code>logJWKError</code> scenarios<br> <li> Cover JSON, base64, EOF, URL/net/syscall errors<br> <li> Verify no-log on nil error and generic fallback</ul> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/7650/files#diff-592434f2495b588f61da231f73c67b9f8682d74e718b814bb46a5ded0aabf32b">+113/-0</a> </td> </tr> <tr> <td> <details> <summary><strong>mw_external_oauth_test.go</strong><dd><code>Test OAuth JWK error logging path</code> </dd></summary> <hr> gateway/mw_external_oauth_test.go <ul><li>Add test to assert logging on invalid JWKS<br> <li> Use test logger hook to capture errors</ul> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/7650/files#diff-3bde461a83fa23b503cf80349641129e4c69e6ae1afb7b628a882fada3a54566">+53/-3</a> </td> </tr> <tr> <td> <details> <summary><strong>mw_jwt_test.go</strong><dd><code>Tests for JWT middleware error logging</code> </dd></summary> <hr> gateway/mw_jwt_test.go <ul><li>Add tests for legacy and network error logging<br> <li> Test invalid base64 JWTSource logging<br> <li> Utilize test server and logger hooks</ul> </details> </td> <td><a href="https://github.com/TykTechnologies/tyk/pull/7650/files#diff-406bf8fdb6c0cc77f04c6245c70abfc592ddb1525aa843200d850e14d135ebfc">+103/-9</a> </td> </tr> </table></td></tr></tr></tbody></table> </details> ___ <!---TykTechnologies/jira-linter starts here--> ### Ticket Details <details> <summary> <a href="https://tyktech.atlassian.net/browse/TT-14494" title="TT-14494" target="_blank">TT-14494</a> </summary> | | | |---------|----| | Status | In Dev | | Summary | Unclear Gateway error logs when handling JWKS URLs | Generated at: 2025-12-18 09:57:48 </details> <!---TykTechnologies/jira-linter ends here--> --------- Co-authored-by: Tyk Bot <bot@tyk.io> Co-authored-by: Nuray Ahmadova <nuray.akhmedova@gmail.com>



Description
Fixed vague error logging (e.g., "illegal base64 data") for JWKS failures. Previously, generic errors made it difficult to distinguish between misconfiguration, network outages, or upstream content issues. The Gateway now specifically classifies these errors—identifying invalid Base64 configuration, network resolution failures, and invalid JSON content—to make debugging faster and more actionable.
Related Issue
TT-14494
Motivation and Context
Generic error messages hindered the diagnosis of authentication issues involving JWKS. This change ensures logs clearly distinguish between misconfiguration, network outages, and upstream content issues, providing immediate, actionable feedback to administrators.
How This Has Been Tested
Refactored and added new table-driven unit tests in
TestJWTMiddleware_ErrorLoggingandTestGatewayLogJWKErrorto verify:jwt_source/jwks_urifor valid Base64.Screenshots (if appropriate)
Types of changes
Checklist
Ticket Details
TT-14494
Generated at: 2025-12-17 05:40:11
Ticket Details
TT-14494
Generated at: 2025-12-17 11:33:27