From 64f591875db7fead5c88e0b4d0df24338232d069 Mon Sep 17 00:00:00 2001 From: Chris Baker Date: Tue, 8 Jan 2019 00:09:21 +0000 Subject: [PATCH] moved interp key regex out to a helper function --- command/agent/command.go | 19 +++++++++---------- command/agent/config_parse.go | 4 +--- helper/funcs.go | 13 +++++++++++++ 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/command/agent/command.go b/command/agent/command.go index fdab3e91d..5740f8e91 100644 --- a/command/agent/command.go +++ b/command/agent/command.go @@ -9,24 +9,25 @@ import ( "os/signal" "path/filepath" "reflect" - "regexp" "sort" "strconv" "strings" "syscall" "time" - metrics "github.com/armon/go-metrics" + "github.com/hashicorp/nomad/helper" + + "github.com/armon/go-metrics" "github.com/armon/go-metrics/circonus" "github.com/armon/go-metrics/datadog" "github.com/armon/go-metrics/prometheus" "github.com/hashicorp/consul/lib" - checkpoint "github.com/hashicorp/go-checkpoint" - discover "github.com/hashicorp/go-discover" - gsyslog "github.com/hashicorp/go-syslog" + "github.com/hashicorp/go-checkpoint" + "github.com/hashicorp/go-discover" + "github.com/hashicorp/go-syslog" "github.com/hashicorp/logutils" - flaghelper "github.com/hashicorp/nomad/helper/flag-helpers" - gatedwriter "github.com/hashicorp/nomad/helper/gated-writer" + "github.com/hashicorp/nomad/helper/flag-helpers" + "github.com/hashicorp/nomad/helper/gated-writer" "github.com/hashicorp/nomad/nomad/structs/config" "github.com/hashicorp/nomad/version" "github.com/mitchellh/cli" @@ -187,8 +188,6 @@ func (c *Command) readConfig() *Config { // Parse the meta flags. metaLength := len(meta) if metaLength != 0 { - validKeyRe, _ := regexp.Compile(`^[^.]+(\.[^.]+)*$`) - cmdConfig.Client.Meta = make(map[string]string, metaLength) for _, kv := range meta { parts := strings.SplitN(kv, "=", 2) @@ -197,7 +196,7 @@ func (c *Command) readConfig() *Config { return nil } - if !validKeyRe.MatchString(parts[0]) { + if !helper.IsValidInterpVariable(parts[0]) { c.Ui.Error(fmt.Sprintf("Invalid Client.Meta key: %v", parts[0])) return nil } diff --git a/command/agent/config_parse.go b/command/agent/config_parse.go index 6e681b54b..ed8e09dbb 100644 --- a/command/agent/config_parse.go +++ b/command/agent/config_parse.go @@ -6,7 +6,6 @@ import ( "io" "os" "path/filepath" - "regexp" "time" multierror "github.com/hashicorp/go-multierror" @@ -439,9 +438,8 @@ func parseClient(result **ClientConfig, list *ast.ObjectList) error { } } - validKeyRe, _ := regexp.Compile(`^[^.]+(\.[^.]+)*$`) for k := range config.Meta { - if !validKeyRe.MatchString(k) { + if !helper.IsValidInterpVariable(k) { return fmt.Errorf("invalid Client.Meta key: %v", k) } } diff --git a/helper/funcs.go b/helper/funcs.go index cf9cc3dae..1daa8c8ff 100644 --- a/helper/funcs.go +++ b/helper/funcs.go @@ -15,6 +15,11 @@ import ( // validUUID is used to check if a given string looks like a UUID var validUUID = regexp.MustCompile(`(?i)^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$`) +// validInterpVarKey matches valid dotted variable names for interpolation. The +// string must begin with one or more non-dot characters which may be followed +// by sequences containing a dot followed by a one or more non-dot characters. +var validInterpVarKey = regexp.MustCompile(`^[^.]+(\.[^.]+)*$`) + // IsUUID returns true if the given string is a valid UUID. func IsUUID(str string) bool { const uuidLen = 36 @@ -25,6 +30,14 @@ func IsUUID(str string) bool { return validUUID.MatchString(str) } +// IsValidInterpVariable returns true if a valid dotted variable names for +// interpolation. The string must begin with one or more non-dot characters +// which may be followed by sequences containing a dot followed by a one or more +// non-dot characters. +func IsValidInterpVariable(str string) bool { + return validInterpVarKey.MatchString(str) +} + // HashUUID takes an input UUID and returns a hashed version of the UUID to // ensure it is well distributed. func HashUUID(input string) (output string, hashed bool) {