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

Goja JSVM for Gateway #1961

Closed
wants to merge 10 commits into from
5 changes: 3 additions & 2 deletions api_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ type APISpec struct {
OrgSessionManager SessionHandler
EventPaths map[apidef.TykEvent][]config.TykEventHandler
Health HealthChecker
JSVM JSVM
JSVM TykJSVM
ResponseChain []TykResponseHandler
RoundRobin RoundRobin
URLRewriteEnabled bool
Expand Down Expand Up @@ -251,6 +251,7 @@ func (a APIDefinitionLoader) MakeSpec(def *apidef.APIDefinition, logger *logrus.

// Create and init the virtual Machine
if config.Global().EnableJSVM {
spec.JSVM = InitJSVM()
spec.JSVM.Init(spec, logger)
}

Expand Down Expand Up @@ -792,7 +793,7 @@ func (a APIDefinitionLoader) compileVirtualPathspathSpec(paths []apidef.VirtualM
// Extend with method actions
newSpec.VirtualPathSpec = stringSpec

preLoadVirtualMetaCode(&newSpec.VirtualPathSpec, &apiSpec.JSVM)
preLoadVirtualMetaCode(&newSpec.VirtualPathSpec, apiSpec.JSVM)

urlSpec = append(urlSpec, newSpec)
}
Expand Down
16 changes: 8 additions & 8 deletions api_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func processSpec(spec *APISpec, apisByListen map[string]int,

mwPaths, mwAuthCheckFunc, mwPreFuncs, mwPostFuncs, mwPostAuthCheckFuncs, mwDriver = loadCustomMiddleware(spec)

if config.Global().EnableJSVM && mwDriver == apidef.OttoDriver {
if config.Global().EnableJSVM && (mwDriver == apidef.OttoDriver || mwDriver == apidef.GojaDriver) {
spec.JSVM.LoadJSPaths(mwPaths, prefix)
}

Expand Down Expand Up @@ -277,7 +277,7 @@ func processSpec(spec *APISpec, apisByListen map[string]int,
handleCORS(&chainArray, spec)

for _, obj := range mwPreFuncs {
if mwDriver != apidef.OttoDriver {
if mwDriver != apidef.OttoDriver && mwDriver != apidef.GojaDriver {

coprocessLog.Debug("Registering coprocess middleware, hook name: ", obj.Name, "hook type: Pre", ", driver: ", mwDriver)
mwAppendEnabled(&chainArray, &CoProcessMiddleware{baseMid, coprocess.HookType_Pre, obj.Name, mwDriver})
Expand Down Expand Up @@ -307,7 +307,7 @@ func processSpec(spec *APISpec, apisByListen map[string]int,
mwAppendEnabled(&chainArray, &TransformMethod{BaseMiddleware: baseMid})

for _, obj := range mwPostFuncs {
if mwDriver != apidef.OttoDriver {
if mwDriver != apidef.OttoDriver && mwDriver != apidef.GojaDriver {

coprocessLog.Debug("Registering coprocess middleware, hook name: ", obj.Name, "hook type: Post", ", driver: ", mwDriver)
mwAppendEnabled(&chainArray, &CoProcessMiddleware{baseMid, coprocess.HookType_Post, obj.Name, mwDriver})
Expand All @@ -327,7 +327,7 @@ func processSpec(spec *APISpec, apisByListen map[string]int,

// Add pre-process MW
for _, obj := range mwPreFuncs {
if mwDriver != apidef.OttoDriver {
if mwDriver != apidef.OttoDriver && mwDriver != apidef.GojaDriver {

coprocessLog.Debug("Registering coprocess middleware, hook name: ", obj.Name, "hook type: Pre", ", driver: ", mwDriver)
mwAppendEnabled(&chainArray, &CoProcessMiddleware{baseMid, coprocess.HookType_Pre, obj.Name, mwDriver})
Expand Down Expand Up @@ -367,8 +367,8 @@ func processSpec(spec *APISpec, apisByListen map[string]int,
logger.Info("Checking security policy: OpenID")
}

coprocessAuth := EnableCoProcess && mwDriver != apidef.OttoDriver && spec.EnableCoProcessAuth
ottoAuth := !coprocessAuth && mwDriver == apidef.OttoDriver && spec.EnableCoProcessAuth
coprocessAuth := EnableCoProcess && mwDriver != apidef.OttoDriver && mwDriver != apidef.GojaDriver && spec.EnableCoProcessAuth
jsvmAuth := !coprocessAuth && (mwDriver == apidef.OttoDriver || mwDriver == apidef.GojaDriver) && spec.EnableCoProcessAuth

if coprocessAuth {
// TODO: check if mwAuthCheckFunc is available/valid
Expand All @@ -378,7 +378,7 @@ func processSpec(spec *APISpec, apisByListen map[string]int,
mwAppendEnabled(&authArray, &CoProcessMiddleware{baseMid, coprocess.HookType_CustomKeyCheck, mwAuthCheckFunc.Name, mwDriver})
}

if ottoAuth {
if jsvmAuth {

logger.Info("----> Checking security policy: JS Plugin")

Expand Down Expand Up @@ -414,7 +414,7 @@ func processSpec(spec *APISpec, apisByListen map[string]int,
mwAppendEnabled(&chainArray, &VirtualEndpoint{BaseMiddleware: baseMid})

for _, obj := range mwPostFuncs {
if mwDriver != apidef.OttoDriver {
if mwDriver != apidef.OttoDriver && mwDriver != apidef.GojaDriver {

coprocessLog.Debug("Registering coprocess middleware, hook name: ", obj.Name, "hook type: Post", ", driver: ", mwDriver)
mwAppendEnabled(&chainArray, &CoProcessMiddleware{baseMid, coprocess.HookType_Post, obj.Name, mwDriver})
Expand Down
1 change: 1 addition & 0 deletions apidef/api_definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
RequestJSON RequestInputType = "json"

OttoDriver MiddlewareDriver = "otto"
GojaDriver MiddlewareDriver = "goja"
PythonDriver MiddlewareDriver = "python"
LuaDriver MiddlewareDriver = "lua"
GrpcDriver MiddlewareDriver = "grpc"
Expand Down
3 changes: 3 additions & 0 deletions cli/linter/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@
"enable_jsvm": {
"type": "boolean"
},
"enable_v2_jsvm": {
"type": "boolean"
},
"jsvm_timeout": {
"type": "integer"
},
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ type Config struct {
ControlAPIPort int `json:"control_api_port"`
EnableCustomDomains bool `json:"enable_custom_domains"`
EnableJSVM bool `json:"enable_jsvm"`
EnableV2JSVM bool `json:"enable_v2_jsvm"`
JSVMTimeout int `json:"jsvm_timeout"`
CoProcessOptions CoProcessConfig `json:"coprocess_options"`
HideGeneratorHeader bool `json:"hide_generator_header"`
Expand Down
2 changes: 1 addition & 1 deletion coprocess_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (b *Bundle) AddToSpec() {
b.Spec.CustomMiddleware = b.Manifest.CustomMiddleware

// Call HandleMiddlewareCache only when using rich plugins:
if GlobalDispatcher != nil && b.Spec.CustomMiddleware.Driver != apidef.OttoDriver {
if GlobalDispatcher != nil && (b.Spec.CustomMiddleware.Driver != apidef.OttoDriver && b.Spec.CustomMiddleware.Driver != apidef.GojaDriver) {
GlobalDispatcher.HandleMiddlewareCache(&b.Manifest, b.Path)
}
}
Expand Down
3 changes: 1 addition & 2 deletions coprocess_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ import (
"net/url"
"testing"

"github.com/Sirupsen/logrus"
"github.com/golang/protobuf/proto"
"github.com/justinas/alice"

"github.com/Sirupsen/logrus"

"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/coprocess"
)
Expand Down
2 changes: 1 addition & 1 deletion jsvm_event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ func (l *JSVMEventHandler) HandleEvent(em config.EventMessage) {
}

// Execute the method name with the JSON object
GlobalEventsJSVM.VM.Run(l.methodName + `.DoProcessEvent(` + string(msgAsJSON) + `,` + l.SpecJSON + `);`)
GlobalEventsJSVM.Run(l.methodName + `.DoProcessEvent(` + string(msgAsJSON) + `,` + l.SpecJSON + `);`)
}
Loading