From 6b458160b931b6ea6ff3536b756337dfb1a5a86e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 8 Dec 2016 23:01:51 -0500 Subject: [PATCH] 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. --- config/config.go | 10 +++++++++- config/loader_hcl.go | 5 +++++ config/loader_test.go | 14 ++++++++++++++ config/test-fixtures/var_int.tf | 1 + config/test-fixtures/var_int_bare.tf | 1 + 5 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 config/test-fixtures/var_int.tf create mode 100644 config/test-fixtures/var_int_bare.tf diff --git a/config/config.go b/config/config.go index da981019c..eb2adb8ab 100644 --- a/config/config.go +++ b/config/config.go @@ -18,7 +18,7 @@ import ( // NameRegexp is the regular expression that all names (modules, providers, // 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 // of Terraform templates. @@ -282,6 +282,14 @@ func (c *Config) Validate() error { 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 { if v.Type() == VariableTypeUnknown { errs = append(errs, fmt.Errorf( diff --git a/config/loader_hcl.go b/config/loader_hcl.go index 0a025913d..881a98312 100644 --- a/config/loader_hcl.go +++ b/config/loader_hcl.go @@ -404,6 +404,11 @@ func loadVariablesHcl(list *ast.ObjectList) ([]*Variable, error) { } 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 diff --git a/config/loader_test.go b/config/loader_test.go index f7fad33c0..7dae362cd 100644 --- a/config/loader_test.go +++ b/config/loader_test.go @@ -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) { dir := filepath.Join(fixtureDir, "dir-basic") c, err := LoadDir(dir) diff --git a/config/test-fixtures/var_int.tf b/config/test-fixtures/var_int.tf new file mode 100644 index 000000000..cd3dca18c --- /dev/null +++ b/config/test-fixtures/var_int.tf @@ -0,0 +1 @@ +variable "1x1" {} diff --git a/config/test-fixtures/var_int_bare.tf b/config/test-fixtures/var_int_bare.tf new file mode 100644 index 000000000..b08e61364 --- /dev/null +++ b/config/test-fixtures/var_int_bare.tf @@ -0,0 +1 @@ +variable 1x1 {}