Skip to content

Commit

Permalink
oc new-app: allow 'dot' in ENV variable names
Browse files Browse the repository at this point in the history
fixes: openshift#8771

reuses validators from kubernetes/kubernetes#48986
  • Loading branch information
Jan Wozniak committed May 11, 2018
1 parent 8af9b16 commit eeefe42
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
11 changes: 5 additions & 6 deletions pkg/oc/generate/app/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"

"github.com/joho/godotenv"
utilenv "github.com/openshift/origin/pkg/oc/util/env"
"k8s.io/apimachinery/pkg/util/validation"
kapi "k8s.io/kubernetes/pkg/apis/core"
)

Expand Down Expand Up @@ -39,13 +39,12 @@ func ParseEnvironment(vals ...string) (Environment, []string, []error) {
duplicates := []string{}
env := make(Environment)
for _, s := range vals {
valid := utilenv.IsValidEnvironmentArgument(s)
p := strings.SplitN(s, "=", 2)
if !valid || len(p) != 2 {
errs = append(errs, fmt.Errorf("invalid parameter assignment in %q", s))
key, val := p[0], p[1]
if err := validation.IsEnvVarName(key); len(err) != 0 {
errs = append(errs, fmt.Errorf("invalid parameter assignment in %q, %v", s, err))
continue
}
key, val := p[0], p[1]
if _, exists := env[key]; exists {
duplicates = append(duplicates, key)
continue
Expand Down Expand Up @@ -166,7 +165,7 @@ func LoadEnvironmentFile(filename string, stdin io.Reader) (Environment, error)
return nil, fmt.Errorf("Cannot read variables from file %q: %s", errorFilename, err)
}
for k, v := range env {
if !utilenv.IsValidEnvironmentArgument(fmt.Sprintf("%s=%s", k, v)) {
if err := validation.IsEnvVarName(k); len(err) != 0 {
return nil, fmt.Errorf("invalid parameter assignment in %s=%s", k, v)
}
}
Expand Down
19 changes: 7 additions & 12 deletions pkg/oc/util/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,16 @@ import (
"strings"

"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation"
kapi "k8s.io/kubernetes/pkg/apis/core"
)

var argumentEnvironment = regexp.MustCompile(`(?ms)^(.+)\=(.*)$`)
var validArgumentEnvironment = regexp.MustCompile(`(?ms)^(\w+)\=(.*)$`)

func IsEnvironmentArgument(s string) bool {
return argumentEnvironment.MatchString(s)
}

func IsValidEnvironmentArgument(s string) bool {
return validArgumentEnvironment.MatchString(s)
}

func SplitEnvironmentFromResources(args []string) (resources, envArgs []string, ok bool) {
first := true
for _, s := range args {
Expand Down Expand Up @@ -50,8 +46,6 @@ func parseIntoEnvVar(spec []string, defaultReader io.Reader, envVarType string)
var remove []string
for _, envSpec := range spec {
switch {
case !IsValidEnvironmentArgument(envSpec) && !strings.HasSuffix(envSpec, "-"):
return nil, nil, fmt.Errorf("%ss must be of the form key=value and can only contain letters, numbers, and underscores", envVarType)
case envSpec == "-":
if defaultReader == nil {
return nil, nil, fmt.Errorf("when '-' is used, STDIN must be open")
Expand All @@ -63,13 +57,14 @@ func parseIntoEnvVar(spec []string, defaultReader io.Reader, envVarType string)
env = append(env, fileEnv...)
case strings.Contains(envSpec, "="):
parts := strings.SplitN(envSpec, "=", 2)
if len(parts) != 2 {
return nil, nil, fmt.Errorf("invalid %s: %v", envVarType, envSpec)
n, v := parts[0], parts[1]
if errs := validation.IsEnvVarName(n); len(errs) != 0 {
return nil, nil, fmt.Errorf("%ss env validation failed with errors: %v", envVarType, errs)
}
exists.Insert(parts[0])
exists.Insert(n)
env = append(env, kapi.EnvVar{
Name: parts[0],
Value: parts[1],
Name: n,
Value: v,
})
case strings.HasSuffix(envSpec, "-"):
remove = append(remove, envSpec[:len(envSpec)-1])
Expand Down

0 comments on commit eeefe42

Please sign in to comment.