Merge pull request #25458 from errm/env-var-style-config-keys

Automatic merge from submit-queue

Allow Secret & ConfigMap keys to contain caps, dots, and underscores

[![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/.github/PULL_REQUEST_TEMPLATE.md?pixel)]()

Re: #23722

This makes loosens the regex used in in Secrets and ConfigMap,
in order to make environment variable style keys valid
This commit is contained in:
Kubernetes Submit Queue 2016-08-02 21:00:52 -07:00 committed by GitHub
commit 1edf254efb
4 changed files with 22 additions and 10 deletions

View File

@ -5980,7 +5980,7 @@ func TestValidateSecret(t *testing.T) {
overMaxSize.Data = map[string][]byte{
"over": make([]byte, api.MaxSecretSize+1),
}
invalidKey.Data["a..b"] = []byte("whoops")
invalidKey.Data["a*b"] = []byte("whoops")
leadingDotKey.Data[".key"] = []byte("bar")
dotKey.Data["."] = []byte("bar")
doubleDotKey.Data[".."] = []byte("bar")
@ -6585,7 +6585,7 @@ func TestValidateConfigMap(t *testing.T) {
invalidName = newConfigMap("NoUppercaseOrSpecialCharsLike=Equals", "validns", nil)
emptyNs = newConfigMap("validname", "", nil)
invalidNs = newConfigMap("validname", "NoUppercaseOrSpecialCharsLike=Equals", nil)
invalidKey = newConfigMap("validname", "validns", map[string]string{"a..b": "value"})
invalidKey = newConfigMap("validname", "validns", map[string]string{"a*b": "value"})
leadingDotKey = newConfigMap("validname", "validns", map[string]string{".ab": "value"})
dotKey = newConfigMap("validname", "validns", map[string]string{".": "value"})
doubleDotKey = newConfigMap("validname", "validns", map[string]string{"..": "value"})

View File

@ -93,7 +93,7 @@ func TestUpdate(t *testing.T) {
// invalid updateFunc
func(obj runtime.Object) runtime.Object {
cfg := obj.(*api.ConfigMap)
cfg.Data["badKey"] = "value"
cfg.Data["bad*Key"] = "value"
return cfg
},
)

View File

@ -247,19 +247,26 @@ func IsHTTPHeaderName(value string) []string {
return nil
}
const configMapKeyFmt = "\\.?" + dns1123SubdomainFmt
const configMapKeyFmt = `[-._a-zA-Z0-9]+`
var configMapKeyRegexp = regexp.MustCompile("^" + configMapKeyFmt + "$")
// IsConfigMapKey tests for a string that conforms to the definition of a
// subdomain in DNS (RFC 1123), except that a leading dot is allowed
// IsConfigMapKey tests for a string that is a valid key for a ConfigMap or Secret
func IsConfigMapKey(value string) []string {
var errs []string
if len(value) > DNS1123SubdomainMaxLength {
errs = append(errs, MaxLenError(DNS1123SubdomainMaxLength))
}
if !configMapKeyRegexp.MatchString(value) {
errs = append(errs, RegexError(configMapKeyFmt, "key.name"))
errs = append(errs, RegexError(configMapKeyFmt, "key.name", "KEY_NAME", "key-name"))
}
if value == "." {
errs = append(errs, `must not be '.'`)
}
if value == ".." {
errs = append(errs, `must not be '..'`)
} else if strings.HasPrefix(value, "..") {
errs = append(errs, `must not start with '..'`)
}
return errs
}

View File

@ -377,11 +377,14 @@ func TestIsValidPercent(t *testing.T) {
func TestIsConfigMapKey(t *testing.T) {
successCases := []string{
"a",
"good",
"good-good",
"still.good",
"this.is.also.good",
".so.is.this",
"THIS_IS_GOOD",
"so_is_this_17",
}
for i := range successCases {
@ -391,14 +394,16 @@ func TestIsConfigMapKey(t *testing.T) {
}
failureCases := []string{
"bad_bad",
".",
"..",
"..bad",
"bad.",
"b*d",
"bad!&bad",
}
for i := range failureCases {
if errs := IsConfigMapKey(failureCases[i]); len(errs) == 0 {
t.Errorf("[%d] expected success", i)
t.Errorf("[%d] expected failure", i)
}
}
}