From b7e91466f53ae400a82c462f235fe1abcf10dcb6 Mon Sep 17 00:00:00 2001 From: Adam Chalmers Date: Wed, 18 Nov 2020 10:18:08 -0600 Subject: [PATCH] TUN-3558: cloudflared allows empty config files --- cmd/cloudflared/config/configuration.go | 5 +++++ cmd/cloudflared/config/manager.go | 11 ++++++++--- cmd/cloudflared/config/manager_test.go | 2 +- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/cmd/cloudflared/config/configuration.go b/cmd/cloudflared/config/configuration.go index a3b927d3..e75e45a2 100644 --- a/cmd/cloudflared/config/configuration.go +++ b/cmd/cloudflared/config/configuration.go @@ -2,6 +2,7 @@ package config import ( "fmt" + "io" "net/url" "os" "path/filepath" @@ -392,6 +393,10 @@ func ReadConfigFile(c *cli.Context, log logger.Service) (*configFileSettings, er } defer file.Close() if err := yaml.NewDecoder(file).Decode(&configuration); err != nil { + if err == io.EOF { + log.Errorf("Configuration file %s was empty", configFile) + return &configuration, nil + } return nil, errors.Wrap(err, "error parsing YAML in config file at "+configFile) } configuration.sourceFile = configFile diff --git a/cmd/cloudflared/config/manager.go b/cmd/cloudflared/config/manager.go index cd4faa9d..d299c06a 100644 --- a/cmd/cloudflared/config/manager.go +++ b/cmd/cloudflared/config/manager.go @@ -1,6 +1,7 @@ package config import ( + "io" "os" "github.com/cloudflare/cloudflared/logger" @@ -27,7 +28,7 @@ type FileManager struct { notifier Notifier configPath string logger logger.Service - ReadConfig func(string) (Root, error) + ReadConfig func(string, logger.Service) (Root, error) } // NewFileManager creates a config manager @@ -59,7 +60,7 @@ func (m *FileManager) Start(notifier Notifier) error { // GetConfig reads the yaml file from the disk func (m *FileManager) GetConfig() (Root, error) { - return m.ReadConfig(m.configPath) + return m.ReadConfig(m.configPath, m.logger) } // Shutdown stops the watcher @@ -67,7 +68,7 @@ func (m *FileManager) Shutdown() { m.watcher.Shutdown() } -func readConfigFromPath(configPath string) (Root, error) { +func readConfigFromPath(configPath string, log logger.Service) (Root, error) { if configPath == "" { return Root{}, errors.New("unable to find config file") } @@ -80,6 +81,10 @@ func readConfigFromPath(configPath string) (Root, error) { var config Root if err := yaml.NewDecoder(file).Decode(&config); err != nil { + if err == io.EOF { + log.Errorf("Configuration file %s was empty", configPath) + return Root{}, nil + } return Root{}, errors.Wrap(err, "error parsing YAML in config file at "+configPath) } diff --git a/cmd/cloudflared/config/manager_test.go b/cmd/cloudflared/config/manager_test.go index 10416048..116f5a21 100644 --- a/cmd/cloudflared/config/manager_test.go +++ b/cmd/cloudflared/config/manager_test.go @@ -57,7 +57,7 @@ func TestConfigChanged(t *testing.T) { }, }, } - configRead := func(configPath string) (Root, error) { + configRead := func(configPath string, log logger.Service) (Root, error) { return *c, nil } wait := make(chan struct{})