0
Fork 0
mirror of https://github.com/project-zot/zot.git synced 2024-12-30 22:34:13 -05:00

Target for cheking not commited config files.

Signed-off-by: laurentiuNiculae <themelopeus@gmail.com>

Separated updateDistSpec functionality

Removed rewriting of config when distSpecVersion was wrong
This commit is contained in:
laurentiuNiculae 2022-03-30 16:02:03 +03:00 committed by Ramkumar Chinchani
parent efc55b013e
commit 0d4cc8736d
4 changed files with 79 additions and 90 deletions

View file

@ -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:

View file

@ -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

View file

@ -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
}

View file

@ -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)
})
})
}