Merge pull request #4762 from craigbarrau/master

Prevalidate AMI Name
This commit is contained in:
Matthew Hooker 2017-04-04 21:52:49 -07:00 committed by GitHub
commit e362f8a08c
2 changed files with 33 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package common
import (
"fmt"
"regexp"
"github.com/hashicorp/packer/template/interpolate"
)
@ -68,6 +69,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
}

View File

@ -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 = "+aaa"
if err := c.Prepare(nil); err == nil {
t.Fatal("shouldn't be able to have an ami name with invalid characters")
}
}