add //go:build directives to prevent downgrading to go1.16 language
This is a follow-up to 0e73168b7e6d1d029d76d05b843b1aaec46739a8
This repository is not yet a module (i.e., does not have a `go.mod`). This
is not problematic when building the code in GOPATH or "vendor" mode, but
when using the code as a module-dependency (in module-mode), different semantics
are applied since Go1.21, which switches Go _language versions_ on a per-module,
per-package, or even per-file base.
A condensed summary of that logic [is as follows][1]:
- For modules that have a go.mod containing a go version directive; that
version is considered a minimum _required_ version (starting with the
go1.19.13 and go1.20.8 patch releases: before those, it was only a
recommendation).
- For dependencies that don't have a go.mod (not a module), go language
version go1.16 is assumed.
- Likewise, for modules that have a go.mod, but the file does not have a
go version directive, go language version go1.16 is assumed.
- If a go.work file is present, but does not have a go version directive,
language version go1.17 is assumed.
When switching language versions, Go _downgrades_ the language version,
which means that language features (such as generics, and `any`) are not
available, and compilation fails. For example:
# github.com/docker/cli/cli/context/store
/go/pkg/mod/github.com/docker/cli@v25.0.0-beta.2+incompatible/cli/context/store/storeconfig.go:6:24: predeclared any requires go1.18 or later (-lang was set to go1.16; check go.mod)
/go/pkg/mod/github.com/docker/cli@v25.0.0-beta.2+incompatible/cli/context/store/store.go:74:12: predeclared any requires go1.18 or later (-lang was set to go1.16; check go.mod)
Note that these fallbacks are per-module, per-package, and can even be
per-file, so _(indirect) dependencies_ can still use modern language
features, as long as their respective go.mod has a version specified.
Unfortunately, these failures do not occur when building locally (using
vendor / GOPATH mode), but will affect consumers of the module.
Obviously, this situation is not ideal, and the ultimate solution is to
move to go modules (add a go.mod), but this comes with a non-insignificant
risk in other areas (due to our complex dependency tree).
We can revert to using go1.16 language features only, but this may be
limiting, and may still be problematic when (e.g.) matching signatures
of dependencies.
There is an escape hatch: adding a `//go:build` directive to files that
make use of go language features. From the [go toolchain docs][2]:
> The go line for each module sets the language version the compiler enforces
> when compiling packages in that module. The language version can be changed
> on a per-file basis by using a build constraint.
>
> For example, a module containing code that uses the Go 1.21 language version
> should have a `go.mod` file with a go line such as `go 1.21` or `go 1.21.3`.
> If a specific source file should be compiled only when using a newer Go
> toolchain, adding `//go:build go1.22` to that source file both ensures that
> only Go 1.22 and newer toolchains will compile the file and also changes
> the language version in that file to Go 1.22.
This patch adds `//go:build` directives to those files using recent additions
to the language. It's currently using go1.19 as version to match the version
in our "vendor.mod", but we can consider being more permissive ("any" requires
go1.18 or up), or more "optimistic" (force go1.21, which is the version we
currently use to build).
For completeness sake, note that any file _without_ a `//go:build` directive
will continue to use go1.16 language version when used as a module.
[1]: https://github.com/golang/go/blob/58c28ba286dd0e98fe4cca80f5d64bbcb824a685/src/cmd/go/internal/gover/version.go#L9-L56
[2]; https://go.dev/doc/toolchain#:~:text=The%20go%20line%20for,file%20to%20Go%201.22
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-12-14 07:51:57 -05:00
|
|
|
// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
|
2024-06-18 06:07:25 -04:00
|
|
|
//go:build go1.21
|
add //go:build directives to prevent downgrading to go1.16 language
This is a follow-up to 0e73168b7e6d1d029d76d05b843b1aaec46739a8
This repository is not yet a module (i.e., does not have a `go.mod`). This
is not problematic when building the code in GOPATH or "vendor" mode, but
when using the code as a module-dependency (in module-mode), different semantics
are applied since Go1.21, which switches Go _language versions_ on a per-module,
per-package, or even per-file base.
A condensed summary of that logic [is as follows][1]:
- For modules that have a go.mod containing a go version directive; that
version is considered a minimum _required_ version (starting with the
go1.19.13 and go1.20.8 patch releases: before those, it was only a
recommendation).
- For dependencies that don't have a go.mod (not a module), go language
version go1.16 is assumed.
- Likewise, for modules that have a go.mod, but the file does not have a
go version directive, go language version go1.16 is assumed.
- If a go.work file is present, but does not have a go version directive,
language version go1.17 is assumed.
When switching language versions, Go _downgrades_ the language version,
which means that language features (such as generics, and `any`) are not
available, and compilation fails. For example:
# github.com/docker/cli/cli/context/store
/go/pkg/mod/github.com/docker/cli@v25.0.0-beta.2+incompatible/cli/context/store/storeconfig.go:6:24: predeclared any requires go1.18 or later (-lang was set to go1.16; check go.mod)
/go/pkg/mod/github.com/docker/cli@v25.0.0-beta.2+incompatible/cli/context/store/store.go:74:12: predeclared any requires go1.18 or later (-lang was set to go1.16; check go.mod)
Note that these fallbacks are per-module, per-package, and can even be
per-file, so _(indirect) dependencies_ can still use modern language
features, as long as their respective go.mod has a version specified.
Unfortunately, these failures do not occur when building locally (using
vendor / GOPATH mode), but will affect consumers of the module.
Obviously, this situation is not ideal, and the ultimate solution is to
move to go modules (add a go.mod), but this comes with a non-insignificant
risk in other areas (due to our complex dependency tree).
We can revert to using go1.16 language features only, but this may be
limiting, and may still be problematic when (e.g.) matching signatures
of dependencies.
There is an escape hatch: adding a `//go:build` directive to files that
make use of go language features. From the [go toolchain docs][2]:
> The go line for each module sets the language version the compiler enforces
> when compiling packages in that module. The language version can be changed
> on a per-file basis by using a build constraint.
>
> For example, a module containing code that uses the Go 1.21 language version
> should have a `go.mod` file with a go line such as `go 1.21` or `go 1.21.3`.
> If a specific source file should be compiled only when using a newer Go
> toolchain, adding `//go:build go1.22` to that source file both ensures that
> only Go 1.22 and newer toolchains will compile the file and also changes
> the language version in that file to Go 1.22.
This patch adds `//go:build` directives to those files using recent additions
to the language. It's currently using go1.19 as version to match the version
in our "vendor.mod", but we can consider being more permissive ("any" requires
go1.18 or up), or more "optimistic" (force go1.21, which is the version we
currently use to build).
For completeness sake, note that any file _without_ a `//go:build` directive
will continue to use go1.16 language version when used as a module.
[1]: https://github.com/golang/go/blob/58c28ba286dd0e98fe4cca80f5d64bbcb824a685/src/cmd/go/internal/gover/version.go#L9-L56
[2]; https://go.dev/doc/toolchain#:~:text=The%20go%20line%20for,file%20to%20Go%201.22
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-12-14 07:51:57 -05:00
|
|
|
|
2018-12-17 05:27:07 -05:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
2019-06-05 12:12:34 -04:00
|
|
|
"archive/tar"
|
2019-05-14 16:14:39 -04:00
|
|
|
"archive/zip"
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2019-03-06 09:01:12 -05:00
|
|
|
"crypto/rand"
|
2019-05-14 16:14:39 -04:00
|
|
|
"encoding/json"
|
2023-02-20 19:51:57 -05:00
|
|
|
"fmt"
|
2019-05-14 16:14:39 -04:00
|
|
|
"io"
|
2018-12-17 05:27:07 -05:00
|
|
|
"os"
|
2019-05-14 16:14:39 -04:00
|
|
|
"path"
|
2023-02-20 19:51:57 -05:00
|
|
|
"path/filepath"
|
2018-12-17 05:27:07 -05:00
|
|
|
"testing"
|
|
|
|
|
2022-09-28 16:18:51 -04:00
|
|
|
"github.com/docker/docker/errdefs"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2022-09-28 16:18:51 -04:00
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2018-12-17 05:27:07 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type endpoint struct {
|
|
|
|
Foo string `json:"a_very_recognizable_field_name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type context struct {
|
|
|
|
Bar string `json:"another_very_recognizable_field_name"`
|
|
|
|
}
|
|
|
|
|
2023-11-20 12:04:36 -05:00
|
|
|
var testCfg = NewConfig(func() any { return &context{} },
|
|
|
|
EndpointTypeGetter("ep1", func() any { return &endpoint{} }),
|
|
|
|
EndpointTypeGetter("ep2", func() any { return &endpoint{} }),
|
2018-12-17 05:27:07 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestExportImport(t *testing.T) {
|
2022-02-25 08:37:07 -05:00
|
|
|
s := New(t.TempDir(), testCfg)
|
|
|
|
err := s.CreateOrUpdate(
|
2019-04-18 09:12:30 -04:00
|
|
|
Metadata{
|
2023-11-20 12:04:36 -05:00
|
|
|
Endpoints: map[string]any{
|
2018-12-17 05:27:07 -05:00
|
|
|
"ep1": endpoint{Foo: "bar"},
|
|
|
|
},
|
|
|
|
Metadata: context{Bar: "baz"},
|
2018-11-09 09:10:41 -05:00
|
|
|
Name: "source",
|
2018-12-17 05:27:07 -05:00
|
|
|
})
|
|
|
|
assert.NilError(t, err)
|
2019-03-06 09:01:12 -05:00
|
|
|
file1 := make([]byte, 1500)
|
|
|
|
rand.Read(file1)
|
|
|
|
file2 := make([]byte, 3700)
|
|
|
|
rand.Read(file2)
|
2019-04-18 09:12:30 -04:00
|
|
|
err = s.ResetEndpointTLSMaterial("source", "ep1", &EndpointTLSData{
|
2018-12-17 05:27:07 -05:00
|
|
|
Files: map[string][]byte{
|
2019-03-06 09:01:12 -05:00
|
|
|
"file1": file1,
|
|
|
|
"file2": file2,
|
2018-12-17 05:27:07 -05:00
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
r := Export("source", s)
|
|
|
|
defer r.Close()
|
|
|
|
err = Import("dest", s, r)
|
|
|
|
assert.NilError(t, err)
|
2019-04-18 09:12:30 -04:00
|
|
|
srcMeta, err := s.GetMetadata("source")
|
2018-12-17 05:27:07 -05:00
|
|
|
assert.NilError(t, err)
|
2019-04-18 09:12:30 -04:00
|
|
|
destMeta, err := s.GetMetadata("dest")
|
2018-12-17 05:27:07 -05:00
|
|
|
assert.NilError(t, err)
|
2018-11-09 09:10:41 -05:00
|
|
|
assert.DeepEqual(t, destMeta.Metadata, srcMeta.Metadata)
|
|
|
|
assert.DeepEqual(t, destMeta.Endpoints, srcMeta.Endpoints)
|
2019-04-18 09:12:30 -04:00
|
|
|
srcFileList, err := s.ListTLSFiles("source")
|
2018-12-17 05:27:07 -05:00
|
|
|
assert.NilError(t, err)
|
2019-04-18 09:12:30 -04:00
|
|
|
destFileList, err := s.ListTLSFiles("dest")
|
2018-12-17 05:27:07 -05:00
|
|
|
assert.NilError(t, err)
|
2019-03-06 09:01:12 -05:00
|
|
|
assert.Equal(t, 1, len(destFileList))
|
|
|
|
assert.Equal(t, 1, len(srcFileList))
|
|
|
|
assert.Equal(t, 2, len(destFileList["ep1"]))
|
|
|
|
assert.Equal(t, 2, len(srcFileList["ep1"]))
|
2019-04-18 09:12:30 -04:00
|
|
|
srcData1, err := s.GetTLSData("source", "ep1", "file1")
|
2018-12-17 05:27:07 -05:00
|
|
|
assert.NilError(t, err)
|
2019-03-06 09:01:12 -05:00
|
|
|
assert.DeepEqual(t, file1, srcData1)
|
2019-04-18 09:12:30 -04:00
|
|
|
srcData2, err := s.GetTLSData("source", "ep1", "file2")
|
2018-12-17 05:27:07 -05:00
|
|
|
assert.NilError(t, err)
|
2019-03-06 09:01:12 -05:00
|
|
|
assert.DeepEqual(t, file2, srcData2)
|
2019-04-18 09:12:30 -04:00
|
|
|
destData1, err := s.GetTLSData("dest", "ep1", "file1")
|
2019-03-06 09:01:12 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, file1, destData1)
|
2019-04-18 09:12:30 -04:00
|
|
|
destData2, err := s.GetTLSData("dest", "ep1", "file2")
|
2019-03-06 09:01:12 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, file2, destData2)
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemove(t *testing.T) {
|
2022-02-25 08:37:07 -05:00
|
|
|
s := New(t.TempDir(), testCfg)
|
|
|
|
err := s.CreateOrUpdate(
|
2019-04-18 09:12:30 -04:00
|
|
|
Metadata{
|
2023-11-20 12:04:36 -05:00
|
|
|
Endpoints: map[string]any{
|
2018-12-17 05:27:07 -05:00
|
|
|
"ep1": endpoint{Foo: "bar"},
|
|
|
|
},
|
|
|
|
Metadata: context{Bar: "baz"},
|
2018-11-09 09:10:41 -05:00
|
|
|
Name: "source",
|
2018-12-17 05:27:07 -05:00
|
|
|
})
|
|
|
|
assert.NilError(t, err)
|
2019-04-18 09:12:30 -04:00
|
|
|
assert.NilError(t, s.ResetEndpointTLSMaterial("source", "ep1", &EndpointTLSData{
|
2018-12-17 05:27:07 -05:00
|
|
|
Files: map[string][]byte{
|
|
|
|
"file1": []byte("test-data"),
|
|
|
|
},
|
|
|
|
}))
|
2019-04-18 09:12:30 -04:00
|
|
|
assert.NilError(t, s.Remove("source"))
|
|
|
|
_, err = s.GetMetadata("source")
|
2022-09-28 16:18:51 -04:00
|
|
|
assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
|
2019-04-18 09:12:30 -04:00
|
|
|
f, err := s.ListTLSFiles("source")
|
2018-12-17 05:27:07 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, 0, len(f))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListEmptyStore(t *testing.T) {
|
2022-02-25 08:37:07 -05:00
|
|
|
result, err := New(t.TempDir(), testCfg).List()
|
2018-12-17 05:27:07 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, len(result) == 0)
|
|
|
|
}
|
2018-11-09 09:10:41 -05:00
|
|
|
|
|
|
|
func TestErrHasCorrectContext(t *testing.T) {
|
2022-02-25 08:37:07 -05:00
|
|
|
_, err := New(t.TempDir(), testCfg).GetMetadata("no-exists")
|
2018-11-09 09:10:41 -05:00
|
|
|
assert.ErrorContains(t, err, "no-exists")
|
2022-09-28 16:18:51 -04:00
|
|
|
assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
|
2018-11-09 09:10:41 -05:00
|
|
|
}
|
2019-05-14 16:14:39 -04:00
|
|
|
|
|
|
|
func TestDetectImportContentType(t *testing.T) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
r := bufio.NewReader(buf)
|
|
|
|
ct, err := getImportContentType(r)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, zipType != ct)
|
|
|
|
}
|
|
|
|
|
2019-06-05 12:12:34 -04:00
|
|
|
func TestImportTarInvalid(t *testing.T) {
|
2022-02-25 08:37:07 -05:00
|
|
|
testDir := t.TempDir()
|
2019-06-05 12:12:34 -04:00
|
|
|
tf := path.Join(testDir, "test.context")
|
|
|
|
|
|
|
|
f, err := os.Create(tf)
|
|
|
|
assert.NilError(t, err)
|
2019-10-29 05:24:55 -04:00
|
|
|
defer f.Close()
|
2019-06-05 12:12:34 -04:00
|
|
|
|
|
|
|
tw := tar.NewWriter(f)
|
|
|
|
hdr := &tar.Header{
|
|
|
|
Name: "dummy-file",
|
2022-09-30 13:13:22 -04:00
|
|
|
Mode: 0o600,
|
2019-06-05 12:12:34 -04:00
|
|
|
Size: int64(len("hello world")),
|
|
|
|
}
|
|
|
|
err = tw.WriteHeader(hdr)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
_, err = tw.Write([]byte("hello world"))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
err = tw.Close()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
source, err := os.Open(tf)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer source.Close()
|
|
|
|
var r io.Reader = source
|
|
|
|
s := New(testDir, testCfg)
|
|
|
|
err = Import("tarInvalid", s, r)
|
2020-09-16 10:35:04 -04:00
|
|
|
assert.ErrorContains(t, err, "unexpected context file")
|
2019-06-05 12:12:34 -04:00
|
|
|
}
|
|
|
|
|
2019-05-14 16:14:39 -04:00
|
|
|
func TestImportZip(t *testing.T) {
|
2022-02-25 08:37:07 -05:00
|
|
|
testDir := t.TempDir()
|
2019-05-14 16:14:39 -04:00
|
|
|
zf := path.Join(testDir, "test.zip")
|
|
|
|
|
|
|
|
f, err := os.Create(zf)
|
|
|
|
assert.NilError(t, err)
|
2019-10-29 05:24:55 -04:00
|
|
|
defer f.Close()
|
2019-05-14 16:14:39 -04:00
|
|
|
w := zip.NewWriter(f)
|
|
|
|
|
|
|
|
meta, err := json.Marshal(Metadata{
|
2023-11-20 12:04:36 -05:00
|
|
|
Endpoints: map[string]any{
|
2019-05-14 16:14:39 -04:00
|
|
|
"ep1": endpoint{Foo: "bar"},
|
|
|
|
},
|
|
|
|
Metadata: context{Bar: "baz"},
|
|
|
|
Name: "source",
|
|
|
|
})
|
|
|
|
assert.NilError(t, err)
|
2022-09-28 16:18:51 -04:00
|
|
|
files := []struct {
|
2019-05-14 16:14:39 -04:00
|
|
|
Name, Body string
|
|
|
|
}{
|
|
|
|
{"meta.json", string(meta)},
|
|
|
|
{path.Join("tls", "docker", "ca.pem"), string([]byte("ca.pem"))},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
f, err := w.Create(file.Name)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
_, err = f.Write([]byte(file.Body))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = w.Close()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
source, err := os.Open(zf)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
ct, err := getImportContentType(bufio.NewReader(source))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, zipType, ct)
|
|
|
|
|
|
|
|
source, _ = os.Open(zf)
|
|
|
|
defer source.Close()
|
|
|
|
var r io.Reader = source
|
|
|
|
s := New(testDir, testCfg)
|
|
|
|
err = Import("zipTest", s, r)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
}
|
2019-06-05 12:12:34 -04:00
|
|
|
|
|
|
|
func TestImportZipInvalid(t *testing.T) {
|
2022-02-25 08:37:07 -05:00
|
|
|
testDir := t.TempDir()
|
2019-06-05 12:12:34 -04:00
|
|
|
zf := path.Join(testDir, "test.zip")
|
|
|
|
|
|
|
|
f, err := os.Create(zf)
|
|
|
|
assert.NilError(t, err)
|
2019-10-29 05:24:55 -04:00
|
|
|
defer f.Close()
|
2019-06-05 12:12:34 -04:00
|
|
|
w := zip.NewWriter(f)
|
|
|
|
|
|
|
|
df, err := w.Create("dummy-file")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
_, err = df.Write([]byte("hello world"))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
err = w.Close()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
source, err := os.Open(zf)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer source.Close()
|
|
|
|
var r io.Reader = source
|
|
|
|
s := New(testDir, testCfg)
|
|
|
|
err = Import("zipInvalid", s, r)
|
2020-09-16 10:35:04 -04:00
|
|
|
assert.ErrorContains(t, err, "unexpected context file")
|
2019-06-05 12:12:34 -04:00
|
|
|
}
|
2023-02-20 19:51:57 -05:00
|
|
|
|
|
|
|
func TestCorruptMetadata(t *testing.T) {
|
|
|
|
tempDir := t.TempDir()
|
|
|
|
s := New(tempDir, testCfg)
|
|
|
|
err := s.CreateOrUpdate(
|
|
|
|
Metadata{
|
2023-11-20 12:04:36 -05:00
|
|
|
Endpoints: map[string]any{
|
2023-02-20 19:51:57 -05:00
|
|
|
"ep1": endpoint{Foo: "bar"},
|
|
|
|
},
|
|
|
|
Metadata: context{Bar: "baz"},
|
|
|
|
Name: "source",
|
|
|
|
})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
// Simulate the meta.json file getting corrupted
|
|
|
|
// by some external process.
|
|
|
|
contextDir := s.meta.contextDir(contextdirOf("source"))
|
|
|
|
contextFile := filepath.Join(contextDir, metaFile)
|
|
|
|
err = os.WriteFile(contextFile, nil, 0o600)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
// Assert that the error message gives the user some clue where to look.
|
|
|
|
_, err = s.GetMetadata("source")
|
|
|
|
assert.ErrorContains(t, err, fmt.Sprintf("parsing %s: unexpected end of JSON input", contextFile))
|
|
|
|
}
|