-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit ccb1955
authored
fix(ecs_patterns): openListener should be false when custom sg is provided (#35297)
### Issue # (if applicable)
Closes #35292.
### Reason for this change
This change addresses a security vulnerability in the ECS patterns where providing a custom load balancer would still result in the creation of overly permissive internet access rules, potentially bypassing user-intended security controls.
**The Security Issue:**
The `openListener` property controls whether CDK automatically creates security group ingress rules allowing internet traffic to reach the load balancer:
- **`openListener: true` (current default)**: CDK automatically creates an `AWS::EC2::SecurityGroupIngress` rule with `CidrIp: '0.0.0.0/0'`, allowing **anyone on the internet** to access the load balancer on the listener port
- **`openListener: false`**: CDK does **not** create automatic ingress rules, leaving security group management entirely to the user
**The Problem:**
Currently, `openListener` always defaults to `true`, regardless of whether users have provided custom load balancers with their own security groups. This creates a security gap where:
1. Users carefully configure custom load balancers with restrictive security groups
2. CDK still automatically adds `0.0.0.0/0` ingress rules, potentially bypassing the user's intended access controls
3. The load balancer becomes accessible from the internet even when users intended to restrict access
**Why This Matters:**
When users provide custom load balancers, they typically want to manage security group rules themselves. The automatic creation of `0.0.0.0/0` rules can unintentionally expose services to the internet, creating a security vulnerability that users may not immediately notice.
### Description of changes
This implementation adds a smart default mechanism for the `openListener` property in `ApplicationLoadBalancedServiceBase` that detects when users provide custom load balancers (which typically have custom security groups) and automatically defaults `openListener` to `false` for improved security.
**Key changes made:**
- **Smart Default Logic**: Added detection for custom load balancers in `ApplicationLoadBalancedServiceBase.ts`
- **Feature Flag Protection**: Implemented `@aws-cdk/aws-ecs-patterns:smartDefaultOpenListener` feature flag to ensure backward compatibility
- **Secure Default Behavior**: When feature flag is enabled and custom load balancer is detected, `openListener` defaults to `false` instead of `true`
- **Override Capability**: Users can still explicitly set `openListener: true` to override the smart default if needed
- **Comprehensive Testing**: Added 5 new test cases covering all scenarios including backward compatibility
**Technical implementation details:**
- The smart default logic checks if the feature flag is enabled and if a custom load balancer is provided
- When both conditions are met, `openListener` defaults to `false`, preventing automatic creation of `AWS::EC2::SecurityGroupIngress` rules with `CidrIp: '0.0.0.0/0'`
- When feature flag is disabled or no custom load balancer is provided, behavior remains unchanged (defaults to `true`)
- The implementation uses existing CDK patterns and requires no new dependencies
**CloudFormation Impact:**
- **Before (or with `openListener: true`)**: CDK generates `AWS::EC2::SecurityGroupIngress` resources allowing internet access (`CidrIp: '0.0.0.0/0'`)
- **After (with smart default `openListener: false`)**: No automatic ingress rules are created, users maintain full control over security group configuration
**Design decisions made:**
- **Feature Flag Approach**: Chose feature flag implementation to ensure zero breaking changes for existing users
- **Custom Load Balancer Detection**: Used `props.loadBalancer !== undefined` as the detection mechanism since custom load balancers typically indicate custom security group management
- **Explicit Override Support**: Maintained ability for users to explicitly set `openListener: true` to override smart defaults when needed
- **Conservative Approach**: Only applies smart default when feature flag is explicitly enabled, ensuring opt-in behavior
**Alternatives considered and rejected:**
- **Always-on behavior**: Rejected due to potential breaking changes for existing users
- **Security group inspection**: Rejected due to complexity and potential for false positives
- **Warning-only approach**: Rejected as it doesn't actually fix the security vulnerability
**Problem Description:**
```mermaid
flowchart TD
A[User Creates ECS Service] --> B[User Provides Custom Load Balancer<br/>with Custom Security Groups]
B --> C[User Configures Restrictive Rules<br/>e.g., only VPC access]
C --> D[CDK Always Defaults openListener = true]
D --> E[CDK Creates 0.0.0.0/0 Ingress Rule<br/>1 parent 86638f6 commit ccb1955Copy full SHA for ccb1955
File tree
Expand file treeCollapse file tree
17 files changed
+35701
-3
lines changedOpen diff view settings
Filter options
- packages
- @aws-cdk-testing/framework-integ/test/aws-ecs-patterns/test/fargate
- integ.alb-fargate-service-smart-defaults.js.snapshot
- asset.7131a84a4415494610e808e07b00bedc7d2578a973f5069309ad6a8a74cf00db.bundle
- aws-cdk-lib
- aws-ecs-patterns
- lib/base
- test/fargate
- cx-api
- lib
- test
Expand file treeCollapse file tree
17 files changed
+35701
-3
lines changedOpen diff view settings
Collapse file
packages/@aws-cdk-testing/framework-integ/test/aws-ecs-patterns/test/fargate/integ.alb-fargate-service-smart-defaults.js.snapshot/albFargateServiceSmartDefaultsTestDefaultTestDeployAssert0E39EDDC.assets.json
Copy file name to clipboardExpand all lines: packages/@aws-cdk-testing/framework-integ/test/aws-ecs-patterns/test/fargate/integ.alb-fargate-service-smart-defaults.js.snapshot/albFargateServiceSmartDefaultsTestDefaultTestDeployAssert0E39EDDC.assets.json+33Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
0 commit comments