提交 fba5f9a2 编写于 作者: P Pablo Caderno

Added validation for profile name

It must be container friendly
上级 d7341186
......@@ -44,6 +44,11 @@ var ProfileCmd = &cobra.Command{
}
profile := args[0]
// Check whether the profile name is container friendly
if !config.ProfileNameValid(profile) {
out.WarningT("Profile name '{{.profilename}}' is not valid", out.V{"profilename": profile})
exit.UsageT("Only alphanumeric, dots, underscores and dashes '-' are permitted. Minimum 2 characters, starting by alphanumeric.")
}
/**
we need to add code over here to check whether the profile
name is in the list of reserved keywords
......
......@@ -144,6 +144,10 @@ func runStart(cmd *cobra.Command, args []string) {
registryMirror = viper.GetStringSlice("registry_mirror")
}
if !config.ProfileNameValid(ClusterFlagValue()) {
out.WarningT("Profile name '{{.name}}' is not valid", out.V{"name": ClusterFlagValue()})
exit.UsageT("Only alphanumeric, dots, underscores and dashes '-' are permitted. Minimum 2 characters, starting by alphanumeric.")
}
existing, err := config.Load(ClusterFlagValue())
if err != nil && !config.IsNotExist(err) {
exit.WithCodeT(exit.Data, "Unable to load config: {{.error}}", out.V{"error": err})
......
......@@ -21,6 +21,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/golang/glog"
......@@ -83,6 +84,16 @@ func PrimaryControlPlane(cc *ClusterConfig) (Node, error) {
return cp, nil
}
// ProfileNameValid checks if the profile name is container name friendly
func ProfileNameValid(name string) bool {
// RestrictedNameChars collects the characters allowed to represent a name
const RestrictedNameChars = `[a-zA-Z0-9][a-zA-Z0-9_.-]`
var validName = regexp.MustCompile(`^` + RestrictedNameChars + `+$`)
return validName.MatchString(name)
}
// ProfileNameInReservedKeywords checks if the profile is an internal keywords
func ProfileNameInReservedKeywords(name string) bool {
for _, v := range keywords {
......
......@@ -72,6 +72,27 @@ func TestListProfiles(t *testing.T) {
}
}
func TestProfileNameValid(t *testing.T) {
var testCases = []struct {
name string
expected bool
}{
{"meaningful_name", true},
{"meaningful_name@", false},
{"n_a_m_e_2", true},
{"n", false},
{"_name", false},
{"N__a.M--E12567", true},
}
for _, tt := range testCases {
got := ProfileNameValid(tt.name)
if got != tt.expected {
t.Errorf("expected ProfileNameValid(%s)=%t but got %t ", tt.name, tt.expected, got)
}
}
}
func TestProfileNameInReservedKeywords(t *testing.T) {
var testCases = []struct {
name string
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册