-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
193 lines (168 loc) · 5.2 KB
/
main.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
package main
import (
"flag"
"os"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"go.uber.org/zap"
)
var (
vpcID string
availabilityZone string
region string
duration time.Duration
logger *zap.Logger
)
func init() {
logger = zap.NewExample()
defer logger.Sync()
flag.StringVar(®ion, "region", os.Getenv("AWS_REGION"), "AWS region")
flag.StringVar(&vpcID, "vpc", "", "AWS vpc ID, required")
flag.StringVar(&availabilityZone, "az", "", "AWS availability zone name, required")
flag.DurationVar(&duration, "duration", time.Duration(60), "AWS availability zone network partiton duration (default 60s)")
}
func main() {
flag.Parse()
if vpcID == "" || availabilityZone == "" {
logger.Fatal("must specipy vpcID and availabilityZone")
}
ec2Client := ec2.New(session.New())
// list subnets by vpc ID and availability zone name
subnets, err := ec2Client.DescribeSubnets(&ec2.DescribeSubnetsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("vpc-id"),
Values: []*string{
aws.String(vpcID),
},
},
{
Name: aws.String("availability-zone"),
Values: []*string{
aws.String(availabilityZone),
},
},
},
})
if err != nil {
logger.Fatal("DescribeSubnets", zap.Error(err))
}
if len(subnets.Subnets) == 0 {
logger.Info("no subnets in vpc", zap.String("vpcID", vpcID), zap.String("availabilityZoneName", availabilityZone))
return
}
logger.Info("DescribeSubnetsResult", zap.Any("Subnets", subnets.Subnets))
// save original AclAssociationId and NetworkAclId mapping
// AclAssociationId will used for replace network acl, AclAssociationId will updated when execute replace
// save original NetworkAclId used for recovery
originAclAssociations := make(map[string]string)
for _, subnet := range subnets.Subnets {
// list network acl by subnet id
subnetID := subnet.SubnetId
aclFilters := &ec2.DescribeNetworkAclsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("vpc-id"),
Values: []*string{
aws.String(vpcID),
},
},
{
Name: aws.String("association.subnet-id"),
Values: []*string{
subnetID,
},
},
},
}
acls, err := ec2Client.DescribeNetworkAcls(aclFilters)
if err != nil {
logger.Fatal("DescribeNetworkAcls", zap.Error(err))
}
for _, acl := range acls.NetworkAcls {
for _, ass := range acl.Associations {
if *ass.SubnetId == *subnetID {
originAclAssociations[*ass.NetworkAclAssociationId] = *ass.NetworkAclId
}
}
}
}
logger.Info("DescribeNetworkAclsResult", zap.Any("originAclAssociations", originAclAssociations))
acl, err := ec2Client.CreateNetworkAcl(&ec2.CreateNetworkAclInput{
VpcId: aws.String(vpcID),
})
if err != nil {
logger.Fatal("CreateNetworkAcl", zap.Error(err))
}
aclID := acl.NetworkAcl.NetworkAclId
logger.Info("CreateNetworkAclResult", zap.String("aclID", *aclID))
// The rule deny all out bound traffic to anywhere
engress := &ec2.CreateNetworkAclEntryInput{
CidrBlock: aws.String("0.0.0.0/0"),
Egress: aws.Bool(true),
NetworkAclId: aclID,
PortRange: &ec2.PortRange{
From: aws.Int64(53),
To: aws.Int64(53),
},
Protocol: aws.String("-1"),
RuleAction: aws.String("deny"),
RuleNumber: aws.Int64(100),
}
_, err = ec2Client.CreateNetworkAclEntry(engress)
if err != nil {
logger.Fatal("CreateNetworkAclEntry", zap.Error(err))
}
// The rule deny all in bound traffic from anywhere
ingress := &ec2.CreateNetworkAclEntryInput{
CidrBlock: aws.String("0.0.0.0/0"),
Egress: aws.Bool(false),
NetworkAclId: aclID,
PortRange: &ec2.PortRange{
From: aws.Int64(53),
To: aws.Int64(53),
},
Protocol: aws.String("-1"),
RuleAction: aws.String("deny"),
RuleNumber: aws.Int64(101),
}
_, err = ec2Client.CreateNetworkAclEntry(ingress)
if err != nil {
logger.Fatal("CreateNetworkAclEntry", zap.Error(err))
}
newAclAssociations := make(map[string]string)
for assID, originAclID := range originAclAssociations {
replacedNetworkAclAssociation, err := ec2Client.ReplaceNetworkAclAssociation(&ec2.ReplaceNetworkAclAssociationInput{
AssociationId: aws.String(assID),
NetworkAclId: aclID,
})
if err != nil {
logger.Fatal("ReplaceNetworkAclAssociation", zap.Error(err))
}
// save NewAssociationId and originAclID for recovery
newAclAssociations[*replacedNetworkAclAssociation.NewAssociationId] = originAclID
}
logger.Info("inject availability zone network partition successful")
logger.Info("sleep", zap.Duration("durtion", duration))
time.Sleep(duration * time.Second)
logger.Info("start recovery error injection")
for newAssID, originAclID := range newAclAssociations {
_, err := ec2Client.ReplaceNetworkAclAssociation(&ec2.ReplaceNetworkAclAssociationInput{
AssociationId: aws.String(newAssID),
NetworkAclId: aws.String(originAclID),
})
if err != nil {
logger.Fatal("ReplaceNetworkAclAssociation", zap.Error(err))
}
}
logger.Info("recovery acl associations successful")
_, err = ec2Client.DeleteNetworkAcl(&ec2.DeleteNetworkAclInput{
NetworkAclId: aclID,
})
if err != nil {
logger.Fatal("DeleteNetworkAcl", zap.Error(err))
}
logger.Info("recovery network acl successful")
}