@@ -115,6 +115,17 @@ export interface SecurityGroupProps {
115115 * The VPC in which to create the security group.
116116 */
117117 vpc : VpcNetworkRef ;
118+
119+ /**
120+ * Whether to allow all outbound traffic by default.
121+ *
122+ * If this is set to true, there will only be a single egress rule which allows all
123+ * outbound traffic. If this is set to false, no outbound traffic will be allowed by
124+ * default and all egress traffic must be explicitly authorized.
125+ *
126+ * @default true
127+ */
128+ allowAllOutbound ?: boolean ;
118129}
119130
120131/**
@@ -149,11 +160,16 @@ export class SecurityGroup extends SecurityGroupRef implements ITaggable {
149160 private readonly directIngressRules : cloudformation . SecurityGroupResource . IngressProperty [ ] = [ ] ;
150161 private readonly directEgressRules : cloudformation . SecurityGroupResource . EgressProperty [ ] = [ ] ;
151162
163+ private readonly allowAllOutbound : boolean ;
164+
152165 constructor ( parent : Construct , name : string , props : SecurityGroupProps ) {
153166 super ( parent , name ) ;
154167
155168 this . tags = new TagManager ( this , { initialTags : props . tags } ) ;
156169 const groupDescription = props . description || this . path ;
170+
171+ this . allowAllOutbound = props . allowAllOutbound !== false ;
172+
157173 this . securityGroup = new cloudformation . SecurityGroupResource ( this , 'Resource' , {
158174 groupName : props . groupName ,
159175 groupDescription,
@@ -166,6 +182,8 @@ export class SecurityGroup extends SecurityGroupRef implements ITaggable {
166182 this . securityGroupId = this . securityGroup . securityGroupId ;
167183 this . groupName = this . securityGroup . securityGroupName ;
168184 this . vpcId = this . securityGroup . securityGroupVpcId ;
185+
186+ this . addDefaultEgressRule ( ) ;
169187 }
170188
171189 public addIngressRule ( peer : ISecurityGroupRule , connection : IPortRange , description ?: string ) {
@@ -186,6 +204,18 @@ export class SecurityGroup extends SecurityGroupRef implements ITaggable {
186204 }
187205
188206 public addEgressRule ( peer : ISecurityGroupRule , connection : IPortRange , description ?: string ) {
207+ if ( this . allowAllOutbound ) {
208+ // In the case of "allowAllOutbound", we don't add any more rules. There
209+ // is only one rule which allows all traffic and that subsumes any other
210+ // rule.
211+ return ;
212+ } else {
213+ // Otherwise, if the bogus rule exists we can now remove it because the
214+ // presence of any other rule will get rid of EC2's implicit "all
215+ // outbound" rule anyway.
216+ this . removeNoTrafficRule ( ) ;
217+ }
218+
189219 if ( ! peer . canInlineRule || ! connection . canInlineRule ) {
190220 super . addEgressRule ( peer , connection , description ) ;
191221 return ;
@@ -195,11 +225,22 @@ export class SecurityGroup extends SecurityGroupRef implements ITaggable {
195225 description = `from ${ peer . uniqueId } :${ connection } ` ;
196226 }
197227
198- this . addDirectEgressRule ( {
228+ const rule = {
199229 ...peer . toEgressRuleJSON ( ) ,
200230 ...connection . toRuleJSON ( ) ,
201231 description
202- } ) ;
232+ } ;
233+
234+ if ( isAllTrafficRule ( rule ) ) {
235+ // We cannot allow this; if someone adds the rule in this way, it will be
236+ // removed again if they add other rules. We also can't automatically switch
237+ // to "allOutbound=true" mode, because we might have already emitted
238+ // EgressRule objects (which count as rules added later) and there's no way
239+ // to recall those. Better to prevent this for now.
240+ throw new Error ( 'Cannot add an "all traffic" egress rule in this way; set allowAllOutbound=true on the SecurityGroup instead.' ) ;
241+ }
242+
243+ this . addDirectEgressRule ( rule ) ;
203244 }
204245
205246 /**
@@ -233,8 +274,66 @@ export class SecurityGroup extends SecurityGroupRef implements ITaggable {
233274 private hasEgressRule ( rule : cloudformation . SecurityGroupResource . EgressProperty ) : boolean {
234275 return this . directEgressRules . findIndex ( r => egressRulesEqual ( r , rule ) ) > - 1 ;
235276 }
277+
278+ /**
279+ * Add the default egress rule to the securityGroup
280+ *
281+ * This depends on allowAllOutbound:
282+ *
283+ * - If allowAllOutbound is true, we *TECHNICALLY* don't need to do anything, because
284+ * EC2 is going to create this default rule anyway. But, for maximum readability
285+ * of the template, we will add one anyway.
286+ * - If allowAllOutbound is false, we add a bogus rule that matches no traffic in
287+ * order to get rid of the default "all outbound" rule that EC2 creates by default.
288+ * If other rules happen to get added later, we remove the bogus rule again so
289+ * that it doesn't clutter up the template too much (even though that's not
290+ * strictly necessary).
291+ */
292+ private addDefaultEgressRule ( ) {
293+ if ( this . allowAllOutbound ) {
294+ this . directEgressRules . push ( ALLOW_ALL_RULE ) ;
295+ } else {
296+ this . directEgressRules . push ( MATCH_NO_TRAFFIC ) ;
297+ }
298+ }
299+
300+ /**
301+ * Remove the bogus rule if it exists
302+ */
303+ private removeNoTrafficRule ( ) {
304+ const i = this . directEgressRules . findIndex ( r => egressRulesEqual ( r , MATCH_NO_TRAFFIC ) ) ;
305+ if ( i > - 1 ) {
306+ this . directEgressRules . splice ( i , 1 ) ;
307+ }
308+ }
236309}
237310
311+ /**
312+ * Egress rule that purposely matches no traffic
313+ *
314+ * This is used in order to disable the "all traffic" default of Security Groups.
315+ *
316+ * No machine can ever actually have the 255.255.255.255 IP address, but
317+ * in order to lock it down even more we'll restrict to a nonexistent
318+ * ICMP traffic type.
319+ */
320+ const MATCH_NO_TRAFFIC = {
321+ cidrIp : '255.255.255.255/32' ,
322+ description : 'Disallow all traffic' ,
323+ ipProtocol : 'icmp' ,
324+ fromPort : 252 ,
325+ toPort : 86
326+ } ;
327+
328+ /**
329+ * Egress rule that matches all traffic
330+ */
331+ const ALLOW_ALL_RULE = {
332+ cidrIp : '0.0.0.0/0' ,
333+ description : 'Allow all outbound traffic by default' ,
334+ ipProtocol : '-1' ,
335+ } ;
336+
238337export interface ConnectionRule {
239338 /**
240339 * The IP protocol name (tcp, udp, icmp) or number (see Protocol Numbers).
@@ -315,3 +414,10 @@ function egressRulesEqual(a: cloudformation.SecurityGroupResource.EgressProperty
315414 && a . destinationPrefixListId === b . destinationPrefixListId
316415 && a . destinationSecurityGroupId === b . destinationSecurityGroupId ;
317416}
417+
418+ /**
419+ * Whether this rule refers to all traffic
420+ */
421+ function isAllTrafficRule ( rule : any ) {
422+ return rule . cidrIp === '0.0.0.0/0' && rule . ipProtocol === '-1' ;
423+ }
0 commit comments