Browse Source

Merge pull request #1631 from mavenugo/nstrict

Reverting the strict network name check
Santhosh Manohar 8 years ago
parent
commit
9e630d61d5

+ 5 - 13
libnetwork/config/config.go

@@ -1,8 +1,6 @@
 package config
 
 import (
-	"fmt"
-	"regexp"
 	"strings"
 
 	"github.com/BurntSushi/toml"
@@ -17,12 +15,6 @@ import (
 	"github.com/docker/libnetwork/osl"
 )
 
-// restrictedNameRegex represents the regular expression which regulates the allowed network or endpoint names.
-const restrictedNameRegex = `^[\w]+[\w-. ]*[\w]+$`
-
-// RestrictedNamePattern is a regular expression to validate names against the collection of restricted characters.
-var restrictedNamePattern = regexp.MustCompile(restrictedNameRegex)
-
 // Config encapsulates configurations of various Libnetwork components
 type Config struct {
 	Daemon          DaemonCfg
@@ -240,12 +232,12 @@ func (c *Config) ProcessOptions(options ...Option) {
 	}
 }
 
-// ValidateName validates configuration objects supported by libnetwork
-func ValidateName(name string) error {
-	if !restrictedNamePattern.MatchString(name) {
-		return fmt.Errorf("%q includes invalid characters, resource name has to conform to %q", name, restrictedNameRegex)
+// IsValidName validates configuration objects supported by libnetwork
+func IsValidName(name string) bool {
+	if strings.TrimSpace(name) == "" {
+		return false
 	}
-	return nil
+	return true
 }
 
 // OptionLocalKVProvider function returns an option setter for kvstore provider

+ 7 - 30
libnetwork/config/config_test.go

@@ -46,37 +46,14 @@ func TestOptionsLabels(t *testing.T) {
 }
 
 func TestValidName(t *testing.T) {
-	assertName(t, "test", true)
-	assertName(t, "test1", true)
-	assertName(t, "test1.2_3", true)
-	assertName(t, "_test", true)
-	assertName(t, "test_", true)
-	assertName(t, "looks-good", true)
-	assertName(t, " test", false)
-	assertName(t, "test ", false)
-	assertName(t, "test.", false)
-	assertName(t, ".test", false)
-	assertName(t, "", false)
-	assertName(t, "   ", false)
-	assertName(t, "<>$$^", false)
-	assertName(t, "this is a good network name", true)
-	assertName(t, "this is also-good", true)
-	assertName(t, " this is a not good network name", false)
-	assertName(t, "this is a not either ", false)
-	assertName(t, "this one\nis not good", false)
-	assertName(t, "this one\tis not good", false)
-	assertName(t, "this one\ris not good", false)
-	assertName(t, "this one\vis not good", false)
-	assertName(t, "this one\fis not good", false)
-}
-func assertName(t *testing.T, name string, mustSucceed bool) {
-	msg := "Name validation succeeds for a case when it is expected to fail"
-	if mustSucceed {
-		msg = "Name validation fails for a name that must be accepted"
+	if !IsValidName("test") {
+		t.Fatal("Name validation fails for a name that must be accepted")
+	}
+	if IsValidName("") {
+		t.Fatal("Name validation succeeds for a case when it is expected to fail")
 	}
-	err := ValidateName(name)
-	if (err == nil) != mustSucceed {
-		t.Fatalf("%s: %s", msg, name)
+	if IsValidName("   ") {
+		t.Fatal("Name validation succeeds for a case when it is expected to fail")
 	}
 }
 

+ 2 - 2
libnetwork/controller.go

@@ -653,8 +653,8 @@ func (c *controller) NewNetwork(networkType, name string, id string, options ...
 		}
 	}
 
-	if err := config.ValidateName(name); err != nil {
-		return nil, ErrInvalidName(err.Error())
+	if !config.IsValidName(name) {
+		return nil, ErrInvalidName(name)
 	}
 
 	if id == "" {

+ 1 - 1
libnetwork/error.go

@@ -69,7 +69,7 @@ func (ii ErrInvalidID) Error() string {
 func (ii ErrInvalidID) BadRequest() {}
 
 // ErrInvalidName is returned when a query-by-name or resource create method is
-// invoked with an invalid name parameter
+// invoked with an empty name parameter
 type ErrInvalidName string
 
 func (in ErrInvalidName) Error() string {

+ 2 - 3
libnetwork/network.go

@@ -879,9 +879,8 @@ func (n *network) addEndpoint(ep *endpoint) error {
 
 func (n *network) CreateEndpoint(name string, options ...EndpointOption) (Endpoint, error) {
 	var err error
-
-	if err = config.ValidateName(name); err != nil {
-		return nil, ErrInvalidName(err.Error())
+	if !config.IsValidName(name) {
+		return nil, ErrInvalidName(name)
 	}
 
 	if _, err = n.EndpointByName(name); err == nil {