Merge pull request #7032 from hashicorp/fix_region_validation

move region validation to run so that we don't break validation when no creds are set
This commit is contained in:
Megan Marsh 2018-11-28 15:02:18 -08:00 committed by GitHub
commit a2c3b35bff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 7 additions and 30 deletions

View File

@ -162,13 +162,6 @@ func (c *AccessConfig) Prepare(ctx *interpolate.Context) []error {
fmt.Errorf("`access_key` and `secret_key` must both be either set or not set.")) fmt.Errorf("`access_key` and `secret_key` must both be either set or not set."))
} }
if c.RawRegion != "" && !c.SkipValidation {
err := c.ValidateRegion(c.RawRegion)
if err != nil {
errs = append(errs, fmt.Errorf("error validating region: %s", err.Error()))
}
}
return errs return errs
} }

View File

@ -96,14 +96,6 @@ func (c *AMIConfig) Prepare(accessConfig *AccessConfig, ctx *interpolate.Context
func (c *AMIConfig) prepareRegions(accessConfig *AccessConfig) (errs []error) { func (c *AMIConfig) prepareRegions(accessConfig *AccessConfig) (errs []error) {
if len(c.AMIRegions) > 0 { if len(c.AMIRegions) > 0 {
if !c.AMISkipRegionValidation {
// Verify the regions are real
err := accessConfig.ValidateRegion(c.AMIRegions...)
if err != nil {
errs = append(errs, fmt.Errorf("error validating regions: %v", err))
}
}
regionSet := make(map[string]struct{}) regionSet := make(map[string]struct{})
regions := make([]string, 0, len(c.AMIRegions)) regions := make([]string, 0, len(c.AMIRegions))

View File

@ -25,7 +25,6 @@ func getFakeAccessConfig(region string) *AccessConfig {
func TestAMIConfigPrepare_name(t *testing.T) { func TestAMIConfigPrepare_name(t *testing.T) {
c := testAMIConfig() c := testAMIConfig()
accessConf := testAccessConfig() accessConf := testAccessConfig()
c.AMISkipRegionValidation = true
if err := c.Prepare(accessConf, nil); err != nil { if err := c.Prepare(accessConf, nil); err != nil {
t.Fatalf("shouldn't have err: %s", err) t.Fatalf("shouldn't have err: %s", err)
} }
@ -53,7 +52,6 @@ func (m *mockEC2Client) DescribeRegions(*ec2.DescribeRegionsInput) (*ec2.Describ
func TestAMIConfigPrepare_regions(t *testing.T) { func TestAMIConfigPrepare_regions(t *testing.T) {
c := testAMIConfig() c := testAMIConfig()
c.AMIRegions = nil c.AMIRegions = nil
c.AMISkipRegionValidation = true
var errs []error var errs []error
var err error var err error
@ -63,7 +61,6 @@ func TestAMIConfigPrepare_regions(t *testing.T) {
t.Fatalf("shouldn't have err: %#v", errs) t.Fatalf("shouldn't have err: %#v", errs)
} }
c.AMISkipRegionValidation = false
c.AMIRegions, err = listEC2Regions(mockConn) c.AMIRegions, err = listEC2Regions(mockConn)
if err != nil { if err != nil {
t.Fatalf("shouldn't have err: %s", err.Error()) t.Fatalf("shouldn't have err: %s", err.Error())
@ -71,11 +68,6 @@ func TestAMIConfigPrepare_regions(t *testing.T) {
if errs = c.prepareRegions(accessConf); len(errs) > 0 { if errs = c.prepareRegions(accessConf); len(errs) > 0 {
t.Fatalf("shouldn't have err: %#v", errs) t.Fatalf("shouldn't have err: %#v", errs)
} }
c.AMIRegions = []string{"foo"}
if errs = c.prepareRegions(accessConf); len(errs) == 0 {
t.Fatal("should have error")
}
errs = errs[:0] errs = errs[:0]
c.AMIRegions = []string{"us-east-1", "us-west-1", "us-east-1"} c.AMIRegions = []string{"us-east-1", "us-west-1", "us-east-1"}
@ -89,11 +81,9 @@ func TestAMIConfigPrepare_regions(t *testing.T) {
} }
c.AMIRegions = []string{"custom"} c.AMIRegions = []string{"custom"}
c.AMISkipRegionValidation = true
if errs = c.prepareRegions(accessConf); len(errs) > 0 { if errs = c.prepareRegions(accessConf); len(errs) > 0 {
t.Fatal("shouldn't have error") t.Fatal("shouldn't have error")
} }
c.AMISkipRegionValidation = false
c.AMIRegions = []string{"us-east-1", "us-east-2", "us-west-1"} c.AMIRegions = []string{"us-east-1", "us-east-2", "us-west-1"}
c.AMIRegionKMSKeyIDs = map[string]string{ c.AMIRegionKMSKeyIDs = map[string]string{
@ -142,11 +132,9 @@ func TestAMIConfigPrepare_regions(t *testing.T) {
"us-west-1": "789-012-3456", "us-west-1": "789-012-3456",
} }
c.AMISkipRegionValidation = true
if err := c.Prepare(accessConf, nil); err == nil { if err := c.Prepare(accessConf, nil); err == nil {
t.Fatal("should have error b/c theres a region in in ami_regions that isn't in the key map") t.Fatal("should have error b/c theres a region in in ami_regions that isn't in the key map")
} }
c.AMISkipRegionValidation = false
c.SnapshotUsers = []string{"foo", "bar"} c.SnapshotUsers = []string{"foo", "bar"}
c.AMIKmsKeyId = "123-abc-456" c.AMIKmsKeyId = "123-abc-456"
@ -172,7 +160,6 @@ func TestAMIConfigPrepare_regions(t *testing.T) {
func TestAMIConfigPrepare_Share_EncryptedBoot(t *testing.T) { func TestAMIConfigPrepare_Share_EncryptedBoot(t *testing.T) {
c := testAMIConfig() c := testAMIConfig()
c.AMISkipRegionValidation = true
c.AMIUsers = []string{"testAccountID"} c.AMIUsers = []string{"testAccountID"}
c.AMIEncryptBootVolume = true c.AMIEncryptBootVolume = true
@ -191,7 +178,6 @@ func TestAMIConfigPrepare_Share_EncryptedBoot(t *testing.T) {
func TestAMINameValidation(t *testing.T) { func TestAMINameValidation(t *testing.T) {
c := testAMIConfig() c := testAMIConfig()
c.AMISkipRegionValidation = true
accessConf := testAccessConfig() accessConf := testAccessConfig()

View File

@ -86,11 +86,17 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
} }
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
session, err := b.config.Session() session, err := b.config.Session()
if err != nil { if err != nil {
return nil, err return nil, err
} }
if !b.config.AMISkipRegionValidation {
regionsToValidate := append(b.config.AMIRegions, b.config.RawRegion)
err := b.config.AccessConfig.ValidateRegion(regionsToValidate...)
if err != nil {
return nil, fmt.Errorf("error validating regions: %v", err)
}
}
ec2conn := ec2.New(session) ec2conn := ec2.New(session)
// Setup the state bag and initial state for the steps // Setup the state bag and initial state for the steps