2013-07-20 22:50:55 -04:00
|
|
|
package common
|
2013-06-11 17:37:10 -04:00
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-06-11 17:37:10 -04:00
|
|
|
"fmt"
|
2015-04-05 17:58:48 -04:00
|
|
|
"log"
|
2018-08-14 06:04:13 -04:00
|
|
|
"strings"
|
2015-04-05 17:58:48 -04:00
|
|
|
"time"
|
|
|
|
|
2015-06-03 17:13:52 -04:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2017-07-31 14:38:30 -04:00
|
|
|
"github.com/aws/aws-sdk-go/aws/request"
|
2015-06-03 17:13:52 -04:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/common/uuid"
|
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
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"
|
2013-06-11 17:37:10 -04:00
|
|
|
)
|
|
|
|
|
2013-07-20 22:50:55 -04:00
|
|
|
type StepSecurityGroup struct {
|
2019-03-30 18:47:03 -04:00
|
|
|
CommConfig *communicator.Config
|
|
|
|
SecurityGroupFilter SecurityGroupFilterOptions
|
|
|
|
SecurityGroupIds []string
|
|
|
|
TemporarySGSourceCidrs []string
|
2013-07-20 22:50:55 -04:00
|
|
|
|
|
|
|
createdGroupId string
|
2013-06-11 17:37:10 -04:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepSecurityGroup) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2013-08-31 15:58:55 -04:00
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2018-08-14 06:04:13 -04:00
|
|
|
vpcId := state.Get("vpc_id").(string)
|
2013-06-11 17:37:10 -04:00
|
|
|
|
2013-10-02 13:52:16 -04:00
|
|
|
if len(s.SecurityGroupIds) > 0 {
|
2017-01-18 18:25:31 -05:00
|
|
|
_, err := ec2conn.DescribeSecurityGroups(
|
|
|
|
&ec2.DescribeSecurityGroupsInput{
|
|
|
|
GroupIds: aws.StringSlice(s.SecurityGroupIds),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Couldn't find specified security group: %s", err)
|
|
|
|
log.Printf("[DEBUG] %s", err.Error())
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-10-02 13:52:16 -04:00
|
|
|
log.Printf("Using specified security groups: %v", s.SecurityGroupIds)
|
|
|
|
state.Put("securityGroupIds", s.SecurityGroupIds)
|
2013-07-09 11:57:21 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2018-08-14 06:04:13 -04:00
|
|
|
if !s.SecurityGroupFilter.Empty() {
|
|
|
|
|
|
|
|
params := &ec2.DescribeSecurityGroupsInput{}
|
2018-08-24 13:47:11 -04:00
|
|
|
if vpcId != "" {
|
|
|
|
s.SecurityGroupFilter.Filters[aws.String("vpc-id")] = &vpcId
|
2018-08-14 06:04:13 -04:00
|
|
|
}
|
2018-08-24 13:47:11 -04:00
|
|
|
params.Filters = buildEc2Filters(s.SecurityGroupFilter.Filters)
|
2018-08-14 06:04:13 -04:00
|
|
|
|
|
|
|
log.Printf("Using SecurityGroup Filters %v", params)
|
|
|
|
|
|
|
|
sgResp, err := ec2conn.DescribeSecurityGroups(params)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Couldn't find security groups for filter: %s", err)
|
|
|
|
log.Printf("[DEBUG] %s", err.Error())
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
securityGroupIds := []string{}
|
|
|
|
for _, sg := range sgResp.SecurityGroups {
|
|
|
|
securityGroupIds = append(securityGroupIds, *sg.GroupId)
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message(fmt.Sprintf("Found Security Group(s): %s", strings.Join(securityGroupIds, ", ")))
|
|
|
|
state.Put("securityGroupIds", securityGroupIds)
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2015-06-14 02:12:59 -04:00
|
|
|
port := s.CommConfig.Port()
|
|
|
|
if port == 0 {
|
2017-08-03 16:54:57 -04:00
|
|
|
if s.CommConfig.Type != "none" {
|
|
|
|
panic("port must be set to a non-zero value.")
|
|
|
|
}
|
2013-07-20 22:51:25 -04:00
|
|
|
}
|
|
|
|
|
2013-06-11 17:37:10 -04:00
|
|
|
// Create the group
|
2017-06-09 15:05:59 -04:00
|
|
|
groupName := fmt.Sprintf("packer_%s", uuid.TimeOrderedUUID())
|
|
|
|
ui.Say(fmt.Sprintf("Creating temporary security group for this instance: %s", groupName))
|
2015-04-05 17:58:48 -04:00
|
|
|
group := &ec2.CreateSecurityGroupInput{
|
|
|
|
GroupName: &groupName,
|
|
|
|
Description: aws.String("Temporary group for Packer"),
|
2013-07-22 01:46:11 -04:00
|
|
|
}
|
2017-05-17 12:45:20 -04:00
|
|
|
|
2018-08-14 06:04:13 -04:00
|
|
|
group.VpcId = &vpcId
|
2017-05-17 12:45:20 -04:00
|
|
|
|
2013-07-22 01:46:11 -04:00
|
|
|
groupResp, err := ec2conn.CreateSecurityGroup(group)
|
2013-06-11 17:37:10 -04:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(err.Error())
|
2015-04-06 22:11:34 -04:00
|
|
|
state.Put("error", err)
|
2013-06-11 17:37:10 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the group ID so we can delete it later
|
2015-08-17 20:44:01 -04:00
|
|
|
s.createdGroupId = *groupResp.GroupId
|
2015-04-05 17:58:48 -04:00
|
|
|
|
2017-10-29 19:12:27 -04:00
|
|
|
// Wait for the security group become available for authorizing
|
|
|
|
log.Printf("[DEBUG] Waiting for temporary security group: %s", s.createdGroupId)
|
|
|
|
err = waitUntilSecurityGroupExists(ec2conn,
|
|
|
|
&ec2.DescribeSecurityGroupsInput{
|
|
|
|
GroupIds: []*string{aws.String(s.createdGroupId)},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err == nil {
|
|
|
|
log.Printf("[DEBUG] Found security group %s", s.createdGroupId)
|
|
|
|
} else {
|
|
|
|
err := fmt.Errorf("Timed out waiting for security group %s: %s", s.createdGroupId, err)
|
|
|
|
log.Printf("[DEBUG] %s", err.Error())
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2019-03-30 18:47:03 -04:00
|
|
|
// map the list of temporary security group CIDRs bundled with config to
|
|
|
|
// types expected by EC2.
|
|
|
|
groupIpRanges := []*ec2.IpRange{}
|
|
|
|
for _, cidr := range s.TemporarySGSourceCidrs {
|
|
|
|
ipRange := ec2.IpRange{
|
|
|
|
CidrIp: aws.String(cidr),
|
|
|
|
}
|
|
|
|
groupIpRanges = append(groupIpRanges, &ipRange)
|
|
|
|
}
|
|
|
|
|
2015-04-05 17:58:48 -04:00
|
|
|
// Authorize the SSH access for the security group
|
2017-10-29 19:12:27 -04:00
|
|
|
groupRules := &ec2.AuthorizeSecurityGroupIngressInput{
|
2017-10-29 19:13:49 -04:00
|
|
|
GroupId: groupResp.GroupId,
|
|
|
|
IpPermissions: []*ec2.IpPermission{
|
|
|
|
{
|
2019-03-30 18:47:03 -04:00
|
|
|
FromPort: aws.Int64(int64(port)),
|
|
|
|
ToPort: aws.Int64(int64(port)),
|
|
|
|
IpRanges: groupIpRanges,
|
2017-10-29 19:13:49 -04:00
|
|
|
IpProtocol: aws.String("tcp"),
|
|
|
|
},
|
|
|
|
},
|
2013-06-11 17:37:10 -04:00
|
|
|
}
|
|
|
|
|
2015-06-14 02:12:59 -04:00
|
|
|
ui.Say(fmt.Sprintf(
|
2019-03-30 18:47:03 -04:00
|
|
|
"Authorizing access to port %d from %v in the temporary security groups...",
|
|
|
|
port, s.TemporarySGSourceCidrs),
|
|
|
|
)
|
2017-10-29 19:12:27 -04:00
|
|
|
_, err = ec2conn.AuthorizeSecurityGroupIngress(groupRules)
|
2013-12-28 12:03:22 -05:00
|
|
|
if err != nil {
|
2017-10-29 19:12:27 -04:00
|
|
|
err := fmt.Errorf("Error authorizing temporary security group: %s", err)
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("error", err)
|
2013-06-11 17:37:10 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set some state data for use in future steps
|
2013-10-02 13:52:16 -04:00
|
|
|
state.Put("securityGroupIds", []string{s.createdGroupId})
|
2013-06-11 17:37:10 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
func (s *StepSecurityGroup) Cleanup(state multistep.StateBag) {
|
2013-07-20 22:50:55 -04:00
|
|
|
if s.createdGroupId == "" {
|
2013-06-11 17:37:10 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-06-11 17:37:10 -04:00
|
|
|
|
|
|
|
ui.Say("Deleting temporary security group...")
|
2013-08-12 16:34:55 -04:00
|
|
|
|
|
|
|
var err error
|
|
|
|
for i := 0; i < 5; i++ {
|
2015-08-17 20:44:01 -04:00
|
|
|
_, err = ec2conn.DeleteSecurityGroup(&ec2.DeleteSecurityGroupInput{GroupId: &s.createdGroupId})
|
2013-08-12 16:43:52 -04:00
|
|
|
if err == nil {
|
|
|
|
break
|
2013-08-12 16:34:55 -04:00
|
|
|
}
|
2013-08-12 16:43:52 -04:00
|
|
|
|
|
|
|
log.Printf("Error deleting security group: %s", err)
|
|
|
|
time.Sleep(5 * time.Second)
|
2013-08-12 16:34:55 -04:00
|
|
|
}
|
|
|
|
|
2013-06-11 17:37:10 -04:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf(
|
2013-07-20 22:50:55 -04:00
|
|
|
"Error cleaning up security group. Please delete the group manually: %s", s.createdGroupId))
|
2013-06-11 17:37:10 -04:00
|
|
|
}
|
|
|
|
}
|
2017-01-18 18:11:52 -05:00
|
|
|
|
|
|
|
func waitUntilSecurityGroupExists(c *ec2.EC2, input *ec2.DescribeSecurityGroupsInput) error {
|
2017-07-31 14:38:30 -04:00
|
|
|
ctx := aws.BackgroundContext()
|
|
|
|
w := request.Waiter{
|
|
|
|
Name: "DescribeSecurityGroups",
|
2017-01-18 18:11:52 -05:00
|
|
|
MaxAttempts: 40,
|
2017-11-02 03:25:54 -04:00
|
|
|
Delay: request.ConstantWaiterDelay(5 * time.Second),
|
2017-07-31 14:38:30 -04:00
|
|
|
Acceptors: []request.WaiterAcceptor{
|
2017-01-18 18:11:52 -05:00
|
|
|
{
|
2017-07-31 14:38:30 -04:00
|
|
|
State: request.SuccessWaiterState,
|
|
|
|
Matcher: request.PathWaiterMatch,
|
2017-01-18 18:11:52 -05:00
|
|
|
Argument: "length(SecurityGroups[]) > `0`",
|
|
|
|
Expected: true,
|
|
|
|
},
|
|
|
|
{
|
2017-07-31 14:38:30 -04:00
|
|
|
State: request.RetryWaiterState,
|
|
|
|
Matcher: request.ErrorWaiterMatch,
|
2017-01-18 18:11:52 -05:00
|
|
|
Argument: "",
|
|
|
|
Expected: "InvalidGroup.NotFound",
|
|
|
|
},
|
|
|
|
{
|
2017-07-31 14:38:30 -04:00
|
|
|
State: request.RetryWaiterState,
|
|
|
|
Matcher: request.ErrorWaiterMatch,
|
2017-01-18 18:11:52 -05:00
|
|
|
Argument: "",
|
|
|
|
Expected: "InvalidSecurityGroupID.NotFound",
|
|
|
|
},
|
|
|
|
},
|
2017-07-31 14:38:30 -04:00
|
|
|
Logger: c.Config.Logger,
|
|
|
|
NewRequest: func(opts []request.Option) (*request.Request, error) {
|
|
|
|
var inCpy *ec2.DescribeSecurityGroupsInput
|
|
|
|
if input != nil {
|
|
|
|
tmp := *input
|
|
|
|
inCpy = &tmp
|
|
|
|
}
|
|
|
|
req, _ := c.DescribeSecurityGroupsRequest(inCpy)
|
|
|
|
req.SetContext(ctx)
|
|
|
|
req.ApplyOptions(opts...)
|
|
|
|
return req, nil
|
|
|
|
},
|
2017-01-18 18:11:52 -05:00
|
|
|
}
|
2017-07-31 14:38:30 -04:00
|
|
|
return w.WaitWithContext(ctx)
|
2017-01-18 18:11:52 -05:00
|
|
|
}
|