Skip to content

Commit

Permalink
[Bug Fix] state/s3 - Ownership controls & public access block config
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwittig committed Aug 17, 2023
1 parent 6e988df commit a2beb75
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
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, '']]
HasCloudFrontAccessLogWriteOrNotBlockPublicAccess: !Or [!Condition HasCloudFrontAccessLogWrite, !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 [HasCloudFrontAccessLogWriteOrNotBlockPublicAccess, {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
67 changes: 64 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,82 @@
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() {
this.test("VpcEndpointRead");
}

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

}

0 comments on commit a2beb75

Please sign in to comment.