From 4137660b7abf698061e917d77c91bd8cafdebd1d Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Fri, 19 May 2017 02:11:43 -0700 Subject: [PATCH] fix ami name validation --- builder/amazon/common/ami_config.go | 13 ++++++------- builder/amazon/common/ami_config_test.go | 4 ++-- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/builder/amazon/common/ami_config.go b/builder/amazon/common/ami_config.go index a0f299d37..037e77a4e 100644 --- a/builder/amazon/common/ami_config.go +++ b/builder/amazon/common/ami_config.go @@ -2,7 +2,6 @@ package common import ( "fmt" - "regexp" "github.com/hashicorp/packer/template/interpolate" ) @@ -73,12 +72,12 @@ func (c *AMIConfig) Prepare(ctx *interpolate.Context) []error { errs = append(errs, fmt.Errorf("AMIName must be between 3 and 128 characters long")) } - amiNameRe := regexp.MustCompile(`^[0-9a-zA-Z().\-/_]+$`) - if !amiNameRe.MatchString(c.AMIName) { - errs = append(errs, fmt.Errorf("AMIName should only contain letters,"+ - " numbers, '(', ')', '.', '-', '/' and '_'. You can use the "+ - "`clean_ami_name` template filter to automatically clean your ami "+ - "name.")) + if c.AMIName != templateCleanAMIName(c.AMIName) { + errs = append(errs, fmt.Errorf("AMIName should only contain "+ + "alphanumeric characters, parentheses (()), square brackets ([]), spaces "+ + "( ), periods (.), slashes (/), dashes (-), single quotes ('), at-signs "+ + "(@), or underscores(_). You can use the `clean_ami_name` template "+ + "filter to automatically clean your ami name.")) } if len(errs) > 0 { diff --git a/builder/amazon/common/ami_config_test.go b/builder/amazon/common/ami_config_test.go index ce85e7fef..60fbc1208 100644 --- a/builder/amazon/common/ami_config_test.go +++ b/builder/amazon/common/ami_config_test.go @@ -97,9 +97,9 @@ func TestAMINameValidation(t *testing.T) { t.Fatal("shouldn't be able to have an ami name with invalid characters") } - c.AMIName = "foo().-/_bar" + c.AMIName = "fooBAR1()[] ./-'@_" if err := c.Prepare(nil); err != nil { - t.Fatal("expected 'foobar' to be a valid ami name") + t.Fatal("should be able to use all of the allowed AMI characters") } c.AMIName = `xyz-base-2017-04-05-1934`