DataSourceProxy: Handle URL parsing error (#23731)

* pluginproxy: Handle URL parsing error
* pkg/api: Validate data source URLs
* pkg/api: Return 400 for URL validation error
This commit is contained in:
Arve Knudsen
2020-04-22 10:30:06 +02:00
committed by GitHub
parent c3e3067cf4
commit 7d88018531
4 changed files with 115 additions and 27 deletions
+21 -3
View File
@@ -31,6 +31,20 @@ var (
client = newHTTPClient()
)
type URLValidationError struct {
error
url string
}
func (e URLValidationError) Error() string {
return fmt.Sprintf("Validation of URL %q failed: %s", e.url, e.error.Error())
}
func (e URLValidationError) Unwrap() error {
return e.error
}
type DataSourceProxy struct {
ds *models.DataSource
ctx *models.ReqContext
@@ -70,8 +84,12 @@ func (lw *logWrapper) Write(p []byte) (n int, err error) {
}
// NewDataSourceProxy creates a new Datasource proxy
func NewDataSourceProxy(ds *models.DataSource, plugin *plugins.DataSourcePlugin, ctx *models.ReqContext, proxyPath string, cfg *setting.Cfg) *DataSourceProxy {
targetURL, _ := url.Parse(ds.Url)
func NewDataSourceProxy(ds *models.DataSource, plugin *plugins.DataSourcePlugin, ctx *models.ReqContext,
proxyPath string, cfg *setting.Cfg) (*DataSourceProxy, error) {
targetURL, err := url.Parse(ds.Url)
if err != nil {
return nil, URLValidationError{error: err, url: ds.Url}
}
return &DataSourceProxy{
ds: ds,
@@ -80,7 +98,7 @@ func NewDataSourceProxy(ds *models.DataSource, plugin *plugins.DataSourcePlugin,
proxyPath: proxyPath,
targetUrl: targetURL,
cfg: cfg,
}
}, nil
}
func newHTTPClient() httpClient {