From bc597e6b5ecf06a31c6a637d04d66b7e1120c564 Mon Sep 17 00:00:00 2001 From: Justin Chadwell Date: Wed, 14 Jun 2023 12:17:32 +0100 Subject: [PATCH 1/2] bake: fix incorrect dockerfile resolution against cwd:// context We need to resolve the strip the cwd:// prefix before attempting to resolve the dockerfile. Otherwise, we'll get the cwd:// prefix in the dockerfile name, which isn't stripped out later. Signed-off-by: Justin Chadwell (cherry picked from commit 431732f5d1f029f14adbf5828ae9798c0b9b3936) --- bake/bake.go | 6 +++--- bake/bake_test.go | 15 ++++++++------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/bake/bake.go b/bake/bake.go index 39cd927e..0fac4e3a 100644 --- a/bake/bake.go +++ b/bake/bake.go @@ -1048,12 +1048,12 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) { bi.DockerfileInline = *t.DockerfileInline } updateContext(&bi, inp) - if !build.IsRemoteURL(bi.ContextPath) && bi.ContextState == nil && !path.IsAbs(bi.DockerfilePath) { - bi.DockerfilePath = path.Join(bi.ContextPath, bi.DockerfilePath) - } if strings.HasPrefix(bi.ContextPath, "cwd://") { bi.ContextPath = path.Clean(strings.TrimPrefix(bi.ContextPath, "cwd://")) } + if !build.IsRemoteURL(bi.ContextPath) && bi.ContextState == nil && !path.IsAbs(bi.DockerfilePath) { + bi.DockerfilePath = path.Join(bi.ContextPath, bi.DockerfilePath) + } for k, v := range bi.NamedContexts { if strings.HasPrefix(v.Path, "cwd://") { bi.NamedContexts[k] = build.NamedContext{Path: path.Clean(strings.TrimPrefix(v.Path, "cwd://"))} diff --git a/bake/bake_test.go b/bake/bake_test.go index efebea20..75306cb2 100644 --- a/bake/bake_test.go +++ b/bake/bake_test.go @@ -386,18 +386,19 @@ func TestHCLCwdPrefix(t *testing.T) { m, g, err := ReadTargets(ctx, []File{fp}, []string{"app"}, nil, nil) require.NoError(t, err) - require.Equal(t, 1, len(m)) - _, ok := m["app"] - require.True(t, ok) - - _, err = TargetsToBuildOpt(m, &Input{}) + bo, err := TargetsToBuildOpt(m, &Input{}) require.NoError(t, err) + require.Equal(t, 1, len(g)) + require.Equal(t, []string{"app"}, g["default"].Targets) + + require.Equal(t, 1, len(m)) + require.Contains(t, m, "app") require.Equal(t, "test", *m["app"].Dockerfile) require.Equal(t, "foo", *m["app"].Context) - require.Equal(t, 1, len(g)) - require.Equal(t, []string{"app"}, g["default"].Targets) + require.Equal(t, "foo/test", bo["app"].Inputs.DockerfilePath) + require.Equal(t, "foo", bo["app"].Inputs.ContextPath) } func TestOverrideMerge(t *testing.T) { From 9f844df9f761f7c20c99a75dcdd60772828daee2 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Wed, 14 Jun 2023 12:07:31 +0200 Subject: [PATCH 2/2] builder: skip name validation for docker context Although a builder from the store cannot be created unless it has a valid name, this is not the case for a Docker context. We should skip name validation when checking a node from the store and fall back to finding one from Docker context instead. Signed-off-by: CrazyMax (cherry picked from commit b1c5449428436271eaa71b143e9682372ec238a4) --- store/store_test.go | 23 +++++++++++++++++++++++ store/storeutil/storeutil.go | 2 +- store/util.go | 21 ++++++++++++++++++++- 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/store/store_test.go b/store/store_test.go index f116ba14..84584564 100644 --- a/store/store_test.go +++ b/store/store_test.go @@ -235,3 +235,26 @@ func TestNodeManagement(t *testing.T) { require.NotNil(t, ng) require.Equal(t, "mybuild", ng.Name) } + +func TestNodeInvalidName(t *testing.T) { + t.Parallel() + tmpdir := t.TempDir() + + s, err := New(tmpdir) + require.NoError(t, err) + + txn, release, err := s.Txn() + require.NoError(t, err) + defer release() + + _, err = txn.NodeGroupByName("123builder") + require.Error(t, err) + require.True(t, IsErrInvalidName(err)) + + err = txn.Save(&NodeGroup{ + Name: "123builder", + Driver: "mydriver", + }) + require.Error(t, err) + require.True(t, IsErrInvalidName(err)) +} diff --git a/store/storeutil/storeutil.go b/store/storeutil/storeutil.go index ce01d385..41e7c264 100644 --- a/store/storeutil/storeutil.go +++ b/store/storeutil/storeutil.go @@ -74,7 +74,7 @@ func GetCurrentInstance(txn *store.Txn, dockerCli command.Cli) (*store.NodeGroup func GetNodeGroup(txn *store.Txn, dockerCli command.Cli, name string) (*store.NodeGroup, error) { ng, err := txn.NodeGroupByName(name) if err != nil { - if !os.IsNotExist(errors.Cause(err)) { + if !os.IsNotExist(errors.Cause(err)) && !store.IsErrInvalidName(err) { return nil, err } } diff --git a/store/util.go b/store/util.go index 278b64c3..269ca4a0 100644 --- a/store/util.go +++ b/store/util.go @@ -11,9 +11,28 @@ import ( var namePattern = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9\.\-_]*$`) +type errInvalidName struct { + error +} + +func (e *errInvalidName) Error() string { + return e.error.Error() +} + +func (e *errInvalidName) Unwrap() error { + return e.error +} + +func IsErrInvalidName(err error) bool { + _, ok := err.(*errInvalidName) + return ok +} + func ValidateName(s string) (string, error) { if !namePattern.MatchString(s) { - return "", errors.Errorf("invalid name %s, name needs to start with a letter and may not contain symbols, except ._-", s) + return "", &errInvalidName{ + errors.Errorf("invalid name %s, name needs to start with a letter and may not contain symbols, except ._-", s), + } } return strings.ToLower(s), nil }