@@ -261,6 +261,69 @@ export = {
261
261
DestinationSecurityGroupId : { "Fn::GetAtt" : [ "SecurityGroupDD263621" , "GroupId" ] } ,
262
262
} ) ) ;
263
263
264
+ test . done ( ) ;
265
+ } ,
266
+ 'Imported SecurityGroup does not create egress rule' ( test : Test ) {
267
+ // GIVEN
268
+ const stack = new Stack ( ) ;
269
+ const vpc = new Vpc ( stack , 'VPC' ) ;
270
+ const sg1 = new SecurityGroup ( stack , 'SomeSecurityGroup' , { vpc, allowAllOutbound : false } ) ;
271
+ const somethingConnectable = new SomethingConnectable ( new Connections ( { securityGroups : [ sg1 ] } ) ) ;
272
+
273
+ const securityGroup = SecurityGroup . fromSecurityGroupId ( stack , 'ImportedSG' , 'sg-12345' ) ;
274
+
275
+ // WHEN
276
+ somethingConnectable . connections . allowFrom ( securityGroup , Port . allTcp ( ) , 'Connect there' ) ;
277
+
278
+ // THEN: rule to generated security group to connect to imported
279
+ expect ( stack ) . to ( haveResource ( "AWS::EC2::SecurityGroupIngress" , {
280
+ GroupId : { "Fn::GetAtt" : [ "SomeSecurityGroupEF219AD6" , "GroupId" ] } ,
281
+ IpProtocol : "tcp" ,
282
+ Description : "Connect there" ,
283
+ SourceSecurityGroupId : "sg-12345" ,
284
+ FromPort : 0 ,
285
+ ToPort : 65535
286
+ } ) ) ;
287
+
288
+ // THEN: rule to imported security group to allow connections from generated
289
+ expect ( stack ) . notTo ( haveResource ( "AWS::EC2::SecurityGroupEgress" ) ) ;
290
+
291
+ test . done ( ) ;
292
+ } ,
293
+ 'Imported SecurityGroup with allowAllOutbound: false DOES create egress rule' ( test : Test ) {
294
+ // GIVEN
295
+ const stack = new Stack ( ) ;
296
+ const vpc = new Vpc ( stack , 'VPC' ) ;
297
+ const sg1 = new SecurityGroup ( stack , 'SomeSecurityGroup' , { vpc, allowAllOutbound : false } ) ;
298
+ const somethingConnectable = new SomethingConnectable ( new Connections ( { securityGroups : [ sg1 ] } ) ) ;
299
+
300
+ const securityGroup = SecurityGroup . fromSecurityGroupId ( stack , 'ImportedSG' , 'sg-12345' , {
301
+ allowAllOutbound : false
302
+ } ) ;
303
+
304
+ // WHEN
305
+ somethingConnectable . connections . allowFrom ( securityGroup , Port . allTcp ( ) , 'Connect there' ) ;
306
+
307
+ // THEN: rule to generated security group to connect to imported
308
+ expect ( stack ) . to ( haveResource ( "AWS::EC2::SecurityGroupIngress" , {
309
+ GroupId : { "Fn::GetAtt" : [ "SomeSecurityGroupEF219AD6" , "GroupId" ] } ,
310
+ IpProtocol : "tcp" ,
311
+ Description : "Connect there" ,
312
+ SourceSecurityGroupId : "sg-12345" ,
313
+ FromPort : 0 ,
314
+ ToPort : 65535
315
+ } ) ) ;
316
+
317
+ // THEN: rule to imported security group to allow connections from generated
318
+ expect ( stack ) . to ( haveResource ( "AWS::EC2::SecurityGroupEgress" , {
319
+ IpProtocol : "tcp" ,
320
+ Description : "Connect there" ,
321
+ FromPort : 0 ,
322
+ GroupId : "sg-12345" ,
323
+ DestinationSecurityGroupId : { "Fn::GetAtt" : [ "SomeSecurityGroupEF219AD6" , "GroupId" ] } ,
324
+ ToPort : 65535
325
+ } ) ) ;
326
+
264
327
test . done ( ) ;
265
328
}
266
329
} ;
0 commit comments