Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lock down buckets created by ADF - block public access #350

Merged
merged 2 commits into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions samples/sample-mono-repo/apps/alpha/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,15 @@ Metadata:
Resources:
Bucket:
Type: AWS::S3::Bucket
Properties:
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
VersioningConfiguration:
Status: Enabled
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
12 changes: 12 additions & 0 deletions samples/sample-mono-repo/apps/beta/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,15 @@ Metadata:
Resources:
Bucket:
Type: AWS::S3::Bucket
Properties:
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
VersioningConfiguration:
Status: Enabled
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
15 changes: 15 additions & 0 deletions src/lambda_codebase/cross_region_bucket/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def create_(event: Mapping[str, Any], _context: Any) -> CloudFormationResponse:
bucket_name_prefix = event["ResourceProperties"]["BucketNamePrefix"]
bucket_name, created = ensure_bucket(region, bucket_name_prefix)
ensure_bucket_encryption(bucket_name, region)
ensure_bucket_has_no_public_access(bucket_name, region)
if policy:
ensure_bucket_policy(bucket_name, region, policy)
return PhysicalResource(region, bucket_name, created).as_cfn_response()
Expand All @@ -87,6 +88,7 @@ def update_(event: Mapping[str, Any], _context: Any) -> CloudFormationResponse:
bucket_name_prefix = event["ResourceProperties"]["BucketNamePrefix"]
bucket_name, created = ensure_bucket(region, bucket_name_prefix)
ensure_bucket_encryption(bucket_name, region)
ensure_bucket_has_no_public_access(bucket_name, region)
if policy:
ensure_bucket_policy(bucket_name, region, policy)
return PhysicalResource(
Expand Down Expand Up @@ -169,6 +171,19 @@ def ensure_bucket_encryption(bucket_name: str, region: str) -> None:
)


def ensure_bucket_has_no_public_access(bucket_name: str, region: str) -> None:
s3_client = get_s3_client(region)
s3_client.put_public_access_block(
Bucket=bucket_name,
PublicAccessBlockConfiguration={
"BlockPublicAcls": True,
"IgnorePublicAcls": True,
"BlockPublicPolicy": True,
"RestrictPublicBuckets": True,
},
)


def ensure_bucket_policy(bucket_name: str, region: str, policy: MutableMapping) -> None:
s3_client = get_s3_client(region)
for action in policy["Statement"]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,13 @@ Resources:
Status: Enabled
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
CodeCommitRole:
Type: AWS::IAM::Role
Properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ Resources:
SSEAlgorithm: AES256
VersioningConfiguration:
Status: Enabled
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
DeploymentFrameworkRegionalPipelineBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Expand Down Expand Up @@ -94,4 +99,4 @@ Outputs:
Description: The KMSKey used for cross region codepipeline deployments
Value: !GetAtt DeploymentFrameworkRegionalKMSKey.Arn
Export:
Name: !Sub "KMSArn-${AWS::Region}"
Name: !Sub "KMSArn-${AWS::Region}"
24 changes: 22 additions & 2 deletions src/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,35 @@ Resources:
BootstrapArtifactStorageBucket:
DeletionPolicy: Retain
Type: AWS::S3::Bucket
Properties:
AccessControl: BucketOwnerFullControl
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
VersioningConfiguration:
Status: Enabled
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
BootstrapTemplatesBucket:
Type: "AWS::S3::Bucket"
DeletionPolicy: Retain
Properties:
AccessControl: BucketOwnerFullControl
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
VersioningConfiguration:
Status: Enabled
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
LambdaLayerVersion:
Type: "AWS::Serverless::LayerVersion"
Properties:
Expand Down Expand Up @@ -757,6 +775,7 @@ Resources:
ServiceToken: !GetAtt CrossRegionBucketHandler.Arn
Region: !Ref DeploymentAccountMainRegion
BucketNamePrefix: !Sub "adf-shared-modules-${DeploymentAccountMainRegion}"
Version: 3.1.2
PolicyDocument:
Statement:
- Action:
Expand Down Expand Up @@ -806,6 +825,7 @@ Resources:
- s3:DeleteBucket
- s3:PutEncryptionConfiguration
- s3:PutBucketPolicy
- s3:PutBucketPublicAccessBlock
Resource: "arn:aws:s3:::adf-shared-modules-*"
- Effect: Allow
Action: ssm:GetParameter
Expand Down