From df6337e8aba47b79aba3673637c94a7c44a22598 Mon Sep 17 00:00:00 2001 From: Craig Barr Date: Wed, 5 Apr 2017 10:46:44 +1000 Subject: [PATCH 1/4] Added AMIName validation (issue 4761) --- builder/amazon/common/ami_config.go | 11 ++++++++++- builder/amazon/common/ami_config_test.go | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/builder/amazon/common/ami_config.go b/builder/amazon/common/ami_config.go index 4f6cd7316..0dc431cda 100644 --- a/builder/amazon/common/ami_config.go +++ b/builder/amazon/common/ami_config.go @@ -2,7 +2,7 @@ package common import ( "fmt" - + "regexp" "github.com/hashicorp/packer/template/interpolate" ) @@ -68,6 +68,15 @@ func (c *AMIConfig) Prepare(ctx *interpolate.Context) []error { errs = append(errs, fmt.Errorf("Cannot share snapshot encrypted with default KMS key")) } + if len(c.AMIName) < 3 || len(c.AMIName) > 128 { + errs = append(errs, fmt.Errorf("AMIName must be between 3 and 128 characters long")) + } + + var IsValidName = regexp.MustCompile(`^[a-zA-Z().-/_]+$`).MatchString + if !IsValidName(c.AMIName) { + errs = append(errs, fmt.Errorf("AMIName should only contain letters, numbers, '(', ')', '.', '-', '/' and '_'")) + } + if len(errs) > 0 { return errs } diff --git a/builder/amazon/common/ami_config_test.go b/builder/amazon/common/ami_config_test.go index ecb88bcb4..07a118f48 100644 --- a/builder/amazon/common/ami_config_test.go +++ b/builder/amazon/common/ami_config_test.go @@ -74,3 +74,26 @@ func TestAMIConfigPrepare_Share_EncryptedBoot(t *testing.T) { t.Fatal("shouldn't be able to share ami with encrypted boot volume") } } + +func TestAMINameValidation(t *testing.T) { + c := testAMIConfig() + + c.AMIName = "aa" + if err := c.Prepare(nil); err == nil { + t.Fatal("shouldn't be able to have an ami name with less than 3 characters") + } + + var longAmiName string + for i := 0; i < 129; i++ { + longAmiName += "a" + } + c.AMIName = longAmiName + if err := c.Prepare(nil); err == nil { + t.Fatal("shouldn't be able to have an ami name with great than 128 characters") + } + + c.AMIName = "+" + if err := c.Prepare(nil); err == nil { + t.Fatal("shouldn't be able to have an ami name with invalid characters") + } +} \ No newline at end of file From fdae6858c82b716e7fe32a6900a7d3c2a11887d9 Mon Sep 17 00:00:00 2001 From: Craig Barr Date: Wed, 5 Apr 2017 10:53:58 +1000 Subject: [PATCH 2/4] conforming to the standard for imports --- builder/amazon/common/ami_config.go | 1 + 1 file changed, 1 insertion(+) diff --git a/builder/amazon/common/ami_config.go b/builder/amazon/common/ami_config.go index 0dc431cda..44e72d5e5 100644 --- a/builder/amazon/common/ami_config.go +++ b/builder/amazon/common/ami_config.go @@ -3,6 +3,7 @@ package common import ( "fmt" "regexp" + "github.com/hashicorp/packer/template/interpolate" ) From 7911cfa94b064a4e131df07c95d3da673899a472 Mon Sep 17 00:00:00 2001 From: Craig Barr Date: Wed, 5 Apr 2017 11:02:23 +1000 Subject: [PATCH 3/4] Formatting as per make fmt --- builder/amazon/common/ami_config.go | 2 +- builder/amazon/common/ami_config_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/builder/amazon/common/ami_config.go b/builder/amazon/common/ami_config.go index 44e72d5e5..21438a50b 100644 --- a/builder/amazon/common/ami_config.go +++ b/builder/amazon/common/ami_config.go @@ -3,7 +3,7 @@ package common import ( "fmt" "regexp" - + "github.com/hashicorp/packer/template/interpolate" ) diff --git a/builder/amazon/common/ami_config_test.go b/builder/amazon/common/ami_config_test.go index 07a118f48..efc3584cc 100644 --- a/builder/amazon/common/ami_config_test.go +++ b/builder/amazon/common/ami_config_test.go @@ -96,4 +96,4 @@ func TestAMINameValidation(t *testing.T) { if err := c.Prepare(nil); err == nil { t.Fatal("shouldn't be able to have an ami name with invalid characters") } -} \ No newline at end of file +} From d9041bda0cabce849818d8fc214e467e32077a79 Mon Sep 17 00:00:00 2001 From: Craig Barr Date: Wed, 5 Apr 2017 11:06:59 +1000 Subject: [PATCH 4/4] Improved the test for special character so that it doesn't get halted by less than 3 character check --- builder/amazon/common/ami_config_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builder/amazon/common/ami_config_test.go b/builder/amazon/common/ami_config_test.go index efc3584cc..beccb98a2 100644 --- a/builder/amazon/common/ami_config_test.go +++ b/builder/amazon/common/ami_config_test.go @@ -92,7 +92,7 @@ func TestAMINameValidation(t *testing.T) { t.Fatal("shouldn't be able to have an ami name with great than 128 characters") } - c.AMIName = "+" + c.AMIName = "+aaa" if err := c.Prepare(nil); err == nil { t.Fatal("shouldn't be able to have an ami name with invalid characters") }