2014-11-30 12:38:45 +01:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2014-12-08 11:48:39 +01:00
|
|
|
"fmt"
|
2015-05-06 15:16:46 +02:00
|
|
|
"net"
|
2015-03-11 21:01:07 +01:00
|
|
|
"strconv"
|
|
|
|
|
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-04-13 18:14:21 +02:00
|
|
|
func expandNetworkAclEntries(configured []interface{}, entryType string) ([]*ec2.NetworkACLEntry, error) {
|
|
|
|
entries := make([]*ec2.NetworkACLEntry, 0, len(configured))
|
2014-11-30 12:38:45 +01:00
|
|
|
for _, eRaw := range configured {
|
|
|
|
data := eRaw.(map[string]interface{})
|
2014-12-08 11:48:39 +01:00
|
|
|
protocol := data["protocol"].(string)
|
2015-04-22 12:35:23 +02:00
|
|
|
p, err := strconv.Atoi(protocol)
|
|
|
|
if err != nil {
|
|
|
|
var ok bool
|
|
|
|
p, ok = protocolIntegers()[protocol]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Invalid Protocol %s for rule %#v", protocol, data)
|
|
|
|
}
|
2014-12-08 11:48:39 +01:00
|
|
|
}
|
2015-04-22 12:35:23 +02:00
|
|
|
|
2015-04-13 18:14:21 +02:00
|
|
|
e := &ec2.NetworkACLEntry{
|
2015-03-11 21:01:07 +01:00
|
|
|
Protocol: aws.String(strconv.Itoa(p)),
|
|
|
|
PortRange: &ec2.PortRange{
|
2015-07-28 22:29:46 +02:00
|
|
|
From: aws.Int64(int64(data["from_port"].(int))),
|
|
|
|
To: aws.Int64(int64(data["to_port"].(int))),
|
2014-11-30 12:38:45 +01:00
|
|
|
},
|
2015-07-28 22:29:46 +02:00
|
|
|
Egress: aws.Bool((entryType == "egress")),
|
2015-03-11 21:01:07 +01:00
|
|
|
RuleAction: aws.String(data["action"].(string)),
|
2015-07-28 22:29:46 +02:00
|
|
|
RuleNumber: aws.Int64(int64(data["rule_no"].(int))),
|
2015-03-11 21:01:07 +01:00
|
|
|
CIDRBlock: aws.String(data["cidr_block"].(string)),
|
2014-11-30 12:38:45 +01:00
|
|
|
}
|
2015-05-29 23:48:50 +02:00
|
|
|
|
|
|
|
// Specify additional required fields for ICMP
|
|
|
|
if p == 1 {
|
|
|
|
e.ICMPTypeCode = &ec2.ICMPTypeCode{}
|
|
|
|
if v, ok := data["icmp_code"]; ok {
|
2015-07-28 22:29:46 +02:00
|
|
|
e.ICMPTypeCode.Code = aws.Int64(int64(v.(int)))
|
2015-05-29 23:48:50 +02:00
|
|
|
}
|
|
|
|
if v, ok := data["icmp_type"]; ok {
|
2015-07-28 22:29:46 +02:00
|
|
|
e.ICMPTypeCode.Type = aws.Int64(int64(v.(int)))
|
2015-05-29 23:48:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-30 12:38:45 +01:00
|
|
|
entries = append(entries, e)
|
|
|
|
}
|
2014-12-08 11:48:39 +01:00
|
|
|
return entries, nil
|
2014-11-30 12:38:45 +01:00
|
|
|
}
|
|
|
|
|
2015-04-13 18:14:21 +02:00
|
|
|
func flattenNetworkAclEntries(list []*ec2.NetworkACLEntry) []map[string]interface{} {
|
2014-11-30 12:38:45 +01:00
|
|
|
entries := make([]map[string]interface{}, 0, len(list))
|
|
|
|
|
2014-12-01 09:49:05 +01:00
|
|
|
for _, entry := range list {
|
2014-11-30 12:38:45 +01:00
|
|
|
entries = append(entries, map[string]interface{}{
|
2015-03-11 21:01:07 +01:00
|
|
|
"from_port": *entry.PortRange.From,
|
|
|
|
"to_port": *entry.PortRange.To,
|
|
|
|
"action": *entry.RuleAction,
|
|
|
|
"rule_no": *entry.RuleNumber,
|
|
|
|
"protocol": *entry.Protocol,
|
|
|
|
"cidr_block": *entry.CIDRBlock,
|
2014-12-01 09:49:05 +01:00
|
|
|
})
|
|
|
|
}
|
2015-04-13 18:14:21 +02:00
|
|
|
|
2014-11-30 12:38:45 +01:00
|
|
|
return entries
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-12-01 09:49:05 +01:00
|
|
|
func protocolIntegers() map[string]int {
|
|
|
|
var protocolIntegers = make(map[string]int)
|
2014-11-30 12:38:45 +01:00
|
|
|
protocolIntegers = map[string]int{
|
2015-06-12 05:23:42 +02:00
|
|
|
// defined at https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml
|
|
|
|
"ah": 51,
|
|
|
|
"esp": 50,
|
2014-12-01 09:49:05 +01:00
|
|
|
"udp": 17,
|
|
|
|
"tcp": 6,
|
|
|
|
"icmp": 1,
|
2015-02-20 19:22:26 +01:00
|
|
|
"all": -1,
|
2014-11-30 12:38:45 +01:00
|
|
|
}
|
|
|
|
return protocolIntegers
|
|
|
|
}
|
2015-05-06 15:14:33 +02:00
|
|
|
|
|
|
|
// expectedPortPair stores a pair of ports we expect to see together.
|
|
|
|
type expectedPortPair struct {
|
|
|
|
to_port int64
|
|
|
|
from_port int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// validatePorts ensures the ports and protocol match expected
|
|
|
|
// values.
|
|
|
|
func validatePorts(to int64, from int64, expected expectedPortPair) bool {
|
|
|
|
if to != expected.to_port || from != expected.from_port {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2015-05-06 15:16:46 +02:00
|
|
|
|
|
|
|
// validateCIDRBlock ensures the passed CIDR block represents an implied
|
|
|
|
// network, and not an overly-specified IP address.
|
|
|
|
func validateCIDRBlock(cidr string) error {
|
|
|
|
_, ipnet, err := net.ParseCIDR(cidr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ipnet.String() != cidr {
|
|
|
|
return fmt.Errorf("%s is not a valid mask; did you mean %s?", cidr, ipnet)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|