Skip to content

Commit cbce6b4

Browse files
committed
Warn about .sops.yml files found while searching for .sops.yaml.
Signed-off-by: Felix Fontein <felix@fontein.de>
1 parent 8c91a3b commit cbce6b4

2 files changed

Lines changed: 57 additions & 15 deletions

File tree

cmd/sops/main.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ import (
4444
"github.com/getsops/sops/v3/version"
4545
)
4646

47-
var log *logrus.Logger
47+
var (
48+
log *logrus.Logger
49+
50+
// Whether the config file warning was already shown to the user.
51+
// Used and set by findConfigFile().
52+
showedConfigFileWarning bool
53+
)
4854

4955
func init() {
5056
log = logging.NewLogger("CMD")
@@ -364,7 +370,7 @@ func main() {
364370
if c.GlobalString("config") != "" {
365371
configPath = c.GlobalString("config")
366372
} else {
367-
configPath, err = config.FindConfigFile(".")
373+
configPath, err = findConfigFile()
368374
if err != nil {
369375
return common.NewExitError(err, codes.ErrorGeneric)
370376
}
@@ -685,7 +691,7 @@ func main() {
685691
if c.GlobalString("config") != "" {
686692
configPath = c.GlobalString("config")
687693
} else {
688-
configPath, err = config.FindConfigFile(".")
694+
configPath, err = findConfigFile()
689695
if err != nil {
690696
return common.NewExitError(err, codes.ErrorGeneric)
691697
}
@@ -2183,11 +2189,21 @@ func keyservices(c *cli.Context) (svcs []keyservice.KeyServiceClient) {
21832189
return
21842190
}
21852191

2192+
// Wrapper of config.FindConfigFileEx that takes care of handling the returned wraning.
2193+
func findConfigFile() (string, error) {
2194+
configPath, err, warn := config.FindConfigFileEx(".")
2195+
if len(warn) > 0 && !showedConfigFileWarning {
2196+
showedConfigFileWarning = true
2197+
log.Warn(warn)
2198+
}
2199+
return configPath, err
2200+
}
2201+
21862202
func loadStoresConfig(context *cli.Context, path string) (*config.StoresConfig, error) {
21872203
configPath := context.GlobalString("config")
21882204
if configPath == "" {
2189-
// Ignore config not found errors returned from FindConfigFile since the config file is not mandatory
2190-
foundPath, err := config.FindConfigFile(".")
2205+
// Ignore config not found errors returned from findConfigFile since the config file is not mandatory
2206+
foundPath, err := findConfigFile()
21912207
if err != nil {
21922208
return config.NewStoresConfig(), nil
21932209
}
@@ -2322,14 +2338,14 @@ func keyGroups(c *cli.Context, file string) ([]sops.KeyGroup, error) {
23222338
return []sops.KeyGroup{group}, nil
23232339
}
23242340

2325-
// loadConfig will look for an existing config file, either provided through the command line, or using config.FindConfigFile.
2341+
// loadConfig will look for an existing config file, either provided through the command line, or using findConfigFile
23262342
// Since a config file is not required, this function does not error when one is not found, and instead returns a nil config pointer
23272343
func loadConfig(c *cli.Context, file string, kmsEncryptionContext map[string]*string) (*config.Config, error) {
23282344
var err error
23292345
configPath := c.GlobalString("config")
23302346
if configPath == "" {
2331-
// Ignore config not found errors returned from FindConfigFile since the config file is not mandatory
2332-
configPath, err = config.FindConfigFile(".")
2347+
// Ignore config not found errors returned from findConfigFile since the config file is not mandatory
2348+
configPath, err = findConfigFile()
23332349
if err != nil {
23342350
// If we can't find a config file, but we were not explicitly requested to, assume it does not exist
23352351
return nil, nil

config/config.go

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,48 @@ func (fs osFS) Stat(name string) (os.FileInfo, error) {
3737
var fs fileSystem = osFS{stat: os.Stat}
3838

3939
const (
40-
maxDepth = 100
41-
configFileName = ".sops.yaml"
40+
maxDepth = 100
41+
configFileName = ".sops.yaml"
42+
misspelledConfigFilename = ".sops.yml"
4243
)
4344

44-
// FindConfigFile looks for a sops config file in the current working directory and on parent directories, up to the limit defined by the maxDepth constant.
45-
func FindConfigFile(start string) (string, error) {
45+
// FindConfigFileEx looks for a sops config file in the current working directory and on parent directories, up to the limit defined by the maxDepth constant.
46+
// The third return value is a warning in case misspelled config files were found.
47+
func FindConfigFileEx(start string) (string, error, string) {
4648
filepath := path.Dir(start)
49+
var foundMisspelledPath string
50+
var warning string
4751
for i := 0; i < maxDepth; i++ {
48-
_, err := fs.Stat(path.Join(filepath, configFileName))
52+
configPath := path.Join(filepath, configFileName)
53+
_, err := fs.Stat(configPath)
4954
if err != nil {
55+
misspelledPath := path.Join(filepath, misspelledConfigFilename)
56+
_, err = fs.Stat(misspelledPath)
57+
if err == nil && len(foundMisspelledPath) == 0 {
58+
foundMisspelledPath = misspelledPath
59+
}
5060
filepath = path.Join(filepath, "..")
5161
} else {
52-
return path.Join(filepath, configFileName), nil
62+
if len(foundMisspelledPath) > 0 {
63+
warning = fmt.Sprintf(
64+
"Ignoring %q when searching for config file. The config file must be called %q."+
65+
" Found and using %q further up the directory tree instead.",
66+
foundMisspelledPath, configFileName, configPath)
67+
}
68+
return configPath, nil, warning
5369
}
5470
}
55-
return "", fmt.Errorf("Config file not found")
71+
err := fmt.Errorf("Config file not found")
72+
if len(foundMisspelledPath) > 0 {
73+
warning = fmt.Sprintf("Ignoring %q when searching for config file. The config file must be called %q.", foundMisspelledPath, configFileName)
74+
}
75+
return "", err, warning
76+
}
77+
78+
// FindConfigFile looks for a sops config file in the current working directory and on parent directories, up to the limit defined by the maxDepth constant.
79+
func FindConfigFile(start string) (string, error) {
80+
config, err, _ := FindConfigFileEx(start)
81+
return config, err
5682
}
5783

5884
type DotenvStoreConfig struct{}

0 commit comments

Comments
 (0)