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

fix(aws-cloudfront): Fix loggingConfig being ignored #809

Merged
merged 11 commits into from
Oct 2, 2018
34 changes: 27 additions & 7 deletions packages/@aws-cdk/aws-cloudfront/lib/web_distribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,28 @@ export enum SecurityPolicyProtocol {
}

/**
* CloudFront supports logging of incoming requests and can log details to a given S3 Bucket.
*
* If you wish to configure logging you can configure details about it.
*
* @default bucket: if you do not pass a bucket for logging - we'll create one
* @default includeCookies: false by default
* @default prefix: no prefix is set by default.
* Logging configuration for incoming requests
*/
export interface LoggingConfiguration {
/**
* Bucket to log requests to
*
* @default A logging bucket is automatically created
*/
readonly bucket?: s3.BucketRef,

/**
* Whether to include the cookies in the logs
*
* @default false
*/
readonly includeCookies?: boolean,

/**
* Where in the bucket to store logs
*
* @default No prefix
*/
readonly prefix?: string
}

Expand Down Expand Up @@ -622,6 +633,15 @@ export class CloudFrontWebDistribution extends cdk.Construct {
};
}

if (props.loggingConfig) {
this.loggingBucket = props.loggingConfig.bucket || new s3.Bucket(this, `LoggingBucket`);
distributionConfig.logging = {
bucket: this.loggingBucket.domainName,
includeCookies: props.loggingConfig.includeCookies || false,
prefix: props.loggingConfig.prefix
};
}

const distribution = new cloudformation.DistributionResource(this, 'CFDistribution', {distributionConfig});
this.domainName = distribution.distributionDomainName;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
{
"Resources": {
"Bucket83908E77": {
"Type": "AWS::S3::Bucket"
},
"AnAmazingWebsiteProbablyCFDistribution47E3983B": {
"Type": "AWS::CloudFront::Distribution",
"Properties": {
"DistributionConfig": {
"CacheBehaviors": [],
"DefaultCacheBehavior": {
"AllowedMethods": [
"GET",
"HEAD"
],
"CachedMethods": [
"GET",
"HEAD"
],
"ForwardedValues": {
"Cookies": {
"Forward": "none"
},
"QueryString": false
},
"TargetOriginId": "origin1",
"ViewerProtocolPolicy": "redirect-to-https"
},
"DefaultRootObject": "index.html",
"Enabled": true,
"HttpVersion": "http2",
"IPV6Enabled": true,
"Logging": {
"Bucket": {
"Fn::GetAtt": [
"Bucket83908E77",
"DomainName"
]
},
"IncludeCookies": true,
"Prefix": "test-prefix"
},
"Origins": [
{
"CustomOriginConfig": {
"HTTPPort": 80,
"HTTPSPort": 443,
"OriginKeepaliveTimeout": 5,
"OriginProtocolPolicy": "https-only",
"OriginReadTimeout": 30,
"OriginSSLProtocols": [
"TLSv1.2"
]
},
"DomainName": "brelandm.a2z.com",
"Id": "origin1",
"OriginCustomHeaders": [
{
"HeaderName": "X-Custom-Header",
"HeaderValue": "somevalue"
}
]
}
],
"PriceClass": "PriceClass_100",
"ViewerCertificate": {
"CloudFrontDefaultCertificate": true
}
}
}
},
"AnAmazingWebsiteProbably2LoggingBucket222F7CE9": {
"Type": "AWS::S3::Bucket"
},
"AnAmazingWebsiteProbably2CFDistribution7C1CCD12": {
"Type": "AWS::CloudFront::Distribution",
"Properties": {
"DistributionConfig": {
"CacheBehaviors": [],
"DefaultCacheBehavior": {
"AllowedMethods": [
"GET",
"HEAD"
],
"CachedMethods": [
"GET",
"HEAD"
],
"ForwardedValues": {
"Cookies": {
"Forward": "none"
},
"QueryString": false
},
"TargetOriginId": "origin1",
"ViewerProtocolPolicy": "redirect-to-https"
},
"DefaultRootObject": "index.html",
"Enabled": true,
"HttpVersion": "http2",
"IPV6Enabled": true,
"Logging": {
"Bucket": {
"Fn::GetAtt": [
"AnAmazingWebsiteProbably2LoggingBucket222F7CE9",
"DomainName"
]
},
"IncludeCookies": false
},
"Origins": [
{
"CustomOriginConfig": {
"HTTPPort": 80,
"HTTPSPort": 443,
"OriginKeepaliveTimeout": 5,
"OriginProtocolPolicy": "https-only",
"OriginReadTimeout": 30,
"OriginSSLProtocols": [
"TLSv1.2"
]
},
"DomainName": "brelandm.a2z.com",
"Id": "origin1",
"OriginCustomHeaders": [
{
"HeaderName": "X-Custom-Header",
"HeaderValue": "somevalue"
}
]
}
],
"PriceClass": "PriceClass_100",
"ViewerCertificate": {
"CloudFrontDefaultCertificate": true
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import s3 = require('@aws-cdk/aws-s3');
import cdk = require('@aws-cdk/cdk');
import cloudfront = require('../lib');

const app = new cdk.App(process.argv);

const stack = new cdk.Stack(app, 'aws-cdk-cloudfront-custom');

const loggingBucket = new s3.Bucket(stack, 'Bucket');

new cloudfront.CloudFrontWebDistribution(stack, 'AnAmazingWebsiteProbably', {
originConfigs: [
{
originHeaders: {
"X-Custom-Header": "somevalue",
},
customOriginSource: {
domainName: "brelandm.a2z.com",
},
behaviors: [
{
isDefaultBehavior: true,
}
]
}
],
loggingConfig: {
bucket: loggingBucket,
includeCookies: true,
prefix: 'test-prefix'
}
});

new cloudfront.CloudFrontWebDistribution(stack, 'AnAmazingWebsiteProbably2', {
originConfigs: [
{
originHeaders: {
"X-Custom-Header": "somevalue",
},
customOriginSource: {
domainName: "brelandm.a2z.com",
},
behaviors: [
{
isDefaultBehavior: true,
}
]
}
],
loggingConfig: {}
});

process.stdout.write(app.run());
18 changes: 18 additions & 0 deletions packages/@aws-cdk/aws-s3/lib/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ export interface BucketRefProps {
* policy, won't work.
*/
bucketName?: string;

/**
* The domain name of the bucket.
*
* @default Inferred from bucket name
*/
bucketDomainName?: string;
}

/**
Expand Down Expand Up @@ -72,6 +79,11 @@ export abstract class BucketRef extends cdk.Construct {
*/
public abstract readonly bucketName: string;

/**
* The domain of the bucket.
*/
public abstract readonly domainName: string;

/**
* Optional KMS encryption key associated with this bucket.
*/
Expand Down Expand Up @@ -701,6 +713,7 @@ export interface NotificationKeyFilter {
class ImportedBucketRef extends BucketRef {
public readonly bucketArn: string;
public readonly bucketName: string;
public readonly domainName: string;
public readonly encryptionKey?: kms.EncryptionKey;

protected policy?: BucketPolicy;
Expand All @@ -716,7 +729,12 @@ class ImportedBucketRef extends BucketRef {

this.bucketArn = parseBucketArn(props);
this.bucketName = bucketName;
this.domainName = props.bucketDomainName || this.generateDomainName();
this.autoCreatePolicy = false;
this.policy = undefined;
}

private generateDomainName() {
return `${this.bucketName}.s3.amazonaws.com`;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"Resources": {
"MyBucketF68F3FF0": {
"Type": "AWS::S3::Bucket"
}
},
"Outputs": {
"RealBucketDomain": {
"Value": {
"Fn::GetAtt":["MyBucketF68F3FF0","DomainName"]
},
"Export": {
"Name": "aws-cdk-s3-urls:RealBucketDomain"
}
},
"ImportedBucketDomain": {
"Value": "my-bucket-test.s3.amazonaws.com",
"Export": {
"Name": "aws-cdk-s3-urls:ImportedBucketDomain"
}
}
}
}

22 changes: 22 additions & 0 deletions packages/@aws-cdk/aws-s3/test/integ.bucket.domain-name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import cdk = require('@aws-cdk/cdk');
import s3 = require('../lib');

class TestStack extends cdk.Stack {
constructor(parent: cdk.App, id: string) {
super(parent, id);

/// !show
const bucket = new s3.Bucket(this, 'MyBucket');
const bucket2 = s3.Bucket.import(this, "MyBucket2", {
bucketArn: "arn:aws:s3:::my-bucket-test"
});

new cdk.Output(this, 'RealBucketDomain', { value: bucket.domainName });
new cdk.Output(this, 'ImportedBucketDomain', { value: bucket2.domainName });
/// !hide
}
}

const app = new cdk.App(process.argv);
new TestStack(app, 'aws-cdk-s3-urls');
process.stdout.write(app.run());