Skip to content

Commit 7fcc173

Browse files
committed
Moved unsupported plugins verification to processRequest
1 parent 82447be commit 7fcc173

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

gateway/coprocess.go

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -275,28 +275,13 @@ func (gw *Gateway) CoProcessInit() {
275275

276276
// EnabledForSpec checks if this middleware should be enabled for a given API.
277277
func (m *CoProcessMiddleware) EnabledForSpec() bool {
278-
279278
if !m.Gw.GetConfig().CoProcessOptions.EnableCoProcess {
280279
log.WithFields(logrus.Fields{
281280
"prefix": "coprocess",
282281
}).Error("Your API specifies a CP custom middleware, either Tyk wasn't build with CP support or CP is not enabled in your Tyk configuration file!")
283282
return false
284283
}
285284

286-
var supported bool
287-
for _, driver := range supportedDrivers {
288-
if m.Spec.CustomMiddleware.Driver == driver {
289-
supported = true
290-
}
291-
}
292-
293-
if !supported {
294-
log.WithFields(logrus.Fields{
295-
"prefix": "coprocess",
296-
}).Errorf("Unsupported driver '%s'", m.Spec.CustomMiddleware.Driver)
297-
return false
298-
}
299-
300285
log.WithFields(logrus.Fields{
301286
"prefix": "coprocess",
302287
}).Debug("Enabling CP middleware.")
@@ -306,13 +291,9 @@ func (m *CoProcessMiddleware) EnabledForSpec() bool {
306291

307292
// ProcessRequest will run any checks on the request on the way through the system, return an error to have the chain fail
308293
func (m *CoProcessMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Request, _ interface{}) (error, int) {
309-
if d := loadedDrivers[m.Spec.CustomMiddleware.Driver]; d == nil {
310-
log.WithFields(logrus.Fields{
311-
"prefix": "coprocess",
312-
}).Errorf("Driver '%s' isn't loaded", m.Spec.CustomMiddleware.Driver)
313-
respCode := http.StatusInternalServerError
314-
315-
return errors.New(http.StatusText(respCode)), respCode
294+
errorCode, err := m.validateDriver()
295+
if err != nil {
296+
return err, errorCode
316297
}
317298

318299
if m.HookType == coprocess.HookType_CustomKeyCheck {
@@ -513,6 +494,38 @@ func (m *CoProcessMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Requ
513494
return nil, http.StatusOK
514495
}
515496

497+
func (m *CoProcessMiddleware) validateDriver() (int, error) {
498+
if !m.isDriverSupported() {
499+
log.WithFields(logrus.Fields{
500+
"prefix": "coprocess",
501+
}).Errorf("Unsupported driver '%s'", m.Spec.CustomMiddleware.Driver)
502+
respCode := http.StatusInternalServerError
503+
504+
return respCode, errors.New(http.StatusText(respCode))
505+
}
506+
507+
if d := loadedDrivers[m.Spec.CustomMiddleware.Driver]; d == nil {
508+
log.WithFields(logrus.Fields{
509+
"prefix": "coprocess",
510+
}).Errorf("Driver '%s' isn't loaded", m.Spec.CustomMiddleware.Driver)
511+
respCode := http.StatusInternalServerError
512+
513+
return respCode, errors.New(http.StatusText(respCode))
514+
}
515+
516+
return http.StatusOK, nil
517+
}
518+
519+
func (m *CoProcessMiddleware) isDriverSupported() bool {
520+
for _, driver := range supportedDrivers {
521+
if m.Spec.CustomMiddleware.Driver == driver {
522+
return true
523+
}
524+
}
525+
526+
return false
527+
}
528+
516529
type CustomMiddlewareResponseHook struct {
517530
BaseTykResponseHandler
518531
mw *CoProcessMiddleware

0 commit comments

Comments
 (0)