2014-07-08 20:06:39 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2014-08-21 07:24:13 +02:00
|
|
|
"bytes"
|
2014-07-08 20:06:39 +02:00
|
|
|
"fmt"
|
2014-08-21 23:10:09 +02:00
|
|
|
"sort"
|
2014-07-08 20:06:39 +02:00
|
|
|
"log"
|
2014-07-17 20:14:51 +02:00
|
|
|
"time"
|
2014-07-08 20:06:39 +02:00
|
|
|
|
2014-08-21 07:24:13 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/hashcode"
|
2014-07-17 20:14:51 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2014-08-20 19:40:18 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2014-07-08 20:06:39 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/goamz/ec2"
|
|
|
|
)
|
|
|
|
|
2014-08-20 19:40:18 +02:00
|
|
|
func resourceAwsSecurityGroup() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsSecurityGroupCreate,
|
|
|
|
Read: resourceAwsSecurityGroupRead,
|
2014-08-20 19:54:43 +02:00
|
|
|
Update: resourceAwsSecurityGroupUpdate,
|
2014-08-20 19:40:18 +02:00
|
|
|
Delete: resourceAwsSecurityGroupDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"description": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"vpc_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"ingress": &schema.Schema{
|
2014-08-21 07:24:13 +02:00
|
|
|
Type: schema.TypeSet,
|
2014-08-20 19:40:18 +02:00
|
|
|
Required: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"from_port": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"to_port": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"protocol": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"cidr_blocks": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
},
|
|
|
|
|
|
|
|
"security_groups": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-08-21 07:24:13 +02:00
|
|
|
Set: resourceAwsSecurityGroupIngressHash,
|
2014-08-20 19:40:18 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"owner_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-21 07:24:13 +02:00
|
|
|
func resourceAwsSecurityGroupIngressHash(v interface{}) int {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
m := v.(map[string]interface{})
|
|
|
|
buf.WriteString(fmt.Sprintf("%d-", m["from_port"].(int)))
|
|
|
|
buf.WriteString(fmt.Sprintf("%d-", m["to_port"].(int)))
|
|
|
|
buf.WriteString(fmt.Sprintf("%d-", m["protocol"].(string)))
|
|
|
|
|
2014-08-21 23:10:09 +02:00
|
|
|
// We need to make sure to sort the strings below so that we always
|
|
|
|
// generate the same hash code no matter what is in the set.
|
2014-08-21 07:24:13 +02:00
|
|
|
if v, ok := m["cidr_blocks"]; ok {
|
2014-08-21 23:10:09 +02:00
|
|
|
vs := v.([]interface{})
|
|
|
|
s := make([]string, len(vs))
|
|
|
|
for i, raw := range vs {
|
|
|
|
s[i] = raw.(string)
|
|
|
|
}
|
|
|
|
sort.Strings(s)
|
|
|
|
|
|
|
|
for _, v := range s {
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", v))
|
2014-08-21 07:24:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if v, ok := m["security_groups"]; ok {
|
2014-08-21 23:10:09 +02:00
|
|
|
vs := v.([]interface{})
|
|
|
|
s := make([]string, len(vs))
|
|
|
|
for i, raw := range vs {
|
|
|
|
s[i] = raw.(string)
|
|
|
|
}
|
|
|
|
sort.Strings(s)
|
|
|
|
|
|
|
|
for _, v := range s {
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", v))
|
2014-08-21 07:24:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hashcode.String(buf.String())
|
|
|
|
}
|
|
|
|
|
2014-08-20 19:40:18 +02:00
|
|
|
func resourceAwsSecurityGroupCreate(d *schema.ResourceData, meta interface{}) error {
|
2014-07-08 20:06:39 +02:00
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
ec2conn := p.ec2conn
|
|
|
|
|
|
|
|
securityGroupOpts := ec2.SecurityGroup{
|
2014-08-20 19:40:18 +02:00
|
|
|
Name: d.Get("name").(string),
|
2014-07-17 02:13:16 +02:00
|
|
|
}
|
|
|
|
|
2014-08-20 19:40:18 +02:00
|
|
|
if v := d.Get("vpc_id"); v != nil {
|
|
|
|
securityGroupOpts.VpcId = v.(string)
|
2014-07-17 02:13:16 +02:00
|
|
|
}
|
|
|
|
|
2014-08-20 19:40:18 +02:00
|
|
|
if v := d.Get("description"); v != nil {
|
|
|
|
securityGroupOpts.Description = v.(string)
|
2014-07-08 20:06:39 +02:00
|
|
|
}
|
|
|
|
|
2014-08-20 19:40:18 +02:00
|
|
|
log.Printf(
|
|
|
|
"[DEBUG] Security Group create configuration: %#v", securityGroupOpts)
|
2014-07-08 20:06:39 +02:00
|
|
|
createResp, err := ec2conn.CreateSecurityGroup(securityGroupOpts)
|
|
|
|
if err != nil {
|
2014-08-20 19:40:18 +02:00
|
|
|
return fmt.Errorf("Error creating Security Group: %s", err)
|
2014-07-08 20:06:39 +02:00
|
|
|
}
|
|
|
|
|
2014-08-20 19:40:18 +02:00
|
|
|
d.SetId(createResp.Id)
|
2014-07-08 22:33:59 +02:00
|
|
|
group := createResp.SecurityGroup
|
2014-07-08 20:06:39 +02:00
|
|
|
|
2014-08-20 19:40:18 +02:00
|
|
|
log.Printf("[INFO] Security Group ID: %s", d.Id())
|
2014-07-17 20:14:51 +02:00
|
|
|
|
|
|
|
// Wait for the security group to truly exist
|
|
|
|
log.Printf(
|
2014-07-29 18:45:57 +02:00
|
|
|
"[DEBUG] Waiting for Security Group (%s) to exist",
|
2014-08-20 19:40:18 +02:00
|
|
|
d.Id())
|
2014-07-17 20:14:51 +02:00
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{""},
|
|
|
|
Target: "exists",
|
2014-08-20 19:40:18 +02:00
|
|
|
Refresh: SGStateRefreshFunc(ec2conn, d.Id()),
|
2014-07-17 20:14:51 +02:00
|
|
|
Timeout: 1 * time.Minute,
|
|
|
|
}
|
|
|
|
if _, err := stateConf.WaitForState(); err != nil {
|
2014-08-20 19:40:18 +02:00
|
|
|
return fmt.Errorf(
|
2014-07-29 18:45:57 +02:00
|
|
|
"Error waiting for Security Group (%s) to become available: %s",
|
2014-08-20 19:40:18 +02:00
|
|
|
d.Id(), err)
|
2014-07-17 20:14:51 +02:00
|
|
|
}
|
2014-07-08 20:06:39 +02:00
|
|
|
|
2014-07-08 22:33:59 +02:00
|
|
|
// Expand the "ingress" array to goamz compat []ec2.IPPerm
|
2014-08-20 19:40:18 +02:00
|
|
|
ingressRaw := d.Get("ingress")
|
|
|
|
if ingressRaw == nil {
|
2014-08-21 07:24:13 +02:00
|
|
|
ingressRaw = new(schema.Set)
|
2014-07-08 23:47:03 +02:00
|
|
|
}
|
2014-08-21 07:24:13 +02:00
|
|
|
ingressList := ingressRaw.(*schema.Set).List()
|
2014-08-20 19:40:18 +02:00
|
|
|
if len(ingressList) > 0 {
|
2014-08-20 19:54:43 +02:00
|
|
|
ingressRules := expandIPPerms(ingressList)
|
2014-07-08 22:33:59 +02:00
|
|
|
_, err = ec2conn.AuthorizeSecurityGroup(group, ingressRules)
|
|
|
|
if err != nil {
|
2014-08-20 19:40:18 +02:00
|
|
|
return fmt.Errorf("Error authorizing security group ingress rules: %s", err)
|
2014-07-08 22:33:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-20 19:40:18 +02:00
|
|
|
return resourceAwsSecurityGroupRead(d, meta)
|
2014-07-08 20:06:39 +02:00
|
|
|
}
|
|
|
|
|
2014-08-20 19:54:43 +02:00
|
|
|
func resourceAwsSecurityGroupUpdate(d *schema.ResourceData, meta interface{}) error {
|
2014-08-20 20:18:00 +02:00
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
ec2conn := p.ec2conn
|
|
|
|
|
|
|
|
sgRaw, _, err := SGStateRefreshFunc(ec2conn, d.Id())()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if sgRaw == nil {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
group := sgRaw.(*ec2.SecurityGroupInfo).SecurityGroup
|
|
|
|
|
|
|
|
if d.HasChange("ingress") {
|
|
|
|
o, n := d.GetChange("ingress")
|
|
|
|
if o == nil {
|
2014-08-21 07:24:13 +02:00
|
|
|
o = new(schema.Set)
|
2014-08-20 20:18:00 +02:00
|
|
|
}
|
|
|
|
if n == nil {
|
2014-08-21 07:24:13 +02:00
|
|
|
n = new(schema.Set)
|
2014-08-20 20:18:00 +02:00
|
|
|
}
|
|
|
|
|
2014-08-21 07:24:13 +02:00
|
|
|
os := o.(*schema.Set)
|
|
|
|
ns := n.(*schema.Set)
|
|
|
|
|
|
|
|
remove := expandIPPerms(os.Difference(ns).List())
|
|
|
|
add := expandIPPerms(ns.Difference(os).List())
|
2014-08-20 20:18:00 +02:00
|
|
|
|
|
|
|
// TODO: We need to handle partial state better in the in-between
|
|
|
|
// in this update.
|
2014-08-21 21:00:32 +02:00
|
|
|
|
|
|
|
// TODO: It'd be nicer to authorize before removing, but then we have
|
|
|
|
// to deal with complicated unrolling to get individual CIDR blocks
|
|
|
|
// to avoid authorizing already authorized sources. Removing before
|
|
|
|
// adding is easier here, and Terraform should be fast enough to
|
|
|
|
// not have service issues.
|
|
|
|
|
2014-08-21 20:58:55 +02:00
|
|
|
if len(remove) > 0 {
|
|
|
|
// Revoke the old rules
|
|
|
|
_, err = ec2conn.RevokeSecurityGroup(group, remove)
|
2014-08-21 07:24:13 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error authorizing security group ingress rules: %s", err)
|
|
|
|
}
|
2014-08-20 20:18:00 +02:00
|
|
|
}
|
|
|
|
|
2014-08-21 20:58:55 +02:00
|
|
|
if len(add) > 0 {
|
|
|
|
// Authorize the new rules
|
|
|
|
_, err := ec2conn.AuthorizeSecurityGroup(group, add)
|
2014-08-21 07:24:13 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error authorizing security group ingress rules: %s", err)
|
|
|
|
}
|
2014-08-20 20:18:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-20 19:54:43 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-20 19:40:18 +02:00
|
|
|
func resourceAwsSecurityGroupDelete(d *schema.ResourceData, meta interface{}) error {
|
2014-07-08 20:06:39 +02:00
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
ec2conn := p.ec2conn
|
|
|
|
|
2014-08-20 19:40:18 +02:00
|
|
|
log.Printf("[DEBUG] Security Group destroy: %v", d.Id())
|
2014-07-08 20:06:39 +02:00
|
|
|
|
2014-08-20 19:40:18 +02:00
|
|
|
_, err := ec2conn.DeleteSecurityGroup(ec2.SecurityGroup{Id: d.Id()})
|
2014-07-08 20:06:39 +02:00
|
|
|
if err != nil {
|
|
|
|
ec2err, ok := err.(*ec2.Error)
|
|
|
|
if ok && ec2err.Code == "InvalidGroup.NotFound" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-20 19:40:18 +02:00
|
|
|
func resourceAwsSecurityGroupRead(d *schema.ResourceData, meta interface{}) error {
|
2014-07-08 20:06:39 +02:00
|
|
|
p := meta.(*ResourceProvider)
|
|
|
|
ec2conn := p.ec2conn
|
|
|
|
|
2014-08-20 19:40:18 +02:00
|
|
|
sgRaw, _, err := SGStateRefreshFunc(ec2conn, d.Id())()
|
2014-07-08 20:06:39 +02:00
|
|
|
if err != nil {
|
2014-08-20 19:40:18 +02:00
|
|
|
return err
|
2014-07-08 20:06:39 +02:00
|
|
|
}
|
2014-07-17 20:14:51 +02:00
|
|
|
if sgRaw == nil {
|
2014-08-20 19:40:18 +02:00
|
|
|
d.SetId("")
|
|
|
|
return nil
|
2014-07-17 20:14:51 +02:00
|
|
|
}
|
2014-07-08 20:06:39 +02:00
|
|
|
|
2014-08-20 19:40:18 +02:00
|
|
|
sg := sgRaw.(*ec2.SecurityGroupInfo)
|
2014-07-08 20:06:39 +02:00
|
|
|
|
2014-08-20 19:40:18 +02:00
|
|
|
var deps []terraform.ResourceDependency
|
2014-07-08 20:06:39 +02:00
|
|
|
|
2014-08-20 19:40:18 +02:00
|
|
|
// Gather our ingress rules
|
|
|
|
ingressRules := make([]map[string]interface{}, len(sg.IPPerms))
|
|
|
|
for i, perm := range sg.IPPerms {
|
2014-07-29 18:06:28 +02:00
|
|
|
n := make(map[string]interface{})
|
|
|
|
n["from_port"] = perm.FromPort
|
|
|
|
n["protocol"] = perm.Protocol
|
|
|
|
n["to_port"] = perm.ToPort
|
|
|
|
|
|
|
|
if len(perm.SourceIPs) > 0 {
|
|
|
|
n["cidr_blocks"] = perm.SourceIPs
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(perm.SourceGroups) > 0 {
|
|
|
|
// We depend on other security groups
|
|
|
|
for _, v := range perm.SourceGroups {
|
2014-08-20 19:40:18 +02:00
|
|
|
deps = append(deps,
|
2014-07-29 18:06:28 +02:00
|
|
|
terraform.ResourceDependency{ID: v.Id},
|
|
|
|
)
|
|
|
|
}
|
2014-08-20 19:40:18 +02:00
|
|
|
|
2014-07-29 18:06:28 +02:00
|
|
|
n["security_groups"] = flattenSecurityGroups(perm.SourceGroups)
|
|
|
|
}
|
|
|
|
|
2014-08-20 19:40:18 +02:00
|
|
|
ingressRules[i] = n
|
2014-07-09 02:24:50 +02:00
|
|
|
}
|
|
|
|
|
2014-08-20 19:40:18 +02:00
|
|
|
if v := d.Get("vpc_id"); v != nil && v.(string) != "" {
|
|
|
|
deps = append(deps,
|
|
|
|
terraform.ResourceDependency{ID: v.(string)},
|
2014-07-17 19:03:15 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2014-08-20 19:40:18 +02:00
|
|
|
d.Set("description", sg.Description)
|
|
|
|
d.Set("name", sg.Name)
|
|
|
|
d.Set("vpc_id", sg.VpcId)
|
|
|
|
d.Set("owner_id", sg.OwnerId)
|
|
|
|
d.Set("ingress", ingressRules)
|
|
|
|
d.SetDependencies(deps)
|
2014-07-08 20:06:39 +02:00
|
|
|
|
2014-08-20 19:40:18 +02:00
|
|
|
return nil
|
2014-07-15 18:40:20 +02:00
|
|
|
}
|
2014-07-17 20:14:51 +02:00
|
|
|
|
|
|
|
// SGStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
|
|
|
|
// a security group.
|
|
|
|
func SGStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc {
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
sgs := []ec2.SecurityGroup{ec2.SecurityGroup{Id: id}}
|
|
|
|
resp, err := conn.SecurityGroups(sgs, nil)
|
|
|
|
if err != nil {
|
2014-07-17 20:28:40 +02:00
|
|
|
if ec2err, ok := err.(*ec2.Error); ok {
|
|
|
|
if ec2err.Code == "InvalidSecurityGroupID.NotFound" ||
|
|
|
|
ec2err.Code == "InvalidGroup.NotFound" {
|
|
|
|
resp = nil
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2014-07-17 20:14:51 +02:00
|
|
|
log.Printf("Error on SGStateRefresh: %s", err)
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp == nil {
|
|
|
|
return nil, "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
group := &resp.Groups[0]
|
|
|
|
return group, "exists", nil
|
|
|
|
}
|
|
|
|
}
|