@@ -178,6 +178,7 @@ describe('security group', () => {
178
178
Peer . anyIpv4 ( ) ,
179
179
Peer . anyIpv6 ( ) ,
180
180
Peer . prefixList ( 'pl-012345' ) ,
181
+ Peer . securityGroupId ( 'sg-012345678' ) ,
181
182
] ;
182
183
183
184
const ports = [
@@ -337,6 +338,125 @@ describe('security group', () => {
337
338
} ) ;
338
339
} ) ;
339
340
341
+ describe ( 'Peer security group ID validation' , ( ) => {
342
+ test ( 'passes with valid security group ID' , ( ) => {
343
+ //GIVEN
344
+ const securityGroupIds = [ 'sg-12345678' , 'sg-0123456789abcdefg' ] ;
345
+
346
+ // THEN
347
+ for ( const securityGroupId of securityGroupIds ) {
348
+ expect ( Peer . securityGroupId ( securityGroupId ) . uniqueId ) . toEqual ( securityGroupId ) ;
349
+ }
350
+ } ) ;
351
+
352
+ test ( 'passes with valid security group ID and source owner id' , ( ) => {
353
+ //GIVEN
354
+ const securityGroupIds = [ 'sg-12345678' , 'sg-0123456789abcdefg' ] ;
355
+ const ownerIds = [ '000000000000' , '000000000001' ] ;
356
+
357
+ // THEN
358
+ for ( const securityGroupId of securityGroupIds ) {
359
+ for ( const ownerId of ownerIds ) {
360
+ expect ( Peer . securityGroupId ( securityGroupId , ownerId ) . uniqueId ) . toEqual ( securityGroupId ) ;
361
+ }
362
+ }
363
+ } ) ;
364
+
365
+ test ( 'passes with unresolved security group id token or owner id token' , ( ) => {
366
+ // GIVEN
367
+ Token . asString ( 'securityGroupId' ) ;
368
+
369
+ const securityGroupId = Lazy . string ( { produce : ( ) => 'sg-01234567' } ) ;
370
+ const ownerId = Lazy . string ( { produce : ( ) => '000000000000' } ) ;
371
+ Peer . securityGroupId ( securityGroupId ) ;
372
+ Peer . securityGroupId ( securityGroupId , ownerId ) ;
373
+
374
+ // THEN: don't throw
375
+ } ) ;
376
+
377
+ test ( 'throws if invalid security group ID' , ( ) => {
378
+ // THEN
379
+ expect ( ( ) => {
380
+ Peer . securityGroupId ( 'invalid' ) ;
381
+ } ) . toThrow ( / I n v a l i d s e c u r i t y g r o u p I D / ) ;
382
+
383
+
384
+ } ) ;
385
+
386
+ test ( 'throws if invalid source security group id' , ( ) => {
387
+ // THEN
388
+ expect ( ( ) => {
389
+ Peer . securityGroupId ( 'sg-12345678' , 'invalid' ) ;
390
+ } ) . toThrow ( / I n v a l i d s e c u r i t y g r o u p o w n e r I D / ) ;
391
+ } ) ;
392
+ } ) ;
393
+
394
+ describe ( 'SourceSecurityGroupOwnerId property validation' , ( ) => {
395
+ test ( 'SourceSecurityGroupOwnerId property is not present when value is not provided to ingress rule' , ( ) => {
396
+ // GIVEN
397
+ const stack = new Stack ( undefined , 'TestStack' ) ;
398
+ const vpc = new Vpc ( stack , 'VPC' ) ;
399
+ const sg = new SecurityGroup ( stack , 'SG' , { vpc } ) ;
400
+
401
+ //WHEN
402
+ sg . addIngressRule ( Peer . securityGroupId ( 'sg-123456789' ) , Port . allTcp ( ) , 'no owner id property' ) ;
403
+
404
+ //THEN
405
+ expect ( stack ) . toHaveResource ( 'AWS::EC2::SecurityGroup' , {
406
+ SecurityGroupIngress : [ {
407
+ SourceSecurityGroupId : 'sg-123456789' ,
408
+ Description : 'no owner id property' ,
409
+ FromPort : 0 ,
410
+ ToPort : 65535 ,
411
+ IpProtocol : 'tcp' ,
412
+ } ] ,
413
+ } ) ;
414
+ } ) ;
415
+
416
+ test ( 'SourceSecurityGroupOwnerId property is present when value is provided to ingress rule' , ( ) => {
417
+ // GIVEN
418
+ const stack = new Stack ( undefined , 'TestStack' ) ;
419
+ const vpc = new Vpc ( stack , 'VPC' ) ;
420
+ const sg = new SecurityGroup ( stack , 'SG' , { vpc } ) ;
421
+
422
+ //WHEN
423
+ sg . addIngressRule ( Peer . securityGroupId ( 'sg-123456789' , '000000000000' ) , Port . allTcp ( ) , 'contains owner id property' ) ;
424
+
425
+ //THEN
426
+ expect ( stack ) . toHaveResource ( 'AWS::EC2::SecurityGroup' , {
427
+ SecurityGroupIngress : [ {
428
+ SourceSecurityGroupId : 'sg-123456789' ,
429
+ SourceSecurityGroupOwnerId : '000000000000' ,
430
+ Description : 'contains owner id property' ,
431
+ FromPort : 0 ,
432
+ ToPort : 65535 ,
433
+ IpProtocol : 'tcp' ,
434
+ } ] ,
435
+ } ) ;
436
+ } ) ;
437
+
438
+ test ( 'SourceSecurityGroupOwnerId property is not present when value is provided to egress rule' , ( ) => {
439
+ // GIVEN
440
+ const stack = new Stack ( undefined , 'TestStack' ) ;
441
+ const vpc = new Vpc ( stack , 'VPC' ) ;
442
+ const sg = new SecurityGroup ( stack , 'SG' , { vpc, allowAllOutbound : false } ) ;
443
+
444
+ //WHEN
445
+ sg . addEgressRule ( Peer . securityGroupId ( 'sg-123456789' , '000000000000' ) , Port . allTcp ( ) , 'no owner id property' ) ;
446
+
447
+ //THEN
448
+ expect ( stack ) . toHaveResource ( 'AWS::EC2::SecurityGroup' , {
449
+ SecurityGroupEgress : [ {
450
+ DestinationSecurityGroupId : 'sg-123456789' ,
451
+ Description : 'no owner id property' ,
452
+ FromPort : 0 ,
453
+ ToPort : 65535 ,
454
+ IpProtocol : 'tcp' ,
455
+ } ] ,
456
+ } ) ;
457
+ } ) ;
458
+ } ) ;
459
+
340
460
testDeprecated ( 'can look up a security group' , ( ) => {
341
461
const app = new App ( ) ;
342
462
const stack = new Stack ( app , 'stack' , {
0 commit comments