From abfdfcf4056656a085954d91a03ce71a4bf77980 Mon Sep 17 00:00:00 2001 From: Serge Zaitsev Date: Mon, 13 Sep 2021 14:32:11 +0300 Subject: [PATCH] Plugins: Support multiple HTTP methods in plugin.json routes (#39069) * Fix: support comma-separated methods in plugin.json * update docs about multiple methods per route --- docs/sources/developers/plugins/metadata.md | 22 +++++++++---------- .../developers/plugins/plugin.schema.json | 2 +- pkg/api/app_routes.go | 5 ++++- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/docs/sources/developers/plugins/metadata.md b/docs/sources/developers/plugins/metadata.md index f2be588ede9..9937037ebe0 100644 --- a/docs/sources/developers/plugins/metadata.md +++ b/docs/sources/developers/plugins/metadata.md @@ -184,17 +184,17 @@ For data source plugins. Proxy routes used for plugin authentication and adding ### Properties -| Property | Type | Required | Description | -| -------------- | ----------------------- | -------- | ------------------------------------------------------------------------------------------------------- | -| `body` | [object](#body) | No | For data source plugins. Route headers set the body content and length to the proxied request. | -| `headers` | array | No | For data source plugins. Route headers adds HTTP headers to the proxied request. | -| `jwtTokenAuth` | [object](#jwttokenauth) | No | For data source plugins. Token authentication section used with an JWT OAuth API. | -| `method` | string | No | For data source plugins. Route method matches the HTTP verb like GET or POST. | -| `path` | string | No | For data source plugins. The route path that is replaced by the route URL field when proxying the call. | -| `reqRole` | string | No | | -| `reqSignedIn` | boolean | No | | -| `tokenAuth` | [object](#tokenauth) | No | For data source plugins. Token authentication section used with an OAuth API. | -| `url` | string | No | For data source plugins. Route URL is where the request is proxied to. | +| Property | Type | Required | Description | +| -------------- | ----------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------- | +| `body` | [object](#body) | No | For data source plugins. Route headers set the body content and length to the proxied request. | +| `headers` | array | No | For data source plugins. Route headers adds HTTP headers to the proxied request. | +| `jwtTokenAuth` | [object](#jwttokenauth) | No | For data source plugins. Token authentication section used with an JWT OAuth API. | +| `method` | string | No | For data source plugins. Route method matches the HTTP verb like GET or POST. Multiple methods can be provided as a comma-separated list. | +| `path` | string | No | For data source plugins. The route path that is replaced by the route URL field when proxying the call. | +| `reqRole` | string | No | | +| `reqSignedIn` | boolean | No | | +| `tokenAuth` | [object](#tokenauth) | No | For data source plugins. Token authentication section used with an OAuth API. | +| `url` | string | No | For data source plugins. Route URL is where the request is proxied to. | ### body diff --git a/docs/sources/developers/plugins/plugin.schema.json b/docs/sources/developers/plugins/plugin.schema.json index 3e79178b666..dc75ea122d2 100644 --- a/docs/sources/developers/plugins/plugin.schema.json +++ b/docs/sources/developers/plugins/plugin.schema.json @@ -340,7 +340,7 @@ }, "method": { "type": "string", - "description": "For data source plugins. Route method matches the HTTP verb like GET or POST." + "description": "For data source plugins. Route method matches the HTTP verb like GET or POST. Multiple methods can be provided as a comma-separated list." }, "url": { "type": "string", diff --git a/pkg/api/app_routes.go b/pkg/api/app_routes.go index b30a757cd76..bc2ef26948f 100644 --- a/pkg/api/app_routes.go +++ b/pkg/api/app_routes.go @@ -4,6 +4,7 @@ import ( "crypto/tls" "net" "net/http" + "strings" "time" "github.com/grafana/grafana/pkg/api/pluginproxy" @@ -47,7 +48,9 @@ func (hs *HTTPServer) initAppPluginRoutes(r *macaron.Macaron) { } } handlers = append(handlers, AppPluginRoute(route, plugin.Id, hs)) - r.Handle(route.Method, url, handlers) + for _, method := range strings.Split(route.Method, ",") { + r.Handle(strings.TrimSpace(method), url, handlers) + } log.Debugf("Plugins: Adding proxy route %s", url) } }