move kms/encrypted validation to block devices

This commit is contained in:
Matthew Hooker 2018-01-12 15:10:51 -08:00
parent 0023aa11cf
commit cea2ab8c6d
No known key found for this signature in database
GPG Key ID: 7B5F933D9CE8C6A1
2 changed files with 25 additions and 6 deletions

View File

@ -1,6 +1,7 @@
package common package common
import ( import (
"fmt"
"strings" "strings"
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
@ -86,10 +87,29 @@ func buildBlockDevices(b []BlockDevice) []*ec2.BlockDeviceMapping {
return blockDevices return blockDevices
} }
func (b *BlockDevices) Prepare(ctx *interpolate.Context) []error { func (b *BlockDevice) Prepare(ctx *interpolate.Context) error {
// Warn that encrypted must be true when setting kms_key_id
if b.KmsKeyId != "" && b.Encrypted == false {
return fmt.Errorf("The device %v, must also have `encrypted: "+
"true` when setting a kms_key_id.", b.DeviceName)
}
return nil return nil
} }
func (b *BlockDevices) Prepare(ctx *interpolate.Context) (errs []error) {
for _, d := range b.AMIMappings {
if err := d.Prepare(ctx); err != nil {
errs = append(errs, fmt.Errorf("AMIMapping: %s", err.Error()))
}
}
for _, d := range b.LaunchMappings {
if err := d.Prepare(ctx); err != nil {
errs = append(errs, fmt.Errorf("LaunchMapping: %s", err.Error()))
}
}
return errs
}
func (b *AMIBlockDevices) BuildAMIDevices() []*ec2.BlockDeviceMapping { func (b *AMIBlockDevices) BuildAMIDevices() []*ec2.BlockDeviceMapping {
return buildBlockDevices(b.AMIMappings) return buildBlockDevices(b.AMIMappings)
} }

View File

@ -56,12 +56,11 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
var errs *packer.MultiError var errs *packer.MultiError
errs = packer.MultiErrorAppend(errs, b.config.AccessConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.AccessConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.RunConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.RunConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.launchBlockDevices.Prepare(&b.config.ctx)...)
// Warn that encrypted must be true when setting kms_key_id for _, d := range b.config.VolumeMappings {
for _, device := range b.config.VolumeMappings { if err := d.Prepare(&b.config.ctx); err != nil {
if device.KmsKeyId != "" && device.Encrypted == false { errs = packer.MultiErrorAppend(errs, fmt.Errorf("AMIMapping: %s", err.Error()))
errs = packer.MultiErrorAppend(errs, fmt.Errorf("The device %v, must also have `encrypted: "+
"true` when setting a kms_key_id.", device.DeviceName))
} }
} }