diff --git a/Makefile b/Makefile index 98aa887b..4d3f14fc 100644 --- a/Makefile +++ b/Makefile @@ -150,8 +150,31 @@ run: binary test ./bin/zot-$(OS)-$(ARCH) serve examples/config-test.json .PHONY: verify-config -verify-config: binary - $(foreach file, $(wildcard examples/config-*), ./bin/zot-$(OS)-$(ARCH) verify $(file) || exit 1;) +verify-config: _verify-config verify-config-warnings verify-config-commited + +.PHONY: _verify-config +_verify-config: binary + rm -f output.txt + $(foreach file, $(wildcard examples/config-*), ./bin/zot-$(OS)-$(ARCH) verify $(file) 2>&1 | tee -a output.txt || exit 1;) + +.PHONY: verify-config-warnings +verify-config-warnings: _verify-config + $(eval WARNINGS = $(shell cat output.txt | grep -c '"warn"')) + $(eval ERRORS = $(shell cat output.txt | grep -c '"error"')) + @if [ $(WARNINGS) != 0 ] || [ $(ERRORS) != 0 ]; then \ + echo "verify-config-warnings: warnings or errors found while verifying configs"; \ + rm output.txt; \ + exit 1; \ + fi + rm -f output.txt + +.PHONY: verify-config-commited +verify-config-commited: _verify-config + $(eval UNCOMMITED_FILES = $(shell git status --porcelain | grep -c examples/config-)) + @if [ $(UNCOMMITED_FILES) != 0 ]; then \ + echo "Uncommited config files, make sure all config files are commited. Verify might have changed a config file.";\ + exit 1;\ + fi; \ .PHONY: binary-container binary-container: diff --git a/examples/config-example.yaml b/examples/config-example.yaml index 60292072..818f4818 100644 --- a/examples/config-example.yaml +++ b/examples/config-example.yaml @@ -1,32 +1,29 @@ ---- -distSpecVersion: - 0.1.0-dev -storage: - rootDirectory: /tmp/zot +distspecversion: 1.0.1 http: address: 127.0.0.1 + allowreadaccess: false + auth: + faildelay: 5 + htpasswd: + path: test/data/htpasswd + ldap: + address: ldap.example.org + basedn: ou=Users,dc=example,dc=org + binddn: cn=ldap-searcher,ou=Users,dc=example,dc=org + bindpassword: ldap-searcher-password + port: 389 + skipverify: false + starttls: false + subtreesearch: true + userattribute: uid port: 8080 realm: zot tls: cert: test/data/server.cert key: test/data/server.key - auth: - ldap: - address: ldap.example.org - port: 389 - startTLS: false - baseDN: ou=Users,dc=example,dc=org - userAttribute: uid - bindDN: cn=ldap-searcher,ou=Users,dc=example,dc=org - bindPassword: ldap-searcher-password - skipVerify: false - subtreeSearch: true - htpasswd: - path: test/data/htpasswd - failDelay: 5 - allowReadAccess: false log: + audit: /tmp/zot-audit.log level: debug output: /tmp/zot.log - audit: /tmp/zot-audit.log - +storage: + rootdirectory: /tmp/zot diff --git a/pkg/cli/root.go b/pkg/cli/root.go index 0ce72804..7ed6d232 100644 --- a/pkg/cli/root.go +++ b/pkg/cli/root.go @@ -354,24 +354,23 @@ func applyDefaultValues(config *config.Config, viperInstance *viper.Viper) { } } - if config.DistSpecVersion != distspec.Version { - log.Warn().Err(errors.ErrBadConfig). - Msgf("config dist-spec version: %s differs from version actually used: %s, will be corrected automatically", - config.DistSpecVersion, distspec.Version) - - // rewrite the config file - viperInstance.Set("distSpecVersion", distspec.Version) - - err := viperInstance.WriteConfig() - if err != nil { - log.Warn().Err(errors.ErrBadConfig). - Msg("can't rewrite the config file") - } - - config.DistSpecVersion = distspec.Version + if !config.Storage.GC && viperInstance.Get("storage::gcdelay") == nil { + config.Storage.GCDelay = 0 } } +func updateDistSpecVersion(config *config.Config) { + if config.DistSpecVersion == distspec.Version { + return + } + + log.Warn(). + Msgf("config dist-spec version: %s differs from version actually used: %s", + config.DistSpecVersion, distspec.Version) + + config.DistSpecVersion = distspec.Version +} + func LoadConfiguration(config *config.Config, configPath string) error { // Default is dot (.) but because we allow glob patterns in authz // we need another key delimiter. @@ -413,5 +412,8 @@ func LoadConfiguration(config *config.Config, configPath string) error { return err } + // update distSpecVersion + updateDistSpecVersion(config) + return nil } diff --git a/pkg/cli/root_test.go b/pkg/cli/root_test.go index b2461415..e755b30c 100644 --- a/pkg/cli/root_test.go +++ b/pkg/cli/root_test.go @@ -10,7 +10,6 @@ import ( "testing" "time" - distspec "github.com/opencontainers/distribution-spec/specs-go" . "github.com/smartystreets/goconvey/convey" "gopkg.in/resty.v1" "zotregistry.io/zot/pkg/api" @@ -354,6 +353,24 @@ func TestGC(t *testing.T) { err = cli.LoadConfiguration(config, file.Name()) So(err, ShouldNotBeNil) }) + + Convey("GC delay when GC = false", func() { + config := config.New() + + file, err := ioutil.TempFile("", "gc-false-config-*.json") + So(err, ShouldBeNil) + defer os.Remove(file.Name()) + + content := []byte(`{"distSpecVersion": "1.0.0", "storage": {"rootDirectory": "/tmp/zot", + "gc": false}, "http": {"address": "127.0.0.1", "port": "8080", "ReadOnly": false}, + "log": {"level": "debug"}}`) + + err = ioutil.WriteFile(file.Name(), content, 0o600) + So(err, ShouldBeNil) + err = cli.LoadConfiguration(config, file.Name()) + So(err, ShouldBeNil) + So(config.Storage.GCDelay, ShouldEqual, 0) + }) }) } @@ -533,53 +550,3 @@ func TestScrub(t *testing.T) { }) }) } - -func TestApplyDefaultValues(t *testing.T) { - Convey("Test rewriting of config file if version is wrong", t, func() { - configContent := []byte(`{"distSpecVersion": "1.0.0555", "storage": {"rootDirectory": "/tmp/zot"}, - "http": {"address": "127.0.0.1", "port": "8080", "ReadOnly": false}, - "log": {"level": "debug"}} - `) - oldConfig := config.New() - - file, err := ioutil.TempFile("", "default-val-test-config*.json") - So(err, ShouldBeNil) - - defer os.Remove(file.Name()) - _, err = file.Write(configContent) - So(err, ShouldBeNil) - Convey("The file can be rewritten", func() { - err = os.Chmod(file.Name(), 0o777) - So(err, ShouldBeNil) - - err = cli.LoadConfiguration(oldConfig, file.Name()) - So(err, ShouldBeNil) - - configContent, err = ioutil.ReadFile(file.Name()) - So(err, ShouldBeNil) - - newConfig := config.New() - err = json.Unmarshal(configContent, newConfig) - So(err, ShouldBeNil) - So(newConfig.DistSpecVersion, ShouldEqual, distspec.Version) - So(newConfig.DistSpecVersion, ShouldEqual, distspec.Version) - }) - - Convey("The file can't be rewritten", func() { - err = os.Chmod(file.Name(), 0o444) - So(err, ShouldBeNil) - - err = cli.LoadConfiguration(oldConfig, file.Name()) - So(err, ShouldBeNil) - - configContent, err = ioutil.ReadFile(file.Name()) - So(err, ShouldBeNil) - - newConfig := config.New() - err = json.Unmarshal(configContent, newConfig) - So(err, ShouldBeNil) - So(newConfig.DistSpecVersion, ShouldEqual, "1.0.0555") - So(oldConfig.DistSpecVersion, ShouldEqual, distspec.Version) - }) - }) -}