moved interp key regex out to a helper function

This commit is contained in:
Chris Baker 2019-01-08 00:09:21 +00:00
parent 1984805f86
commit bf00f93d87
3 changed files with 23 additions and 13 deletions

View File

@ -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
}

View File

@ -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)
}
}

View File

@ -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) {