Skip to content

Commit dc888d5

Browse files
authored
Merge pull request #1820 from felixfontein/warn-sops-yml
Warn about `.sops.yml` files found while searching for `.sops.yaml`
2 parents 8c91a3b + 6d6e72c commit dc888d5

File tree

2 files changed

+77
-18
lines changed

2 files changed

+77
-18
lines changed

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.LookupConfigFile that takes care of handling the returned warning.
2193+
func findConfigFile() (string, error) {
2194+
result, err := config.LookupConfigFile(".")
2195+
if len(result.Warning) > 0 && !showedConfigFileWarning {
2196+
showedConfigFileWarning = true
2197+
log.Warn(result.Warning)
2198+
}
2199+
return result.Path, 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: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,65 @@ 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+
alternateConfigName = ".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+
// ConfigFileResult contains the path to a config file and any warnings
46+
type ConfigFileResult struct {
47+
Path string
48+
Warning string
49+
}
50+
51+
// LookupConfigFile looks for a sops config file in the current working directory
52+
// and on parent directories, up to the maxDepth limit.
53+
// It returns a result containing the file path and any warnings.
54+
func LookupConfigFile(start string) (ConfigFileResult, error) {
4655
filepath := path.Dir(start)
56+
var foundAlternatePath string
57+
4758
for i := 0; i < maxDepth; i++ {
48-
_, err := fs.Stat(path.Join(filepath, configFileName))
49-
if err != nil {
50-
filepath = path.Join(filepath, "..")
51-
} else {
52-
return path.Join(filepath, configFileName), nil
59+
configPath := path.Join(filepath, configFileName)
60+
_, err := fs.Stat(configPath)
61+
if err == nil {
62+
result := ConfigFileResult{Path: configPath}
63+
64+
if foundAlternatePath != "" {
65+
result.Warning = fmt.Sprintf(
66+
"ignoring %q when searching for config file; the config file must be called %q; using %q instead",
67+
foundAlternatePath, configFileName, configPath)
68+
}
69+
return result, nil
5370
}
71+
72+
// Check for alternate filename if we haven't found one yet
73+
if foundAlternatePath == "" {
74+
alternatePath := path.Join(filepath, alternateConfigName)
75+
_, altErr := fs.Stat(alternatePath)
76+
if altErr == nil {
77+
foundAlternatePath = alternatePath
78+
}
79+
}
80+
81+
filepath = path.Join(filepath, "..")
5482
}
55-
return "", fmt.Errorf("Config file not found")
83+
84+
// No config file found
85+
result := ConfigFileResult{}
86+
if foundAlternatePath != "" {
87+
result.Warning = fmt.Sprintf(
88+
"ignoring %q when searching for config file; the config file must be called %q",
89+
foundAlternatePath, configFileName)
90+
}
91+
92+
return result, fmt.Errorf("config file not found")
93+
}
94+
95+
// 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.
96+
func FindConfigFile(start string) (string, error) {
97+
result, err := LookupConfigFile(start)
98+
return result.Path, err
5699
}
57100

58101
type DotenvStoreConfig struct{}

0 commit comments

Comments
 (0)