mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Check for empty URLs when creating/updating a data source (#42837)
* checks for empty URLs added * check for TimeSeriesTypeNot to fix InfluxDB alerts * log a warning when a data frame is ignored * fix: add brittle Prometheus URL input selector needs a proper aria-label or test-data-id selector * test: add URL input aria-label needs to use the grafana/e2e-selectors package * test: run ci * add URL validation for specific data sources, e2e tests * Update pkg/api/datasource/validation.go Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com> * delete duplicated logs * delete unnecessary leading newline Co-authored-by: gillesdemey <gilles.de.mey@gmail.com> Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com>
This commit is contained in:
@@ -1,17 +1,37 @@
|
||||
package datasource
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/tsdb/mssql"
|
||||
)
|
||||
|
||||
var logger = log.New("datasource")
|
||||
|
||||
// requiredURL contains the set of data sources that require a URL.
|
||||
var requiredURL = map[string]bool{
|
||||
models.DS_GRAPHITE: true,
|
||||
models.DS_INFLUXDB: true,
|
||||
models.DS_INFLUXDB_08: true,
|
||||
models.DS_ES: true,
|
||||
models.DS_PROMETHEUS: true,
|
||||
models.DS_ALERTMANAGER: true,
|
||||
models.DS_JAEGER: true,
|
||||
models.DS_LOKI: true,
|
||||
models.DS_OPENTSDB: true,
|
||||
models.DS_TEMPO: true,
|
||||
models.DS_ZIPKIN: true,
|
||||
models.DS_MYSQL: true,
|
||||
models.DS_POSTGRES: true,
|
||||
models.DS_MSSQL: true,
|
||||
}
|
||||
|
||||
// URLValidationError represents an error from validating a data source URL.
|
||||
type URLValidationError struct {
|
||||
Err error
|
||||
@@ -40,6 +60,11 @@ var reURL = regexp.MustCompile("^[^:]*://")
|
||||
// The data source's type and URL must be provided. If successful, the valid URL object is returned, otherwise an
|
||||
// error is returned.
|
||||
func ValidateURL(typeName, urlStr string) (*url.URL, error) {
|
||||
// Check for empty URLs
|
||||
if _, exists := requiredURL[typeName]; exists && strings.TrimSpace(urlStr) == "" {
|
||||
return nil, URLValidationError{Err: errors.New("empty URL string"), URL: ""}
|
||||
}
|
||||
|
||||
var u *url.URL
|
||||
var err error
|
||||
switch strings.ToLower(typeName) {
|
||||
|
||||
Reference in New Issue
Block a user