| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2017-08-22 23:14:15 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/pluginproxy" | 
					
						
							| 
									
										
										
										
											2019-02-24 06:35:26 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/infra/metrics" | 
					
						
							| 
									
										
										
										
											2015-02-05 17:37:13 +08:00
										 |  |  | 	m "github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2017-08-23 16:52:31 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/plugins" | 
					
						
							| 
									
										
										
										
											2017-01-16 19:16:41 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | func (hs *HTTPServer) ProxyDataSourceRequest(c *m.ReqContext) { | 
					
						
							| 
									
										
										
										
											2019-07-16 22:58:46 +08:00
										 |  |  | 	c.TimeRequest(metrics.MDataSourceProxyReqTimer) | 
					
						
							| 
									
										
										
										
											2016-06-03 15:17:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-11 17:29:14 +08:00
										 |  |  | 	dsId := c.ParamsInt64(":id") | 
					
						
							| 
									
										
										
										
											2018-10-26 16:40:33 +08:00
										 |  |  | 	ds, err := hs.DatasourceCache.GetDatasource(dsId, c.SignedInUser, c.SkipCache) | 
					
						
							| 
									
										
										
										
											2015-10-08 23:30:13 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-10-26 16:40:33 +08:00
										 |  |  | 		if err == m.ErrDataSourceAccessDenied { | 
					
						
							| 
									
										
										
										
											2018-10-27 00:34:10 +08:00
										 |  |  | 			c.JsonApiErr(403, "Access denied to datasource", err) | 
					
						
							| 
									
										
										
										
											2018-10-26 16:40:33 +08:00
										 |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | 		c.JsonApiErr(500, "Unable to load datasource meta data", err) | 
					
						
							| 
									
										
										
										
											2015-03-12 00:34:11 +08:00
										 |  |  | 		return | 
					
						
							| 
									
										
										
										
											2014-12-29 20:36:08 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-23 16:52:31 +08:00
										 |  |  | 	// find plugin
 | 
					
						
							|  |  |  | 	plugin, ok := plugins.DataSources[ds.Type] | 
					
						
							|  |  |  | 	if !ok { | 
					
						
							|  |  |  | 		c.JsonApiErr(500, "Unable to find datasource plugin", err) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-20 01:02:04 +08:00
										 |  |  | 	// macaron does not include trailing slashes when resolving a wildcard path
 | 
					
						
							| 
									
										
										
										
											2018-10-03 18:55:01 +08:00
										 |  |  | 	proxyPath := ensureProxyPathTrailingSlash(c.Req.URL.Path, c.Params("*")) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-14 20:04:47 +08:00
										 |  |  | 	proxy := pluginproxy.NewDataSourceProxy(ds, plugin, c, proxyPath, hs.Cfg) | 
					
						
							| 
									
										
										
										
											2018-10-03 18:55:01 +08:00
										 |  |  | 	proxy.HandleRequest() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ensureProxyPathTrailingSlash Check for a trailing slash in original path and makes
 | 
					
						
							|  |  |  | // sure that a trailing slash is added to proxy path, if not already exists.
 | 
					
						
							|  |  |  | func ensureProxyPathTrailingSlash(originalPath, proxyPath string) string { | 
					
						
							| 
									
										
										
										
											2018-09-19 02:16:09 +08:00
										 |  |  | 	if len(proxyPath) > 1 { | 
					
						
							| 
									
										
										
										
											2018-10-03 18:55:01 +08:00
										 |  |  | 		if originalPath[len(originalPath)-1] == '/' && proxyPath[len(proxyPath)-1] != '/' { | 
					
						
							|  |  |  | 			return proxyPath + "/" | 
					
						
							| 
									
										
										
										
											2018-09-19 02:16:09 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-03 18:55:01 +08:00
										 |  |  | 	return proxyPath | 
					
						
							| 
									
										
										
										
											2016-08-17 13:33:59 +08:00
										 |  |  | } |