diff --git a/examples/config-allextensions.json b/examples/config-allextensions.json index b811ace1..0adaa9f4 100644 --- a/examples/config-allextensions.json +++ b/examples/config-allextensions.json @@ -1,5 +1,5 @@ { - "version": "0.1.0-dev", + "distSpecVersion": "1.0.0", "storage": { "rootDirectory": "/tmp/zot" }, diff --git a/examples/config-bearer-auth.json b/examples/config-bearer-auth.json index e4af52c0..65d265b5 100644 --- a/examples/config-bearer-auth.json +++ b/examples/config-bearer-auth.json @@ -1,5 +1,5 @@ { - "version":"0.1.0-dev", + "distSpecVersion":"1.0.0", "storage":{ "rootDirectory":"/tmp/zot" }, diff --git a/examples/config-commit.json b/examples/config-commit.json index 4b44c170..7570e945 100644 --- a/examples/config-commit.json +++ b/examples/config-commit.json @@ -1,5 +1,5 @@ { - "version": "0.1.0-dev", + "distSpecVersion": "1.0.0", "storage": { "rootDirectory": "/tmp/zot", "commit": true diff --git a/examples/config-conformance.json b/examples/config-conformance.json index c44c4ff9..5a6fd924 100644 --- a/examples/config-conformance.json +++ b/examples/config-conformance.json @@ -1,5 +1,5 @@ { - "version":"0.1.0-dev", + "distSpecVersion":"1.0.0", "storage":{ "rootDirectory":"/tmp/zot", "gc": false, diff --git a/examples/config-cve.json b/examples/config-cve.json index d55c6813..80810724 100644 --- a/examples/config-cve.json +++ b/examples/config-cve.json @@ -1,5 +1,5 @@ { - "version": "0.1.0-dev", + "distSpecVersion": "1.0.0", "storage": { "rootDirectory": "/tmp/zot" }, diff --git a/examples/config-example.json b/examples/config-example.json index a26a61e3..fce65db4 100644 --- a/examples/config-example.json +++ b/examples/config-example.json @@ -1,5 +1,5 @@ { - "version":"0.1.0-dev", + "distSpecVersion":"1.0.0", "storage":{ "rootDirectory":"/tmp/zot" }, diff --git a/examples/config-example.yaml b/examples/config-example.yaml index 0a3741b7..60292072 100644 --- a/examples/config-example.yaml +++ b/examples/config-example.yaml @@ -1,5 +1,5 @@ --- -version: +distSpecVersion: 0.1.0-dev storage: rootDirectory: /tmp/zot diff --git a/examples/config-gc.json b/examples/config-gc.json index 5baf4822..d1b53e2c 100644 --- a/examples/config-gc.json +++ b/examples/config-gc.json @@ -1,5 +1,5 @@ { - "version": "0.1.0-dev", + "distSpecVersion": "1.0.0", "storage": { "rootDirectory": "/tmp/zot", "gc": true, diff --git a/examples/config-metrics.json b/examples/config-metrics.json index fd81a1c7..2ddcbf6a 100644 --- a/examples/config-metrics.json +++ b/examples/config-metrics.json @@ -1,5 +1,5 @@ { - "version": "0.1.0-dev", + "distSpecVersion": "1.0.0", "storage": { "rootDirectory": "/tmp/zot" }, diff --git a/examples/config-minimal.json b/examples/config-minimal.json index 5b6bc60d..007e7a24 100644 --- a/examples/config-minimal.json +++ b/examples/config-minimal.json @@ -1,5 +1,5 @@ { - "version": "0.1.0-dev", + "distSpecVersion": "1.0.0", "storage": { "rootDirectory": "/tmp/zot" }, diff --git a/examples/config-multiple-cve.json b/examples/config-multiple-cve.json index f3c58820..39547d8d 100644 --- a/examples/config-multiple-cve.json +++ b/examples/config-multiple-cve.json @@ -1,5 +1,5 @@ { - "version": "0.1.0-dev", + "distSpecVersion": "1.0.0", "storage": { "rootDirectory": "/tmp/zot", "dedupe": true, diff --git a/examples/config-multiple.json b/examples/config-multiple.json index bf005425..deb5dfdc 100644 --- a/examples/config-multiple.json +++ b/examples/config-multiple.json @@ -1,5 +1,5 @@ { - "version": "0.1.0-dev", + "distSpecVersion": "1.0.0", "storage": { "rootDirectory": "/tmp/zot", "dedupe": true, diff --git a/examples/config-policy.json b/examples/config-policy.json index 6d68196b..0f63fa03 100644 --- a/examples/config-policy.json +++ b/examples/config-policy.json @@ -1,5 +1,5 @@ { - "version": "0.1.0-dev", + "distSpecVersion": "1.0.0", "storage": { "rootDirectory": "/tmp/zot" }, diff --git a/examples/config-ratelimit.json b/examples/config-ratelimit.json index d661fa87..1bad1a64 100644 --- a/examples/config-ratelimit.json +++ b/examples/config-ratelimit.json @@ -1,5 +1,5 @@ { - "version": "0.1.0-dev", + "distSpecVersion": "1.0.0", "storage": { "rootDirectory": "/tmp/zot" }, diff --git a/examples/config-s3.json b/examples/config-s3.json index 69890e0e..62e33372 100644 --- a/examples/config-s3.json +++ b/examples/config-s3.json @@ -1,5 +1,5 @@ { - "version": "0.1.0-dev", + "distSpecVersion": "1.0.0", "storage": { "rootDirectory": "/zot", "storageDriver": { diff --git a/examples/config-sync.json b/examples/config-sync.json index 62aaa3cd..f06f95a3 100644 --- a/examples/config-sync.json +++ b/examples/config-sync.json @@ -1,5 +1,5 @@ { - "version":"0.1.0-dev", + "distSpecVersion":"1.0.0", "storage":{ "rootDirectory":"/tmp/zot" }, diff --git a/examples/config-test.json b/examples/config-test.json index dd306b46..324cb21a 100644 --- a/examples/config-test.json +++ b/examples/config-test.json @@ -1,5 +1,5 @@ { - "version":"0.1.0-dev", + "distSpecVersion":"1.0.0", "storage":{ "rootDirectory":"/tmp/zot" }, diff --git a/examples/config-tls.json b/examples/config-tls.json index 9e4a1b9b..3d6eba63 100644 --- a/examples/config-tls.json +++ b/examples/config-tls.json @@ -1,5 +1,5 @@ { - "version":"0.1.0-dev", + "distSpecVersion":"1.0.0", "storage":{ "rootDirectory":"/tmp/zot" }, diff --git a/go.mod b/go.mod index 3476cb5b..ee9180df 100644 --- a/go.mod +++ b/go.mod @@ -33,7 +33,6 @@ require ( github.com/notaryproject/notation v0.7.0-alpha.1 github.com/notaryproject/notation-go-lib v1.0.0-alpha-1 github.com/olekukonko/tablewriter v0.0.5 - github.com/opencontainers/distribution-spec/specs-go v0.0.0-20220217185014-dd38b7ed8a99 github.com/opencontainers/go-digest v1.0.0 github.com/opencontainers/image-spec v1.0.3-0.20220114050600-8b9d41f48198 github.com/opencontainers/umoci v0.4.8-0.20210922062158-e60a0cc726e6 @@ -287,6 +286,7 @@ require ( github.com/oklog/run v1.1.0 // indirect github.com/oklog/ulid v1.3.1 // indirect github.com/open-policy-agent/opa v0.37.0 // indirect + github.com/opencontainers/distribution-spec v1.0.0 github.com/opencontainers/runc v1.1.0 // indirect github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417 // indirect github.com/opentracing/opentracing-go v1.2.0 // indirect diff --git a/go.sum b/go.sum index dc7e7ba9..9f2f06b5 100644 --- a/go.sum +++ b/go.sum @@ -2023,8 +2023,8 @@ github.com/open-policy-agent/opa v0.32.0/go.mod h1:5sJdtc+1/U8zy/j30njpQl6u9rM4M github.com/open-policy-agent/opa v0.35.0/go.mod h1:xEmekKlk6/c+so5HF9wtPnGPXDfBuBsrMGhSHOHEF+U= github.com/open-policy-agent/opa v0.37.0 h1:OUXB+RAcxQpmXeNW2BN1wYzQQvVCPF1T9zv+QXGr9Wg= github.com/open-policy-agent/opa v0.37.0/go.mod h1:xX3NUCZuXK8f0CNhFQvhm4495mZLptf94pIkWRLaFqo= -github.com/opencontainers/distribution-spec/specs-go v0.0.0-20220217185014-dd38b7ed8a99 h1:yGqpYh1h3ZXxGg4Mqd5ZwhlwQ9wXOgvuodxbxJA7iMY= -github.com/opencontainers/distribution-spec/specs-go v0.0.0-20220217185014-dd38b7ed8a99/go.mod h1:aA4vdXRS8E1TG7pLZOz85InHi3BiPdErh8IpJN6E0x4= +github.com/opencontainers/distribution-spec v1.0.0 h1:3qi5mXdKrXCacBYm8y46okmGmCR0pSgfaEd2/T0QXWE= +github.com/opencontainers/distribution-spec v1.0.0/go.mod h1:copR2flp+jTEvQIFMb6MIx45OkrxzqyjszPDT3hx/5Q= github.com/opencontainers/go-digest v0.0.0-20170106003457-a6d0ee40d420/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s= github.com/opencontainers/go-digest v0.0.0-20180430190053-c9281466c8b2/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s= github.com/opencontainers/go-digest v1.0.0-rc1/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s= diff --git a/pkg/api/config/config.go b/pkg/api/config/config.go index 1ad3347a..ab36a087 100644 --- a/pkg/api/config/config.go +++ b/pkg/api/config/config.go @@ -122,26 +122,26 @@ type Policy struct { } type Config struct { - Version string - GoVersion string - Commit string - BinaryType string - AccessControl *AccessControlConfig - Storage GlobalStorageConfig - HTTP HTTPConfig - Log *LogConfig - Extensions *extconf.ExtensionConfig + DistSpecVersion string `json:"distSpecVersion" mapstructure:"distSpecVersion"` + GoVersion string + Commit string + BinaryType string + AccessControl *AccessControlConfig + Storage GlobalStorageConfig + HTTP HTTPConfig + Log *LogConfig + Extensions *extconf.ExtensionConfig } func New() *Config { return &Config{ - Version: distspec.Version, - GoVersion: GoVersion, - Commit: Commit, - BinaryType: BinaryType, - Storage: GlobalStorageConfig{GC: true, GCDelay: storage.DefaultGCDelay, Dedupe: true}, - HTTP: HTTPConfig{Address: "127.0.0.1", Port: "8080"}, - Log: &LogConfig{Level: "debug"}, + DistSpecVersion: distspec.Version, + GoVersion: GoVersion, + Commit: Commit, + BinaryType: BinaryType, + Storage: GlobalStorageConfig{GC: true, GCDelay: storage.DefaultGCDelay, Dedupe: true}, + HTTP: HTTPConfig{Address: "127.0.0.1", Port: "8080"}, + Log: &LogConfig{Level: "debug"}, } } diff --git a/pkg/api/controller.go b/pkg/api/controller.go index 946d6c8e..a1fce970 100644 --- a/pkg/api/controller.go +++ b/pkg/api/controller.go @@ -156,7 +156,9 @@ func (c *Controller) Run() error { return err } - monitoring.SetServerInfo(c.Metrics, c.Config.Commit, c.Config.BinaryType, c.Config.GoVersion, c.Config.Version) + monitoring.SetServerInfo(c.Metrics, c.Config.Commit, c.Config.BinaryType, c.Config.GoVersion, + c.Config.DistSpecVersion) + _ = NewRouteHandler(c) addr := fmt.Sprintf("%s:%s", c.Config.HTTP.Address, c.Config.HTTP.Port) diff --git a/pkg/cli/root.go b/pkg/cli/root.go index c1faeed7..103f4dd3 100644 --- a/pkg/cli/root.go +++ b/pkg/cli/root.go @@ -363,6 +363,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 + } } func LoadConfiguration(config *config.Config, configPath string) { diff --git a/pkg/cli/root_test.go b/pkg/cli/root_test.go index a8ef71ff..866aca8b 100644 --- a/pkg/cli/root_test.go +++ b/pkg/cli/root_test.go @@ -10,6 +10,7 @@ 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" @@ -282,7 +283,7 @@ func TestVerify(t *testing.T) { tmpfile, err := ioutil.TempFile("", "zot-test*.json") So(err, ShouldBeNil) defer os.Remove(tmpfile.Name()) // clean up - content := []byte(`{"version": "0.1.0-dev", "storage": {"rootDirectory": "/tmp/zot"}, + content := []byte(`{"distSpecVersion": "1.0.0", "storage": {"rootDirectory": "/tmp/zot"}, "http": {"address": "127.0.0.1", "port": "8080", "ReadOnly": false}, "log": {"level": "debug"}}`) _, err = tmpfile.Write(content) @@ -527,3 +528,51 @@ 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) + + cli.LoadConfiguration(oldConfig, file.Name()) + + 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) + + cli.LoadConfiguration(oldConfig, file.Name()) + + 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) + }) + }) +} diff --git a/test/cluster/config-minio.json b/test/cluster/config-minio.json index 49035a89..9da67e6e 100644 --- a/test/cluster/config-minio.json +++ b/test/cluster/config-minio.json @@ -1,5 +1,5 @@ { - "version": "0.1.0-dev", + "distSpecVersion": "1.0.0", "storage": { "rootDirectory": "/zot", "gc": false,