Skip to content

Commit

Permalink
Fix @aws-cloudfront-origins.
Browse files Browse the repository at this point in the history
  • Loading branch information
skinny85 committed Jul 28, 2020
1 parent c64844f commit d4901c5
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 44 deletions.
33 changes: 8 additions & 25 deletions packages/@aws-cdk/aws-cloudfront-origins/lib/s3-origin.ts
Expand Up @@ -26,39 +26,22 @@ export interface S3OriginProps {
*
* @experimental
*/
export class S3Origin extends cloudfront.Origin {

private readonly origin: cloudfront.Origin;
export class S3Origin implements cloudfront.IOrigin {
private readonly origin: cloudfront.IOrigin;

constructor(bucket: s3.IBucket, props: S3OriginProps = {}) {
let proxyOrigin;
if (bucket.isWebsite) {
proxyOrigin = new cloudfront.HttpOrigin(bucket.bucketWebsiteDomainName, {
this.origin = bucket.isWebsite
? new cloudfront.HttpOrigin(bucket.bucketWebsiteDomainName, {
protocolPolicy: cloudfront.OriginProtocolPolicy.HTTP_ONLY, // S3 only supports HTTP for website buckets
...props,
});
} else {
proxyOrigin = new cloudfront.S3Origin({
})
: new cloudfront.S3Origin({
bucket,
...props,
});
}

super(proxyOrigin.domainName);

this.origin = proxyOrigin;
}

public get id() {
return this.origin.id;
public bind(scope: cdk.Construct, options: cloudfront.OriginBindOptions): cloudfront.OriginBindConfig {
return this.origin.bind(scope, options);
}

public bind(scope: cdk.Construct, options: cloudfront.OriginBindOptions) {
this.origin.bind(scope, options);
}

public renderOrigin() {
return this.origin.renderOrigin();
}

}
Expand Up @@ -15,9 +15,9 @@ beforeEach(() => {

test('Renders minimal example with just a domain name', () => {
const origin = new HttpOrigin('www.example.com');
origin.bind(stack, { originIndex: 0 });
const originBindConfig = origin.bind(stack, { originId: 'StackOrigin029E19582' });

expect(origin.renderOrigin()).toEqual({
expect(originBindConfig.originProperty).toEqual({
id: 'StackOrigin029E19582',
domainName: 'www.example.com',
customOriginConfig: {
Expand All @@ -32,9 +32,9 @@ test('Can customize properties of the origin', () => {
readTimeout: Duration.seconds(10),
protocolPolicy: cloudfront.OriginProtocolPolicy.MATCH_VIEWER,
});
origin.bind(stack, { originIndex: 0 });
const originBindConfig = origin.bind(stack, { originId: 'StackOrigin029E19582' });

expect(origin.renderOrigin()).toEqual({
expect(originBindConfig.originProperty).toEqual({
id: 'StackOrigin029E19582',
domainName: 'www.example.com',
originCustomHeaders: [{
Expand Down
Expand Up @@ -23,7 +23,7 @@
"Principal": {
"CanonicalUser": {
"Fn::GetAtt": [
"DistributionS3Origin115FD918D",
"DistributionOrigin1S3Origin5F5C0696",
"S3CanonicalUserId"
]
}
Expand Down Expand Up @@ -56,7 +56,7 @@
}
}
},
"DistributionS3Origin115FD918D": {
"DistributionOrigin1S3Origin5F5C0696": {
"Type": "AWS::CloudFront::CloudFrontOriginAccessIdentity",
"Properties": {
"CloudFrontOriginAccessIdentityConfig": {
Expand Down Expand Up @@ -92,7 +92,7 @@
[
"origin-access-identity/cloudfront/",
{
"Ref": "DistributionS3Origin115FD918D"
"Ref": "DistributionOrigin1S3Origin5F5C0696"
}
]
]
Expand All @@ -104,4 +104,4 @@
}
}
}
}
}
Expand Up @@ -21,9 +21,9 @@ test('Renders minimal example with just a load balancer', () => {
});

const origin = new LoadBalancerV2Origin(loadBalancer);
origin.bind(stack, { originIndex: 0 });
const originBindConfig = origin.bind(stack, { originId: 'StackOrigin029E19582' });

expect(origin.renderOrigin()).toEqual({
expect(originBindConfig.originProperty).toEqual({
id: 'StackOrigin029E19582',
domainName: loadBalancer.loadBalancerDnsName,
customOriginConfig: {
Expand All @@ -43,9 +43,9 @@ test('Can customize properties of the origin', () => {
connectionTimeout: Duration.seconds(5),
protocolPolicy: cloudfront.OriginProtocolPolicy.MATCH_VIEWER,
});
origin.bind(stack, { originIndex: 0 });
const originBindConfig = origin.bind(stack, { originId: 'StackOrigin029E19582' });

expect(origin.renderOrigin()).toEqual({
expect(originBindConfig.originProperty).toEqual({
id: 'StackOrigin029E19582',
domainName: loadBalancer.loadBalancerDnsName,
connectionAttempts: 3,
Expand Down
14 changes: 7 additions & 7 deletions packages/@aws-cdk/aws-cloudfront-origins/test/s3-origin.test.ts
Expand Up @@ -17,9 +17,9 @@ test('With non-website bucket, renders all required properties, including S3Orig
const bucket = new s3.Bucket(stack, 'Bucket');

const origin = new S3Origin(bucket);
origin.bind(stack, { originIndex: 0 });
const originBindConfig = origin.bind(stack, { originId: 'StackOrigin029E19582' });

expect(origin.renderOrigin()).toEqual({
expect(originBindConfig.originProperty).toEqual({
id: 'StackOrigin029E19582',
domainName: bucket.bucketRegionalDomainName,
s3OriginConfig: {
Expand All @@ -34,9 +34,9 @@ test('With website bucket, renders all required properties, including custom ori
});

const origin = new S3Origin(bucket);
origin.bind(stack, { originIndex: 0 });
const originBindConfig = origin.bind(stack, { originId: 'StackOrigin029E19582' });

expect(origin.renderOrigin()).toEqual({
expect(originBindConfig.originProperty).toEqual({
id: 'StackOrigin029E19582',
domainName: bucket.bucketWebsiteDomainName,
customOriginConfig: {
Expand All @@ -51,14 +51,14 @@ test('Respects props passed down to underlying origin', () => {
});

const origin = new S3Origin(bucket, { originPath: '/website' });
origin.bind(stack, { originIndex: 0 });
const originBindConfig = origin.bind(stack, { originId: 'StackOrigin029E19582' });

expect(origin.renderOrigin()).toEqual({
expect(originBindConfig.originProperty).toEqual({
id: 'StackOrigin029E19582',
domainName: bucket.bucketWebsiteDomainName,
originPath: '/website',
customOriginConfig: {
originProtocolPolicy: 'http-only',
},
});
});
});

0 comments on commit d4901c5

Please sign in to comment.