2014-07-10 22:39:43 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-07-16 17:14:23 +02:00
|
|
|
"reflect"
|
2016-08-15 16:11:52 +02:00
|
|
|
"regexp"
|
2015-04-22 19:56:06 +02:00
|
|
|
"strings"
|
2014-07-16 18:01:56 +02:00
|
|
|
"testing"
|
2014-07-10 22:39:43 +02:00
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2016-02-11 17:07:36 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2014-07-10 22:39:43 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2016-01-20 17:52:46 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2014-07-10 22:39:43 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2016-03-25 19:49:44 +01:00
|
|
|
func TestProtocolStateFunc(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
input interface{}
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
input: "tcp",
|
|
|
|
expected: "tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: 6,
|
|
|
|
expected: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "17",
|
|
|
|
expected: "udp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "all",
|
|
|
|
expected: "-1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "-1",
|
|
|
|
expected: "-1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: -1,
|
|
|
|
expected: "",
|
|
|
|
},
|
2016-03-28 20:08:53 +02:00
|
|
|
{
|
|
|
|
input: "1",
|
|
|
|
expected: "icmp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "icmp",
|
|
|
|
expected: "icmp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: 1,
|
|
|
|
expected: "",
|
|
|
|
},
|
2016-03-25 19:49:44 +01:00
|
|
|
}
|
|
|
|
for _, c := range cases {
|
|
|
|
result := protocolStateFunc(c.input)
|
|
|
|
if result != c.expected {
|
|
|
|
t.Errorf("Error matching protocol, expected (%s), got (%s)", c.expected, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProtocolForValue(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
input string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
input: "tcp",
|
|
|
|
expected: "tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "6",
|
|
|
|
expected: "tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "udp",
|
|
|
|
expected: "udp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "17",
|
|
|
|
expected: "udp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "all",
|
|
|
|
expected: "-1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "-1",
|
|
|
|
expected: "-1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "tCp",
|
|
|
|
expected: "tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "6",
|
|
|
|
expected: "tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "UDp",
|
|
|
|
expected: "udp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "17",
|
|
|
|
expected: "udp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "ALL",
|
|
|
|
expected: "-1",
|
|
|
|
},
|
2016-03-28 20:08:53 +02:00
|
|
|
{
|
|
|
|
input: "icMp",
|
|
|
|
expected: "icmp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "1",
|
|
|
|
expected: "icmp",
|
|
|
|
},
|
2016-03-25 19:49:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
result := protocolForValue(c.input)
|
|
|
|
if result != c.expected {
|
|
|
|
t.Errorf("Error matching protocol, expected (%s), got (%s)", c.expected, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-20 17:52:46 +01:00
|
|
|
func TestResourceAwsSecurityGroupIPPermGather(t *testing.T) {
|
|
|
|
raw := []*ec2.IpPermission{
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2016-01-20 17:52:46 +01:00
|
|
|
IpProtocol: aws.String("tcp"),
|
|
|
|
FromPort: aws.Int64(int64(1)),
|
|
|
|
ToPort: aws.Int64(int64(-1)),
|
2017-03-14 12:02:50 +01:00
|
|
|
IpRanges: []*ec2.IpRange{{CidrIp: aws.String("0.0.0.0/0")}},
|
2016-01-20 17:52:46 +01:00
|
|
|
UserIdGroupPairs: []*ec2.UserIdGroupPair{
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2016-03-04 09:28:37 +01:00
|
|
|
GroupId: aws.String("sg-11111"),
|
2016-01-20 17:52:46 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2016-01-20 17:52:46 +01:00
|
|
|
IpProtocol: aws.String("tcp"),
|
|
|
|
FromPort: aws.Int64(int64(80)),
|
|
|
|
ToPort: aws.Int64(int64(80)),
|
|
|
|
UserIdGroupPairs: []*ec2.UserIdGroupPair{
|
2016-03-04 09:28:37 +01:00
|
|
|
// VPC
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2016-03-04 09:28:37 +01:00
|
|
|
GroupId: aws.String("sg-22222"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2016-03-04 09:28:37 +01:00
|
|
|
IpProtocol: aws.String("tcp"),
|
|
|
|
FromPort: aws.Int64(int64(443)),
|
|
|
|
ToPort: aws.Int64(int64(443)),
|
|
|
|
UserIdGroupPairs: []*ec2.UserIdGroupPair{
|
|
|
|
// Classic
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2016-03-04 09:28:37 +01:00
|
|
|
UserId: aws.String("12345"),
|
|
|
|
GroupId: aws.String("sg-33333"),
|
|
|
|
GroupName: aws.String("ec2_classic"),
|
|
|
|
},
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2016-03-04 09:28:37 +01:00
|
|
|
UserId: aws.String("amazon-elb"),
|
|
|
|
GroupId: aws.String("sg-d2c979d3"),
|
|
|
|
GroupName: aws.String("amazon-elb-sg"),
|
2016-01-20 17:52:46 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2016-06-06 12:07:19 +02:00
|
|
|
IpProtocol: aws.String("-1"),
|
|
|
|
FromPort: aws.Int64(int64(0)),
|
|
|
|
ToPort: aws.Int64(int64(0)),
|
2017-03-14 12:02:50 +01:00
|
|
|
PrefixListIds: []*ec2.PrefixListId{{PrefixListId: aws.String("pl-12345678")}},
|
2016-06-06 12:07:19 +02:00
|
|
|
UserIdGroupPairs: []*ec2.UserIdGroupPair{
|
|
|
|
// VPC
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2016-06-06 12:07:19 +02:00
|
|
|
GroupId: aws.String("sg-22222"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-01-20 17:52:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
local := []map[string]interface{}{
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2016-01-20 17:52:46 +01:00
|
|
|
"protocol": "tcp",
|
|
|
|
"from_port": int64(1),
|
|
|
|
"to_port": int64(-1),
|
|
|
|
"cidr_blocks": []string{"0.0.0.0/0"},
|
|
|
|
"self": true,
|
|
|
|
},
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2016-01-20 17:52:46 +01:00
|
|
|
"protocol": "tcp",
|
|
|
|
"from_port": int64(80),
|
|
|
|
"to_port": int64(80),
|
|
|
|
"security_groups": schema.NewSet(schema.HashString, []interface{}{
|
2016-03-04 09:28:37 +01:00
|
|
|
"sg-22222",
|
|
|
|
}),
|
|
|
|
},
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2016-03-04 09:28:37 +01:00
|
|
|
"protocol": "tcp",
|
|
|
|
"from_port": int64(443),
|
|
|
|
"to_port": int64(443),
|
|
|
|
"security_groups": schema.NewSet(schema.HashString, []interface{}{
|
|
|
|
"ec2_classic",
|
|
|
|
"amazon-elb/amazon-elb-sg",
|
2016-01-20 17:52:46 +01:00
|
|
|
}),
|
|
|
|
},
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2016-06-06 12:07:19 +02:00
|
|
|
"protocol": "-1",
|
|
|
|
"from_port": int64(0),
|
|
|
|
"to_port": int64(0),
|
|
|
|
"prefix_list_ids": []string{"pl-12345678"},
|
|
|
|
"security_groups": schema.NewSet(schema.HashString, []interface{}{
|
|
|
|
"sg-22222",
|
|
|
|
}),
|
|
|
|
},
|
2016-01-20 17:52:46 +01:00
|
|
|
}
|
|
|
|
|
2016-03-04 09:28:37 +01:00
|
|
|
out := resourceAwsSecurityGroupIPPermGather("sg-11111", raw, aws.String("12345"))
|
2016-01-20 17:52:46 +01:00
|
|
|
for _, i := range out {
|
|
|
|
// loop and match rules, because the ordering is not guarneteed
|
|
|
|
for _, l := range local {
|
|
|
|
if i["from_port"] == l["from_port"] {
|
|
|
|
|
|
|
|
if i["to_port"] != l["to_port"] {
|
|
|
|
t.Fatalf("to_port does not match")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := i["cidr_blocks"]; ok {
|
|
|
|
if !reflect.DeepEqual(i["cidr_blocks"], l["cidr_blocks"]) {
|
|
|
|
t.Fatalf("error matching cidr_blocks")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := i["security_groups"]; ok {
|
|
|
|
outSet := i["security_groups"].(*schema.Set)
|
|
|
|
localSet := l["security_groups"].(*schema.Set)
|
|
|
|
|
|
|
|
if !outSet.Equal(localSet) {
|
|
|
|
t.Fatalf("Security Group sets are not equal")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-08 01:04:38 +02:00
|
|
|
func TestAccAWSSecurityGroup_basic(t *testing.T) {
|
2015-03-09 16:02:27 +01:00
|
|
|
var group ec2.SecurityGroup
|
2014-07-16 17:14:23 +02:00
|
|
|
|
2014-07-10 22:39:43 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
2016-04-21 01:41:45 +02:00
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
IDRefreshName: "aws_security_group.web",
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupDestroy,
|
2014-07-10 22:39:43 +02:00
|
|
|
Steps: []resource.TestStep{
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2014-07-10 22:39:43 +02:00
|
|
|
Config: testAccAWSSecurityGroupConfig,
|
2014-07-16 17:14:23 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupExists("aws_security_group.web", &group),
|
|
|
|
testAccCheckAWSSecurityGroupAttributes(&group),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "name", "terraform_acceptance_test_example"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "description", "Used in the terraform acceptance tests"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2015-03-30 21:54:03 +02:00
|
|
|
"aws_security_group.web", "ingress.3629188364.protocol", "tcp"),
|
2014-07-16 17:14:23 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-03-30 21:54:03 +02:00
|
|
|
"aws_security_group.web", "ingress.3629188364.from_port", "80"),
|
2014-07-16 17:14:23 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-03-30 21:54:03 +02:00
|
|
|
"aws_security_group.web", "ingress.3629188364.to_port", "8000"),
|
2014-07-16 17:14:23 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-03-30 21:54:03 +02:00
|
|
|
"aws_security_group.web", "ingress.3629188364.cidr_blocks.#", "1"),
|
2014-07-16 17:14:23 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-03-30 21:54:03 +02:00
|
|
|
"aws_security_group.web", "ingress.3629188364.cidr_blocks.0", "10.0.0.0/8"),
|
2014-07-16 17:14:23 +02:00
|
|
|
),
|
2014-07-10 22:39:43 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-03-14 12:02:50 +01:00
|
|
|
func TestAccAWSSecurityGroup_ipv6(t *testing.T) {
|
|
|
|
var group ec2.SecurityGroup
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
IDRefreshName: "aws_security_group.web",
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: testAccAWSSecurityGroupConfigIpv6,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupExists("aws_security_group.web", &group),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "name", "terraform_acceptance_test_example"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "description", "Used in the terraform acceptance tests"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "ingress.2293451516.protocol", "tcp"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "ingress.2293451516.from_port", "80"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "ingress.2293451516.to_port", "8000"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "ingress.2293451516.ipv6_cidr_blocks.#", "1"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "ingress.2293451516.ipv6_cidr_blocks.0", "::/0"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-08-15 16:11:52 +02:00
|
|
|
func TestAccAWSSecurityGroup_tagsCreatedFirst(t *testing.T) {
|
|
|
|
var group ec2.SecurityGroup
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2016-08-15 16:11:52 +02:00
|
|
|
Config: testAccAWSSecurityGroupConfigForTagsOrdering,
|
|
|
|
ExpectError: regexp.MustCompile("InvalidParameterValue"),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupExists("aws_security_group.foo", &group),
|
|
|
|
testAccCheckTags(&group.Tags, "Name", "tf-acc-test"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-12-17 18:24:24 +01:00
|
|
|
func TestAccAWSSecurityGroup_namePrefix(t *testing.T) {
|
2015-12-04 15:21:08 +01:00
|
|
|
var group ec2.SecurityGroup
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
2016-04-21 01:41:45 +02:00
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
IDRefreshName: "aws_security_group.baz",
|
|
|
|
IDRefreshIgnore: []string{"name_prefix"},
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupDestroy,
|
2015-12-04 15:21:08 +01:00
|
|
|
Steps: []resource.TestStep{
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2015-12-04 15:21:08 +01:00
|
|
|
Config: testAccAWSSecurityGroupPrefixNameConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupExists("aws_security_group.baz", &group),
|
|
|
|
testAccCheckAWSSecurityGroupGeneratedNamePrefix(
|
|
|
|
"aws_security_group.baz", "baz-"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-09-30 23:19:16 +02:00
|
|
|
func TestAccAWSSecurityGroup_self(t *testing.T) {
|
2015-03-09 16:02:27 +01:00
|
|
|
var group ec2.SecurityGroup
|
2014-09-30 23:19:16 +02:00
|
|
|
|
|
|
|
checkSelf := func(s *terraform.State) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if e := recover(); e != nil {
|
|
|
|
err = fmt.Errorf("bad: %#v", group)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
if *group.IpPermissions[0].UserIdGroupPairs[0].GroupId != *group.GroupId {
|
2014-09-30 23:19:16 +02:00
|
|
|
return fmt.Errorf("bad: %#v", group)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
2016-04-21 01:41:45 +02:00
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
IDRefreshName: "aws_security_group.web",
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupDestroy,
|
2014-09-30 23:19:16 +02:00
|
|
|
Steps: []resource.TestStep{
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2014-09-30 23:19:16 +02:00
|
|
|
Config: testAccAWSSecurityGroupConfigSelf,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupExists("aws_security_group.web", &group),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "name", "terraform_acceptance_test_example"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "description", "Used in the terraform acceptance tests"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2015-04-02 16:00:47 +02:00
|
|
|
"aws_security_group.web", "ingress.3971148406.protocol", "tcp"),
|
2014-09-30 23:19:16 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-04-02 16:00:47 +02:00
|
|
|
"aws_security_group.web", "ingress.3971148406.from_port", "80"),
|
2014-09-30 23:19:16 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-04-02 16:00:47 +02:00
|
|
|
"aws_security_group.web", "ingress.3971148406.to_port", "8000"),
|
2014-09-30 23:19:16 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-04-02 16:00:47 +02:00
|
|
|
"aws_security_group.web", "ingress.3971148406.self", "true"),
|
2014-09-30 23:19:16 +02:00
|
|
|
checkSelf,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-17 19:03:15 +02:00
|
|
|
func TestAccAWSSecurityGroup_vpc(t *testing.T) {
|
2015-03-09 16:02:27 +01:00
|
|
|
var group ec2.SecurityGroup
|
2014-07-17 19:03:15 +02:00
|
|
|
|
|
|
|
testCheck := func(*terraform.State) error {
|
2015-08-17 20:27:16 +02:00
|
|
|
if *group.VpcId == "" {
|
2014-07-17 19:03:15 +02:00
|
|
|
return fmt.Errorf("should have vpc ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
2016-04-21 01:41:45 +02:00
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
IDRefreshName: "aws_security_group.web",
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupDestroy,
|
2014-07-17 19:03:15 +02:00
|
|
|
Steps: []resource.TestStep{
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2014-07-17 19:03:15 +02:00
|
|
|
Config: testAccAWSSecurityGroupConfigVpc,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupExists("aws_security_group.web", &group),
|
|
|
|
testAccCheckAWSSecurityGroupAttributes(&group),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "name", "terraform_acceptance_test_example"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "description", "Used in the terraform acceptance tests"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2015-03-30 21:54:03 +02:00
|
|
|
"aws_security_group.web", "ingress.3629188364.protocol", "tcp"),
|
2014-07-17 19:03:15 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-03-30 21:54:03 +02:00
|
|
|
"aws_security_group.web", "ingress.3629188364.from_port", "80"),
|
2014-07-17 19:03:15 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-03-30 21:54:03 +02:00
|
|
|
"aws_security_group.web", "ingress.3629188364.to_port", "8000"),
|
2014-07-17 19:03:15 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-03-30 21:54:03 +02:00
|
|
|
"aws_security_group.web", "ingress.3629188364.cidr_blocks.#", "1"),
|
2014-07-17 19:03:15 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-03-30 21:54:03 +02:00
|
|
|
"aws_security_group.web", "ingress.3629188364.cidr_blocks.0", "10.0.0.0/8"),
|
2015-01-23 15:46:20 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-03-30 21:54:03 +02:00
|
|
|
"aws_security_group.web", "egress.3629188364.protocol", "tcp"),
|
2015-01-23 15:46:20 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-03-30 21:54:03 +02:00
|
|
|
"aws_security_group.web", "egress.3629188364.from_port", "80"),
|
2015-01-23 15:46:20 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-03-30 21:54:03 +02:00
|
|
|
"aws_security_group.web", "egress.3629188364.to_port", "8000"),
|
2015-01-23 15:46:20 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-03-30 21:54:03 +02:00
|
|
|
"aws_security_group.web", "egress.3629188364.cidr_blocks.#", "1"),
|
2015-01-23 15:46:20 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
2015-03-30 21:54:03 +02:00
|
|
|
"aws_security_group.web", "egress.3629188364.cidr_blocks.0", "10.0.0.0/8"),
|
2014-07-17 19:03:15 +02:00
|
|
|
testCheck,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-05-05 05:55:32 +02:00
|
|
|
func TestAccAWSSecurityGroup_vpcNegOneIngress(t *testing.T) {
|
|
|
|
var group ec2.SecurityGroup
|
|
|
|
|
|
|
|
testCheck := func(*terraform.State) error {
|
2015-08-17 20:27:16 +02:00
|
|
|
if *group.VpcId == "" {
|
2015-05-05 05:55:32 +02:00
|
|
|
return fmt.Errorf("should have vpc ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
2016-04-21 01:41:45 +02:00
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
IDRefreshName: "aws_security_group.web",
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupDestroy,
|
2015-05-05 05:55:32 +02:00
|
|
|
Steps: []resource.TestStep{
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2015-05-05 05:55:32 +02:00
|
|
|
Config: testAccAWSSecurityGroupConfigVpcNegOneIngress,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupExists("aws_security_group.web", &group),
|
|
|
|
testAccCheckAWSSecurityGroupAttributesNegOneProtocol(&group),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "name", "terraform_acceptance_test_example"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "description", "Used in the terraform acceptance tests"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "ingress.956249133.protocol", "-1"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "ingress.956249133.from_port", "0"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "ingress.956249133.to_port", "0"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "ingress.956249133.cidr_blocks.#", "1"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "ingress.956249133.cidr_blocks.0", "10.0.0.0/8"),
|
|
|
|
testCheck,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2016-09-21 20:52:23 +02:00
|
|
|
func TestAccAWSSecurityGroup_vpcProtoNumIngress(t *testing.T) {
|
|
|
|
var group ec2.SecurityGroup
|
|
|
|
|
|
|
|
testCheck := func(*terraform.State) error {
|
|
|
|
if *group.VpcId == "" {
|
|
|
|
return fmt.Errorf("should have vpc ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
IDRefreshName: "aws_security_group.web",
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2016-09-21 20:52:23 +02:00
|
|
|
Config: testAccAWSSecurityGroupConfigVpcProtoNumIngress,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupExists("aws_security_group.web", &group),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "name", "terraform_acceptance_test_example"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "description", "Used in the terraform acceptance tests"),
|
|
|
|
resource.TestCheckResourceAttr(
|
2016-09-21 23:12:56 +02:00
|
|
|
"aws_security_group.web", "ingress.2449525218.protocol", "50"),
|
2016-09-21 20:52:23 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2016-09-21 23:12:56 +02:00
|
|
|
"aws_security_group.web", "ingress.2449525218.from_port", "0"),
|
2016-09-21 20:52:23 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2016-09-21 23:12:56 +02:00
|
|
|
"aws_security_group.web", "ingress.2449525218.to_port", "0"),
|
2016-09-21 20:52:23 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2016-09-21 23:12:56 +02:00
|
|
|
"aws_security_group.web", "ingress.2449525218.cidr_blocks.#", "1"),
|
2016-09-21 20:52:23 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
2016-09-21 23:12:56 +02:00
|
|
|
"aws_security_group.web", "ingress.2449525218.cidr_blocks.0", "10.0.0.0/8"),
|
2016-09-21 20:52:23 +02:00
|
|
|
testCheck,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2014-07-29 18:06:28 +02:00
|
|
|
func TestAccAWSSecurityGroup_MultiIngress(t *testing.T) {
|
2015-03-09 16:02:27 +01:00
|
|
|
var group ec2.SecurityGroup
|
2014-07-29 18:06:28 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
2016-04-21 01:41:45 +02:00
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
IDRefreshName: "aws_security_group.web",
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupDestroy,
|
2014-07-29 18:06:28 +02:00
|
|
|
Steps: []resource.TestStep{
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2014-07-29 18:06:28 +02:00
|
|
|
Config: testAccAWSSecurityGroupConfigMultiIngress,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupExists("aws_security_group.web", &group),
|
|
|
|
),
|
|
|
|
},
|
2014-08-20 20:18:00 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAWSSecurityGroup_Change(t *testing.T) {
|
2015-03-09 16:02:27 +01:00
|
|
|
var group ec2.SecurityGroup
|
2014-08-20 20:18:00 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
2016-04-21 01:41:45 +02:00
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
IDRefreshName: "aws_security_group.web",
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupDestroy,
|
2014-08-20 20:18:00 +02:00
|
|
|
Steps: []resource.TestStep{
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2014-08-20 20:18:00 +02:00
|
|
|
Config: testAccAWSSecurityGroupConfig,
|
2014-07-29 18:06:28 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupExists("aws_security_group.web", &group),
|
|
|
|
),
|
|
|
|
},
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2014-08-20 20:18:00 +02:00
|
|
|
Config: testAccAWSSecurityGroupConfigChange,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupExists("aws_security_group.web", &group),
|
|
|
|
testAccCheckAWSSecurityGroupAttributesChanged(&group),
|
|
|
|
),
|
|
|
|
},
|
2014-07-29 18:06:28 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-22 19:56:06 +02:00
|
|
|
func TestAccAWSSecurityGroup_generatedName(t *testing.T) {
|
|
|
|
var group ec2.SecurityGroup
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
2016-04-21 01:41:45 +02:00
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
IDRefreshName: "aws_security_group.web",
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupDestroy,
|
2015-04-22 19:56:06 +02:00
|
|
|
Steps: []resource.TestStep{
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2015-04-22 19:56:06 +02:00
|
|
|
Config: testAccAWSSecurityGroupConfig_generatedName,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupExists("aws_security_group.web", &group),
|
2015-04-22 20:27:20 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "description", "Managed by Terraform"),
|
2015-04-22 19:56:06 +02:00
|
|
|
func(s *terraform.State) error {
|
|
|
|
if group.GroupName == nil {
|
|
|
|
return fmt.Errorf("bad: No SG name")
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(*group.GroupName, "terraform-") {
|
|
|
|
return fmt.Errorf("No terraform- prefix: %s", *group.GroupName)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-06 12:01:21 +02:00
|
|
|
func TestAccAWSSecurityGroup_DefaultEgress_VPC(t *testing.T) {
|
2015-05-01 21:33:23 +02:00
|
|
|
|
2015-04-20 20:38:21 +02:00
|
|
|
// VPC
|
|
|
|
resource.Test(t, resource.TestCase{
|
2016-04-21 01:41:45 +02:00
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
IDRefreshName: "aws_security_group.worker",
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupDestroy,
|
2015-04-20 20:38:21 +02:00
|
|
|
Steps: []resource.TestStep{
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2015-04-20 20:38:21 +02:00
|
|
|
Config: testAccAWSSecurityGroupConfigDefaultEgress,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupExistsWithoutDefault("aws_security_group.worker"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2016-06-06 12:01:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAWSSecurityGroup_DefaultEgress_Classic(t *testing.T) {
|
2015-04-20 20:38:21 +02:00
|
|
|
|
|
|
|
// Classic
|
|
|
|
var group ec2.SecurityGroup
|
|
|
|
resource.Test(t, resource.TestCase{
|
2016-04-21 01:41:45 +02:00
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
IDRefreshName: "aws_security_group.web",
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupDestroy,
|
2015-04-20 20:38:21 +02:00
|
|
|
Steps: []resource.TestStep{
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2015-04-20 20:38:21 +02:00
|
|
|
Config: testAccAWSSecurityGroupConfigClassic,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupExists("aws_security_group.web", &group),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2015-05-01 21:33:23 +02:00
|
|
|
}
|
|
|
|
|
2016-02-11 17:07:36 +01:00
|
|
|
// Testing drift detection with groups containing the same port and types
|
|
|
|
func TestAccAWSSecurityGroup_drift(t *testing.T) {
|
|
|
|
var group ec2.SecurityGroup
|
|
|
|
resource.Test(t, resource.TestCase{
|
2016-04-21 17:36:54 +02:00
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupDestroy,
|
2016-02-11 17:07:36 +01:00
|
|
|
Steps: []resource.TestStep{
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2016-02-11 17:07:36 +01:00
|
|
|
Config: testAccAWSSecurityGroupConfig_drift(),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupExists("aws_security_group.web", &group),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "description", "Used in the terraform acceptance tests"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "ingress.3629188364.protocol", "tcp"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "ingress.3629188364.from_port", "80"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "ingress.3629188364.to_port", "8000"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "ingress.3629188364.cidr_blocks.#", "1"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "ingress.3629188364.cidr_blocks.0", "10.0.0.0/8"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAWSSecurityGroup_drift_complex(t *testing.T) {
|
|
|
|
var group ec2.SecurityGroup
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
2016-04-21 17:36:54 +02:00
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupDestroy,
|
2016-02-11 17:07:36 +01:00
|
|
|
Steps: []resource.TestStep{
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2016-02-11 17:07:36 +01:00
|
|
|
Config: testAccAWSSecurityGroupConfig_drift_complex(),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupExists("aws_security_group.web", &group),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "description", "Used in the terraform acceptance tests"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "ingress.3629188364.protocol", "tcp"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "ingress.3629188364.from_port", "80"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "ingress.3629188364.to_port", "8000"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "ingress.3629188364.cidr_blocks.#", "1"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "ingress.3629188364.cidr_blocks.0", "10.0.0.0/8"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-03-16 16:49:51 +01:00
|
|
|
func TestAccAWSSecurityGroup_invalidCIDRBlock(t *testing.T) {
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: testAccAWSSecurityGroupInvalidIngressCidr,
|
|
|
|
ExpectError: regexp.MustCompile("invalid CIDR address: 1.2.3.4/33"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Config: testAccAWSSecurityGroupInvalidEgressCidr,
|
|
|
|
ExpectError: regexp.MustCompile("invalid CIDR address: 1.2.3.4/33"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Config: testAccAWSSecurityGroupInvalidIPv6IngressCidr,
|
|
|
|
ExpectError: regexp.MustCompile("invalid CIDR address: ::/244"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Config: testAccAWSSecurityGroupInvalidIPv6EgressCidr,
|
|
|
|
ExpectError: regexp.MustCompile("invalid CIDR address: ::/244"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-10 22:39:43 +02:00
|
|
|
func testAccCheckAWSSecurityGroupDestroy(s *terraform.State) error {
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
2014-07-10 22:39:43 +02:00
|
|
|
|
2014-09-17 02:44:42 +02:00
|
|
|
for _, rs := range s.RootModule().Resources {
|
2014-07-10 22:39:43 +02:00
|
|
|
if rs.Type != "aws_security_group" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve our group
|
2015-04-09 16:10:04 +02:00
|
|
|
req := &ec2.DescribeSecurityGroupsInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
GroupIds: []*string{aws.String(rs.Primary.ID)},
|
2015-03-09 16:02:27 +01:00
|
|
|
}
|
|
|
|
resp, err := conn.DescribeSecurityGroups(req)
|
2014-07-10 22:39:43 +02:00
|
|
|
if err == nil {
|
2015-08-17 20:27:16 +02:00
|
|
|
if len(resp.SecurityGroups) > 0 && *resp.SecurityGroups[0].GroupId == rs.Primary.ID {
|
2014-09-17 02:44:42 +02:00
|
|
|
return fmt.Errorf("Security Group (%s) still exists.", rs.Primary.ID)
|
2014-07-10 22:39:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-20 13:21:23 +02:00
|
|
|
ec2err, ok := err.(awserr.Error)
|
2014-07-10 22:39:43 +02:00
|
|
|
if !ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Confirm error code is what we want
|
2015-05-20 13:21:23 +02:00
|
|
|
if ec2err.Code() != "InvalidGroup.NotFound" {
|
2014-07-10 22:39:43 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-04 15:21:08 +01:00
|
|
|
func testAccCheckAWSSecurityGroupGeneratedNamePrefix(
|
2015-12-17 18:24:24 +01:00
|
|
|
resource, prefix string) resource.TestCheckFunc {
|
2015-12-04 15:21:08 +01:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
r, ok := s.RootModule().Resources[resource]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Resource not found")
|
|
|
|
}
|
|
|
|
name, ok := r.Primary.Attributes["name"]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Name attr not found: %#v", r.Primary.Attributes)
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(name, prefix) {
|
|
|
|
return fmt.Errorf("Name: %q, does not have prefix: %q", name, prefix)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 16:02:27 +01:00
|
|
|
func testAccCheckAWSSecurityGroupExists(n string, group *ec2.SecurityGroup) resource.TestCheckFunc {
|
2014-07-10 22:39:43 +02:00
|
|
|
return func(s *terraform.State) error {
|
2014-09-17 02:44:42 +02:00
|
|
|
rs, ok := s.RootModule().Resources[n]
|
2014-07-10 22:39:43 +02:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:44:42 +02:00
|
|
|
if rs.Primary.ID == "" {
|
2014-07-10 22:39:43 +02:00
|
|
|
return fmt.Errorf("No Security Group is set")
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:05:55 +02:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
2015-04-09 16:10:04 +02:00
|
|
|
req := &ec2.DescribeSecurityGroupsInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
GroupIds: []*string{aws.String(rs.Primary.ID)},
|
2014-07-10 22:39:43 +02:00
|
|
|
}
|
2015-03-09 16:02:27 +01:00
|
|
|
resp, err := conn.DescribeSecurityGroups(req)
|
2014-07-10 22:39:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
if len(resp.SecurityGroups) > 0 && *resp.SecurityGroups[0].GroupId == rs.Primary.ID {
|
2015-04-09 16:10:04 +02:00
|
|
|
*group = *resp.SecurityGroups[0]
|
2014-07-10 22:39:43 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-25 06:50:35 +02:00
|
|
|
return fmt.Errorf("Security Group not found")
|
2014-07-10 22:39:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 16:02:27 +01:00
|
|
|
func testAccCheckAWSSecurityGroupAttributes(group *ec2.SecurityGroup) resource.TestCheckFunc {
|
2014-07-16 17:14:23 +02:00
|
|
|
return func(s *terraform.State) error {
|
2015-08-17 20:27:16 +02:00
|
|
|
p := &ec2.IpPermission{
|
2015-07-28 22:29:46 +02:00
|
|
|
FromPort: aws.Int64(80),
|
|
|
|
ToPort: aws.Int64(8000),
|
2015-08-17 20:27:16 +02:00
|
|
|
IpProtocol: aws.String("tcp"),
|
2017-03-14 12:02:50 +01:00
|
|
|
IpRanges: []*ec2.IpRange{{CidrIp: aws.String("10.0.0.0/8")}},
|
2014-07-16 17:14:23 +02:00
|
|
|
}
|
|
|
|
|
2015-03-09 16:02:27 +01:00
|
|
|
if *group.GroupName != "terraform_acceptance_test_example" {
|
|
|
|
return fmt.Errorf("Bad name: %s", *group.GroupName)
|
2014-07-16 17:14:23 +02:00
|
|
|
}
|
|
|
|
|
2015-03-09 16:02:27 +01:00
|
|
|
if *group.Description != "Used in the terraform acceptance tests" {
|
|
|
|
return fmt.Errorf("Bad description: %s", *group.Description)
|
2014-07-16 17:14:23 +02:00
|
|
|
}
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
if len(group.IpPermissions) == 0 {
|
2014-09-30 23:19:16 +02:00
|
|
|
return fmt.Errorf("No IPPerms")
|
|
|
|
}
|
|
|
|
|
2014-07-16 17:14:23 +02:00
|
|
|
// Compare our ingress
|
2015-08-17 20:27:16 +02:00
|
|
|
if !reflect.DeepEqual(group.IpPermissions[0], p) {
|
2014-07-16 17:14:23 +02:00
|
|
|
return fmt.Errorf(
|
|
|
|
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
|
2015-08-17 20:27:16 +02:00
|
|
|
group.IpPermissions[0],
|
2014-07-16 17:14:23 +02:00
|
|
|
p)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-05 05:55:32 +02:00
|
|
|
func testAccCheckAWSSecurityGroupAttributesNegOneProtocol(group *ec2.SecurityGroup) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
2015-08-17 20:27:16 +02:00
|
|
|
p := &ec2.IpPermission{
|
|
|
|
IpProtocol: aws.String("-1"),
|
2017-03-14 12:02:50 +01:00
|
|
|
IpRanges: []*ec2.IpRange{{CidrIp: aws.String("10.0.0.0/8")}},
|
2015-05-05 05:55:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if *group.GroupName != "terraform_acceptance_test_example" {
|
|
|
|
return fmt.Errorf("Bad name: %s", *group.GroupName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if *group.Description != "Used in the terraform acceptance tests" {
|
|
|
|
return fmt.Errorf("Bad description: %s", *group.Description)
|
|
|
|
}
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
if len(group.IpPermissions) == 0 {
|
2015-05-05 05:55:32 +02:00
|
|
|
return fmt.Errorf("No IPPerms")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare our ingress
|
2015-08-17 20:27:16 +02:00
|
|
|
if !reflect.DeepEqual(group.IpPermissions[0], p) {
|
2015-05-05 05:55:32 +02:00
|
|
|
return fmt.Errorf(
|
|
|
|
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
|
2015-08-17 20:27:16 +02:00
|
|
|
group.IpPermissions[0],
|
2015-05-05 05:55:32 +02:00
|
|
|
p)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-14 23:07:01 +02:00
|
|
|
func TestAccAWSSecurityGroup_tags(t *testing.T) {
|
2015-03-09 16:02:27 +01:00
|
|
|
var group ec2.SecurityGroup
|
2014-10-14 23:07:01 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2014-10-14 23:07:01 +02:00
|
|
|
Config: testAccAWSSecurityGroupConfigTags,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupExists("aws_security_group.foo", &group),
|
2015-05-12 21:58:10 +02:00
|
|
|
testAccCheckTags(&group.Tags, "foo", "bar"),
|
2014-10-14 23:07:01 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2014-10-14 23:07:01 +02:00
|
|
|
Config: testAccAWSSecurityGroupConfigTagsUpdate,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupExists("aws_security_group.foo", &group),
|
2015-05-12 21:58:10 +02:00
|
|
|
testAccCheckTags(&group.Tags, "foo", ""),
|
|
|
|
testAccCheckTags(&group.Tags, "bar", "baz"),
|
2016-08-15 16:11:52 +02:00
|
|
|
testAccCheckTags(&group.Tags, "env", "Production"),
|
2014-10-14 23:07:01 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-02-11 17:07:36 +01:00
|
|
|
func TestAccAWSSecurityGroup_CIDRandGroups(t *testing.T) {
|
|
|
|
var group ec2.SecurityGroup
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2016-02-11 17:07:36 +01:00
|
|
|
Config: testAccAWSSecurityGroupCombindCIDRandGroups,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupExists("aws_security_group.mixed", &group),
|
|
|
|
// testAccCheckAWSSecurityGroupAttributes(&group),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-03-04 09:28:37 +01:00
|
|
|
func TestAccAWSSecurityGroup_ingressWithCidrAndSGs(t *testing.T) {
|
|
|
|
var group ec2.SecurityGroup
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2016-03-04 09:28:37 +01:00
|
|
|
Config: testAccAWSSecurityGroupConfig_ingressWithCidrAndSGs,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupExists("aws_security_group.web", &group),
|
|
|
|
testAccCheckAWSSecurityGroupSGandCidrAttributes(&group),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "name", "terraform_acceptance_test_example"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "description", "Used in the terraform acceptance tests"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "ingress.#", "2"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test requires an EC2 Classic region
|
|
|
|
func TestAccAWSSecurityGroup_ingressWithCidrAndSGs_classic(t *testing.T) {
|
|
|
|
var group ec2.SecurityGroup
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2016-03-04 09:28:37 +01:00
|
|
|
Config: testAccAWSSecurityGroupConfig_ingressWithCidrAndSGs_classic,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupExists("aws_security_group.web", &group),
|
|
|
|
testAccCheckAWSSecurityGroupSGandCidrAttributes(&group),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "name", "terraform_acceptance_test_example"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "description", "Used in the terraform acceptance tests"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.web", "ingress.#", "2"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-06 12:07:19 +02:00
|
|
|
func TestAccAWSSecurityGroup_egressWithPrefixList(t *testing.T) {
|
|
|
|
var group ec2.SecurityGroup
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2016-06-06 12:07:19 +02:00
|
|
|
Config: testAccAWSSecurityGroupConfigPrefixListEgress,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupExists("aws_security_group.egress", &group),
|
|
|
|
testAccCheckAWSSecurityGroupPrefixListAttributes(&group),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.egress", "egress.#", "1"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-05 23:53:04 +02:00
|
|
|
func TestAccAWSSecurityGroup_ipv4andipv6Egress(t *testing.T) {
|
|
|
|
var group ec2.SecurityGroup
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: testAccAWSSecurityGroupConfigIpv4andIpv6Egress,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupExists("aws_security_group.egress", &group),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group.egress", "egress.#", "2"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-03-04 09:28:37 +01:00
|
|
|
func testAccCheckAWSSecurityGroupSGandCidrAttributes(group *ec2.SecurityGroup) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
if *group.GroupName != "terraform_acceptance_test_example" {
|
|
|
|
return fmt.Errorf("Bad name: %s", *group.GroupName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if *group.Description != "Used in the terraform acceptance tests" {
|
|
|
|
return fmt.Errorf("Bad description: %s", *group.Description)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(group.IpPermissions) == 0 {
|
|
|
|
return fmt.Errorf("No IPPerms")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(group.IpPermissions) != 2 {
|
|
|
|
return fmt.Errorf("Expected 2 ingress rules, got %d", len(group.IpPermissions))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range group.IpPermissions {
|
|
|
|
if *p.FromPort == int64(22) {
|
|
|
|
if len(p.IpRanges) != 1 || p.UserIdGroupPairs != nil {
|
|
|
|
return fmt.Errorf("Found ip perm of 22, but not the right ipranges / pairs: %s", p)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
} else if *p.FromPort == int64(80) {
|
|
|
|
if len(p.IpRanges) != 1 || len(p.UserIdGroupPairs) != 1 {
|
|
|
|
return fmt.Errorf("Found ip perm of 80, but not the right ipranges / pairs: %s", p)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Found a rouge rule")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-06 12:07:19 +02:00
|
|
|
func testAccCheckAWSSecurityGroupPrefixListAttributes(group *ec2.SecurityGroup) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
if *group.GroupName != "terraform_acceptance_test_prefix_list_egress" {
|
|
|
|
return fmt.Errorf("Bad name: %s", *group.GroupName)
|
|
|
|
}
|
|
|
|
if *group.Description != "Used in the terraform acceptance tests" {
|
|
|
|
return fmt.Errorf("Bad description: %s", *group.Description)
|
|
|
|
}
|
|
|
|
if len(group.IpPermissionsEgress) == 0 {
|
|
|
|
return fmt.Errorf("No egress IPPerms")
|
|
|
|
}
|
|
|
|
if len(group.IpPermissionsEgress) != 1 {
|
|
|
|
return fmt.Errorf("Expected 1 egress rule, got %d", len(group.IpPermissions))
|
|
|
|
}
|
|
|
|
|
|
|
|
p := group.IpPermissionsEgress[0]
|
|
|
|
|
|
|
|
if len(p.PrefixListIds) != 1 {
|
|
|
|
return fmt.Errorf("Expected 1 prefix list, got %d", len(p.PrefixListIds))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 16:02:27 +01:00
|
|
|
func testAccCheckAWSSecurityGroupAttributesChanged(group *ec2.SecurityGroup) resource.TestCheckFunc {
|
2014-08-20 20:18:00 +02:00
|
|
|
return func(s *terraform.State) error {
|
2015-08-17 20:27:16 +02:00
|
|
|
p := []*ec2.IpPermission{
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2015-07-28 22:29:46 +02:00
|
|
|
FromPort: aws.Int64(80),
|
|
|
|
ToPort: aws.Int64(9000),
|
2015-08-17 20:27:16 +02:00
|
|
|
IpProtocol: aws.String("tcp"),
|
2017-03-14 12:02:50 +01:00
|
|
|
IpRanges: []*ec2.IpRange{{CidrIp: aws.String("10.0.0.0/8")}},
|
2014-08-20 20:18:00 +02:00
|
|
|
},
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2015-07-28 22:29:46 +02:00
|
|
|
FromPort: aws.Int64(80),
|
|
|
|
ToPort: aws.Int64(8000),
|
2015-08-17 20:27:16 +02:00
|
|
|
IpProtocol: aws.String("tcp"),
|
|
|
|
IpRanges: []*ec2.IpRange{
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2015-08-17 20:27:16 +02:00
|
|
|
CidrIp: aws.String("0.0.0.0/0"),
|
2015-04-09 16:10:04 +02:00
|
|
|
},
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2015-08-17 20:27:16 +02:00
|
|
|
CidrIp: aws.String("10.0.0.0/8"),
|
2015-04-09 16:10:04 +02:00
|
|
|
},
|
|
|
|
},
|
2014-08-20 20:18:00 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-03-09 16:02:27 +01:00
|
|
|
if *group.GroupName != "terraform_acceptance_test_example" {
|
|
|
|
return fmt.Errorf("Bad name: %s", *group.GroupName)
|
2014-08-20 20:18:00 +02:00
|
|
|
}
|
|
|
|
|
2015-03-09 16:02:27 +01:00
|
|
|
if *group.Description != "Used in the terraform acceptance tests" {
|
|
|
|
return fmt.Errorf("Bad description: %s", *group.Description)
|
2014-08-20 20:18:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compare our ingress
|
2015-08-17 20:27:16 +02:00
|
|
|
if len(group.IpPermissions) != 2 {
|
2014-08-20 20:18:00 +02:00
|
|
|
return fmt.Errorf(
|
|
|
|
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
|
2015-08-17 20:27:16 +02:00
|
|
|
group.IpPermissions,
|
2014-08-20 20:18:00 +02:00
|
|
|
p)
|
|
|
|
}
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
if *group.IpPermissions[0].ToPort == 8000 {
|
|
|
|
group.IpPermissions[1], group.IpPermissions[0] =
|
|
|
|
group.IpPermissions[0], group.IpPermissions[1]
|
2014-08-20 20:18:00 +02:00
|
|
|
}
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
if !reflect.DeepEqual(group.IpPermissions, p) {
|
2014-08-20 20:18:00 +02:00
|
|
|
return fmt.Errorf(
|
|
|
|
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
|
2015-08-17 20:27:16 +02:00
|
|
|
group.IpPermissions,
|
2014-08-20 20:18:00 +02:00
|
|
|
p)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-01 21:33:23 +02:00
|
|
|
func testAccCheckAWSSecurityGroupExistsWithoutDefault(n string) resource.TestCheckFunc {
|
2015-04-20 20:38:21 +02:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No Security Group is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
|
|
|
req := &ec2.DescribeSecurityGroupsInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
GroupIds: []*string{aws.String(rs.Primary.ID)},
|
2015-04-20 20:38:21 +02:00
|
|
|
}
|
|
|
|
resp, err := conn.DescribeSecurityGroups(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
if len(resp.SecurityGroups) > 0 && *resp.SecurityGroups[0].GroupId == rs.Primary.ID {
|
2015-04-20 20:38:21 +02:00
|
|
|
group := *resp.SecurityGroups[0]
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
if len(group.IpPermissionsEgress) != 1 {
|
|
|
|
return fmt.Errorf("Security Group should have only 1 egress rule, got %d", len(group.IpPermissionsEgress))
|
2015-04-20 20:38:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2015-05-01 21:33:23 +02:00
|
|
|
}
|
|
|
|
|
2016-06-15 21:37:45 +02:00
|
|
|
func TestAccAWSSecurityGroup_failWithDiffMismatch(t *testing.T) {
|
|
|
|
var group ec2.SecurityGroup
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2017-03-14 12:02:50 +01:00
|
|
|
{
|
2016-06-15 21:37:45 +02:00
|
|
|
Config: testAccAWSSecurityGroupConfig_failWithDiffMismatch,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupExists("aws_security_group.nat", &group),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-08-15 16:11:52 +02:00
|
|
|
const testAccAWSSecurityGroupConfigForTagsOrdering = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "web" {
|
|
|
|
name = "terraform_acceptance_test_example"
|
|
|
|
description = "Used in the terraform acceptance tests"
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
|
|
|
|
ingress {
|
|
|
|
protocol = "6"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 80000
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
|
|
|
}
|
|
|
|
|
|
|
|
egress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
|
|
|
}
|
|
|
|
|
|
|
|
tags {
|
|
|
|
Name = "tf-acc-test"
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
2017-03-14 12:02:50 +01:00
|
|
|
const testAccAWSSecurityGroupConfigIpv6 = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "web" {
|
|
|
|
name = "terraform_acceptance_test_example"
|
|
|
|
description = "Used in the terraform acceptance tests"
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
|
|
|
|
ingress {
|
|
|
|
protocol = "6"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
ipv6_cidr_blocks = ["::/0"]
|
|
|
|
}
|
|
|
|
|
|
|
|
egress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
ipv6_cidr_blocks = ["::/0"]
|
|
|
|
}
|
|
|
|
|
|
|
|
tags {
|
|
|
|
Name = "tf-acc-test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2014-07-10 22:39:43 +02:00
|
|
|
const testAccAWSSecurityGroupConfig = `
|
2016-04-21 01:41:45 +02:00
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
2014-07-10 22:39:43 +02:00
|
|
|
resource "aws_security_group" "web" {
|
2014-12-12 23:21:20 +01:00
|
|
|
name = "terraform_acceptance_test_example"
|
|
|
|
description = "Used in the terraform acceptance tests"
|
2016-04-21 01:41:45 +02:00
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
2014-12-12 23:21:20 +01:00
|
|
|
|
|
|
|
ingress {
|
2016-03-25 19:49:44 +01:00
|
|
|
protocol = "6"
|
2014-12-12 23:21:20 +01:00
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
|
|
|
}
|
2015-04-09 16:10:04 +02:00
|
|
|
|
2015-05-01 21:56:16 +02:00
|
|
|
egress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
|
|
|
}
|
|
|
|
|
2015-04-09 16:10:04 +02:00
|
|
|
tags {
|
|
|
|
Name = "tf-acc-test"
|
|
|
|
}
|
2014-07-10 22:39:43 +02:00
|
|
|
}
|
|
|
|
`
|
2014-07-17 19:03:15 +02:00
|
|
|
|
2014-08-20 20:18:00 +02:00
|
|
|
const testAccAWSSecurityGroupConfigChange = `
|
2016-04-21 01:41:45 +02:00
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
2014-08-20 20:18:00 +02:00
|
|
|
resource "aws_security_group" "web" {
|
2014-12-12 23:21:20 +01:00
|
|
|
name = "terraform_acceptance_test_example"
|
|
|
|
description = "Used in the terraform acceptance tests"
|
2016-04-21 01:41:45 +02:00
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
2014-12-12 23:21:20 +01:00
|
|
|
|
|
|
|
ingress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 9000
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
|
|
|
}
|
|
|
|
|
|
|
|
ingress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
cidr_blocks = ["0.0.0.0/0", "10.0.0.0/8"]
|
|
|
|
}
|
2015-05-01 21:56:16 +02:00
|
|
|
|
|
|
|
egress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
|
|
|
}
|
2014-08-20 20:18:00 +02:00
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2014-09-30 23:19:16 +02:00
|
|
|
const testAccAWSSecurityGroupConfigSelf = `
|
2016-04-21 01:41:45 +02:00
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
2014-09-30 23:19:16 +02:00
|
|
|
resource "aws_security_group" "web" {
|
2014-12-12 23:21:20 +01:00
|
|
|
name = "terraform_acceptance_test_example"
|
|
|
|
description = "Used in the terraform acceptance tests"
|
2016-04-21 01:41:45 +02:00
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
2014-12-12 23:21:20 +01:00
|
|
|
|
|
|
|
ingress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
self = true
|
|
|
|
}
|
2015-05-01 21:56:16 +02:00
|
|
|
|
|
|
|
egress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
|
|
|
}
|
2014-09-30 23:19:16 +02:00
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2014-07-17 19:03:15 +02:00
|
|
|
const testAccAWSSecurityGroupConfigVpc = `
|
|
|
|
resource "aws_vpc" "foo" {
|
2014-12-12 23:21:20 +01:00
|
|
|
cidr_block = "10.1.0.0/16"
|
2014-07-17 19:03:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "web" {
|
2014-12-12 23:21:20 +01:00
|
|
|
name = "terraform_acceptance_test_example"
|
|
|
|
description = "Used in the terraform acceptance tests"
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
|
|
|
|
ingress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
|
|
|
}
|
2015-01-23 15:46:20 +01:00
|
|
|
|
|
|
|
egress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
|
|
|
}
|
2014-07-17 19:03:15 +02:00
|
|
|
}
|
|
|
|
`
|
2014-07-29 18:06:28 +02:00
|
|
|
|
2015-05-05 05:55:32 +02:00
|
|
|
const testAccAWSSecurityGroupConfigVpcNegOneIngress = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "web" {
|
|
|
|
name = "terraform_acceptance_test_example"
|
|
|
|
description = "Used in the terraform acceptance tests"
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
|
|
|
|
ingress {
|
|
|
|
protocol = "-1"
|
|
|
|
from_port = 0
|
|
|
|
to_port = 0
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
2016-09-21 20:52:23 +02:00
|
|
|
|
|
|
|
const testAccAWSSecurityGroupConfigVpcProtoNumIngress = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "web" {
|
|
|
|
name = "terraform_acceptance_test_example"
|
|
|
|
description = "Used in the terraform acceptance tests"
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
|
|
|
|
ingress {
|
|
|
|
protocol = "50"
|
|
|
|
from_port = 0
|
|
|
|
to_port = 0
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2014-07-29 18:06:28 +02:00
|
|
|
const testAccAWSSecurityGroupConfigMultiIngress = `
|
2016-04-21 01:41:45 +02:00
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
2014-07-29 18:06:28 +02:00
|
|
|
resource "aws_security_group" "worker" {
|
2014-12-12 23:21:20 +01:00
|
|
|
name = "terraform_acceptance_test_example_1"
|
|
|
|
description = "Used in the terraform acceptance tests"
|
2016-04-21 01:41:45 +02:00
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
2014-12-12 23:21:20 +01:00
|
|
|
|
|
|
|
ingress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
|
|
|
}
|
2015-05-01 21:56:16 +02:00
|
|
|
|
|
|
|
egress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
|
|
|
}
|
2014-07-29 18:06:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "web" {
|
2014-12-12 23:21:20 +01:00
|
|
|
name = "terraform_acceptance_test_example_2"
|
|
|
|
description = "Used in the terraform acceptance tests"
|
2016-04-21 01:41:45 +02:00
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
2014-12-12 23:21:20 +01:00
|
|
|
|
|
|
|
ingress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 22
|
|
|
|
to_port = 22
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
|
|
|
}
|
|
|
|
|
|
|
|
ingress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 800
|
|
|
|
to_port = 800
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
|
|
|
}
|
|
|
|
|
|
|
|
ingress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
security_groups = ["${aws_security_group.worker.id}"]
|
|
|
|
}
|
2015-05-01 21:56:16 +02:00
|
|
|
|
|
|
|
egress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
|
|
|
}
|
2014-07-29 18:06:28 +02:00
|
|
|
}
|
|
|
|
`
|
2014-10-14 23:07:01 +02:00
|
|
|
|
|
|
|
const testAccAWSSecurityGroupConfigTags = `
|
2016-04-21 01:41:45 +02:00
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
2014-10-14 23:07:01 +02:00
|
|
|
resource "aws_security_group" "foo" {
|
2016-04-21 01:41:45 +02:00
|
|
|
name = "terraform_acceptance_test_example"
|
2014-12-12 23:21:20 +01:00
|
|
|
description = "Used in the terraform acceptance tests"
|
2016-04-21 01:41:45 +02:00
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
2014-12-12 23:21:20 +01:00
|
|
|
|
|
|
|
ingress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
|
|
|
}
|
|
|
|
|
2015-05-01 21:56:16 +02:00
|
|
|
egress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
|
|
|
}
|
|
|
|
|
2014-12-12 23:21:20 +01:00
|
|
|
tags {
|
|
|
|
foo = "bar"
|
|
|
|
}
|
2014-10-14 23:07:01 +02:00
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const testAccAWSSecurityGroupConfigTagsUpdate = `
|
2016-04-21 01:41:45 +02:00
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
2014-10-14 23:07:01 +02:00
|
|
|
resource "aws_security_group" "foo" {
|
2014-12-12 23:21:20 +01:00
|
|
|
name = "terraform_acceptance_test_example"
|
|
|
|
description = "Used in the terraform acceptance tests"
|
2016-04-21 01:41:45 +02:00
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
2014-12-12 23:21:20 +01:00
|
|
|
|
|
|
|
ingress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
|
|
|
}
|
|
|
|
|
2015-05-01 21:56:16 +02:00
|
|
|
egress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
|
|
|
}
|
|
|
|
|
2014-12-12 23:21:20 +01:00
|
|
|
tags {
|
|
|
|
bar = "baz"
|
2016-08-15 16:11:52 +02:00
|
|
|
env = "Production"
|
2014-12-12 23:21:20 +01:00
|
|
|
}
|
2014-10-14 23:07:01 +02:00
|
|
|
}
|
|
|
|
`
|
2015-04-22 19:56:06 +02:00
|
|
|
|
|
|
|
const testAccAWSSecurityGroupConfig_generatedName = `
|
2016-04-21 01:41:45 +02:00
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
2015-04-22 19:56:06 +02:00
|
|
|
resource "aws_security_group" "web" {
|
2016-04-21 01:41:45 +02:00
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
|
2015-04-22 19:56:06 +02:00
|
|
|
ingress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
|
|
|
}
|
|
|
|
|
2015-05-01 21:56:16 +02:00
|
|
|
egress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
|
|
|
}
|
|
|
|
|
2015-04-22 19:56:06 +02:00
|
|
|
tags {
|
|
|
|
Name = "tf-acc-test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
2015-05-01 21:33:23 +02:00
|
|
|
|
|
|
|
const testAccAWSSecurityGroupConfigDefaultEgress = `
|
2015-05-01 21:56:16 +02:00
|
|
|
resource "aws_vpc" "tf_sg_egress_test" {
|
|
|
|
cidr_block = "10.0.0.0/16"
|
|
|
|
tags {
|
|
|
|
Name = "tf_sg_egress_test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-01 21:33:23 +02:00
|
|
|
resource "aws_security_group" "worker" {
|
|
|
|
name = "terraform_acceptance_test_example_1"
|
|
|
|
description = "Used in the terraform acceptance tests"
|
2015-05-01 21:56:16 +02:00
|
|
|
vpc_id = "${aws_vpc.tf_sg_egress_test.id}"
|
2015-05-01 21:33:23 +02:00
|
|
|
|
|
|
|
egress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
2015-05-01 21:56:16 +02:00
|
|
|
|
|
|
|
const testAccAWSSecurityGroupConfigClassic = `
|
|
|
|
provider "aws" {
|
|
|
|
region = "us-east-1"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "web" {
|
|
|
|
name = "terraform_acceptance_test_example_1"
|
|
|
|
description = "Used in the terraform acceptance tests"
|
|
|
|
}
|
|
|
|
`
|
2015-12-04 15:21:08 +01:00
|
|
|
|
|
|
|
const testAccAWSSecurityGroupPrefixNameConfig = `
|
|
|
|
provider "aws" {
|
|
|
|
region = "us-east-1"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "baz" {
|
|
|
|
name_prefix = "baz-"
|
|
|
|
description = "Used in the terraform acceptance tests"
|
|
|
|
}
|
|
|
|
`
|
2016-02-11 17:07:36 +01:00
|
|
|
|
|
|
|
func testAccAWSSecurityGroupConfig_drift() string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "aws_security_group" "web" {
|
|
|
|
name = "tf_acc_%d"
|
|
|
|
description = "Used in the terraform acceptance tests"
|
|
|
|
|
|
|
|
ingress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
|
|
|
}
|
|
|
|
|
|
|
|
ingress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
cidr_blocks = ["206.0.0.0/8"]
|
|
|
|
}
|
|
|
|
|
|
|
|
tags {
|
|
|
|
Name = "tf-acc-test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`, acctest.RandInt())
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccAWSSecurityGroupConfig_drift_complex() string {
|
|
|
|
return fmt.Sprintf(`
|
2016-04-21 01:41:45 +02:00
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
2016-02-11 17:07:36 +01:00
|
|
|
resource "aws_security_group" "otherweb" {
|
|
|
|
name = "tf_acc_%d"
|
|
|
|
description = "Used in the terraform acceptance tests"
|
2016-04-21 01:41:45 +02:00
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
2016-02-11 17:07:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "web" {
|
|
|
|
name = "tf_acc_%d"
|
|
|
|
description = "Used in the terraform acceptance tests"
|
2016-04-21 01:41:45 +02:00
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
2016-02-11 17:07:36 +01:00
|
|
|
|
|
|
|
ingress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
|
|
|
}
|
|
|
|
|
|
|
|
ingress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
cidr_blocks = ["206.0.0.0/8"]
|
|
|
|
}
|
|
|
|
|
|
|
|
ingress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 22
|
|
|
|
to_port = 22
|
|
|
|
security_groups = ["${aws_security_group.otherweb.id}"]
|
|
|
|
}
|
|
|
|
|
|
|
|
egress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
cidr_blocks = ["206.0.0.0/8"]
|
|
|
|
}
|
|
|
|
|
|
|
|
egress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
|
|
|
}
|
|
|
|
|
|
|
|
egress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 22
|
|
|
|
to_port = 22
|
|
|
|
security_groups = ["${aws_security_group.otherweb.id}"]
|
|
|
|
}
|
|
|
|
|
|
|
|
tags {
|
|
|
|
Name = "tf-acc-test"
|
|
|
|
}
|
|
|
|
}`, acctest.RandInt(), acctest.RandInt())
|
|
|
|
}
|
|
|
|
|
2017-03-16 16:49:51 +01:00
|
|
|
const testAccAWSSecurityGroupInvalidIngressCidr = `
|
|
|
|
resource "aws_security_group" "foo" {
|
|
|
|
name = "testing-foo"
|
|
|
|
description = "foo-testing"
|
|
|
|
ingress {
|
|
|
|
from_port = 0
|
|
|
|
to_port = 0
|
|
|
|
protocol = "-1"
|
|
|
|
cidr_blocks = ["1.2.3.4/33"]
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
|
|
|
const testAccAWSSecurityGroupInvalidEgressCidr = `
|
|
|
|
resource "aws_security_group" "foo" {
|
|
|
|
name = "testing-foo"
|
|
|
|
description = "foo-testing"
|
|
|
|
egress {
|
|
|
|
from_port = 0
|
|
|
|
to_port = 0
|
|
|
|
protocol = "-1"
|
|
|
|
cidr_blocks = ["1.2.3.4/33"]
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
|
|
|
const testAccAWSSecurityGroupInvalidIPv6IngressCidr = `
|
|
|
|
resource "aws_security_group" "foo" {
|
|
|
|
name = "testing-foo"
|
|
|
|
description = "foo-testing"
|
|
|
|
ingress {
|
|
|
|
from_port = 0
|
|
|
|
to_port = 0
|
|
|
|
protocol = "-1"
|
|
|
|
ipv6_cidr_blocks = ["::/244"]
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
|
|
|
const testAccAWSSecurityGroupInvalidIPv6EgressCidr = `
|
|
|
|
resource "aws_security_group" "foo" {
|
|
|
|
name = "testing-foo"
|
|
|
|
description = "foo-testing"
|
|
|
|
egress {
|
|
|
|
from_port = 0
|
|
|
|
to_port = 0
|
|
|
|
protocol = "-1"
|
|
|
|
ipv6_cidr_blocks = ["::/244"]
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
2016-02-11 17:07:36 +01:00
|
|
|
const testAccAWSSecurityGroupCombindCIDRandGroups = `
|
2016-04-21 01:41:45 +02:00
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
2016-02-11 17:07:36 +01:00
|
|
|
resource "aws_security_group" "two" {
|
|
|
|
name = "tf-test-1"
|
2016-04-21 01:41:45 +02:00
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
2016-02-11 17:07:36 +01:00
|
|
|
tags {
|
|
|
|
Name = "tf-test-1"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "one" {
|
|
|
|
name = "tf-test-2"
|
2016-04-21 01:41:45 +02:00
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
2016-02-11 17:07:36 +01:00
|
|
|
tags {
|
|
|
|
Name = "tf-test-w"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "three" {
|
|
|
|
name = "tf-test-3"
|
2016-04-21 01:41:45 +02:00
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
2016-02-11 17:07:36 +01:00
|
|
|
tags {
|
|
|
|
Name = "tf-test-3"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "mixed" {
|
|
|
|
name = "tf-mix-test"
|
2016-04-21 01:41:45 +02:00
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
2016-02-11 17:07:36 +01:00
|
|
|
|
|
|
|
ingress {
|
|
|
|
from_port = 80
|
|
|
|
to_port = 80
|
|
|
|
protocol = "tcp"
|
|
|
|
cidr_blocks = ["10.0.0.0/16", "10.1.0.0/16", "10.7.0.0/16"]
|
|
|
|
|
|
|
|
security_groups = [
|
|
|
|
"${aws_security_group.one.id}",
|
|
|
|
"${aws_security_group.two.id}",
|
|
|
|
"${aws_security_group.three.id}",
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
tags {
|
|
|
|
Name = "tf-mix-test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
2016-03-04 09:28:37 +01:00
|
|
|
|
|
|
|
const testAccAWSSecurityGroupConfig_ingressWithCidrAndSGs = `
|
2016-04-21 01:41:45 +02:00
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
}
|
|
|
|
|
2016-03-04 09:28:37 +01:00
|
|
|
resource "aws_security_group" "other_web" {
|
|
|
|
name = "tf_other_acc_tests"
|
|
|
|
description = "Used in the terraform acceptance tests"
|
2016-04-21 01:41:45 +02:00
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
2016-03-04 09:28:37 +01:00
|
|
|
|
|
|
|
tags {
|
|
|
|
Name = "tf-acc-test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "web" {
|
|
|
|
name = "terraform_acceptance_test_example"
|
|
|
|
description = "Used in the terraform acceptance tests"
|
2016-04-21 01:41:45 +02:00
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
2016-03-04 09:28:37 +01:00
|
|
|
|
|
|
|
ingress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = "22"
|
|
|
|
to_port = "22"
|
|
|
|
|
|
|
|
cidr_blocks = [
|
|
|
|
"192.168.0.1/32",
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
ingress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
|
|
|
security_groups = ["${aws_security_group.other_web.id}"]
|
|
|
|
}
|
|
|
|
|
|
|
|
egress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
|
|
|
}
|
|
|
|
|
|
|
|
tags {
|
|
|
|
Name = "tf-acc-test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const testAccAWSSecurityGroupConfig_ingressWithCidrAndSGs_classic = `
|
|
|
|
provider "aws" {
|
|
|
|
region = "us-east-1"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "other_web" {
|
|
|
|
name = "tf_other_acc_tests"
|
|
|
|
description = "Used in the terraform acceptance tests"
|
|
|
|
|
|
|
|
tags {
|
|
|
|
Name = "tf-acc-test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "web" {
|
|
|
|
name = "terraform_acceptance_test_example"
|
|
|
|
description = "Used in the terraform acceptance tests"
|
|
|
|
|
|
|
|
ingress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = "22"
|
|
|
|
to_port = "22"
|
|
|
|
|
|
|
|
cidr_blocks = [
|
|
|
|
"192.168.0.1/32",
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
ingress {
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
|
|
|
security_groups = ["${aws_security_group.other_web.name}"]
|
|
|
|
}
|
|
|
|
|
|
|
|
tags {
|
|
|
|
Name = "tf-acc-test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
2016-06-15 21:37:45 +02:00
|
|
|
|
|
|
|
// fails to apply in one pass with the error "diffs didn't match during apply"
|
|
|
|
// GH-2027
|
|
|
|
const testAccAWSSecurityGroupConfig_failWithDiffMismatch = `
|
|
|
|
resource "aws_vpc" "main" {
|
|
|
|
cidr_block = "10.0.0.0/16"
|
|
|
|
|
|
|
|
tags {
|
|
|
|
Name = "tf-test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "ssh_base" {
|
|
|
|
name = "test-ssh-base"
|
|
|
|
vpc_id = "${aws_vpc.main.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "jump" {
|
|
|
|
name = "test-jump"
|
|
|
|
vpc_id = "${aws_vpc.main.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "provision" {
|
|
|
|
name = "test-provision"
|
|
|
|
vpc_id = "${aws_vpc.main.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "nat" {
|
|
|
|
vpc_id = "${aws_vpc.main.id}"
|
|
|
|
name = "nat"
|
|
|
|
description = "For nat servers "
|
|
|
|
|
|
|
|
ingress {
|
|
|
|
from_port = 22
|
|
|
|
to_port = 22
|
|
|
|
protocol = "tcp"
|
|
|
|
security_groups = ["${aws_security_group.jump.id}"]
|
|
|
|
}
|
|
|
|
|
|
|
|
ingress {
|
|
|
|
from_port = 22
|
|
|
|
to_port = 22
|
|
|
|
protocol = "tcp"
|
|
|
|
security_groups = ["${aws_security_group.provision.id}"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
2016-06-30 22:01:38 +02:00
|
|
|
const testAccAWSSecurityGroupConfig_importSelf = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
|
|
|
|
tags {
|
|
|
|
Name = "tf_sg_import_test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "allow_all" {
|
|
|
|
name = "allow_all"
|
|
|
|
description = "Allow all inbound traffic"
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group_rule" "allow_all" {
|
|
|
|
type = "ingress"
|
|
|
|
from_port = 0
|
|
|
|
to_port = 65535
|
|
|
|
protocol = "tcp"
|
|
|
|
cidr_blocks = ["0.0.0.0/0"]
|
|
|
|
|
|
|
|
security_group_id = "${aws_security_group.allow_all.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group_rule" "allow_all-1" {
|
|
|
|
type = "ingress"
|
|
|
|
from_port = 65534
|
|
|
|
to_port = 65535
|
|
|
|
protocol = "tcp"
|
|
|
|
|
|
|
|
self = true
|
|
|
|
security_group_id = "${aws_security_group.allow_all.id}"
|
|
|
|
}
|
|
|
|
`
|
2016-06-06 12:07:19 +02:00
|
|
|
|
2016-12-09 16:50:51 +01:00
|
|
|
const testAccAWSSecurityGroupConfig_importSourceSecurityGroup = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
|
|
|
|
tags {
|
|
|
|
Name = "tf_sg_import_test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "test_group_1" {
|
|
|
|
name = "test group 1"
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "test_group_2" {
|
|
|
|
name = "test group 2"
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "test_group_3" {
|
|
|
|
name = "test group 3"
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group_rule" "allow_test_group_2" {
|
|
|
|
type = "ingress"
|
|
|
|
from_port = 0
|
|
|
|
to_port = 0
|
|
|
|
protocol = "tcp"
|
|
|
|
|
|
|
|
source_security_group_id = "${aws_security_group.test_group_1.id}"
|
|
|
|
security_group_id = "${aws_security_group.test_group_2.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group_rule" "allow_test_group_3" {
|
|
|
|
type = "ingress"
|
|
|
|
from_port = 0
|
|
|
|
to_port = 0
|
|
|
|
protocol = "tcp"
|
|
|
|
|
|
|
|
source_security_group_id = "${aws_security_group.test_group_1.id}"
|
|
|
|
security_group_id = "${aws_security_group.test_group_3.id}"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2017-04-21 18:38:41 +02:00
|
|
|
const testAccAWSSecurityGroupConfig_importIPRangeAndSecurityGroupWithSameRules = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
|
|
|
|
tags {
|
|
|
|
Name = "tf_sg_import_test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "test_group_1" {
|
|
|
|
name = "test group 1"
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "test_group_2" {
|
|
|
|
name = "test group 2"
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group_rule" "allow_security_group" {
|
|
|
|
type = "ingress"
|
|
|
|
from_port = 0
|
|
|
|
to_port = 0
|
|
|
|
protocol = "tcp"
|
|
|
|
|
|
|
|
source_security_group_id = "${aws_security_group.test_group_2.id}"
|
|
|
|
security_group_id = "${aws_security_group.test_group_1.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group_rule" "allow_cidr_block" {
|
|
|
|
type = "ingress"
|
|
|
|
from_port = 0
|
|
|
|
to_port = 0
|
|
|
|
protocol = "tcp"
|
|
|
|
|
|
|
|
cidr_blocks = ["10.0.0.0/32"]
|
|
|
|
security_group_id = "${aws_security_group.test_group_1.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group_rule" "allow_ipv6_cidr_block" {
|
|
|
|
type = "ingress"
|
|
|
|
from_port = 0
|
|
|
|
to_port = 0
|
|
|
|
protocol = "tcp"
|
|
|
|
|
|
|
|
ipv6_cidr_blocks = ["::/0"]
|
|
|
|
security_group_id = "${aws_security_group.test_group_1.id}"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const testAccAWSSecurityGroupConfig_importIPRangesWithSameRules = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
|
|
|
|
tags {
|
|
|
|
Name = "tf_sg_import_test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "test_group_1" {
|
|
|
|
name = "test group 1"
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group_rule" "allow_cidr_block" {
|
|
|
|
type = "ingress"
|
|
|
|
from_port = 0
|
|
|
|
to_port = 0
|
|
|
|
protocol = "tcp"
|
|
|
|
|
|
|
|
cidr_blocks = ["10.0.0.0/32"]
|
|
|
|
security_group_id = "${aws_security_group.test_group_1.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group_rule" "allow_ipv6_cidr_block" {
|
|
|
|
type = "ingress"
|
|
|
|
from_port = 0
|
|
|
|
to_port = 0
|
|
|
|
protocol = "tcp"
|
|
|
|
|
|
|
|
ipv6_cidr_blocks = ["::/0"]
|
|
|
|
security_group_id = "${aws_security_group.test_group_1.id}"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2017-06-05 23:53:04 +02:00
|
|
|
const testAccAWSSecurityGroupConfigIpv4andIpv6Egress = `
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
|
|
|
assign_generated_ipv6_cidr_block = true
|
|
|
|
tags {
|
|
|
|
Name = "tf_sg_ipv4_and_ipv6_acc_test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "egress" {
|
|
|
|
name = "terraform_acceptance_test_example"
|
|
|
|
description = "Used in the terraform acceptance tests"
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
ingress {
|
|
|
|
from_port = 22
|
|
|
|
to_port = 22
|
|
|
|
protocol = "6"
|
|
|
|
cidr_blocks = ["0.0.0.0/0"]
|
|
|
|
}
|
|
|
|
egress {
|
|
|
|
from_port = 0
|
|
|
|
to_port = 0
|
|
|
|
protocol = "-1"
|
|
|
|
cidr_blocks = ["0.0.0.0/0"]
|
|
|
|
}
|
|
|
|
egress {
|
|
|
|
from_port = 0
|
|
|
|
to_port = 0
|
|
|
|
protocol = "-1"
|
|
|
|
ipv6_cidr_blocks = ["::/0"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2016-06-06 12:07:19 +02:00
|
|
|
const testAccAWSSecurityGroupConfigPrefixListEgress = `
|
|
|
|
resource "aws_vpc" "tf_sg_prefix_list_egress_test" {
|
|
|
|
cidr_block = "10.0.0.0/16"
|
|
|
|
tags {
|
|
|
|
Name = "tf_sg_prefix_list_egress_test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_route_table" "default" {
|
|
|
|
vpc_id = "${aws_vpc.tf_sg_prefix_list_egress_test.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_vpc_endpoint" "s3-us-west-2" {
|
|
|
|
vpc_id = "${aws_vpc.tf_sg_prefix_list_egress_test.id}"
|
|
|
|
service_name = "com.amazonaws.us-west-2.s3"
|
|
|
|
route_table_ids = ["${aws_route_table.default.id}"]
|
|
|
|
policy = <<POLICY
|
|
|
|
{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Sid":"AllowAll",
|
|
|
|
"Effect":"Allow",
|
|
|
|
"Principal":"*",
|
|
|
|
"Action":"*",
|
|
|
|
"Resource":"*"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
POLICY
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "egress" {
|
|
|
|
name = "terraform_acceptance_test_prefix_list_egress"
|
|
|
|
description = "Used in the terraform acceptance tests"
|
|
|
|
vpc_id = "${aws_vpc.tf_sg_prefix_list_egress_test.id}"
|
2016-08-15 16:11:52 +02:00
|
|
|
|
2016-06-06 12:07:19 +02:00
|
|
|
egress {
|
|
|
|
protocol = "-1"
|
|
|
|
from_port = 0
|
|
|
|
to_port = 0
|
|
|
|
prefix_list_ids = ["${aws_vpc_endpoint.s3-us-west-2.prefix_list_id}"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|