Skip to content

Commit

Permalink
Add ctx.SetOASDefinition for goplugins
Browse files Browse the repository at this point in the history
  • Loading branch information
furkansenharputlu committed Jul 11, 2023
1 parent ae79c0f commit e72ef95
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 2 deletions.
29 changes: 29 additions & 0 deletions ctx/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"encoding/json"
"net/http"

"github.com/TykTechnologies/tyk/apidef/oas"

"github.com/TykTechnologies/tyk/config"

"github.com/TykTechnologies/tyk/apidef"
Expand Down Expand Up @@ -40,6 +42,7 @@ const (
UrlRewriteTarget
TransformedRequestMethod
Definition
OASDefinition
RequestStatus
GraphQLRequest
GraphQLIsWebSocketUpgrade
Expand Down Expand Up @@ -133,3 +136,29 @@ func GetDefinition(r *http.Request) *apidef.APIDefinition {
}
return nil
}

func SetOASDefinition(r *http.Request, s *oas.OAS) {
ctx := r.Context()
ctx = context.WithValue(ctx, OASDefinition, s)
setContext(r, ctx)
}

func GetOASDefinition(r *http.Request) *oas.OAS {
if v := r.Context().Value(OASDefinition); v != nil {
if val, ok := v.(*oas.OAS); ok {
oasInBytes, err := json.Marshal(val)
if err != nil {
logger.Get().Error("Couldn't marshal OAS object in the request context")
}

var retOAS oas.OAS
err = json.Unmarshal(oasInBytes, &retOAS)
if err != nil {
logger.Get().Error("Couldn't unmarshal OAS object in the request context")
}

return &retOAS
}
}
return nil
}
10 changes: 8 additions & 2 deletions gateway/mw_go_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
"net/http"
"time"

"github.com/TykTechnologies/tyk/ctx"

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

"github.com/sirupsen/logrus"

"github.com/TykTechnologies/tyk/ctx"
"github.com/TykTechnologies/tyk/goplugin"
"github.com/TykTechnologies/tyk/request"
)
Expand Down Expand Up @@ -217,7 +218,12 @@ func (m *GoPluginMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Reque
t1 := time.Now()

// Inject definition into request context:
ctx.SetDefinition(r, m.Spec.APIDefinition)
if m.Spec.IsOAS {
ctx.SetOASDefinition(r, &m.Spec.OAS)
} else {
ctx.SetDefinition(r, m.Spec.APIDefinition)
}

handler(rw, r)

// calculate latency
Expand Down
51 changes: 51 additions & 0 deletions goplugin/mw_go_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
package goplugin_test

import (
"context"
"net/http"
"testing"

"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/apidef/oas"
"github.com/TykTechnologies/tyk/gateway"
"github.com/TykTechnologies/tyk/test"
"github.com/getkin/kin-openapi/openapi3"
"github.com/stretchr/testify/assert"
)

/*func TestMain(m *testing.M) {
Expand Down Expand Up @@ -495,3 +499,50 @@ func TestGoPluginMiddleware_ProcessRequest_ShouldFailWhenNotLoaded(t *testing.T)
}...)
})
}

func TestGoPlugin_AccessingOASAPIDef(t *testing.T) {
ts := gateway.StartTest(nil)
defer ts.Close()

const oasDocTitle = "My OAS Documentation"

oasDoc := oas.OAS{}
oasDoc.OpenAPI = "3.0.3"
oasDoc.Info = &openapi3.Info{
Version: "1",
Title: oasDocTitle,
}
oasDoc.Paths = openapi3.Paths{}

oasDoc.SetTykExtension(&oas.XTykAPIGateway{})

err := oasDoc.Validate(context.Background())
assert.NoError(t, err)

ts.Gw.BuildAndLoadAPI(func(spec *gateway.APISpec) {
spec.IsOAS = true
spec.OAS = oasDoc
spec.Proxy.ListenPath = "/oas-goplugin/"
spec.UseKeylessAccess = true
spec.UseStandardAuth = false
spec.CustomMiddleware = apidef.MiddlewareSection{
Driver: apidef.GoPluginDriver,
Pre: []apidef.MiddlewareDefinition{
{
Name: "MyPluginAccessingOASAPI",
Path: "../test/goplugins/goplugins.so",
},
},
}
})

ts.Run(t, []test.TestCase{
{
Path: "/oas-goplugin/get",
Code: http.StatusOK,
HeadersMatch: map[string]string{
"X-OAS-Doc-Title": oasDocTitle,
},
},
}...)
}
5 changes: 5 additions & 0 deletions test/goplugins/test_goplugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,8 @@ func MyAnalyticsPluginMaskJSONLoginBody(record *analytics.AnalyticsRecord) {
}
}
}

func MyPluginAccessingOASAPI(rw http.ResponseWriter, r *http.Request) {
oas := ctx.GetOASDefinition(r)
rw.Header().Add("X-OAS-Doc-Title", oas.Info.Title)
}

0 comments on commit e72ef95

Please sign in to comment.