-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecurity_group.go
164 lines (128 loc) · 3.95 KB
/
security_group.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
package window
import (
"sort"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
)
type (
SecurityGroup struct {
// A description of the security group.
Description string
// The ID of the security group.
GroupId string
// The name of the security group.
GroupName string
// One or more inbound rules associated with the security group.
IpPermissions []*ec2.IpPermission
// [EC2-VPC] One or more outbound rules associated with the security group.
IpPermissionsEgress []*ec2.IpPermission
// The AWS account ID of the owner of the security group.
OwnerId string
// Any tags assigned to the security group.
Tags []*ec2.Tag
// [EC2-VPC] The ID of the VPC for the security group.
VpcId string
Name string
Id string
State string
Instances []*Instance
ELBs []*ELB
ElasticCacheClusters []*ElasticCacheCluster
DBInstances []*DBInstance
LambdaFunctions []*LambdaFunction
Classic *Classic
VPCs []*VPC
}
SecurityGroupSet []*SecurityGroup
SecurityGroupByNameAsc []*SecurityGroup
SecurityGroupIpPermissionsByPortAsc []*ec2.IpPermission
)
func (a SecurityGroupByNameAsc) Len() int { return len(a) }
func (a SecurityGroupByNameAsc) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a SecurityGroupByNameAsc) Less(i, j int) bool {
return string_less_than(a[i].Name, a[j].Name)
}
func (a SecurityGroupIpPermissionsByPortAsc) Len() int { return len(a) }
func (a SecurityGroupIpPermissionsByPortAsc) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a SecurityGroupIpPermissionsByPortAsc) Less(i, j int) bool {
if a[i].FromPort != nil && a[j].FromPort != nil {
return *a[i].FromPort < *a[j].FromPort
}
return true
}
func LoadSecurityGroups(input *ec2.DescribeSecurityGroupsInput) (map[string]*SecurityGroup, error) {
resp, err := EC2Client.DescribeSecurityGroups(input)
if err != nil {
return nil, err
}
sgs := make(map[string]*SecurityGroup, len(resp.SecurityGroups))
for _, ec2sg := range resp.SecurityGroups {
sg := &SecurityGroup{
Description: aws.StringValue(ec2sg.Description),
GroupId: aws.StringValue(ec2sg.GroupId),
GroupName: aws.StringValue(ec2sg.GroupName),
IpPermissions: ec2sg.IpPermissions,
IpPermissionsEgress: ec2sg.IpPermissionsEgress,
OwnerId: aws.StringValue(ec2sg.OwnerId),
Tags: ec2sg.Tags,
VpcId: aws.StringValue(ec2sg.VpcId),
}
sg.Name = TagOrDefault(sg.Tags, "Name", sg.GroupName, sg.GroupId)
sg.Id = "sg:" + sg.GroupId
sgs[sg.GroupId] = sg
}
return sgs, nil
}
func (sg *SecurityGroup) PortsInvolved() []int {
set := map[int64]struct{}{}
for _, perm := range sg.IpPermissions {
if perm.FromPort != nil {
set[*perm.FromPort] = struct{}{}
}
if perm.ToPort != nil {
set[*perm.ToPort] = struct{}{}
}
if perm.FromPort == nil && perm.ToPort == nil && perm.IpProtocol != nil && *perm.IpProtocol == "-1" {
set[-1] = struct{}{}
}
}
for _, perm := range sg.IpPermissionsEgress {
if perm.FromPort != nil {
set[*perm.FromPort] = struct{}{}
}
if perm.ToPort != nil {
set[*perm.ToPort] = struct{}{}
}
if perm.FromPort == nil && perm.ToPort == nil && perm.IpProtocol != nil && *perm.IpProtocol == "-1" {
set[-1] = struct{}{}
}
}
var ports []int
for i, _ := range set {
ports = append(ports, int(i))
}
sort.Ints(ports)
return ports
}
func (sg *SecurityGroup) Inactive() bool {
return len(sg.Instances) == 0 &&
len(sg.ELBs) == 0 &&
len(sg.DBInstances) == 0 &&
len(sg.ElasticCacheClusters) == 0
}
func (set SecurityGroupSet) PortsInvolved() []int {
var (
portmap = map[int]struct{}{}
ports []int
)
for _, sg := range set {
for _, port := range sg.PortsInvolved() {
portmap[port] = struct{}{}
}
}
for port, _ := range portmap {
ports = append(ports, port)
}
sort.Ints(ports)
return ports
}