/
check.go
197 lines (178 loc) · 5.88 KB
/
check.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
package main
import (
"github.com/aws/aws-sdk-go/service/ec2"
)
var (
checkNameByInstanceName = map[string]string{
"isucon12-qualify-1": "qualify1",
"isucon12-qualify-2": "qualify2",
"isucon12-qualify-3": "qualify3",
}
privateAddressByInstanceName = map[string]string{
"isucon12-qualify-1": "192.168.0.11",
"isucon12-qualify-2": "192.168.0.12",
"isucon12-qualify-3": "192.168.0.13",
}
)
func (c *checker) checkAll() {
c.checkInstances()
c.checkVolumes()
c.checkNetworkInterfaces()
c.checkSecurityGroups()
}
func (c *checker) checkInstances() {
var count int
for _, o := range c.DescribeInstances {
for _, r := range o.Reservations {
for _, i := range r.Instances {
count++
c.checkInstance(i)
}
}
}
if count > 3 {
c.addFailure("%d個の EC2 インスタンスが検出されました (3個である必要があります)", count)
}
}
func (c *checker) checkInstance(i *ec2.Instance) {
id := *i.InstanceId
if *i.InstanceType != c.ExpectedInstanceType {
c.addFailure("%s のインスタンスタイプが %s です (%s である必要があります)", id, *i.InstanceType, c.ExpectedInstanceType)
}
if c.ExpectedAMI != "" && *i.ImageId != c.ExpectedAMI {
c.addFailure("%s の AMI が %s です (%s である必要があります)", id, *i.ImageId, c.ExpectedAMI)
}
if c.ExpectedAZ != "" {
azName := GetAZName(c.DescribeAvailabilityZones, c.ExpectedAZ)
if *i.Placement.AvailabilityZone != azName {
c.addFailure("%s のゾーンが %s です (%s (ID: %s) である必要があります)", id, *i.Placement.AvailabilityZone, azName, c.ExpectedAZ)
}
}
if len(i.BlockDeviceMappings) != 1 {
c.addFailure("%s に %d 個のブロックデバイスが検出されました (1個である必要があります)", id, len(i.BlockDeviceMappings))
} else if i.BlockDeviceMappings[0].Ebs == nil {
c.addFailure("%s のブロックデバイスが EBS ではありません", id)
}
if len(i.NetworkInterfaces) != 1 {
c.addFailure("%s に %d 個のネットワークインターフェイスが検出されました (1個である必要があります)", id, len(i.NetworkInterfaces))
}
}
func (c *checker) checkVolumes() {
for _, o := range c.DescribeVolumes {
for _, v := range o.Volumes {
c.checkVolume(v)
}
}
}
func (c *checker) checkVolume(v *ec2.Volume) {
id := *v.VolumeId
if *v.Size != 20 {
c.addFailure("%s のサイズが %d GB です (20 GB である必要があります)", id, *v.Size)
}
if *v.VolumeType != "gp3" {
c.addFailure("%s のタイプが %s です (gp3 である必要があります)", id, *v.VolumeType)
}
}
func (c *checker) checkNetworkInterfaces() {
allowedInstances := make(map[string]struct{})
for _, o := range c.DescribeInstances {
for _, r := range o.Reservations {
for _, i := range r.Instances {
allowedInstances[*i.InstanceId] = struct{}{}
}
}
}
isAllowed := func(i *ec2.NetworkInterface) bool {
if i.Attachment == nil || i.Attachment.InstanceId == nil {
return false
}
_, ok := allowedInstances[*i.Attachment.InstanceId]
return ok
}
for _, o := range c.DescribeNetworkInterfaces {
for _, i := range o.NetworkInterfaces {
if !isAllowed(i) {
c.addFailure("不明なネットワークインターフェイス (%s) が VPC 内に見つかりました", *i.NetworkInterfaceId)
}
}
}
}
func (c *checker) checkSecurityGroups() {
for _, out := range c.DescribeSecurityGroups {
for _, sg := range out.SecurityGroups {
c.checkSecurityGroup(sg)
}
}
}
func (c *checker) checkSecurityGroup(sg *ec2.SecurityGroup) {
id := *sg.GroupId
var hasIngressSSH, hasIngressHTTPS, hasIngressInternal bool
for _, p := range sg.IpPermissions {
if c.isIngressSSH(p) {
hasIngressSSH = true
continue
}
if c.isIngressHTTPS(p) {
hasIngressHTTPS = true
continue
}
if c.isIngressInternal(p) {
hasIngressInternal = true
continue
}
c.addFailure("%s に不明なルールが見つかりました", id)
}
if !hasIngressSSH {
c.addFailure("%s に SSH を許可するルールがありません", id)
}
if !hasIngressHTTPS {
c.addFailure("%s に HTTPS を許可するルールがありません", id)
}
if !hasIngressInternal {
c.addFailure("%s にインスタンス間通信を許可するルールがありません", id)
}
if len(sg.IpPermissionsEgress) != 1 {
c.addFailure("%s のルールが不正です", id)
}
for _, p := range sg.IpPermissionsEgress {
if !c.isEgressAll(p) {
c.addFailure("%s に不正なルールが見つかりました", id)
}
}
}
func (c *checker) isIngressSSH(p *ec2.IpPermission) bool {
return p.FromPort != nil && *p.FromPort == 22 &&
p.ToPort != nil && *p.ToPort == 22 &&
p.IpProtocol != nil && *p.IpProtocol == "tcp" &&
len(p.IpRanges) == 1 && p.IpRanges[0].CidrIp != nil && *p.IpRanges[0].CidrIp == "0.0.0.0/0" &&
p.Ipv6Ranges == nil &&
p.PrefixListIds == nil &&
p.UserIdGroupPairs == nil
}
func (c *checker) isIngressHTTPS(p *ec2.IpPermission) bool {
return p.FromPort != nil && *p.FromPort == 443 &&
p.ToPort != nil && *p.ToPort == 443 &&
p.IpProtocol != nil && *p.IpProtocol == "tcp" &&
len(p.IpRanges) == 1 && p.IpRanges[0].CidrIp != nil && *p.IpRanges[0].CidrIp == "0.0.0.0/0" &&
p.Ipv6Ranges == nil &&
p.PrefixListIds == nil &&
p.UserIdGroupPairs == nil
}
func (c *checker) isIngressInternal(p *ec2.IpPermission) bool {
return p.FromPort == nil &&
p.ToPort == nil &&
p.IpProtocol != nil && *p.IpProtocol == "-1" &&
len(p.IpRanges) == 1 && p.IpRanges[0].CidrIp != nil && *p.IpRanges[0].CidrIp == "192.168.0.0/24" &&
p.Ipv6Ranges == nil &&
p.PrefixListIds == nil &&
p.UserIdGroupPairs == nil
}
func (c *checker) isEgressAll(p *ec2.IpPermission) bool {
return p.FromPort == nil &&
p.ToPort == nil &&
p.IpProtocol != nil && *p.IpProtocol == "-1" &&
len(p.IpRanges) == 1 && p.IpRanges[0].CidrIp != nil && *p.IpRanges[0].CidrIp == "0.0.0.0/0" &&
p.Ipv6Ranges == nil &&
p.PrefixListIds == nil &&
p.UserIdGroupPairs == nil
}