2015-04-20 20:38:21 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2015-08-24 15:59:54 +02:00
|
|
|
"bytes"
|
2015-04-20 20:38:21 +02:00
|
|
|
"fmt"
|
2015-10-12 22:19:42 +02:00
|
|
|
"log"
|
2017-02-10 18:46:35 +01:00
|
|
|
"regexp"
|
2015-04-20 20:38:21 +02:00
|
|
|
"testing"
|
|
|
|
|
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-04 18:20:22 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2015-04-20 20:38:21 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestIpPermissionIDHash(t *testing.T) {
|
2015-08-17 20:27:16 +02:00
|
|
|
simple := &ec2.IpPermission{
|
|
|
|
IpProtocol: aws.String("tcp"),
|
2015-07-28 22:29:46 +02:00
|
|
|
FromPort: aws.Int64(int64(80)),
|
|
|
|
ToPort: aws.Int64(int64(8000)),
|
2015-08-17 20:27:16 +02:00
|
|
|
IpRanges: []*ec2.IpRange{
|
2016-12-30 01:26:01 +01:00
|
|
|
{
|
2015-08-17 20:27:16 +02:00
|
|
|
CidrIp: aws.String("10.0.0.0/8"),
|
2015-04-20 20:38:21 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
egress := &ec2.IpPermission{
|
|
|
|
IpProtocol: aws.String("tcp"),
|
2015-07-28 22:29:46 +02:00
|
|
|
FromPort: aws.Int64(int64(80)),
|
|
|
|
ToPort: aws.Int64(int64(8000)),
|
2015-08-17 20:27:16 +02:00
|
|
|
IpRanges: []*ec2.IpRange{
|
2016-12-30 01:26:01 +01:00
|
|
|
{
|
2015-08-17 20:27:16 +02:00
|
|
|
CidrIp: aws.String("10.0.0.0/8"),
|
2015-04-20 20:38:21 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
egress_all := &ec2.IpPermission{
|
|
|
|
IpProtocol: aws.String("-1"),
|
|
|
|
IpRanges: []*ec2.IpRange{
|
2016-12-30 01:26:01 +01:00
|
|
|
{
|
2015-08-17 20:27:16 +02:00
|
|
|
CidrIp: aws.String("10.0.0.0/8"),
|
2015-04-20 20:38:21 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
vpc_security_group_source := &ec2.IpPermission{
|
|
|
|
IpProtocol: aws.String("tcp"),
|
2015-07-28 22:29:46 +02:00
|
|
|
FromPort: aws.Int64(int64(80)),
|
|
|
|
ToPort: aws.Int64(int64(8000)),
|
2015-08-17 20:27:16 +02:00
|
|
|
UserIdGroupPairs: []*ec2.UserIdGroupPair{
|
2017-03-14 11:37:59 +01:00
|
|
|
{
|
2015-08-17 20:27:16 +02:00
|
|
|
UserId: aws.String("987654321"),
|
|
|
|
GroupId: aws.String("sg-12345678"),
|
2015-06-11 23:57:43 +02:00
|
|
|
},
|
2017-03-14 11:37:59 +01:00
|
|
|
{
|
2015-08-17 20:27:16 +02:00
|
|
|
UserId: aws.String("123456789"),
|
|
|
|
GroupId: aws.String("sg-987654321"),
|
2015-06-11 23:57:43 +02:00
|
|
|
},
|
2017-03-14 11:37:59 +01:00
|
|
|
{
|
2015-08-17 20:27:16 +02:00
|
|
|
UserId: aws.String("123456789"),
|
|
|
|
GroupId: aws.String("sg-12345678"),
|
2015-06-11 23:57:43 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
security_group_source := &ec2.IpPermission{
|
|
|
|
IpProtocol: aws.String("tcp"),
|
2015-07-28 22:29:46 +02:00
|
|
|
FromPort: aws.Int64(int64(80)),
|
|
|
|
ToPort: aws.Int64(int64(8000)),
|
2015-08-17 20:27:16 +02:00
|
|
|
UserIdGroupPairs: []*ec2.UserIdGroupPair{
|
2017-03-14 11:37:59 +01:00
|
|
|
{
|
2015-08-17 20:27:16 +02:00
|
|
|
UserId: aws.String("987654321"),
|
2015-06-11 23:57:43 +02:00
|
|
|
GroupName: aws.String("my-security-group"),
|
|
|
|
},
|
2017-03-14 11:37:59 +01:00
|
|
|
{
|
2015-08-17 20:27:16 +02:00
|
|
|
UserId: aws.String("123456789"),
|
2015-06-11 23:57:43 +02:00
|
|
|
GroupName: aws.String("my-security-group"),
|
|
|
|
},
|
2017-03-14 11:37:59 +01:00
|
|
|
{
|
2015-08-17 20:27:16 +02:00
|
|
|
UserId: aws.String("123456789"),
|
2015-06-11 23:57:43 +02:00
|
|
|
GroupName: aws.String("my-other-security-group"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-04-20 20:38:21 +02:00
|
|
|
// hardcoded hashes, to detect future change
|
|
|
|
cases := []struct {
|
2015-08-17 20:27:16 +02:00
|
|
|
Input *ec2.IpPermission
|
2015-04-20 20:38:21 +02:00
|
|
|
Type string
|
|
|
|
Output string
|
|
|
|
}{
|
2015-10-12 22:19:42 +02:00
|
|
|
{simple, "ingress", "sgrule-3403497314"},
|
|
|
|
{egress, "egress", "sgrule-1173186295"},
|
|
|
|
{egress_all, "egress", "sgrule-766323498"},
|
|
|
|
{vpc_security_group_source, "egress", "sgrule-351225364"},
|
|
|
|
{security_group_source, "egress", "sgrule-2198807188"},
|
2015-04-20 20:38:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
2015-10-12 22:19:42 +02:00
|
|
|
actual := ipPermissionIDHash("sg-12345", tc.Type, tc.Input)
|
2015-04-20 20:38:21 +02:00
|
|
|
if actual != tc.Output {
|
2015-07-28 22:29:46 +02:00
|
|
|
t.Errorf("input: %s - %s\noutput: %s", tc.Type, tc.Input, actual)
|
2015-04-20 20:38:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-30 21:10:19 +02:00
|
|
|
func TestAccAWSSecurityGroupRule_Ingress_VPC(t *testing.T) {
|
2015-04-20 20:38:21 +02:00
|
|
|
var group ec2.SecurityGroup
|
2017-02-10 19:19:39 +01:00
|
|
|
rInt := acctest.RandInt()
|
2015-04-20 20:38:21 +02:00
|
|
|
|
|
|
|
testRuleCount := func(*terraform.State) error {
|
2015-08-17 20:27:16 +02:00
|
|
|
if len(group.IpPermissions) != 1 {
|
2015-04-20 20:38:21 +02:00
|
|
|
return fmt.Errorf("Wrong Security Group rule count, expected %d, got %d",
|
2015-08-17 20:27:16 +02:00
|
|
|
1, len(group.IpPermissions))
|
2015-04-20 20:38:21 +02:00
|
|
|
}
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
rule := group.IpPermissions[0]
|
2015-04-20 20:38:21 +02:00
|
|
|
if *rule.FromPort != int64(80) {
|
|
|
|
return fmt.Errorf("Wrong Security Group port setting, expected %d, got %d",
|
|
|
|
80, int(*rule.FromPort))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupRuleDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2016-12-30 01:26:01 +01:00
|
|
|
{
|
2017-02-10 19:19:39 +01:00
|
|
|
Config: testAccAWSSecurityGroupRuleIngressConfig(rInt),
|
2015-04-20 20:38:21 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupRuleExists("aws_security_group.web", &group),
|
2015-10-12 22:19:42 +02:00
|
|
|
testAccCheckAWSSecurityGroupRuleAttributes("aws_security_group_rule.ingress_1", &group, nil, "ingress"),
|
2015-04-20 20:38:21 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group_rule.ingress_1", "from_port", "80"),
|
|
|
|
testRuleCount,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-03-25 19:49:44 +01:00
|
|
|
func TestAccAWSSecurityGroupRule_Ingress_Protocol(t *testing.T) {
|
|
|
|
var group ec2.SecurityGroup
|
|
|
|
|
|
|
|
testRuleCount := func(*terraform.State) error {
|
|
|
|
if len(group.IpPermissions) != 1 {
|
|
|
|
return fmt.Errorf("Wrong Security Group rule count, expected %d, got %d",
|
|
|
|
1, len(group.IpPermissions))
|
|
|
|
}
|
|
|
|
|
|
|
|
rule := group.IpPermissions[0]
|
|
|
|
if *rule.FromPort != int64(80) {
|
|
|
|
return fmt.Errorf("Wrong Security Group port setting, expected %d, got %d",
|
|
|
|
80, int(*rule.FromPort))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupRuleDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2016-12-30 01:26:01 +01:00
|
|
|
{
|
2016-03-25 19:49:44 +01:00
|
|
|
Config: testAccAWSSecurityGroupRuleIngress_protocolConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupRuleExists("aws_security_group.web", &group),
|
|
|
|
testAccCheckAWSSecurityGroupRuleAttributes("aws_security_group_rule.ingress_1", &group, nil, "ingress"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group_rule.ingress_1", "from_port", "80"),
|
|
|
|
testRuleCount,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-03-14 11:37:59 +01:00
|
|
|
func TestAccAWSSecurityGroupRule_Ingress_Ipv6(t *testing.T) {
|
|
|
|
var group ec2.SecurityGroup
|
|
|
|
|
|
|
|
testRuleCount := func(*terraform.State) error {
|
|
|
|
if len(group.IpPermissions) != 1 {
|
|
|
|
return fmt.Errorf("Wrong Security Group rule count, expected %d, got %d",
|
|
|
|
1, len(group.IpPermissions))
|
|
|
|
}
|
|
|
|
|
|
|
|
rule := group.IpPermissions[0]
|
|
|
|
if *rule.FromPort != int64(80) {
|
|
|
|
return fmt.Errorf("Wrong Security Group port setting, expected %d, got %d",
|
|
|
|
80, int(*rule.FromPort))
|
|
|
|
}
|
|
|
|
|
|
|
|
ipv6Address := rule.Ipv6Ranges[0]
|
|
|
|
if *ipv6Address.CidrIpv6 != "::/0" {
|
|
|
|
return fmt.Errorf("Wrong Security Group IPv6 address, expected %s, got %s",
|
|
|
|
"::/0", *ipv6Address.CidrIpv6)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupRuleDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: testAccAWSSecurityGroupRuleIngress_ipv6Config,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupRuleExists("aws_security_group.web", &group),
|
|
|
|
testRuleCount,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-07-30 21:10:19 +02:00
|
|
|
func TestAccAWSSecurityGroupRule_Ingress_Classic(t *testing.T) {
|
2015-04-20 20:38:21 +02:00
|
|
|
var group ec2.SecurityGroup
|
2017-02-10 19:19:39 +01:00
|
|
|
rInt := acctest.RandInt()
|
2015-04-20 20:38:21 +02:00
|
|
|
|
|
|
|
testRuleCount := func(*terraform.State) error {
|
2015-08-17 20:27:16 +02:00
|
|
|
if len(group.IpPermissions) != 1 {
|
2015-04-20 20:38:21 +02:00
|
|
|
return fmt.Errorf("Wrong Security Group rule count, expected %d, got %d",
|
2015-08-17 20:27:16 +02:00
|
|
|
1, len(group.IpPermissions))
|
2015-04-20 20:38:21 +02:00
|
|
|
}
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
rule := group.IpPermissions[0]
|
2015-04-20 20:38:21 +02:00
|
|
|
if *rule.FromPort != int64(80) {
|
|
|
|
return fmt.Errorf("Wrong Security Group port setting, expected %d, got %d",
|
|
|
|
80, int(*rule.FromPort))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupRuleDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2016-12-30 01:26:01 +01:00
|
|
|
{
|
2017-02-10 19:19:39 +01:00
|
|
|
Config: testAccAWSSecurityGroupRuleIngressClassicConfig(rInt),
|
2015-04-20 20:38:21 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupRuleExists("aws_security_group.web", &group),
|
2015-10-12 22:19:42 +02:00
|
|
|
testAccCheckAWSSecurityGroupRuleAttributes("aws_security_group_rule.ingress_1", &group, nil, "ingress"),
|
2015-04-20 20:38:21 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"aws_security_group_rule.ingress_1", "from_port", "80"),
|
|
|
|
testRuleCount,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAWSSecurityGroupRule_MultiIngress(t *testing.T) {
|
|
|
|
var group ec2.SecurityGroup
|
|
|
|
|
|
|
|
testMultiRuleCount := func(*terraform.State) error {
|
2015-08-17 20:27:16 +02:00
|
|
|
if len(group.IpPermissions) != 2 {
|
2015-04-20 20:38:21 +02:00
|
|
|
return fmt.Errorf("Wrong Security Group rule count, expected %d, got %d",
|
2015-08-17 20:27:16 +02:00
|
|
|
2, len(group.IpPermissions))
|
2015-04-20 20:38:21 +02:00
|
|
|
}
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
var rule *ec2.IpPermission
|
|
|
|
for _, r := range group.IpPermissions {
|
2015-04-20 20:38:21 +02:00
|
|
|
if *r.FromPort == int64(80) {
|
|
|
|
rule = r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if *rule.ToPort != int64(8000) {
|
|
|
|
return fmt.Errorf("Wrong Security Group port 2 setting, expected %d, got %d",
|
|
|
|
8000, int(*rule.ToPort))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupRuleDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2016-12-30 01:26:01 +01:00
|
|
|
{
|
2015-04-20 20:38:21 +02:00
|
|
|
Config: testAccAWSSecurityGroupRuleConfigMultiIngress,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupRuleExists("aws_security_group.web", &group),
|
|
|
|
testMultiRuleCount,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAWSSecurityGroupRule_Egress(t *testing.T) {
|
|
|
|
var group ec2.SecurityGroup
|
2017-02-10 19:19:39 +01:00
|
|
|
rInt := acctest.RandInt()
|
2015-04-20 20:38:21 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupRuleDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2016-12-30 01:26:01 +01:00
|
|
|
{
|
2017-02-10 19:19:39 +01:00
|
|
|
Config: testAccAWSSecurityGroupRuleEgressConfig(rInt),
|
2015-04-20 20:38:21 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupRuleExists("aws_security_group.web", &group),
|
2015-10-12 22:19:42 +02:00
|
|
|
testAccCheckAWSSecurityGroupRuleAttributes("aws_security_group_rule.egress_1", &group, nil, "egress"),
|
2015-04-20 20:38:21 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-06-10 16:38:11 +02:00
|
|
|
func TestAccAWSSecurityGroupRule_SelfReference(t *testing.T) {
|
|
|
|
var group ec2.SecurityGroup
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupRuleDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2016-12-30 01:26:01 +01:00
|
|
|
{
|
2015-06-10 16:38:11 +02:00
|
|
|
Config: testAccAWSSecurityGroupRuleConfigSelfReference,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupRuleExists("aws_security_group.web", &group),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-12-30 01:26:01 +01:00
|
|
|
func TestAccAWSSecurityGroupRule_ExpectInvalidTypeError(t *testing.T) {
|
2017-02-10 19:19:39 +01:00
|
|
|
rInt := acctest.RandInt()
|
2016-12-30 01:26:01 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupRuleDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
2017-02-10 19:19:39 +01:00
|
|
|
Config: testAccAWSSecurityGroupRuleExpectInvalidType(rInt),
|
2016-12-30 01:26:01 +01:00
|
|
|
ExpectError: regexp.MustCompile(`\\"type\\" contains an invalid Security Group Rule type \\"foobar\\"`),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-03-16 16:49:51 +01:00
|
|
|
func TestAccAWSSecurityGroupRule_ExpectInvalidCIDR(t *testing.T) {
|
|
|
|
rInt := acctest.RandInt()
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupRuleDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: testAccAWSSecurityGroupRuleInvalidIPv4CIDR(rInt),
|
|
|
|
ExpectError: regexp.MustCompile("invalid CIDR address: 1.2.3.4/33"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Config: testAccAWSSecurityGroupRuleInvalidIPv6CIDR(rInt),
|
|
|
|
ExpectError: regexp.MustCompile("invalid CIDR address: ::/244"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-08-18 16:56:54 +02:00
|
|
|
// testing partial match implementation
|
2015-08-21 16:58:56 +02:00
|
|
|
func TestAccAWSSecurityGroupRule_PartialMatching_basic(t *testing.T) {
|
2015-10-12 22:19:42 +02:00
|
|
|
var group ec2.SecurityGroup
|
2017-02-10 19:19:39 +01:00
|
|
|
rInt := acctest.RandInt()
|
2015-10-12 22:19:42 +02:00
|
|
|
|
|
|
|
p := ec2.IpPermission{
|
|
|
|
FromPort: aws.Int64(80),
|
|
|
|
ToPort: aws.Int64(80),
|
|
|
|
IpProtocol: aws.String("tcp"),
|
|
|
|
IpRanges: []*ec2.IpRange{
|
2016-12-30 01:26:01 +01:00
|
|
|
{CidrIp: aws.String("10.0.2.0/24")},
|
|
|
|
{CidrIp: aws.String("10.0.3.0/24")},
|
|
|
|
{CidrIp: aws.String("10.0.4.0/24")},
|
2015-10-12 22:19:42 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
o := ec2.IpPermission{
|
|
|
|
FromPort: aws.Int64(80),
|
|
|
|
ToPort: aws.Int64(80),
|
|
|
|
IpProtocol: aws.String("tcp"),
|
|
|
|
IpRanges: []*ec2.IpRange{
|
2016-12-30 01:26:01 +01:00
|
|
|
{CidrIp: aws.String("10.0.5.0/24")},
|
2015-10-12 22:19:42 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupRuleDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2016-12-30 01:26:01 +01:00
|
|
|
{
|
2017-02-10 19:19:39 +01:00
|
|
|
Config: testAccAWSSecurityGroupRulePartialMatching(rInt),
|
2015-10-12 22:19:42 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupRuleExists("aws_security_group.web", &group),
|
|
|
|
testAccCheckAWSSecurityGroupRuleAttributes("aws_security_group_rule.ingress", &group, &p, "ingress"),
|
|
|
|
testAccCheckAWSSecurityGroupRuleAttributes("aws_security_group_rule.other", &group, &o, "ingress"),
|
|
|
|
testAccCheckAWSSecurityGroupRuleAttributes("aws_security_group_rule.nat_ingress", &group, &o, "ingress"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2015-08-18 16:56:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAWSSecurityGroupRule_PartialMatching_Source(t *testing.T) {
|
2015-10-12 22:19:42 +02:00
|
|
|
var group ec2.SecurityGroup
|
|
|
|
var nat ec2.SecurityGroup
|
|
|
|
var p ec2.IpPermission
|
2017-02-10 19:19:39 +01:00
|
|
|
rInt := acctest.RandInt()
|
2015-10-12 22:19:42 +02:00
|
|
|
|
|
|
|
// This function creates the expected IPPermission with the group id from an
|
|
|
|
// external security group, needed because Security Group IDs are generated on
|
|
|
|
// AWS side and can't be known ahead of time.
|
|
|
|
setupSG := func(*terraform.State) error {
|
|
|
|
if nat.GroupId == nil {
|
|
|
|
return fmt.Errorf("Error: nat group has nil GroupID")
|
|
|
|
}
|
|
|
|
|
|
|
|
p = ec2.IpPermission{
|
|
|
|
FromPort: aws.Int64(80),
|
|
|
|
ToPort: aws.Int64(80),
|
|
|
|
IpProtocol: aws.String("tcp"),
|
|
|
|
UserIdGroupPairs: []*ec2.UserIdGroupPair{
|
2017-03-14 11:37:59 +01:00
|
|
|
{GroupId: nat.GroupId},
|
2015-10-12 22:19:42 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupRuleDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2016-12-30 01:26:01 +01:00
|
|
|
{
|
2017-02-10 19:19:39 +01:00
|
|
|
Config: testAccAWSSecurityGroupRulePartialMatching_Source(rInt),
|
2015-10-12 22:19:42 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupRuleExists("aws_security_group.web", &group),
|
|
|
|
testAccCheckAWSSecurityGroupRuleExists("aws_security_group.nat", &nat),
|
|
|
|
setupSG,
|
|
|
|
testAccCheckAWSSecurityGroupRuleAttributes("aws_security_group_rule.source_ingress", &group, &p, "ingress"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2015-08-24 15:59:54 +02:00
|
|
|
}
|
|
|
|
|
2016-02-25 19:28:58 +01:00
|
|
|
func TestAccAWSSecurityGroupRule_Issue5310(t *testing.T) {
|
|
|
|
var group ec2.SecurityGroup
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupRuleDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2016-12-30 01:26:01 +01:00
|
|
|
{
|
2016-02-25 19:28:58 +01:00
|
|
|
Config: testAccAWSSecurityGroupRuleIssue5310,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupRuleExists("aws_security_group.issue_5310", &group),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-08-24 15:59:54 +02:00
|
|
|
func TestAccAWSSecurityGroupRule_Race(t *testing.T) {
|
|
|
|
var group ec2.SecurityGroup
|
2015-08-18 16:56:54 +02:00
|
|
|
|
2015-08-24 15:59:54 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupRuleDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2016-12-30 01:26:01 +01:00
|
|
|
{
|
2015-08-24 15:59:54 +02:00
|
|
|
Config: testAccAWSSecurityGroupRuleRace,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupRuleExists("aws_security_group.race", &group),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2015-08-18 16:56:54 +02:00
|
|
|
}
|
|
|
|
|
2016-07-20 22:47:10 +02:00
|
|
|
func TestAccAWSSecurityGroupRule_SelfSource(t *testing.T) {
|
|
|
|
var group ec2.SecurityGroup
|
2017-02-10 19:19:39 +01:00
|
|
|
rInt := acctest.RandInt()
|
2016-07-20 22:47:10 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupRuleDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2016-12-30 01:26:01 +01:00
|
|
|
{
|
2017-02-10 19:19:39 +01:00
|
|
|
Config: testAccAWSSecurityGroupRuleSelfInSource(rInt),
|
2016-07-20 22:47:10 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupRuleExists("aws_security_group.web", &group),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-06 12:07:19 +02:00
|
|
|
func TestAccAWSSecurityGroupRule_PrefixListEgress(t *testing.T) {
|
|
|
|
var group ec2.SecurityGroup
|
2016-07-06 16:20:39 +02:00
|
|
|
var endpoint ec2.VpcEndpoint
|
|
|
|
var p ec2.IpPermission
|
|
|
|
|
|
|
|
// This function creates the expected IPPermission with the prefix list ID from
|
|
|
|
// the VPC Endpoint created in the test
|
|
|
|
setupSG := func(*terraform.State) error {
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
|
|
|
prefixListInput := &ec2.DescribePrefixListsInput{
|
|
|
|
Filters: []*ec2.Filter{
|
|
|
|
{Name: aws.String("prefix-list-name"), Values: []*string{endpoint.ServiceName}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Reading VPC Endpoint prefix list: %s", prefixListInput)
|
|
|
|
prefixListsOutput, err := conn.DescribePrefixLists(prefixListInput)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
_, ok := err.(awserr.Error)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Error reading VPC Endpoint prefix list: %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(prefixListsOutput.PrefixLists) != 1 {
|
|
|
|
return fmt.Errorf("There are multiple prefix lists associated with the service name '%s'. Unexpected", prefixListsOutput)
|
|
|
|
}
|
|
|
|
|
|
|
|
p = ec2.IpPermission{
|
|
|
|
IpProtocol: aws.String("-1"),
|
|
|
|
PrefixListIds: []*ec2.PrefixListId{
|
2016-12-30 01:26:01 +01:00
|
|
|
{PrefixListId: prefixListsOutput.PrefixLists[0].PrefixListId},
|
2016-07-06 16:20:39 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-06-06 12:07:19 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSSecurityGroupRuleDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2016-12-30 01:26:01 +01:00
|
|
|
{
|
2016-06-06 12:07:19 +02:00
|
|
|
Config: testAccAWSSecurityGroupRulePrefixListEgressConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSSecurityGroupRuleExists("aws_security_group.egress", &group),
|
2016-07-06 16:20:39 +02:00
|
|
|
// lookup info on the VPC Endpoint created, to populate the expected
|
|
|
|
// IP Perm
|
|
|
|
testAccCheckVpcEndpointExists("aws_vpc_endpoint.s3-us-west-2", &endpoint),
|
|
|
|
setupSG,
|
|
|
|
testAccCheckAWSSecurityGroupRuleAttributes("aws_security_group_rule.egress_1", &group, &p, "egress"),
|
2016-06-06 12:07:19 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-20 20:38:21 +02:00
|
|
|
func testAccCheckAWSSecurityGroupRuleDestroy(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "aws_security_group" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve our group
|
|
|
|
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 {
|
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
|
|
|
return fmt.Errorf("Security Group (%s) still exists.", rs.Primary.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-20 13:21:23 +02:00
|
|
|
ec2err, ok := err.(awserr.Error)
|
2015-04-20 20:38:21 +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" {
|
2015-04-20 20:38:21 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAWSSecurityGroupRuleExists(n string, group *ec2.SecurityGroup) resource.TestCheckFunc {
|
|
|
|
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]
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Security Group not found")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-18 16:56:54 +02:00
|
|
|
func testAccCheckAWSSecurityGroupRuleAttributes(n string, group *ec2.SecurityGroup, p *ec2.IpPermission, ruleType string) resource.TestCheckFunc {
|
2015-04-20 20:38:21 +02:00
|
|
|
return func(s *terraform.State) error {
|
2015-10-12 22:19:42 +02:00
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Security Group Rule Not found: %s", n)
|
|
|
|
}
|
2015-08-18 16:56:54 +02:00
|
|
|
|
2015-10-12 22:19:42 +02:00
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No Security Group Rule is set")
|
2015-04-20 20:38:21 +02:00
|
|
|
}
|
2015-08-18 16:56:54 +02:00
|
|
|
|
2015-10-12 22:19:42 +02:00
|
|
|
if p == nil {
|
|
|
|
p = &ec2.IpPermission{
|
|
|
|
FromPort: aws.Int64(80),
|
|
|
|
ToPort: aws.Int64(8000),
|
|
|
|
IpProtocol: aws.String("tcp"),
|
2016-12-30 01:26:01 +01:00
|
|
|
IpRanges: []*ec2.IpRange{{CidrIp: aws.String("10.0.0.0/8")}},
|
2015-10-12 22:19:42 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-18 16:56:54 +02:00
|
|
|
|
2015-10-12 22:19:42 +02:00
|
|
|
var matchingRule *ec2.IpPermission
|
2015-08-17 20:27:16 +02:00
|
|
|
var rules []*ec2.IpPermission
|
2015-04-20 20:38:21 +02:00
|
|
|
if ruleType == "ingress" {
|
2015-08-17 20:27:16 +02:00
|
|
|
rules = group.IpPermissions
|
2015-04-20 20:38:21 +02:00
|
|
|
} else {
|
2015-08-17 20:27:16 +02:00
|
|
|
rules = group.IpPermissionsEgress
|
2015-04-20 20:38:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(rules) == 0 {
|
|
|
|
return fmt.Errorf("No IPPerms")
|
|
|
|
}
|
|
|
|
|
2015-10-12 22:19:42 +02:00
|
|
|
for _, r := range rules {
|
|
|
|
if r.ToPort != nil && *p.ToPort != *r.ToPort {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.FromPort != nil && *p.FromPort != *r.FromPort {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.IpProtocol != nil && *p.IpProtocol != *r.IpProtocol {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
remaining := len(p.IpRanges)
|
|
|
|
for _, ip := range p.IpRanges {
|
|
|
|
for _, rip := range r.IpRanges {
|
|
|
|
if *ip.CidrIp == *rip.CidrIp {
|
|
|
|
remaining--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if remaining > 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
remaining = len(p.UserIdGroupPairs)
|
|
|
|
for _, ip := range p.UserIdGroupPairs {
|
|
|
|
for _, rip := range r.UserIdGroupPairs {
|
|
|
|
if *ip.GroupId == *rip.GroupId {
|
|
|
|
remaining--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if remaining > 0 {
|
|
|
|
continue
|
|
|
|
}
|
2016-07-06 16:20:39 +02:00
|
|
|
|
|
|
|
remaining = len(p.PrefixListIds)
|
|
|
|
for _, pip := range p.PrefixListIds {
|
|
|
|
for _, rpip := range r.PrefixListIds {
|
|
|
|
if *pip.PrefixListId == *rpip.PrefixListId {
|
|
|
|
remaining--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if remaining > 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-10-12 22:19:42 +02:00
|
|
|
matchingRule = r
|
2015-04-20 20:38:21 +02:00
|
|
|
}
|
|
|
|
|
2015-10-12 22:19:42 +02:00
|
|
|
if matchingRule != nil {
|
|
|
|
log.Printf("[DEBUG] Matching rule found : %s", matchingRule)
|
|
|
|
return nil
|
|
|
|
}
|
2015-08-18 16:56:54 +02:00
|
|
|
|
2015-10-12 22:19:42 +02:00
|
|
|
return fmt.Errorf("Error here\n\tlooking for %s, wasn't found in %s", p, rules)
|
2015-04-20 20:38:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
func testAccAWSSecurityGroupRuleIngressConfig(rInt int) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "aws_security_group" "web" {
|
|
|
|
name = "terraform_test_%d"
|
|
|
|
description = "Used in the terraform acceptance tests"
|
2015-04-20 20:38:21 +02:00
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
tags {
|
|
|
|
Name = "tf-acc-test"
|
|
|
|
}
|
|
|
|
}
|
2015-04-20 20:38:21 +02:00
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
resource "aws_security_group_rule" "ingress_1" {
|
|
|
|
type = "ingress"
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
2015-04-20 20:38:21 +02:00
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
security_group_id = "${aws_security_group.web.id}"
|
|
|
|
}`, rInt)
|
2015-04-20 20:38:21 +02:00
|
|
|
}
|
|
|
|
|
2017-03-14 11:37:59 +01:00
|
|
|
const testAccAWSSecurityGroupRuleIngress_ipv6Config = `
|
|
|
|
resource "aws_vpc" "tftest" {
|
|
|
|
cidr_block = "10.0.0.0/16"
|
|
|
|
|
|
|
|
tags {
|
|
|
|
Name = "tf-testing"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "web" {
|
|
|
|
vpc_id = "${aws_vpc.tftest.id}"
|
|
|
|
|
|
|
|
tags {
|
|
|
|
Name = "tf-acc-test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group_rule" "ingress_1" {
|
|
|
|
type = "ingress"
|
|
|
|
protocol = "6"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
ipv6_cidr_blocks = ["::/0"]
|
|
|
|
|
|
|
|
security_group_id = "${aws_security_group.web.id}"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2016-03-25 19:49:44 +01:00
|
|
|
const testAccAWSSecurityGroupRuleIngress_protocolConfig = `
|
|
|
|
resource "aws_vpc" "tftest" {
|
|
|
|
cidr_block = "10.0.0.0/16"
|
|
|
|
|
|
|
|
tags {
|
|
|
|
Name = "tf-testing"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "web" {
|
|
|
|
vpc_id = "${aws_vpc.tftest.id}"
|
|
|
|
|
|
|
|
tags {
|
|
|
|
Name = "tf-acc-test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group_rule" "ingress_1" {
|
|
|
|
type = "ingress"
|
|
|
|
protocol = "6"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
|
|
|
|
|
|
|
security_group_id = "${aws_security_group.web.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
`
|
|
|
|
|
2016-02-25 19:28:58 +01:00
|
|
|
const testAccAWSSecurityGroupRuleIssue5310 = `
|
|
|
|
provider "aws" {
|
|
|
|
region = "us-east-1"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "issue_5310" {
|
|
|
|
name = "terraform-test-issue_5310"
|
|
|
|
description = "SG for test of issue 5310"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group_rule" "issue_5310" {
|
|
|
|
type = "ingress"
|
|
|
|
from_port = 0
|
|
|
|
to_port = 65535
|
|
|
|
protocol = "tcp"
|
|
|
|
security_group_id = "${aws_security_group.issue_5310.id}"
|
|
|
|
self = true
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
func testAccAWSSecurityGroupRuleIngressClassicConfig(rInt int) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
provider "aws" {
|
|
|
|
region = "us-east-1"
|
|
|
|
}
|
2015-04-20 20:38:21 +02:00
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
resource "aws_security_group" "web" {
|
|
|
|
name = "terraform_test_%d"
|
|
|
|
description = "Used in the terraform acceptance tests"
|
2015-04-20 20:38:21 +02:00
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
tags {
|
|
|
|
Name = "tf-acc-test"
|
|
|
|
}
|
|
|
|
}
|
2015-04-20 20:38:21 +02:00
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
resource "aws_security_group_rule" "ingress_1" {
|
|
|
|
type = "ingress"
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
2015-04-20 20:38:21 +02:00
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
security_group_id = "${aws_security_group.web.id}"
|
|
|
|
}`, rInt)
|
2015-04-20 20:38:21 +02:00
|
|
|
}
|
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
func testAccAWSSecurityGroupRuleEgressConfig(rInt int) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "aws_security_group" "web" {
|
|
|
|
name = "terraform_test_%d"
|
|
|
|
description = "Used in the terraform acceptance tests"
|
2015-04-20 20:38:21 +02:00
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
tags {
|
|
|
|
Name = "tf-acc-test"
|
|
|
|
}
|
|
|
|
}
|
2015-04-20 20:38:21 +02:00
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
resource "aws_security_group_rule" "egress_1" {
|
|
|
|
type = "egress"
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
2015-04-20 20:38:21 +02:00
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
security_group_id = "${aws_security_group.web.id}"
|
|
|
|
}`, rInt)
|
2015-04-20 20:38:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const testAccAWSSecurityGroupRuleConfigMultiIngress = `
|
|
|
|
resource "aws_security_group" "web" {
|
|
|
|
name = "terraform_acceptance_test_example_2"
|
|
|
|
description = "Used in the terraform acceptance tests"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "worker" {
|
|
|
|
name = "terraform_acceptance_test_example_worker"
|
|
|
|
description = "Used in the terraform acceptance tests"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
resource "aws_security_group_rule" "ingress_1" {
|
|
|
|
type = "ingress"
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 22
|
|
|
|
to_port = 22
|
|
|
|
cidr_blocks = ["10.0.0.0/8"]
|
|
|
|
|
|
|
|
security_group_id = "${aws_security_group.web.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group_rule" "ingress_2" {
|
|
|
|
type = "ingress"
|
|
|
|
protocol = "tcp"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 8000
|
|
|
|
self = true
|
|
|
|
|
|
|
|
security_group_id = "${aws_security_group.web.id}"
|
|
|
|
}
|
|
|
|
`
|
2015-06-10 16:38:11 +02:00
|
|
|
|
|
|
|
// check for GH-1985 regression
|
|
|
|
const testAccAWSSecurityGroupRuleConfigSelfReference = `
|
|
|
|
provider "aws" {
|
|
|
|
region = "us-west-2"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_vpc" "main" {
|
|
|
|
cidr_block = "10.0.0.0/16"
|
|
|
|
tags {
|
|
|
|
Name = "sg-self-test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "web" {
|
|
|
|
name = "main"
|
|
|
|
vpc_id = "${aws_vpc.main.id}"
|
|
|
|
tags {
|
|
|
|
Name = "sg-self-test"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group_rule" "self" {
|
|
|
|
type = "ingress"
|
|
|
|
protocol = "-1"
|
|
|
|
from_port = 0
|
|
|
|
to_port = 0
|
|
|
|
self = true
|
|
|
|
security_group_id = "${aws_security_group.web.id}"
|
|
|
|
}
|
|
|
|
`
|
2015-08-18 16:56:54 +02:00
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
func testAccAWSSecurityGroupRulePartialMatching(rInt int) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "aws_vpc" "default" {
|
|
|
|
cidr_block = "10.0.0.0/16"
|
|
|
|
tags {
|
|
|
|
Name = "tf-sg-rule-bug"
|
|
|
|
}
|
|
|
|
}
|
2015-08-18 16:56:54 +02:00
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
resource "aws_security_group" "web" {
|
|
|
|
name = "tf-other-%d"
|
|
|
|
vpc_id = "${aws_vpc.default.id}"
|
|
|
|
tags {
|
|
|
|
Name = "tf-other-sg"
|
|
|
|
}
|
|
|
|
}
|
2015-08-18 16:56:54 +02:00
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
resource "aws_security_group" "nat" {
|
|
|
|
name = "tf-nat-%d"
|
|
|
|
vpc_id = "${aws_vpc.default.id}"
|
|
|
|
tags {
|
|
|
|
Name = "tf-nat-sg"
|
|
|
|
}
|
|
|
|
}
|
2015-08-18 16:56:54 +02:00
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
resource "aws_security_group_rule" "ingress" {
|
|
|
|
type = "ingress"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 80
|
|
|
|
protocol = "tcp"
|
|
|
|
cidr_blocks = ["10.0.2.0/24", "10.0.3.0/24", "10.0.4.0/24"]
|
2015-08-18 16:56:54 +02:00
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
security_group_id = "${aws_security_group.web.id}"
|
|
|
|
}
|
2015-08-18 16:56:54 +02:00
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
resource "aws_security_group_rule" "other" {
|
|
|
|
type = "ingress"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 80
|
|
|
|
protocol = "tcp"
|
|
|
|
cidr_blocks = ["10.0.5.0/24"]
|
2015-08-18 16:56:54 +02:00
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
security_group_id = "${aws_security_group.web.id}"
|
|
|
|
}
|
2015-08-18 16:56:54 +02:00
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
// same a above, but different group, to guard against bad hashing
|
|
|
|
resource "aws_security_group_rule" "nat_ingress" {
|
|
|
|
type = "ingress"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 80
|
|
|
|
protocol = "tcp"
|
|
|
|
cidr_blocks = ["10.0.2.0/24", "10.0.3.0/24", "10.0.4.0/24"]
|
2015-08-18 16:56:54 +02:00
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
security_group_id = "${aws_security_group.nat.id}"
|
|
|
|
}`, rInt, rInt)
|
2015-08-18 16:56:54 +02:00
|
|
|
}
|
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
func testAccAWSSecurityGroupRulePartialMatching_Source(rInt int) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "aws_vpc" "default" {
|
|
|
|
cidr_block = "10.0.0.0/16"
|
|
|
|
tags {
|
|
|
|
Name = "tf-sg-rule-bug"
|
|
|
|
}
|
|
|
|
}
|
2015-08-18 16:56:54 +02:00
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
resource "aws_security_group" "web" {
|
|
|
|
name = "tf-other-%d"
|
|
|
|
vpc_id = "${aws_vpc.default.id}"
|
|
|
|
tags {
|
|
|
|
Name = "tf-other-sg"
|
|
|
|
}
|
|
|
|
}
|
2015-08-18 16:56:54 +02:00
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
resource "aws_security_group" "nat" {
|
|
|
|
name = "tf-nat-%d"
|
|
|
|
vpc_id = "${aws_vpc.default.id}"
|
|
|
|
tags {
|
|
|
|
Name = "tf-nat-sg"
|
|
|
|
}
|
|
|
|
}
|
2015-08-18 16:56:54 +02:00
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
resource "aws_security_group_rule" "source_ingress" {
|
|
|
|
type = "ingress"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 80
|
|
|
|
protocol = "tcp"
|
2015-08-18 16:56:54 +02:00
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
source_security_group_id = "${aws_security_group.nat.id}"
|
|
|
|
security_group_id = "${aws_security_group.web.id}"
|
|
|
|
}
|
2015-08-18 16:56:54 +02:00
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
resource "aws_security_group_rule" "other_ingress" {
|
|
|
|
type = "ingress"
|
|
|
|
from_port = 80
|
|
|
|
to_port = 80
|
|
|
|
protocol = "tcp"
|
|
|
|
cidr_blocks = ["10.0.2.0/24", "10.0.3.0/24", "10.0.4.0/24"]
|
2015-08-18 16:56:54 +02:00
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
security_group_id = "${aws_security_group.web.id}"
|
|
|
|
}`, rInt, rInt)
|
2015-08-18 16:56:54 +02:00
|
|
|
}
|
2015-08-24 15:59:54 +02:00
|
|
|
|
|
|
|
var testAccAWSSecurityGroupRuleRace = func() string {
|
|
|
|
var b bytes.Buffer
|
|
|
|
iterations := 50
|
|
|
|
b.WriteString(fmt.Sprintf(`
|
|
|
|
resource "aws_vpc" "default" {
|
|
|
|
cidr_block = "10.0.0.0/16"
|
|
|
|
tags { Name = "tf-sg-rule-race" }
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "race" {
|
|
|
|
name = "tf-sg-rule-race-group-%d"
|
|
|
|
vpc_id = "${aws_vpc.default.id}"
|
|
|
|
}
|
2016-02-04 18:20:22 +01:00
|
|
|
`, acctest.RandInt()))
|
2015-08-24 15:59:54 +02:00
|
|
|
for i := 1; i < iterations; i++ {
|
|
|
|
b.WriteString(fmt.Sprintf(`
|
|
|
|
resource "aws_security_group_rule" "ingress%d" {
|
|
|
|
security_group_id = "${aws_security_group.race.id}"
|
|
|
|
type = "ingress"
|
|
|
|
from_port = %d
|
|
|
|
to_port = %d
|
|
|
|
protocol = "tcp"
|
|
|
|
cidr_blocks = ["10.0.0.%d/32"]
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group_rule" "egress%d" {
|
|
|
|
security_group_id = "${aws_security_group.race.id}"
|
|
|
|
type = "egress"
|
|
|
|
from_port = %d
|
|
|
|
to_port = %d
|
|
|
|
protocol = "tcp"
|
|
|
|
cidr_blocks = ["10.0.0.%d/32"]
|
|
|
|
}
|
|
|
|
`, i, i, i, i, i, i, i, i))
|
|
|
|
}
|
|
|
|
return b.String()
|
|
|
|
}()
|
2016-06-06 12:07:19 +02:00
|
|
|
|
|
|
|
const testAccAWSSecurityGroupRulePrefixListEgressConfig = `
|
|
|
|
|
|
|
|
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}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group_rule" "egress_1" {
|
|
|
|
type = "egress"
|
|
|
|
protocol = "-1"
|
|
|
|
from_port = 0
|
|
|
|
to_port = 0
|
|
|
|
prefix_list_ids = ["${aws_vpc_endpoint.s3-us-west-2.prefix_list_id}"]
|
|
|
|
security_group_id = "${aws_security_group.egress.id}"
|
|
|
|
}
|
|
|
|
`
|
2016-07-20 22:47:10 +02:00
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
func testAccAWSSecurityGroupRuleSelfInSource(rInt int) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
2016-07-20 22:47:10 +02:00
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
tags {
|
|
|
|
Name = "tf_sg_rule_self_group"
|
|
|
|
}
|
|
|
|
}
|
2016-07-20 22:47:10 +02:00
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
resource "aws_security_group" "web" {
|
|
|
|
name = "allow_all-%d"
|
|
|
|
description = "Allow all inbound traffic"
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
}
|
2016-07-20 22:47:10 +02:00
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
resource "aws_security_group_rule" "allow_self" {
|
|
|
|
type = "ingress"
|
|
|
|
from_port = 0
|
|
|
|
to_port = 0
|
|
|
|
protocol = "-1"
|
|
|
|
security_group_id = "${aws_security_group.web.id}"
|
|
|
|
source_security_group_id = "${aws_security_group.web.id}"
|
|
|
|
}`, rInt)
|
2016-07-20 22:47:10 +02:00
|
|
|
}
|
2016-12-30 01:26:01 +01:00
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
func testAccAWSSecurityGroupRuleExpectInvalidType(rInt int) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "aws_vpc" "foo" {
|
|
|
|
cidr_block = "10.1.0.0/16"
|
2016-12-30 01:26:01 +01:00
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
tags {
|
|
|
|
Name = "tf_sg_rule_self_group"
|
|
|
|
}
|
|
|
|
}
|
2016-12-30 01:26:01 +01:00
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
resource "aws_security_group" "web" {
|
|
|
|
name = "allow_all-%d"
|
|
|
|
description = "Allow all inbound traffic"
|
|
|
|
vpc_id = "${aws_vpc.foo.id}"
|
|
|
|
}
|
2016-12-30 01:26:01 +01:00
|
|
|
|
2017-02-10 19:19:39 +01:00
|
|
|
resource "aws_security_group_rule" "allow_self" {
|
|
|
|
type = "foobar"
|
|
|
|
from_port = 0
|
|
|
|
to_port = 0
|
|
|
|
protocol = "-1"
|
|
|
|
security_group_id = "${aws_security_group.web.id}"
|
|
|
|
source_security_group_id = "${aws_security_group.web.id}"
|
|
|
|
}`, rInt)
|
2016-12-30 01:26:01 +01:00
|
|
|
}
|
2017-03-16 16:49:51 +01:00
|
|
|
|
|
|
|
func testAccAWSSecurityGroupRuleInvalidIPv4CIDR(rInt int) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "aws_security_group" "foo" {
|
|
|
|
name = "testing-failure-%d"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group_rule" "ing" {
|
|
|
|
type = "ingress"
|
|
|
|
from_port = 0
|
|
|
|
to_port = 0
|
|
|
|
protocol = "-1"
|
|
|
|
cidr_blocks = ["1.2.3.4/33"]
|
|
|
|
security_group_id = "${aws_security_group.foo.id}"
|
|
|
|
}`, rInt)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccAWSSecurityGroupRuleInvalidIPv6CIDR(rInt int) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "aws_security_group" "foo" {
|
|
|
|
name = "testing-failure-%d"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group_rule" "ing" {
|
|
|
|
type = "egress"
|
|
|
|
from_port = 0
|
|
|
|
to_port = 0
|
|
|
|
protocol = "-1"
|
|
|
|
ipv6_cidr_blocks = ["::/244"]
|
|
|
|
security_group_id = "${aws_security_group.foo.id}"
|
|
|
|
}`, rInt)
|
|
|
|
}
|