Merge pull request #1879 from crazy-max/fix-ctx-validation

builder: skip name validation for docker context
pull/1891/head
CrazyMax 2 years ago committed by GitHub
commit daba16f4be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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

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

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

Loading…
Cancel
Save