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