TUN-3558: cloudflared allows empty config files

This commit is contained in:
Adam Chalmers 2020-11-18 10:18:08 -06:00
parent 4c1b89576c
commit b7e91466f5
3 changed files with 14 additions and 4 deletions

View File

@ -2,6 +2,7 @@ package config
import ( import (
"fmt" "fmt"
"io"
"net/url" "net/url"
"os" "os"
"path/filepath" "path/filepath"
@ -392,6 +393,10 @@ func ReadConfigFile(c *cli.Context, log logger.Service) (*configFileSettings, er
} }
defer file.Close() defer file.Close()
if err := yaml.NewDecoder(file).Decode(&configuration); err != nil { 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) return nil, errors.Wrap(err, "error parsing YAML in config file at "+configFile)
} }
configuration.sourceFile = configFile configuration.sourceFile = configFile

View File

@ -1,6 +1,7 @@
package config package config
import ( import (
"io"
"os" "os"
"github.com/cloudflare/cloudflared/logger" "github.com/cloudflare/cloudflared/logger"
@ -27,7 +28,7 @@ type FileManager struct {
notifier Notifier notifier Notifier
configPath string configPath string
logger logger.Service logger logger.Service
ReadConfig func(string) (Root, error) ReadConfig func(string, logger.Service) (Root, error)
} }
// NewFileManager creates a config manager // NewFileManager creates a config manager
@ -59,7 +60,7 @@ func (m *FileManager) Start(notifier Notifier) error {
// GetConfig reads the yaml file from the disk // GetConfig reads the yaml file from the disk
func (m *FileManager) GetConfig() (Root, error) { func (m *FileManager) GetConfig() (Root, error) {
return m.ReadConfig(m.configPath) return m.ReadConfig(m.configPath, m.logger)
} }
// Shutdown stops the watcher // Shutdown stops the watcher
@ -67,7 +68,7 @@ func (m *FileManager) Shutdown() {
m.watcher.Shutdown() m.watcher.Shutdown()
} }
func readConfigFromPath(configPath string) (Root, error) { func readConfigFromPath(configPath string, log logger.Service) (Root, error) {
if configPath == "" { if configPath == "" {
return Root{}, errors.New("unable to find config file") return Root{}, errors.New("unable to find config file")
} }
@ -80,6 +81,10 @@ func readConfigFromPath(configPath string) (Root, error) {
var config Root var config Root
if err := yaml.NewDecoder(file).Decode(&config); err != nil { 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) return Root{}, errors.Wrap(err, "error parsing YAML in config file at "+configPath)
} }

View File

@ -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 return *c, nil
} }
wait := make(chan struct{}) wait := make(chan struct{})