Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit 89bacc0

Browse files
committed
aws: error on expndIPPerms(...) if our ports and protocol conflict.
Ingress and egress rules given a "-1" protocol don't have ports when Read out of AWS. This results in hashing problems, as a local config file might contain port declarations AWS can't ever return. Rather than making ports optional fields, which carries with it a huge headache trying to distinguish between zero-value attributes (e.g. 'to_port = 0') and attributes that are simply omitted, simply force the user to opt-in when using the "-1" protocol. If they choose to use it, they must now specify "0" for both to_port and from_port. Any other configuration will error.
1 parent 98d34a3 commit 89bacc0

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

builtin/providers/aws/resource_aws_security_group.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,14 @@ func resourceAwsSecurityGroupUpdateRules(
406406
os := o.(*schema.Set)
407407
ns := n.(*schema.Set)
408408

409-
remove := expandIPPerms(group, os.Difference(ns).List())
410-
add := expandIPPerms(group, ns.Difference(os).List())
409+
remove, err := expandIPPerms(group, os.Difference(ns).List())
410+
if err != nil {
411+
return err
412+
}
413+
add, err := expandIPPerms(group, ns.Difference(os).List())
414+
if err != nil {
415+
return err
416+
}
411417

412418
// TODO: We need to handle partial state better in the in-between
413419
// in this update.

builtin/providers/aws/structure.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ func expandListeners(configured []interface{}) ([]*elb.Listener, error) {
4242
return listeners, nil
4343
}
4444

45-
// Takes the result of flatmap.Expand for an array of ingress/egress
46-
// security group rules and returns EC2 API compatible objects
45+
// Takes the result of flatmap.Expand for an array of ingress/egress security
46+
// group rules and returns EC2 API compatible objects. This function will error
47+
// if it finds invalid permissions input, namely a protocol of "-1" with either
48+
// to_port or from_port set to a non-zero value.
4749
func expandIPPerms(
48-
group *ec2.SecurityGroup, configured []interface{}) []*ec2.IPPermission {
50+
group *ec2.SecurityGroup, configured []interface{}) ([]*ec2.IPPermission, error) {
4951
vpc := group.VPCID != nil
5052

5153
perms := make([]*ec2.IPPermission, len(configured))
@@ -57,6 +59,17 @@ func expandIPPerms(
5759
perm.ToPort = aws.Long(int64(m["to_port"].(int)))
5860
perm.IPProtocol = aws.String(m["protocol"].(string))
5961

62+
// When protocol is "-1", AWS won't store any ports for the
63+
// rule, but also won't error if the user specifies ports other
64+
// than '0'. Force the user to make a deliberate '0' port
65+
// choice when specifying a "-1" protocol, and tell them about
66+
// AWS's behavior in the error message.
67+
if *perm.IPProtocol == "-1" && (*perm.FromPort != 0 || *perm.ToPort != 0) {
68+
return nil, fmt.Errorf(
69+
"from_port (%d) and to_port (%d) must both be 0 to use the the 'ALL' \"-1\" protocol!",
70+
*perm.FromPort, *perm.ToPort)
71+
}
72+
6073
var groups []string
6174
if raw, ok := m["security_groups"]; ok {
6275
list := raw.(*schema.Set).List()
@@ -102,7 +115,7 @@ func expandIPPerms(
102115
perms[i] = &perm
103116
}
104117

105-
return perms
118+
return perms, nil
106119
}
107120

108121
// Takes the result of flatmap.Expand for an array of parameters and

0 commit comments

Comments
 (0)