diff --git a/config/config.go b/config/config.go index 02586ff9b..0391a3c1a 100644 --- a/config/config.go +++ b/config/config.go @@ -50,6 +50,10 @@ var ( `\`, `\\`, ) httpLoadConfigRetryInterval = 10 * time.Second + + // fetchURLRe is a regex to determine whether the requested file should + // be fetched from a remote or read from the filesystem. + fetchURLRe = regexp.MustCompile(`^\w+://`) ) // Config specifies the URL/user/password for the database that telegraf @@ -902,17 +906,21 @@ func escapeEnv(value string) string { } func loadConfig(config string) ([]byte, error) { - u, err := url.Parse(config) - if err != nil { - return nil, err + if fetchURLRe.MatchString(config) { + u, err := url.Parse(config) + if err != nil { + return nil, err + } + + switch u.Scheme { + case "https", "http": + return fetchConfig(u) + default: + return nil, fmt.Errorf("scheme %q not supported", u.Scheme) + } } - switch u.Scheme { - case "https", "http": - return fetchConfig(u) - default: - // If it isn't a https scheme, try it as a file. - } + // If it isn't a https scheme, try it as a file return ioutil.ReadFile(config) } diff --git a/config/config_test.go b/config/config_test.go index 3095ffdf1..91d0a81e8 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -5,6 +5,7 @@ import ( "net/http" "net/http/httptest" "os" + "runtime" "strings" "testing" "time" @@ -323,6 +324,19 @@ func TestConfig_URLRetries3FailsThenPasses(t *testing.T) { require.Equal(t, 4, responseCounter) } +func TestConfig_URLLikeFileName(t *testing.T) { + c := NewConfig() + err := c.LoadConfig("http:##www.example.com.conf") + require.Error(t, err) + + if runtime.GOOS == "windows" { + // The error file not found error message is different on windows + require.Equal(t, "Error loading config file http:##www.example.com.conf: open http:##www.example.com.conf: The system cannot find the file specified.", err.Error()) + } else { + require.Equal(t, "Error loading config file http:##www.example.com.conf: open http:##www.example.com.conf: no such file or directory", err.Error()) + } +} + /*** Mockup INPUT plugin for testing to avoid cyclic dependencies ***/ type MockupInputPlugin struct { Servers []string `toml:"servers"`