Skip to content

Commit

Permalink
do not create cloudfront log bucket if disabled
Browse files Browse the repository at this point in the history
also refactor cloudfront-distribution-defaults

Signed-off-by: Naseem <naseem@toric.com>
  • Loading branch information
naseemkullah committed Aug 16, 2021
1 parent fa6b866 commit 640eda3
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 169 deletions.
2 changes: 1 addition & 1 deletion .viperlightignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ source/patterns/@aws-solutions-constructs/aws-apigateway-iot/test/integ.defaultP
source/patterns/@aws-solutions-constructs/aws-apigateway-iot/test/integ.overrideParams.expected.json:269
source/patterns/@aws-solutions-constructs/aws-apigateway-iot/test/test.apigateway-iot.test.ts:29
source/patterns/@aws-solutions-constructs/aws-apigateway-iot/test/integ.override_auth_api_keys.expected.json:267
source/patterns/@aws-solutions-constructs/aws-cloudfront-s3/test/test.cloudfront-s3.test.ts:123
source/patterns/@aws-solutions-constructs/aws-cloudfront-s3/test/test.cloudfront-s3.test.ts:124
source/patterns/@aws-solutions-constructs/core/test/cloudfront-distribution-s3-helper.test.ts:171
source/patterns/@aws-solutions-constructs/aws-s3-sqs/test/test.s3-sqs.test.ts:251
source/use_cases/aws-custom-glue-etl/stream-producer/generate_data.py:86
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -868,4 +868,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export interface CloudFrontToMediaStoreProps {
export class CloudFrontToMediaStore extends Construct {
public readonly cloudFrontWebDistribution: cloudfront.Distribution;
public readonly mediaStoreContainer: mediastore.CfnContainer;
public readonly cloudFrontLoggingBucket: s3.Bucket;
public readonly cloudFrontLoggingBucket?: s3.Bucket;
public readonly cloudFrontOriginRequestPolicy: cloudfront.OriginRequestPolicy;
public readonly cloudFrontOriginAccessIdentity?: cloudfront.OriginAccessIdentity;
public readonly cloudFrontFunction?: cloudfront.Function;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -419,4 +419,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -352,4 +352,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -387,4 +387,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@
*/

import { ResourcePart, SynthUtils } from '@aws-cdk/assert';
import { CloudFrontToS3, CloudFrontToS3Props } from "../lib";
import * as cdk from "@aws-cdk/core";
import * as s3 from '@aws-cdk/aws-s3';
import '@aws-cdk/assert/jest';
import * as acm from '@aws-cdk/aws-certificatemanager';
import * as s3 from '@aws-cdk/aws-s3';
import * as cdk from "@aws-cdk/core";
import { RemovalPolicy } from '@aws-cdk/core';
import { CloudFrontToS3, CloudFrontToS3Props } from "../lib";

function deploy(stack: cdk.Stack) {
function deploy(stack: cdk.Stack, props?: CloudFrontToS3Props) {
return new CloudFrontToS3(stack, 'test-cloudfront-s3', {
bucketProps: {
removalPolicy: cdk.RemovalPolicy.DESTROY,
}
},
...props
});
}

Expand Down Expand Up @@ -284,4 +285,13 @@ test("Test existingBucketInterface", () => {
]
}
});
});
});


test('test cloudfront disable cloudfront logging', () => {
const stack = new cdk.Stack();

const construct = deploy(stack, {cloudFrontDistributionProps: {enableLogging: false}} );

expect(construct.cloudFrontLoggingBucket === undefined);
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,88 +11,53 @@
* and limitations under the License.
*/

import * as api from '@aws-cdk/aws-apigateway';
import * as cloudfront from '@aws-cdk/aws-cloudfront';
import { FunctionEventType } from '@aws-cdk/aws-cloudfront';
import * as origins from '@aws-cdk/aws-cloudfront-origins';
import * as s3 from '@aws-cdk/aws-s3';
import * as api from '@aws-cdk/aws-apigateway';
import * as mediastore from '@aws-cdk/aws-mediastore';
import * as s3 from '@aws-cdk/aws-s3';
import * as cdk from '@aws-cdk/core';
import { FunctionEventType } from '@aws-cdk/aws-cloudfront';

export function DefaultCloudFrontWebDistributionForApiGatewayProps(apiEndPoint: api.RestApi,
loggingBucket: s3.Bucket,
loggingBucket: s3.Bucket | undefined,
setHttpSecurityHeaders: boolean,
cfFunction?: cloudfront.IFunction): cloudfront.DistributionProps {

const apiEndPointUrlWithoutProtocol = cdk.Fn.select(1, cdk.Fn.split("://", apiEndPoint.url));
const apiEndPointDomainName = cdk.Fn.select(0, cdk.Fn.split("/", apiEndPointUrlWithoutProtocol));

if (setHttpSecurityHeaders) {
return {
defaultBehavior: {
origin: new origins.HttpOrigin(apiEndPointDomainName, {
originPath: `/${apiEndPoint.deploymentStage.stageName}`
}),
functionAssociations: [
{
eventType: FunctionEventType.VIEWER_RESPONSE,
function: cfFunction
}
],
viewerProtocolPolicy: cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS
},
enableLogging: true,
logBucket: loggingBucket,
} as cloudfront.DistributionProps;
} else {
return {
defaultBehavior: {
origin: new origins.HttpOrigin(apiEndPointDomainName, {
originPath: `/${apiEndPoint.deploymentStage.stageName}`
}),
viewerProtocolPolicy: cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS
},
enableLogging: true,
logBucket: loggingBucket,
} as cloudfront.DistributionProps;
}
return {
defaultBehavior: {
origin: new origins.HttpOrigin(apiEndPointDomainName, {
originPath: `/${apiEndPoint.deploymentStage.stageName}`
}),
...getFunctionAssociationsProp(setHttpSecurityHeaders, cfFunction),
viewerProtocolPolicy: cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS
},
enableLogging: true,
logBucket: loggingBucket,
};
}

export function DefaultCloudFrontWebDistributionForS3Props(sourceBucket: s3.IBucket, loggingBucket: s3.Bucket,
export function DefaultCloudFrontWebDistributionForS3Props(sourceBucket: s3.IBucket, loggingBucket: s3.Bucket | undefined,
setHttpSecurityHeaders: boolean,
cfFunction?: cloudfront.IFunction): cloudfront.DistributionProps {

if (setHttpSecurityHeaders) {
return {
defaultBehavior: {
origin: new origins.S3Origin(sourceBucket),
functionAssociations: [
{
eventType: FunctionEventType.VIEWER_RESPONSE,
function: cfFunction
}
],
viewerProtocolPolicy: cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS
},
enableLogging: true,
logBucket: loggingBucket,
defaultRootObject: 'index.html'
} as cloudfront.DistributionProps;
} else {
return {
defaultBehavior: {
origin: new origins.S3Origin(sourceBucket),
viewerProtocolPolicy: cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS
},
enableLogging: true,
logBucket: loggingBucket,
defaultRootObject: 'index.html'
} as cloudfront.DistributionProps;
}
return {
defaultBehavior: {
origin: new origins.S3Origin(sourceBucket),
viewerProtocolPolicy: cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
...getFunctionAssociationsProp(setHttpSecurityHeaders, cfFunction)
},
enableLogging: true,
logBucket: loggingBucket,
defaultRootObject: 'index.html'
};
}

export function DefaultCloudFrontDisributionForMediaStoreProps(mediastoreContainer: mediastore.CfnContainer,
loggingBucket: s3.Bucket,
loggingBucket: s3.Bucket | undefined,
originRequestPolicy: cloudfront.OriginRequestPolicy,
setHttpSecurityHeaders: boolean,
customHeaders?: Record<string, string>,
Expand All @@ -105,35 +70,27 @@ export function DefaultCloudFrontDisributionForMediaStoreProps(mediastoreContain
new origins.HttpOrigin(mediaStoreContainerDomainName, { customHeaders }) :
new origins.HttpOrigin(mediaStoreContainerDomainName);

if (setHttpSecurityHeaders) {
return {
defaultBehavior: {
origin: httpOrigin,
functionAssociations: [
{
eventType: FunctionEventType.VIEWER_RESPONSE,
function: cfFunction
}
],
viewerProtocolPolicy: cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
allowedMethods: cloudfront.AllowedMethods.ALLOW_GET_HEAD_OPTIONS,
cachedMethods: cloudfront.CachedMethods.CACHE_GET_HEAD_OPTIONS,
originRequestPolicy
},
enableLogging: true,
logBucket: loggingBucket
} as cloudfront.DistributionProps;
} else {
return {
defaultBehavior: {
origin: httpOrigin,
viewerProtocolPolicy: cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
allowedMethods: cloudfront.AllowedMethods.ALLOW_GET_HEAD_OPTIONS,
cachedMethods: cloudfront.CachedMethods.CACHE_GET_HEAD_OPTIONS,
originRequestPolicy
},
enableLogging: true,
logBucket: loggingBucket
} as cloudfront.DistributionProps;
}
}
return {
defaultBehavior: {
origin: httpOrigin,
viewerProtocolPolicy: cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
allowedMethods: cloudfront.AllowedMethods.ALLOW_GET_HEAD_OPTIONS,
cachedMethods: cloudfront.CachedMethods.CACHE_GET_HEAD_OPTIONS,
originRequestPolicy,
...getFunctionAssociationsProp(setHttpSecurityHeaders, cfFunction)
},
enableLogging: true,
logBucket: loggingBucket
};
}

function getFunctionAssociationsProp(setHttpSecurityHeaders: boolean, cfFunction: cloudfront.IFunction | undefined) {
return (setHttpSecurityHeaders && cfFunction) ? {
functionAssociations: [
{
eventType: FunctionEventType.VIEWER_RESPONSE,
function: cfFunction
}
]
} : {};
}
Loading

0 comments on commit 640eda3

Please sign in to comment.