-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathconnections.test.ts
334 lines (272 loc) · 12.1 KB
/
connections.test.ts
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import { expect, haveResource } from '@aws-cdk/assert';
import { App, ConstructNode, Stack } from '@aws-cdk/core';
import { nodeunitShim, Test } from 'nodeunit-shim';
import {
Connections,
IConnectable,
Port,
SecurityGroup,
Vpc,
} from '../lib';
nodeunitShim({
'peering between two security groups does not recursive infinitely'(test: Test) {
// GIVEN
const stack = new Stack(undefined, 'TestStack', { env: { account: '12345678', region: 'dummy' }});
const vpc = new Vpc(stack, 'VPC');
const sg1 = new SecurityGroup(stack, 'SG1', { vpc });
const sg2 = new SecurityGroup(stack, 'SG2', { vpc });
const conn1 = new SomethingConnectable(new Connections({ securityGroups: [sg1] }));
const conn2 = new SomethingConnectable(new Connections({ securityGroups: [sg2] }));
// WHEN
conn1.connections.allowTo(conn2, Port.tcp(80), 'Test');
// THEN -- it finishes!
test.done();
},
'(imported) SecurityGroup can be used as target of .allowTo()'(test: Test) {
// GIVEN
const stack = new Stack();
const vpc = new Vpc(stack, 'VPC');
const sg1 = new SecurityGroup(stack, 'SomeSecurityGroup', { vpc, allowAllOutbound: false });
const somethingConnectable = new SomethingConnectable(new Connections({ securityGroups: [sg1] }));
const securityGroup = SecurityGroup.fromSecurityGroupId(stack, 'ImportedSG', 'sg-12345');
// WHEN
somethingConnectable.connections.allowTo(securityGroup, Port.allTcp(), 'Connect there');
// THEN: rule to generated security group to connect to imported
expect(stack).to(haveResource('AWS::EC2::SecurityGroupEgress', {
GroupId: { 'Fn::GetAtt': [ 'SomeSecurityGroupEF219AD6', 'GroupId' ] },
IpProtocol: 'tcp',
Description: 'Connect there',
DestinationSecurityGroupId: 'sg-12345',
FromPort: 0,
ToPort: 65535,
}));
// THEN: rule to imported security group to allow connections from generated
expect(stack).to(haveResource('AWS::EC2::SecurityGroupIngress', {
IpProtocol: 'tcp',
Description: 'Connect there',
FromPort: 0,
GroupId: 'sg-12345',
SourceSecurityGroupId: { 'Fn::GetAtt': [ 'SomeSecurityGroupEF219AD6', 'GroupId' ] },
ToPort: 65535,
}));
test.done();
},
'security groups added to connections after rule still gets rule'(test: Test) {
// GIVEN
const stack = new Stack();
const vpc = new Vpc(stack, 'VPC');
const sg1 = new SecurityGroup(stack, 'SecurityGroup1', { vpc, allowAllOutbound: false });
const sg2 = new SecurityGroup(stack, 'SecurityGroup2', { vpc, allowAllOutbound: false });
const connections = new Connections({ securityGroups: [sg1] });
// WHEN
connections.allowFromAnyIpv4(Port.tcp(88));
connections.addSecurityGroup(sg2);
// THEN
expect(stack).to(haveResource('AWS::EC2::SecurityGroup', {
GroupDescription: 'Default/SecurityGroup1',
SecurityGroupIngress: [
{
Description: 'from 0.0.0.0/0:88',
CidrIp: '0.0.0.0/0',
FromPort: 88,
ToPort: 88,
IpProtocol: 'tcp',
},
],
}));
expect(stack).to(haveResource('AWS::EC2::SecurityGroup', {
GroupDescription: 'Default/SecurityGroup2',
SecurityGroupIngress: [
{
Description: 'from 0.0.0.0/0:88',
CidrIp: '0.0.0.0/0',
FromPort: 88,
ToPort: 88,
IpProtocol: 'tcp',
},
],
}));
test.done();
},
'when security groups are added to target they also get the rule'(test: Test) {
// GIVEN
const stack = new Stack();
const vpc = new Vpc(stack, 'VPC');
const sg1 = new SecurityGroup(stack, 'SecurityGroup1', { vpc, allowAllOutbound: false });
const sg2 = new SecurityGroup(stack, 'SecurityGroup2', { vpc, allowAllOutbound: false });
const sg3 = new SecurityGroup(stack, 'SecurityGroup3', { vpc, allowAllOutbound: false });
const connections1 = new Connections({ securityGroups: [sg1] });
const connections2 = new Connections({ securityGroups: [sg2] });
const connectable = new SomethingConnectable(connections2);
// WHEN
connections1.allowTo(connectable, Port.tcp(88));
connections2.addSecurityGroup(sg3);
// THEN
expect(stack).to(haveResource('AWS::EC2::SecurityGroupIngress', {
GroupId: { 'Fn::GetAtt': [ 'SecurityGroup23BE86BB7', 'GroupId' ] },
SourceSecurityGroupId: { 'Fn::GetAtt': [ 'SecurityGroup1F554B36F', 'GroupId' ] },
FromPort: 88,
ToPort: 88,
}));
expect(stack).to(haveResource('AWS::EC2::SecurityGroupIngress', {
GroupId: { 'Fn::GetAtt': [ 'SecurityGroup3E5E374B9', 'GroupId' ] },
SourceSecurityGroupId: { 'Fn::GetAtt': [ 'SecurityGroup1F554B36F', 'GroupId' ] },
FromPort: 88,
ToPort: 88,
}));
test.done();
},
'multiple security groups allows internally between them'(test: Test) {
// GIVEN
const stack = new Stack();
const vpc = new Vpc(stack, 'VPC');
const sg1 = new SecurityGroup(stack, 'SecurityGroup1', { vpc, allowAllOutbound: false });
const sg2 = new SecurityGroup(stack, 'SecurityGroup2', { vpc, allowAllOutbound: false });
const connections = new Connections({ securityGroups: [sg1] });
// WHEN
connections.allowInternally(Port.tcp(88));
connections.addSecurityGroup(sg2);
// THEN
expect(stack).to(haveResource('AWS::EC2::SecurityGroupIngress', {
GroupId: { 'Fn::GetAtt': [ 'SecurityGroup1F554B36F', 'GroupId' ] },
SourceSecurityGroupId: { 'Fn::GetAtt': [ 'SecurityGroup1F554B36F', 'GroupId' ] },
FromPort: 88,
ToPort: 88,
}));
expect(stack).to(haveResource('AWS::EC2::SecurityGroupEgress', {
DestinationSecurityGroupId: { 'Fn::GetAtt': [ 'SecurityGroup1F554B36F', 'GroupId' ] },
GroupId: { 'Fn::GetAtt': [ 'SecurityGroup1F554B36F', 'GroupId' ] },
FromPort: 88,
ToPort: 88,
}));
test.done();
},
'can establish cross stack Security Group connections - allowFrom'(test: Test) {
// GIVEN
const app = new App();
const stack1 = new Stack(app, 'Stack1');
const vpc1 = new Vpc(stack1, 'VPC');
const sg1 = new SecurityGroup(stack1, 'SecurityGroup', { vpc: vpc1, allowAllOutbound: false });
const stack2 = new Stack(app, 'Stack2');
const vpc2 = new Vpc(stack2, 'VPC');
const sg2 = new SecurityGroup(stack2, 'SecurityGroup', { vpc: vpc2, allowAllOutbound: false });
// WHEN
sg2.connections.allowFrom(sg1, Port.tcp(100));
// THEN -- both rules are in Stack2
ConstructNode.prepare(app.node);
expect(stack2).to(haveResource('AWS::EC2::SecurityGroupIngress', {
GroupId: { 'Fn::GetAtt': [ 'SecurityGroupDD263621', 'GroupId' ] },
SourceSecurityGroupId: { 'Fn::ImportValue': 'Stack1:ExportsOutputFnGetAttSecurityGroupDD263621GroupIdDF6F8B09' },
}));
expect(stack2).to(haveResource('AWS::EC2::SecurityGroupEgress', {
GroupId: { 'Fn::ImportValue': 'Stack1:ExportsOutputFnGetAttSecurityGroupDD263621GroupIdDF6F8B09' },
DestinationSecurityGroupId: { 'Fn::GetAtt': [ 'SecurityGroupDD263621', 'GroupId' ] },
}));
test.done();
},
'can establish cross stack Security Group connections - allowTo'(test: Test) {
// GIVEN
const app = new App();
const stack1 = new Stack(app, 'Stack1');
const vpc1 = new Vpc(stack1, 'VPC');
const sg1 = new SecurityGroup(stack1, 'SecurityGroup', { vpc: vpc1, allowAllOutbound: false });
const stack2 = new Stack(app, 'Stack2');
const vpc2 = new Vpc(stack2, 'VPC');
const sg2 = new SecurityGroup(stack2, 'SecurityGroup', { vpc: vpc2, allowAllOutbound: false });
// WHEN
sg2.connections.allowTo(sg1, Port.tcp(100));
// THEN -- both rules are in Stack2
ConstructNode.prepare(app.node);
expect(stack2).to(haveResource('AWS::EC2::SecurityGroupIngress', {
GroupId: { 'Fn::ImportValue': 'Stack1:ExportsOutputFnGetAttSecurityGroupDD263621GroupIdDF6F8B09' },
SourceSecurityGroupId: { 'Fn::GetAtt': [ 'SecurityGroupDD263621', 'GroupId' ] },
}));
expect(stack2).to(haveResource('AWS::EC2::SecurityGroupEgress', {
GroupId: { 'Fn::GetAtt': [ 'SecurityGroupDD263621', 'GroupId' ] },
DestinationSecurityGroupId: { 'Fn::ImportValue': 'Stack1:ExportsOutputFnGetAttSecurityGroupDD263621GroupIdDF6F8B09' },
}));
test.done();
},
'can establish multiple cross-stack SGs'(test: Test) {
// GIVEN
const app = new App();
const stack1 = new Stack(app, 'Stack1');
const vpc1 = new Vpc(stack1, 'VPC');
const sg1a = new SecurityGroup(stack1, 'SecurityGroupA', { vpc: vpc1, allowAllOutbound: false });
const sg1b = new SecurityGroup(stack1, 'SecurityGroupB', { vpc: vpc1, allowAllOutbound: false });
const stack2 = new Stack(app, 'Stack2');
const vpc2 = new Vpc(stack2, 'VPC');
const sg2 = new SecurityGroup(stack2, 'SecurityGroup', { vpc: vpc2, allowAllOutbound: false });
// WHEN
sg2.connections.allowFrom(sg1a, Port.tcp(100));
sg2.connections.allowFrom(sg1b, Port.tcp(100));
// THEN -- both egress rules are in Stack2
ConstructNode.prepare(app.node);
expect(stack2).to(haveResource('AWS::EC2::SecurityGroupEgress', {
GroupId: { 'Fn::ImportValue': 'Stack1:ExportsOutputFnGetAttSecurityGroupAED40ADC5GroupId1D10C76A' },
DestinationSecurityGroupId: { 'Fn::GetAtt': [ 'SecurityGroupDD263621', 'GroupId' ] },
}));
expect(stack2).to(haveResource('AWS::EC2::SecurityGroupEgress', {
GroupId: { 'Fn::ImportValue': 'Stack1:ExportsOutputFnGetAttSecurityGroupB04591F90GroupIdFA7208D5' },
DestinationSecurityGroupId: { 'Fn::GetAtt': [ 'SecurityGroupDD263621', 'GroupId' ] },
}));
test.done();
},
'Imported SecurityGroup does not create egress rule'(test: Test) {
// GIVEN
const stack = new Stack();
const vpc = new Vpc(stack, 'VPC');
const sg1 = new SecurityGroup(stack, 'SomeSecurityGroup', { vpc, allowAllOutbound: false });
const somethingConnectable = new SomethingConnectable(new Connections({ securityGroups: [sg1] }));
const securityGroup = SecurityGroup.fromSecurityGroupId(stack, 'ImportedSG', 'sg-12345');
// WHEN
somethingConnectable.connections.allowFrom(securityGroup, Port.allTcp(), 'Connect there');
// THEN: rule to generated security group to connect to imported
expect(stack).to(haveResource('AWS::EC2::SecurityGroupIngress', {
GroupId: { 'Fn::GetAtt': [ 'SomeSecurityGroupEF219AD6', 'GroupId' ] },
IpProtocol: 'tcp',
Description: 'Connect there',
SourceSecurityGroupId: 'sg-12345',
FromPort: 0,
ToPort: 65535,
}));
// THEN: rule to imported security group to allow connections from generated
expect(stack).notTo(haveResource('AWS::EC2::SecurityGroupEgress'));
test.done();
},
'Imported SecurityGroup with allowAllOutbound: false DOES create egress rule'(test: Test) {
// GIVEN
const stack = new Stack();
const vpc = new Vpc(stack, 'VPC');
const sg1 = new SecurityGroup(stack, 'SomeSecurityGroup', { vpc, allowAllOutbound: false });
const somethingConnectable = new SomethingConnectable(new Connections({ securityGroups: [sg1] }));
const securityGroup = SecurityGroup.fromSecurityGroupId(stack, 'ImportedSG', 'sg-12345', {
allowAllOutbound: false,
});
// WHEN
somethingConnectable.connections.allowFrom(securityGroup, Port.allTcp(), 'Connect there');
// THEN: rule to generated security group to connect to imported
expect(stack).to(haveResource('AWS::EC2::SecurityGroupIngress', {
GroupId: { 'Fn::GetAtt': [ 'SomeSecurityGroupEF219AD6', 'GroupId' ] },
IpProtocol: 'tcp',
Description: 'Connect there',
SourceSecurityGroupId: 'sg-12345',
FromPort: 0,
ToPort: 65535,
}));
// THEN: rule to imported security group to allow connections from generated
expect(stack).to(haveResource('AWS::EC2::SecurityGroupEgress', {
IpProtocol: 'tcp',
Description: 'Connect there',
FromPort: 0,
GroupId: 'sg-12345',
DestinationSecurityGroupId: { 'Fn::GetAtt': [ 'SomeSecurityGroupEF219AD6', 'GroupId' ] },
ToPort: 65535,
}));
test.done();
},
});
class SomethingConnectable implements IConnectable {
constructor(public readonly connections: Connections) {
}
}