Skip to content

Commit

Permalink
feat(config): handle config files with no explicit extension
Browse files Browse the repository at this point in the history
Signed-off-by: Petu Eusebiu <[email protected]>
  • Loading branch information
eusebiu-constantin-petu-dbk committed Dec 20, 2023
1 parent 59f41ac commit c73f704
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
34 changes: 30 additions & 4 deletions pkg/cli/server/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
Expand All @@ -23,6 +24,7 @@ import (
"zotregistry.io/zot/pkg/api"
"zotregistry.io/zot/pkg/api/config"
"zotregistry.io/zot/pkg/api/constants"
"zotregistry.io/zot/pkg/common"
extconf "zotregistry.io/zot/pkg/extensions/config"
"zotregistry.io/zot/pkg/extensions/monitoring"
zlog "zotregistry.io/zot/pkg/log"
Expand Down Expand Up @@ -736,12 +738,36 @@ func LoadConfiguration(config *config.Config, configPath string) error {
// we need another key delimiter.
viperInstance := viper.NewWithOptions(viper.KeyDelimiter("::"))

viperInstance.SetConfigFile(configPath)
ext := filepath.Ext(configPath)

if err := viperInstance.ReadInConfig(); err != nil {
log.Error().Err(err).Msg("failed to read configuration")
/* if file extension is not supported, try everything
it's also possible that the filename is starting with a dot eg: ".config". */
if !common.Contains(viper.SupportedExts, ext) {
ext = ""
}

return err
switch ext {
case "":
log.Info().Msg("config file with no extension, trying all supported config types")

for _, configType := range viper.SupportedExts {
viperInstance.SetConfigType(configType)
viperInstance.SetConfigFile(configPath)

err := viperInstance.ReadInConfig()
if err == nil {
break
}

log.Debug().Err(err).Str("type", configType).Msg("failed to read configuration with no extension")
}
default:
viperInstance.SetConfigFile(configPath)
if err := viperInstance.ReadInConfig(); err != nil {

Check failure on line 766 in pkg/cli/server/root.go

View workflow job for this annotation

GitHub Actions / lint

if statements should only be cuddled with assignments (wsl)
log.Error().Err(err).Str("path", configPath).Msg("failed to read configuration")

return err
}

Check warning on line 770 in pkg/cli/server/root.go

View check run for this annotation

Codecov / codecov/patch

pkg/cli/server/root.go#L764-L770

Added lines #L764 - L770 were not covered by tests
}

metaData := &mapstructure.Metadata{}
Expand Down
31 changes: 31 additions & 0 deletions pkg/cli/server/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,37 @@ func TestVerify(t *testing.T) {
So(err, ShouldNotBeNil)
})

Convey("Test verify config with no extension", t, func(c C) {
tmpfile, err := os.CreateTemp("", "zot-test*")
So(err, ShouldBeNil)
defer os.Remove(tmpfile.Name()) // clean up
content := []byte(`{"log":{}}`)
_, err = tmpfile.Write(content)
So(err, ShouldBeNil)
err = tmpfile.Close()
So(err, ShouldBeNil)
os.Args = []string{"cli_test", "verify", tmpfile.Name()}
err = cli.NewServerRootCmd().Execute()
So(err, ShouldNotBeNil)
})

Convey("Test verify config with dotted config name", t, func(c C) {
tmpfile, err := os.CreateTemp("", ".zot-test*")
So(err, ShouldBeNil)
defer os.Remove(tmpfile.Name()) // clean up
content := []byte(`
log:
level: debug
`)
_, err = tmpfile.Write(content)
So(err, ShouldBeNil)
err = tmpfile.Close()
So(err, ShouldBeNil)
os.Args = []string{"cli_test", "verify", tmpfile.Name()}
err = cli.NewServerRootCmd().Execute()
So(err, ShouldNotBeNil)
})

Convey("Test verify CVE warn for remote storage", t, func(c C) {
tmpfile, err := os.CreateTemp("", "zot-test*.json")
So(err, ShouldBeNil)
Expand Down

0 comments on commit c73f704

Please sign in to comment.