2017-02-23 00:54:03 -05:00
|
|
|
package common
|
2016-03-23 13:35:47 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-12-04 09:14:53 -05:00
|
|
|
"log"
|
2016-03-23 13:35:47 -04:00
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2016-03-23 13:35:47 -04:00
|
|
|
)
|
|
|
|
|
2017-02-23 00:54:03 -05:00
|
|
|
type StepCreateEncryptedAMICopy struct {
|
|
|
|
image *ec2.Image
|
|
|
|
KeyID string
|
|
|
|
EncryptBootVolume bool
|
|
|
|
Name string
|
2017-08-02 12:45:53 -04:00
|
|
|
AMIMappings []BlockDevice
|
2016-03-23 13:35:47 -04:00
|
|
|
}
|
|
|
|
|
2017-02-23 00:54:03 -05:00
|
|
|
func (s *StepCreateEncryptedAMICopy) Run(state multistep.StateBag) multistep.StepAction {
|
2016-03-23 13:35:47 -04:00
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2017-02-23 00:54:03 -05:00
|
|
|
kmsKeyId := s.KeyID
|
2016-03-23 13:35:47 -04:00
|
|
|
|
|
|
|
// Encrypt boot not set, so skip step
|
2017-02-23 00:54:03 -05:00
|
|
|
if !s.EncryptBootVolume {
|
2016-10-18 08:30:38 -04:00
|
|
|
if kmsKeyId != "" {
|
2017-03-28 21:06:50 -04:00
|
|
|
log.Printf("Ignoring KMS Key ID: %s, encrypted=false", kmsKeyId)
|
2016-10-18 08:30:38 -04:00
|
|
|
}
|
2016-03-23 13:35:47 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Creating Encrypted AMI Copy")
|
|
|
|
|
|
|
|
amis := state.Get("amis").(map[string]string)
|
|
|
|
var region, id string
|
|
|
|
if amis != nil {
|
|
|
|
for region, id = range amis {
|
2017-05-26 15:21:07 -04:00
|
|
|
break // There is only ever one region:ami pair in this map
|
2016-03-23 13:35:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Copying AMI: %s(%s)", region, id))
|
|
|
|
|
2016-10-18 08:30:38 -04:00
|
|
|
if kmsKeyId != "" {
|
2017-05-26 01:03:11 -04:00
|
|
|
ui.Say(fmt.Sprintf("Encrypting with KMS Key ID: %s", kmsKeyId))
|
2016-10-18 08:30:38 -04:00
|
|
|
}
|
|
|
|
|
2016-03-23 13:35:47 -04:00
|
|
|
copyOpts := &ec2.CopyImageInput{
|
2017-02-23 00:54:03 -05:00
|
|
|
Name: &s.Name, // Try to overwrite existing AMI
|
2016-03-23 13:35:47 -04:00
|
|
|
SourceImageId: aws.String(id),
|
|
|
|
SourceRegion: aws.String(region),
|
|
|
|
Encrypted: aws.Bool(true),
|
2016-10-18 08:30:38 -04:00
|
|
|
KmsKeyId: aws.String(kmsKeyId),
|
2016-03-23 13:35:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
copyResp, err := ec2conn.CopyImage(copyOpts)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error copying AMI: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the copy to become ready
|
2017-02-23 00:54:03 -05:00
|
|
|
stateChange := StateChangeConf{
|
2016-03-23 13:35:47 -04:00
|
|
|
Pending: []string{"pending"},
|
|
|
|
Target: "available",
|
2017-02-23 00:54:03 -05:00
|
|
|
Refresh: AMIStateRefreshFunc(ec2conn, *copyResp.ImageId),
|
2016-03-23 13:35:47 -04:00
|
|
|
StepState: state,
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Waiting for AMI copy to become ready...")
|
2017-02-23 00:54:03 -05:00
|
|
|
if _, err := WaitForState(&stateChange); err != nil {
|
2016-03-23 13:35:47 -04:00
|
|
|
err := fmt.Errorf("Error waiting for AMI Copy: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2016-12-04 14:18:27 -05:00
|
|
|
// Get the encrypted AMI image, we need the new snapshot id's
|
|
|
|
encImagesResp, err := ec2conn.DescribeImages(&ec2.DescribeImagesInput{ImageIds: []*string{aws.String(*copyResp.ImageId)}})
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error searching for AMI: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
encImage := encImagesResp.Images[0]
|
|
|
|
var encSnapshots []string
|
|
|
|
for _, blockDevice := range encImage.BlockDeviceMappings {
|
|
|
|
if blockDevice.Ebs != nil && blockDevice.Ebs.SnapshotId != nil {
|
|
|
|
encSnapshots = append(encSnapshots, *blockDevice.Ebs.SnapshotId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 19:21:12 -04:00
|
|
|
// Get the unencrypted AMI image
|
|
|
|
unencImagesResp, err := ec2conn.DescribeImages(&ec2.DescribeImagesInput{ImageIds: []*string{aws.String(id)}})
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error searching for AMI: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
unencImage := unencImagesResp.Images[0]
|
|
|
|
|
2016-03-23 13:35:47 -04:00
|
|
|
// Remove unencrypted AMI
|
2016-10-25 11:01:01 -04:00
|
|
|
ui.Say("Deregistering unencrypted AMI")
|
2016-03-23 13:35:47 -04:00
|
|
|
deregisterOpts := &ec2.DeregisterImageInput{ImageId: aws.String(id)}
|
|
|
|
if _, err := ec2conn.DeregisterImage(deregisterOpts); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error deregistering AMI, may still be around: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2016-03-23 19:21:12 -04:00
|
|
|
// Remove associated unencrypted snapshot(s)
|
|
|
|
ui.Say("Deleting unencrypted snapshots")
|
2016-12-04 14:18:27 -05:00
|
|
|
snapshots := state.Get("snapshots").(map[string][]string)
|
2016-03-23 19:21:12 -04:00
|
|
|
|
2017-08-08 17:28:01 -04:00
|
|
|
OuterLoop:
|
2016-03-23 19:21:12 -04:00
|
|
|
for _, blockDevice := range unencImage.BlockDeviceMappings {
|
2016-12-04 14:18:27 -05:00
|
|
|
if blockDevice.Ebs != nil && blockDevice.Ebs.SnapshotId != nil {
|
2017-08-02 12:45:53 -04:00
|
|
|
// If this packer run didn't create it, then don't delete it
|
|
|
|
for _, origDevice := range s.AMIMappings {
|
|
|
|
if origDevice.SnapshotId == *blockDevice.Ebs.SnapshotId {
|
2017-08-08 17:28:01 -04:00
|
|
|
ui.Message(fmt.Sprintf("Keeping Snapshot ID: %s", *blockDevice.Ebs.SnapshotId))
|
2017-08-08 17:53:56 -04:00
|
|
|
continue OuterLoop
|
2017-08-02 12:45:53 -04:00
|
|
|
}
|
|
|
|
}
|
2017-08-08 17:28:01 -04:00
|
|
|
|
2017-08-02 12:45:53 -04:00
|
|
|
ui.Message(fmt.Sprintf("Deleting Snapshot ID: %s", *blockDevice.Ebs.SnapshotId))
|
2016-12-04 14:18:27 -05:00
|
|
|
deleteSnapOpts := &ec2.DeleteSnapshotInput{
|
|
|
|
SnapshotId: aws.String(*blockDevice.Ebs.SnapshotId),
|
|
|
|
}
|
|
|
|
if _, err := ec2conn.DeleteSnapshot(deleteSnapOpts); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error deleting snapshot, may still be around: %s", err))
|
|
|
|
return multistep.ActionHalt
|
2016-03-23 19:21:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 13:35:47 -04:00
|
|
|
// Replace original AMI ID with Encrypted ID in state
|
|
|
|
amis[region] = *copyResp.ImageId
|
2016-12-04 14:18:27 -05:00
|
|
|
snapshots[region] = encSnapshots
|
2016-03-23 13:35:47 -04:00
|
|
|
state.Put("amis", amis)
|
2016-12-04 14:18:27 -05:00
|
|
|
state.Put("snapshots", snapshots)
|
2016-03-23 13:35:47 -04:00
|
|
|
|
|
|
|
imagesResp, err := ec2conn.DescribeImages(&ec2.DescribeImagesInput{ImageIds: []*string{copyResp.ImageId}})
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error searching for AMI: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
s.image = imagesResp.Images[0]
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2017-02-23 00:54:03 -05:00
|
|
|
func (s *StepCreateEncryptedAMICopy) Cleanup(state multistep.StateBag) {
|
2016-03-23 13:35:47 -04:00
|
|
|
if s.image == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
|
|
|
if !cancelled && !halted {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
2017-05-25 21:49:29 -04:00
|
|
|
ui.Say("Deregistering the AMI because cancellation or error...")
|
2016-03-23 13:35:47 -04:00
|
|
|
deregisterOpts := &ec2.DeregisterImageInput{ImageId: s.image.ImageId}
|
|
|
|
if _, err := ec2conn.DeregisterImage(deregisterOpts); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error deregistering AMI, may still be around: %s", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|