Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

builder: skip name validation for docker context #1879

Merged
merged 1 commit into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
crazy-max marked this conversation as resolved.
Show resolved Hide resolved

err = txn.Save(&NodeGroup{
Name: "123builder",
Driver: "mydriver",
})
require.Error(t, err)
require.True(t, IsErrInvalidName(err))
}
2 changes: 1 addition & 1 deletion store/storeutil/storeutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
jedevc marked this conversation as resolved.
Show resolved Hide resolved
return nil, err
}
}
Expand Down
21 changes: 20 additions & 1 deletion store/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down