2019-02-01 16:20:47 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
|
2020-08-21 12:30:10 -04:00
|
|
|
"github.com/antihax/optional"
|
2020-12-01 18:30:31 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/communicator"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2020-11-12 17:44:02 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/uuid"
|
2020-08-21 12:30:10 -04:00
|
|
|
"github.com/outscale/osc-sdk-go/osc"
|
2019-02-01 16:20:47 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepSecurityGroup struct {
|
|
|
|
CommConfig *communicator.Config
|
|
|
|
SecurityGroupFilter SecurityGroupFilterOptions
|
|
|
|
SecurityGroupIds []string
|
|
|
|
TemporarySGSourceCidr string
|
|
|
|
|
|
|
|
createdGroupId string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepSecurityGroup) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2020-08-21 12:30:10 -04:00
|
|
|
var (
|
2020-11-19 14:54:31 -05:00
|
|
|
ui = state.Get("ui").(packersdk.Ui)
|
2020-08-21 12:30:10 -04:00
|
|
|
conn = state.Get("osc").(*osc.APIClient)
|
|
|
|
netID = state.Get("net_id").(string)
|
|
|
|
)
|
2019-02-01 16:20:47 -05:00
|
|
|
|
|
|
|
if len(s.SecurityGroupIds) > 0 {
|
2020-08-21 12:30:10 -04:00
|
|
|
resp, _, err := conn.SecurityGroupApi.ReadSecurityGroups(context.Background(), &osc.ReadSecurityGroupsOpts{
|
|
|
|
ReadSecurityGroupsRequest: optional.NewInterface(osc.ReadSecurityGroupsRequest{
|
|
|
|
Filters: osc.FiltersSecurityGroup{
|
2019-02-01 16:20:47 -05:00
|
|
|
SecurityGroupIds: s.SecurityGroupIds,
|
|
|
|
},
|
2020-08-21 12:30:10 -04:00
|
|
|
}),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil || len(resp.SecurityGroups) == 0 {
|
2019-02-01 16:20:47 -05:00
|
|
|
err := fmt.Errorf("Couldn't find specified security group: %s", err)
|
|
|
|
log.Printf("[DEBUG] %s", err.Error())
|
|
|
|
state.Put("error", err)
|
2020-08-21 12:30:10 -04:00
|
|
|
|
2019-02-01 16:20:47 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Using specified security groups: %v", s.SecurityGroupIds)
|
|
|
|
state.Put("securityGroupIds", s.SecurityGroupIds)
|
2020-08-21 12:30:10 -04:00
|
|
|
|
2019-02-01 16:20:47 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !s.SecurityGroupFilter.Empty() {
|
2020-08-21 12:30:10 -04:00
|
|
|
filterReq := buildSecurityGroupFilters(s.SecurityGroupFilter.Filters)
|
2019-02-01 16:20:47 -05:00
|
|
|
|
2020-08-21 12:30:10 -04:00
|
|
|
log.Printf("Using SecurityGroup Filters %v", filterReq)
|
2019-02-01 16:20:47 -05:00
|
|
|
|
2020-08-21 12:30:10 -04:00
|
|
|
resp, _, err := conn.SecurityGroupApi.ReadSecurityGroups(context.Background(), &osc.ReadSecurityGroupsOpts{
|
|
|
|
ReadSecurityGroupsRequest: optional.NewInterface(osc.ReadSecurityGroupsRequest{
|
|
|
|
Filters: filterReq,
|
|
|
|
}),
|
|
|
|
})
|
2019-02-01 16:20:47 -05:00
|
|
|
|
2020-08-21 12:30:10 -04:00
|
|
|
if err != nil || len(resp.SecurityGroups) == 0 {
|
2019-02-01 16:20:47 -05:00
|
|
|
err := fmt.Errorf("Couldn't find security groups for filter: %s", err)
|
|
|
|
log.Printf("[DEBUG] %s", err.Error())
|
|
|
|
state.Put("error", err)
|
2020-08-21 12:30:10 -04:00
|
|
|
|
2019-02-01 16:20:47 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
securityGroupIds := []string{}
|
2020-08-21 12:30:10 -04:00
|
|
|
for _, sg := range resp.SecurityGroups {
|
2019-02-01 16:20:47 -05:00
|
|
|
securityGroupIds = append(securityGroupIds, sg.SecurityGroupId)
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message(fmt.Sprintf("Found Security Group(s): %s", strings.Join(securityGroupIds, ", ")))
|
|
|
|
state.Put("securityGroupIds", securityGroupIds)
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2020-08-21 12:30:10 -04:00
|
|
|
/* Create the group */
|
|
|
|
groupName := fmt.Sprintf("packer_osc_%s", uuid.TimeOrderedUUID())
|
2019-02-01 16:20:47 -05:00
|
|
|
ui.Say(fmt.Sprintf("Creating temporary security group for this instance: %s", groupName))
|
2020-08-21 12:30:10 -04:00
|
|
|
|
|
|
|
createSGReq := osc.CreateSecurityGroupRequest{
|
2019-02-01 16:20:47 -05:00
|
|
|
SecurityGroupName: groupName,
|
2020-08-21 12:30:10 -04:00
|
|
|
NetId: netID,
|
2019-02-01 16:20:47 -05:00
|
|
|
Description: "Temporary group for Packer",
|
|
|
|
}
|
|
|
|
|
2020-08-21 12:30:10 -04:00
|
|
|
resp, _, err := conn.SecurityGroupApi.CreateSecurityGroup(context.Background(), &osc.CreateSecurityGroupOpts{
|
|
|
|
CreateSecurityGroupRequest: optional.NewInterface(createSGReq),
|
|
|
|
})
|
2019-02-01 16:20:47 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(err.Error())
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the group ID so we can delete it later
|
2020-08-21 12:30:10 -04:00
|
|
|
s.createdGroupId = resp.SecurityGroup.SecurityGroupId
|
|
|
|
|
|
|
|
port := s.CommConfig.Port()
|
|
|
|
if port == 0 {
|
|
|
|
if s.CommConfig.Type != "none" {
|
|
|
|
state.Put("error", "port must be set to a non-zero value.")
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2019-02-01 16:20:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Authorize the SSH access for the security group
|
2020-08-21 12:30:10 -04:00
|
|
|
createSGRReq := osc.CreateSecurityGroupRuleRequest{
|
|
|
|
SecurityGroupId: resp.SecurityGroup.SecurityGroupId,
|
2019-02-01 16:20:47 -05:00
|
|
|
Flow: "Inbound",
|
2020-08-21 12:30:10 -04:00
|
|
|
Rules: []osc.SecurityGroupRule{
|
2019-02-20 19:41:47 -05:00
|
|
|
{
|
2020-08-21 12:30:10 -04:00
|
|
|
FromPortRange: int32(port),
|
|
|
|
ToPortRange: int32(port),
|
2019-02-01 16:20:47 -05:00
|
|
|
IpRanges: []string{s.TemporarySGSourceCidr},
|
|
|
|
IpProtocol: "tcp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-08-21 12:30:10 -04:00
|
|
|
ui.Say(fmt.Sprintf("Authorizing access to port %d from %s in the temporary security group...", port, s.TemporarySGSourceCidr))
|
|
|
|
|
2020-10-13 12:02:13 -04:00
|
|
|
_, _, err = conn.SecurityGroupRuleApi.CreateSecurityGroupRule(context.Background(), &osc.CreateSecurityGroupRuleOpts{
|
2020-08-21 12:30:10 -04:00
|
|
|
CreateSecurityGroupRuleRequest: optional.NewInterface(createSGRReq),
|
|
|
|
})
|
|
|
|
|
2019-02-01 16:20:47 -05:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error authorizing temporary security group: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set some state data for use in future steps
|
|
|
|
state.Put("securityGroupIds", []string{s.createdGroupId})
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepSecurityGroup) Cleanup(state multistep.StateBag) {
|
|
|
|
if s.createdGroupId == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-21 12:30:10 -04:00
|
|
|
var (
|
2020-11-19 14:54:31 -05:00
|
|
|
ui = state.Get("ui").(packersdk.Ui)
|
2020-08-21 12:30:10 -04:00
|
|
|
conn = state.Get("osc").(*osc.APIClient)
|
|
|
|
)
|
2019-02-01 16:20:47 -05:00
|
|
|
|
|
|
|
ui.Say("Deleting temporary security group...")
|
|
|
|
|
2020-08-21 12:30:10 -04:00
|
|
|
_, _, err := conn.SecurityGroupApi.DeleteSecurityGroup(context.Background(), &osc.DeleteSecurityGroupOpts{
|
|
|
|
DeleteSecurityGroupRequest: optional.NewInterface(osc.DeleteSecurityGroupRequest{
|
|
|
|
SecurityGroupId: s.createdGroupId,
|
|
|
|
}),
|
|
|
|
})
|
2019-02-01 16:20:47 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf(
|
|
|
|
"Error cleaning up security group. Please delete the group manually: %s", s.createdGroupId))
|
|
|
|
}
|
|
|
|
}
|
2020-08-21 12:30:10 -04:00
|
|
|
|
|
|
|
func buildSecurityGroupFilters(input map[string]string) osc.FiltersSecurityGroup {
|
|
|
|
var filters osc.FiltersSecurityGroup
|
|
|
|
|
|
|
|
for k, v := range input {
|
|
|
|
filterValue := []string{v}
|
|
|
|
|
|
|
|
switch name := k; name {
|
|
|
|
case "account_ids":
|
|
|
|
filters.AccountIds = filterValue
|
|
|
|
case "security_group_ids":
|
|
|
|
filters.SecurityGroupIds = filterValue
|
|
|
|
case "security_group_names":
|
|
|
|
filters.SecurityGroupNames = filterValue
|
|
|
|
case "tag_keys":
|
|
|
|
filters.TagKeys = filterValue
|
|
|
|
case "tag_values":
|
|
|
|
filters.TagValues = filterValue
|
|
|
|
case "tags":
|
|
|
|
filters.Tags = filterValue
|
|
|
|
default:
|
|
|
|
log.Printf("[Debug] Unknown Filter Name: %s.", name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filters
|
|
|
|
}
|