|
|
|
|
@ -24,7 +24,7 @@ func TestResourceAwsSecurityGroupIPPermGather(t *testing.T) {
|
|
|
|
|
IpRanges: []*ec2.IpRange{&ec2.IpRange{CidrIp: aws.String("0.0.0.0/0")}},
|
|
|
|
|
UserIdGroupPairs: []*ec2.UserIdGroupPair{
|
|
|
|
|
&ec2.UserIdGroupPair{
|
|
|
|
|
GroupId: aws.String("sg-22222"),
|
|
|
|
|
GroupId: aws.String("sg-11111"),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
@ -33,8 +33,27 @@ func TestResourceAwsSecurityGroupIPPermGather(t *testing.T) {
|
|
|
|
|
FromPort: aws.Int64(int64(80)),
|
|
|
|
|
ToPort: aws.Int64(int64(80)),
|
|
|
|
|
UserIdGroupPairs: []*ec2.UserIdGroupPair{
|
|
|
|
|
// VPC
|
|
|
|
|
&ec2.UserIdGroupPair{
|
|
|
|
|
GroupId: aws.String("sg-22222"),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
&ec2.IpPermission{
|
|
|
|
|
IpProtocol: aws.String("tcp"),
|
|
|
|
|
FromPort: aws.Int64(int64(443)),
|
|
|
|
|
ToPort: aws.Int64(int64(443)),
|
|
|
|
|
UserIdGroupPairs: []*ec2.UserIdGroupPair{
|
|
|
|
|
// Classic
|
|
|
|
|
&ec2.UserIdGroupPair{
|
|
|
|
|
GroupId: aws.String("foo"),
|
|
|
|
|
UserId: aws.String("12345"),
|
|
|
|
|
GroupId: aws.String("sg-33333"),
|
|
|
|
|
GroupName: aws.String("ec2_classic"),
|
|
|
|
|
},
|
|
|
|
|
&ec2.UserIdGroupPair{
|
|
|
|
|
UserId: aws.String("amazon-elb"),
|
|
|
|
|
GroupId: aws.String("sg-d2c979d3"),
|
|
|
|
|
GroupName: aws.String("amazon-elb-sg"),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
@ -53,12 +72,21 @@ func TestResourceAwsSecurityGroupIPPermGather(t *testing.T) {
|
|
|
|
|
"from_port": int64(80),
|
|
|
|
|
"to_port": int64(80),
|
|
|
|
|
"security_groups": schema.NewSet(schema.HashString, []interface{}{
|
|
|
|
|
"foo",
|
|
|
|
|
"sg-22222",
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
map[string]interface{}{
|
|
|
|
|
"protocol": "tcp",
|
|
|
|
|
"from_port": int64(443),
|
|
|
|
|
"to_port": int64(443),
|
|
|
|
|
"security_groups": schema.NewSet(schema.HashString, []interface{}{
|
|
|
|
|
"ec2_classic",
|
|
|
|
|
"amazon-elb/amazon-elb-sg",
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out := resourceAwsSecurityGroupIPPermGather("sg-22222", raw)
|
|
|
|
|
out := resourceAwsSecurityGroupIPPermGather("sg-11111", raw, aws.String("12345"))
|
|
|
|
|
for _, i := range out {
|
|
|
|
|
// loop and match rules, because the ordering is not guarneteed
|
|
|
|
|
for _, l := range local {
|
|
|
|
|
@ -636,6 +664,94 @@ func TestAccAWSSecurityGroup_CIDRandGroups(t *testing.T) {
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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{
|
|
|
|
|
resource.TestStep{
|
|
|
|
|
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{
|
|
|
|
|
resource.TestStep{
|
|
|
|
|
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"),
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func testAccCheckAWSSecurityGroupAttributesChanged(group *ec2.SecurityGroup) resource.TestCheckFunc {
|
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
|
p := []*ec2.IpPermission{
|
|
|
|
|
@ -1141,3 +1257,90 @@ resource "aws_security_group" "mixed" {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
const testAccAWSSecurityGroupConfig_ingressWithCidrAndSGs = `
|
|
|
|
|
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.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"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
`
|
|
|
|
|
|