2014-11-30 12:38:45 +01:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2014-11-30 12:38:45 +01:00
|
|
|
)
|
|
|
|
|
2015-03-11 21:01:07 +01:00
|
|
|
func Test_expandNetworkACLEntry(t *testing.T) {
|
2014-11-30 12:38:45 +01:00
|
|
|
input := []interface{}{
|
|
|
|
map[string]interface{}{
|
2014-12-01 09:49:05 +01:00
|
|
|
"protocol": "tcp",
|
|
|
|
"from_port": 22,
|
|
|
|
"to_port": 22,
|
2014-11-30 12:38:45 +01:00
|
|
|
"cidr_block": "0.0.0.0/0",
|
2014-12-01 09:49:05 +01:00
|
|
|
"action": "deny",
|
|
|
|
"rule_no": 1,
|
2014-11-30 12:38:45 +01:00
|
|
|
},
|
|
|
|
map[string]interface{}{
|
2014-12-01 09:49:05 +01:00
|
|
|
"protocol": "tcp",
|
|
|
|
"from_port": 443,
|
|
|
|
"to_port": 443,
|
2014-11-30 12:38:45 +01:00
|
|
|
"cidr_block": "0.0.0.0/0",
|
2014-12-01 09:49:05 +01:00
|
|
|
"action": "deny",
|
|
|
|
"rule_no": 2,
|
2014-11-30 12:38:45 +01:00
|
|
|
},
|
2015-04-22 12:35:23 +02:00
|
|
|
map[string]interface{}{
|
|
|
|
"protocol": "-1",
|
|
|
|
"from_port": 443,
|
|
|
|
"to_port": 443,
|
|
|
|
"cidr_block": "0.0.0.0/0",
|
|
|
|
"action": "deny",
|
|
|
|
"rule_no": 2,
|
|
|
|
},
|
2014-11-30 12:38:45 +01:00
|
|
|
}
|
2014-12-08 11:48:39 +01:00
|
|
|
expanded, _ := expandNetworkAclEntries(input, "egress")
|
2014-11-30 12:38:45 +01:00
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
expected := []*ec2.NetworkAclEntry{
|
|
|
|
&ec2.NetworkAclEntry{
|
2015-03-11 22:21:22 +01:00
|
|
|
Protocol: aws.String("6"),
|
2015-03-11 21:01:07 +01:00
|
|
|
PortRange: &ec2.PortRange{
|
2015-07-28 22:29:46 +02:00
|
|
|
From: aws.Int64(22),
|
|
|
|
To: aws.Int64(22),
|
2015-03-11 21:01:07 +01:00
|
|
|
},
|
|
|
|
RuleAction: aws.String("deny"),
|
2015-07-28 22:29:46 +02:00
|
|
|
RuleNumber: aws.Int64(1),
|
2015-08-17 20:27:16 +02:00
|
|
|
CidrBlock: aws.String("0.0.0.0/0"),
|
2015-07-28 22:29:46 +02:00
|
|
|
Egress: aws.Bool(true),
|
2014-11-30 12:38:45 +01:00
|
|
|
},
|
2015-08-17 20:27:16 +02:00
|
|
|
&ec2.NetworkAclEntry{
|
2015-03-11 22:21:22 +01:00
|
|
|
Protocol: aws.String("6"),
|
2015-03-11 21:01:07 +01:00
|
|
|
PortRange: &ec2.PortRange{
|
2015-07-28 22:29:46 +02:00
|
|
|
From: aws.Int64(443),
|
|
|
|
To: aws.Int64(443),
|
2015-03-11 21:01:07 +01:00
|
|
|
},
|
|
|
|
RuleAction: aws.String("deny"),
|
2015-07-28 22:29:46 +02:00
|
|
|
RuleNumber: aws.Int64(2),
|
2015-08-17 20:27:16 +02:00
|
|
|
CidrBlock: aws.String("0.0.0.0/0"),
|
2015-07-28 22:29:46 +02:00
|
|
|
Egress: aws.Bool(true),
|
2014-11-30 12:38:45 +01:00
|
|
|
},
|
2015-08-17 20:27:16 +02:00
|
|
|
&ec2.NetworkAclEntry{
|
2015-04-22 12:35:23 +02:00
|
|
|
Protocol: aws.String("-1"),
|
|
|
|
PortRange: &ec2.PortRange{
|
2015-07-28 22:29:46 +02:00
|
|
|
From: aws.Int64(443),
|
|
|
|
To: aws.Int64(443),
|
2015-04-22 12:35:23 +02:00
|
|
|
},
|
|
|
|
RuleAction: aws.String("deny"),
|
2015-07-28 22:29:46 +02:00
|
|
|
RuleNumber: aws.Int64(2),
|
2015-08-17 20:27:16 +02:00
|
|
|
CidrBlock: aws.String("0.0.0.0/0"),
|
2015-07-28 22:29:46 +02:00
|
|
|
Egress: aws.Bool(true),
|
2015-04-22 12:35:23 +02:00
|
|
|
},
|
2014-12-01 09:49:05 +01:00
|
|
|
}
|
2014-11-30 12:38:45 +01:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(expanded, expected) {
|
|
|
|
t.Fatalf(
|
|
|
|
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
|
2014-12-01 09:49:05 +01:00
|
|
|
expanded,
|
2014-11-30 12:38:45 +01:00
|
|
|
expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-03-11 21:01:07 +01:00
|
|
|
func Test_flattenNetworkACLEntry(t *testing.T) {
|
2014-12-01 09:49:05 +01:00
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
apiInput := []*ec2.NetworkAclEntry{
|
|
|
|
&ec2.NetworkAclEntry{
|
2015-03-11 21:01:07 +01:00
|
|
|
Protocol: aws.String("tcp"),
|
|
|
|
PortRange: &ec2.PortRange{
|
2015-07-28 22:29:46 +02:00
|
|
|
From: aws.Int64(22),
|
|
|
|
To: aws.Int64(22),
|
2014-11-30 12:38:45 +01:00
|
|
|
},
|
2015-03-11 21:01:07 +01:00
|
|
|
RuleAction: aws.String("deny"),
|
2015-07-28 22:29:46 +02:00
|
|
|
RuleNumber: aws.Int64(1),
|
2015-08-17 20:27:16 +02:00
|
|
|
CidrBlock: aws.String("0.0.0.0/0"),
|
2014-11-30 12:38:45 +01:00
|
|
|
},
|
2015-08-17 20:27:16 +02:00
|
|
|
&ec2.NetworkAclEntry{
|
2015-03-11 21:01:07 +01:00
|
|
|
Protocol: aws.String("tcp"),
|
|
|
|
PortRange: &ec2.PortRange{
|
2015-07-28 22:29:46 +02:00
|
|
|
From: aws.Int64(443),
|
|
|
|
To: aws.Int64(443),
|
2014-12-01 09:49:05 +01:00
|
|
|
},
|
2015-03-11 21:01:07 +01:00
|
|
|
RuleAction: aws.String("deny"),
|
2015-07-28 22:29:46 +02:00
|
|
|
RuleNumber: aws.Int64(2),
|
2015-08-17 20:27:16 +02:00
|
|
|
CidrBlock: aws.String("0.0.0.0/0"),
|
2014-11-30 12:38:45 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
flattened := flattenNetworkAclEntries(apiInput)
|
|
|
|
|
|
|
|
expected := []map[string]interface{}{
|
2014-12-01 09:49:05 +01:00
|
|
|
map[string]interface{}{
|
|
|
|
"protocol": "tcp",
|
2015-04-13 18:14:21 +02:00
|
|
|
"from_port": int64(22),
|
|
|
|
"to_port": int64(22),
|
2014-11-30 12:38:45 +01:00
|
|
|
"cidr_block": "0.0.0.0/0",
|
2014-12-01 09:49:05 +01:00
|
|
|
"action": "deny",
|
2015-04-13 18:14:21 +02:00
|
|
|
"rule_no": int64(1),
|
2014-11-30 12:38:45 +01:00
|
|
|
},
|
|
|
|
map[string]interface{}{
|
2014-12-01 09:49:05 +01:00
|
|
|
"protocol": "tcp",
|
2015-04-13 18:14:21 +02:00
|
|
|
"from_port": int64(443),
|
|
|
|
"to_port": int64(443),
|
2014-11-30 12:38:45 +01:00
|
|
|
"cidr_block": "0.0.0.0/0",
|
2014-12-01 09:49:05 +01:00
|
|
|
"action": "deny",
|
2015-04-13 18:14:21 +02:00
|
|
|
"rule_no": int64(2),
|
2014-11-30 12:38:45 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(flattened, expected) {
|
|
|
|
t.Fatalf(
|
|
|
|
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
|
2015-04-13 18:14:21 +02:00
|
|
|
flattened,
|
2014-11-30 12:38:45 +01:00
|
|
|
expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-05-06 15:14:33 +02:00
|
|
|
|
|
|
|
func Test_validatePorts(t *testing.T) {
|
|
|
|
for _, ts := range []struct {
|
|
|
|
to int64
|
|
|
|
from int64
|
|
|
|
expected *expectedPortPair
|
|
|
|
wanted bool
|
|
|
|
}{
|
|
|
|
{0, 0, &expectedPortPair{0, 0}, true},
|
|
|
|
{0, 1, &expectedPortPair{0, 0}, false},
|
|
|
|
} {
|
|
|
|
got := validatePorts(ts.to, ts.from, *ts.expected)
|
|
|
|
if got != ts.wanted {
|
|
|
|
t.Fatalf("Got: %t; Expected: %t\n", got, ts.wanted)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-06 15:16:46 +02:00
|
|
|
|
|
|
|
func Test_validateCIDRBlock(t *testing.T) {
|
|
|
|
for _, ts := range []struct {
|
|
|
|
cidr string
|
|
|
|
shouldErr bool
|
|
|
|
}{
|
|
|
|
{"10.2.2.0/24", false},
|
|
|
|
{"10.2.2.0/1234", true},
|
|
|
|
{"10/24", true},
|
|
|
|
{"10.2.2.2/24", true},
|
|
|
|
} {
|
|
|
|
err := validateCIDRBlock(ts.cidr)
|
|
|
|
if ts.shouldErr && err == nil {
|
|
|
|
t.Fatalf("Input '%s' should error but didn't!", ts.cidr)
|
|
|
|
}
|
|
|
|
if !ts.shouldErr && err != nil {
|
|
|
|
t.Fatalf("Got unexpected error for '%s' input: %s", ts.cidr, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|