config: disallow names starting with ints

Fixes #10597

This disallows any names for variables, modules, etc. starting with
ints. This causes parse errors with the new HIL parser and actually
causes long term ambiguities if we allow this.

I've also updated the upgrade guide to note this as a backwards
compatibility and how people can fix this going forward.
This commit is contained in:
Mitchell Hashimoto 2016-12-08 23:01:51 -05:00
parent 9c80c82d9e
commit 6b458160b9
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
5 changed files with 30 additions and 1 deletions

View File

@ -18,7 +18,7 @@ import (
// NameRegexp is the regular expression that all names (modules, providers, // NameRegexp is the regular expression that all names (modules, providers,
// resources, etc.) must follow. // resources, etc.) must follow.
var NameRegexp = regexp.MustCompile(`\A[A-Za-z0-9\-\_]+\z`) var NameRegexp = regexp.MustCompile(`(?i)\A[A-Z_][A-Z0-9\-\_]+\z`)
// Config is the configuration that comes from loading a collection // Config is the configuration that comes from loading a collection
// of Terraform templates. // of Terraform templates.
@ -282,6 +282,14 @@ func (c *Config) Validate() error {
varMap[v.Name] = v varMap[v.Name] = v
} }
for k, _ := range varMap {
if !NameRegexp.MatchString(k) {
errs = append(errs, fmt.Errorf(
"variable %q: variable name must match regular expresion %s",
k, NameRegexp))
}
}
for _, v := range c.Variables { for _, v := range c.Variables {
if v.Type() == VariableTypeUnknown { if v.Type() == VariableTypeUnknown {
errs = append(errs, fmt.Errorf( errs = append(errs, fmt.Errorf(

View File

@ -404,6 +404,11 @@ func loadVariablesHcl(list *ast.ObjectList) ([]*Variable, error) {
} }
n := item.Keys[0].Token.Value().(string) n := item.Keys[0].Token.Value().(string)
if !NameRegexp.MatchString(n) {
return nil, fmt.Errorf(
"position %s: 'variable' name must match regular expression: %s",
item.Pos(), NameRegexp)
}
/* /*
// TODO: catch extra fields // TODO: catch extra fields

View File

@ -447,6 +447,20 @@ func TestLoadFile_variables(t *testing.T) {
} }
} }
func TestLoadFile_varInt(t *testing.T) {
_, err := LoadFile(filepath.Join(fixtureDir, "var_int.tf"))
if err == nil {
t.Fatal("should have error")
}
}
func TestLoadFile_varIntBare(t *testing.T) {
_, err := LoadFile(filepath.Join(fixtureDir, "var_int_bare.tf"))
if err == nil {
t.Fatal("should have error")
}
}
func TestLoadDir_basic(t *testing.T) { func TestLoadDir_basic(t *testing.T) {
dir := filepath.Join(fixtureDir, "dir-basic") dir := filepath.Join(fixtureDir, "dir-basic")
c, err := LoadDir(dir) c, err := LoadDir(dir)

View File

@ -0,0 +1 @@
variable "1x1" {}

View File

@ -0,0 +1 @@
variable 1x1 {}