forked from kubernetes/kops
/
securitygrouprule.go
263 lines (220 loc) · 6 KB
/
securitygrouprule.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package awstasks
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/golang/glog"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
"k8s.io/kops/upup/pkg/fi/cloudup/terraform"
)
//go:generate fitask -type=SecurityGroupRule
type SecurityGroupRule struct {
Name *string
SecurityGroup *SecurityGroup
CIDR *string
Protocol *string
// FromPort is the lower-bound (inclusive) of the port-range
FromPort *int64
// ToPort is the upper-bound (inclusive) of the port-range
ToPort *int64
SourceGroup *SecurityGroup
Egress *bool
}
func (e *SecurityGroupRule) Find(c *fi.Context) (*SecurityGroupRule, error) {
cloud := c.Cloud.(awsup.AWSCloud)
if e.SecurityGroup == nil || e.SecurityGroup.ID == nil {
return nil, nil
}
request := &ec2.DescribeSecurityGroupsInput{
Filters: []*ec2.Filter{
awsup.NewEC2Filter("group-id", *e.SecurityGroup.ID),
},
}
response, err := cloud.EC2().DescribeSecurityGroups(request)
if err != nil {
return nil, fmt.Errorf("error listing SecurityGroup: %v", err)
}
if response == nil || len(response.SecurityGroups) == 0 {
return nil, nil
}
if len(response.SecurityGroups) != 1 {
glog.Fatalf("found multiple security groups for id=%s", *e.SecurityGroup.ID)
}
sg := response.SecurityGroups[0]
//glog.V(2).Info("found existing security group")
var foundRule *ec2.IpPermission
ipPermissions := sg.IpPermissions
if fi.BoolValue(e.Egress) {
ipPermissions = sg.IpPermissionsEgress
}
for _, rule := range ipPermissions {
if e.matches(rule) {
foundRule = rule
break
}
}
if foundRule != nil {
actual := &SecurityGroupRule{
Name: e.Name,
SecurityGroup: &SecurityGroup{ID: e.SecurityGroup.ID},
FromPort: foundRule.FromPort,
ToPort: foundRule.ToPort,
Protocol: foundRule.IpProtocol,
Egress: e.Egress,
}
if aws.StringValue(actual.Protocol) == "-1" {
actual.Protocol = nil
}
if e.CIDR != nil {
actual.CIDR = e.CIDR
}
if e.SourceGroup != nil {
actual.SourceGroup = &SecurityGroup{ID: e.SourceGroup.ID}
}
return actual, nil
}
return nil, nil
}
func (e *SecurityGroupRule) matches(rule *ec2.IpPermission) bool {
if aws.Int64Value(rule.FromPort) != aws.Int64Value(e.FromPort) {
return false
}
if aws.Int64Value(rule.ToPort) != aws.Int64Value(e.ToPort) {
return false
}
matchProtocol := "-1" // Wildcard
if e.Protocol != nil {
matchProtocol = *e.Protocol
}
if aws.StringValue(rule.IpProtocol) != matchProtocol {
return false
}
if e.CIDR != nil {
// TODO: Only if len 1?
match := false
for _, ipRange := range rule.IpRanges {
if aws.StringValue(ipRange.CidrIp) == *e.CIDR {
match = true
break
}
}
if !match {
return false
}
}
if e.SourceGroup != nil {
// TODO: Only if len 1?
match := false
for _, spec := range rule.UserIdGroupPairs {
if aws.StringValue(spec.GroupId) == *e.SourceGroup.ID {
match = true
break
}
}
if !match {
return false
}
}
return true
}
func (e *SecurityGroupRule) Run(c *fi.Context) error {
return fi.DefaultDeltaRunMethod(e, c)
}
func (_ *SecurityGroupRule) CheckChanges(a, e, changes *SecurityGroupRule) error {
if a == nil {
if e.SecurityGroup == nil {
return fi.RequiredField("SecurityGroup")
}
}
return nil
}
func (_ *SecurityGroupRule) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *SecurityGroupRule) error {
if a == nil {
protocol := e.Protocol
if protocol == nil {
protocol = aws.String("-1")
}
ipPermission := &ec2.IpPermission{
IpProtocol: protocol,
FromPort: e.FromPort,
ToPort: e.ToPort,
}
if e.SourceGroup != nil {
ipPermission.UserIdGroupPairs = []*ec2.UserIdGroupPair{
{
GroupId: e.SourceGroup.ID,
},
}
} else {
// Default to 0.0.0.0/0 ?
ipPermission.IpRanges = []*ec2.IpRange{
{CidrIp: e.CIDR},
}
}
if fi.BoolValue(e.Egress) {
request := &ec2.AuthorizeSecurityGroupEgressInput{
GroupId: e.SecurityGroup.ID,
}
request.IpPermissions = []*ec2.IpPermission{ipPermission}
glog.V(2).Infof("Calling EC2 AuthorizeSecurityGroupEgress")
_, err := t.Cloud.EC2().AuthorizeSecurityGroupEgress(request)
if err != nil {
return fmt.Errorf("error creating SecurityGroupEgress: %v", err)
}
} else {
request := &ec2.AuthorizeSecurityGroupIngressInput{
GroupId: e.SecurityGroup.ID,
}
request.IpPermissions = []*ec2.IpPermission{ipPermission}
glog.V(2).Infof("Calling EC2 AuthorizeSecurityGroupIngress")
_, err := t.Cloud.EC2().AuthorizeSecurityGroupIngress(request)
if err != nil {
return fmt.Errorf("error creating SecurityGroupIngress: %v", err)
}
}
}
// No tags on security group rules (there are tags on the group though)
return nil
}
type terraformSecurityGroupIngress struct {
Type *string `json:"type"`
SecurityGroup *terraform.Literal `json:"security_group_id"`
SourceGroup *terraform.Literal `json:"source_security_group_id,omitempty"`
FromPort *int64 `json:"from_port,omitempty"`
ToPort *int64 `json:"to_port,omitempty"`
Protocol *string `json:"protocol,omitempty"`
CIDRBlocks []string `json:"cidr_blocks,omitempty"`
}
func (_ *SecurityGroupRule) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *SecurityGroupRule) error {
tf := &terraformSecurityGroupIngress{
Type: fi.String("ingress"),
SecurityGroup: e.SecurityGroup.TerraformLink(),
FromPort: e.FromPort,
ToPort: e.ToPort,
Protocol: e.Protocol,
}
if fi.BoolValue(e.Egress) {
tf.Type = fi.String("egress")
}
if e.Protocol == nil {
tf.Protocol = fi.String("-1")
tf.FromPort = fi.Int64(0)
tf.ToPort = fi.Int64(0)
}
if tf.FromPort == nil {
// FromPort is required by tf
tf.FromPort = fi.Int64(0)
}
if tf.ToPort == nil {
// ToPort is required by tf
tf.ToPort = fi.Int64(65535)
}
if e.SourceGroup != nil {
tf.SourceGroup = e.SourceGroup.TerraformLink()
}
if e.CIDR != nil {
tf.CIDRBlocks = append(tf.CIDRBlocks, *e.CIDR)
}
return t.RenderResource("aws_security_group_rule", *e.Name, tf)
}