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

[Bug Fix] state/s3 - Ownership controls & public access block config #700

Merged
merged 4 commits into from
Aug 22, 2023
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
7 changes: 4 additions & 3 deletions state/s3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,9 @@ Conditions:
HasPartitionUsGov: !Equals [!Ref 'AWS::Partition', 'aws-us-gov']
HasLambdaFunctionArn: !Not [!Equals [!Ref LambdaFunctionArn, '']]
HasLambdaFunctionFilterPrefix: !Not [!Equals [!Ref LambdaFunctionFilterPrefix, '']]
HasPublicAccessBlock: !Not [!Or [!Condition HasPublicReadAccess, !Condition HasPublicWriteAccess]]
HasBlockPublicAccess: !Not [!Or [!Condition HasPublicReadAccess, !Condition HasPublicWriteAccess]]
HasPermissionsBoundary: !Not [!Equals [!Ref PermissionsBoundary, '']]
HasBucketOwnerPreferred: !Or [!Condition HasCloudFrontAccessLogWrite, !Condition HasS3AccessLogWrite, !Not [!Condition HasBlockPublicAccess]]
Resources:
Bucket: # cannot be deleted with data
Type: 'AWS::S3::Bucket'
Expand All @@ -182,8 +183,8 @@ Resources:
- !If [HasLambdaFunctionArn, {Event: !Ref LambdaFunctionEvent, Function: !Ref LambdaFunctionArn, Filter: !If [HasLambdaFunctionFilterPrefix, {S3Key: {Rules: [{Name: prefix, Value: !Ref LambdaFunctionFilterPrefix}]}}, !Ref 'AWS::NoValue']}, !Ref 'AWS::NoValue']
QueueConfigurations:
- !If [HasS3VirusScan, {Event: 's3:ObjectCreated:*', Queue: {'Fn::ImportValue': !Sub '${ParentS3VirusScanStack}-ScanQueueArn'}}, !Ref 'AWS::NoValue']
OwnershipControls: !If [HasCloudFrontAccessLogWrite, {Rules: [{ObjectOwnership: BucketOwnerPreferred}]}, !Ref 'AWS::NoValue']
PublicAccessBlockConfiguration: !If [HasPublicAccessBlock, {BlockPublicAcls: true, BlockPublicPolicy: true, IgnorePublicAcls: true, RestrictPublicBuckets: true}, !Ref 'AWS::NoValue'] # AWS Foundational Security Best Practices v1.0.0 S3.8
OwnershipControls: !If [HasBucketOwnerPreferred, {Rules: [{ObjectOwnership: BucketOwnerPreferred}]}, {Rules: [{ObjectOwnership: BucketOwnerEnforced}]}]
PublicAccessBlockConfiguration: !If [HasBlockPublicAccess, {BlockPublicAcls: true, BlockPublicPolicy: true, IgnorePublicAcls: true, RestrictPublicBuckets: true}, {BlockPublicAcls: true, BlockPublicPolicy: false, IgnorePublicAcls: true, RestrictPublicBuckets: false}] # AWS Foundational Security Best Practices v1.0.0 S3.8
VersioningConfiguration: !If [HasVersioning, {Status: Enabled}, !If [HadVersioning, {Status: Suspended}, !Ref 'AWS::NoValue']]
BucketEncryption:
ServerSideEncryptionConfiguration:
Expand Down
89 changes: 86 additions & 3 deletions test/src/test/java/de/widdix/awscftemplates/state/TestS3.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,104 @@
package de.widdix.awscftemplates.state;

import com.amazonaws.services.cloudformation.model.Parameter;
import de.widdix.awscftemplates.ACloudFormationTest;
import de.widdix.awscftemplates.Context;
import org.junit.Test;

public class TestS3 extends ACloudFormationTest {

@Test
public void test() {
private void test(final String access) {
final Context context = new Context();
final String stackName = "s3-" + this.random8String();
try {
this.createStack(context, stackName, "state/s3.yaml");
this.createStack(context, stackName, "state/s3.yaml",
new Parameter().withParameterKey("Access").withParameterValue(access));
// TODO how can we check if this stack works?
} finally {
this.deleteStack(context, stackName);
}
}

@Test
public void testPrivate() {
this.test("Private");
}

@Test
public void testPublicRead() {
this.test("PublicRead");
}

@Test
public void testPublicWrite() {
this.test("PublicWrite");
}

@Test
public void testPublicReadAndWrite() {
this.test("PublicReadAndWrite");
}

@Test
public void testCloudFrontRead() {
this.test("CloudFrontRead");
}

@Test
public void testCloudFrontAccessLogWrite() {
this.test("CloudFrontAccessLogWrite");
}

@Test
public void testElbAccessLogWrite() {
this.test("ElbAccessLogWrite");
}

@Test
public void testS3AccessLogWrite() {
this.test("S3AccessLogWrite");
}

@Test
public void testConfigWrite() {
this.test("ConfigWrite");
}

@Test
public void testCloudTrailWrite() {
this.test("CloudTrailWrite");
}

@Test
public void testVpcEndpointRead() {
final Context context = new Context();
final String vpcStackName = "vpc-" + this.random8String();
final String vpcEndpointStackName = "vpc-endpoint-" + this.random8String();
final String stackName = "s3-" + this.random8String();
try {
this.createStack(context, vpcStackName, "vpc/vpc-2azs.yaml");
try {
this.createStack(context, vpcEndpointStackName, "vpc/vpc-endpoint-s3.yaml",
new Parameter().withParameterKey("ParentVPCStack").withParameterValue(vpcStackName));
try {
this.createStack(context, stackName, "state/s3.yaml",
new Parameter().withParameterKey("ParentVpcEndpointStack").withParameterValue(vpcEndpointStackName),
new Parameter().withParameterKey("Access").withParameterValue("VpcEndpointRead"));
// TODO how can we check if this stack works?
} finally {
this.deleteStack(context, stackName);
}
} finally {
this.deleteStack(context, vpcEndpointStackName);
}
} finally {
this.deleteStack(context, vpcStackName);
}
}

@Test
public void testFlowLogWrite() {
this.test("FlowLogWrite");
}

}