Skip to content

Commit f9708a6

Browse files
authored
fix(cloudfront): propagate originAccessControlId CloudFront Origin property to CloudFormation templates (#32020)
### Issue Closes #32018. ### Reason for this change The originAccessControlId property of CloudFront Origin has not been propagated to CloudFormation templates. ### Description of changes Propagate the property to render function. ### Description of how you validated changes have run the [run build over the whole repo](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md#setup) - my computer almost exploded. I have also run tests for the aws-cloudfront which run successfully. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 9f3d09b commit f9708a6

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

packages/aws-cdk-lib/aws-cloudfront/lib/origin.ts

+3
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ export abstract class OriginBase implements IOrigin {
149149
private readonly originShieldRegion?: string;
150150
private readonly originShieldEnabled: boolean;
151151
private readonly originId?: string;
152+
private readonly originAccessControlId?: string;
152153

153154
protected constructor(domainName: string, props: OriginProps = {}) {
154155
validateIntInRangeOrUndefined('connectionTimeout', 1, 10, props.connectionTimeout?.toSeconds());
@@ -163,6 +164,7 @@ export abstract class OriginBase implements IOrigin {
163164
this.originShieldRegion = props.originShieldRegion;
164165
this.originId = props.originId;
165166
this.originShieldEnabled = props.originShieldEnabled ?? true;
167+
this.originAccessControlId = props.originAccessControlId;
166168
}
167169

168170
/**
@@ -187,6 +189,7 @@ export abstract class OriginBase implements IOrigin {
187189
s3OriginConfig,
188190
customOriginConfig,
189191
originShield: this.renderOriginShield(this.originShieldEnabled, this.originShieldRegion),
192+
originAccessControlId: this.originAccessControlId,
190193
},
191194
};
192195
}

packages/aws-cdk-lib/aws-cloudfront/test/distribution.test.ts

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defaultOrigin, defaultOriginGroup } from './test-origin';
1+
import { defaultOrigin, defaultOriginGroup, defaultOriginWithOriginAccessControl } from './test-origin';
22
import { Annotations, Match, Template } from '../../assertions';
33
import * as acm from '../../aws-certificatemanager';
44
import * as cloudwatch from '../../aws-cloudwatch';
@@ -1282,6 +1282,36 @@ test('with publish additional metrics', () => {
12821282
});
12831283
});
12841284

1285+
test('with origin access control id', () => {
1286+
const origin = defaultOriginWithOriginAccessControl();
1287+
new Distribution(stack, 'MyDist', {
1288+
defaultBehavior: { origin },
1289+
publishAdditionalMetrics: true,
1290+
});
1291+
1292+
Template.fromStack(stack).hasResourceProperties('AWS::CloudFront::Distribution', {
1293+
DistributionConfig: {
1294+
DefaultCacheBehavior: {
1295+
CachePolicyId: '658327ea-f89d-4fab-a63d-7e88639e58f6',
1296+
Compress: true,
1297+
TargetOriginId: 'StackMyDistOrigin1D6D5E535',
1298+
ViewerProtocolPolicy: 'allow-all',
1299+
},
1300+
Enabled: true,
1301+
HttpVersion: 'http2',
1302+
IPV6Enabled: true,
1303+
Origins: [{
1304+
DomainName: 'www.example.com',
1305+
Id: 'StackMyDistOrigin1D6D5E535',
1306+
CustomOriginConfig: {
1307+
OriginProtocolPolicy: 'https-only',
1308+
},
1309+
OriginAccessControlId: 'test-origin-access-control-id',
1310+
}],
1311+
},
1312+
});
1313+
});
1314+
12851315
describe('Distribution metrics tests', () => {
12861316
const additionalMetrics = [
12871317
{ name: 'OriginLatency', method: 'metricOriginLatency', statistic: 'Average', additionalMetricsRequired: true, errorMetricName: 'Origin latency' },

packages/aws-cdk-lib/aws-cloudfront/test/test-origin.ts

+31-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
11
import { Construct } from 'constructs';
2-
import { CfnDistribution, IOrigin, OriginBase, OriginBindConfig, OriginBindOptions, OriginProps, OriginProtocolPolicy } from '../lib';
2+
import {
3+
CfnDistribution,
4+
IOrigin,
5+
OriginBase,
6+
OriginBindConfig,
7+
OriginBindOptions,
8+
OriginProps,
9+
OriginProtocolPolicy,
10+
} from '../lib';
311

412
/** Used for testing common Origin functionality */
513
export class TestOrigin extends OriginBase {
6-
constructor(domainName: string, props: OriginProps = {}) { super(domainName, props); }
7-
protected renderCustomOriginConfig(): CfnDistribution.CustomOriginConfigProperty | undefined {
14+
constructor(domainName: string, props: OriginProps = {}) {
15+
super(domainName, props);
16+
}
17+
protected renderCustomOriginConfig():
18+
| CfnDistribution.CustomOriginConfigProperty
19+
| undefined {
820
return { originProtocolPolicy: OriginProtocolPolicy.HTTPS_ONLY };
921
}
1022
}
1123

1224
export class TestOriginGroup implements IOrigin {
13-
constructor(private readonly primaryDomainName: string, private readonly secondaryDomainName: string) { }
25+
constructor(
26+
private readonly primaryDomainName: string,
27+
private readonly secondaryDomainName: string,
28+
) {}
1429
/* eslint-disable @cdklabs/no-core-construct */
1530
public bind(scope: Construct, options: OriginBindOptions): OriginBindConfig {
1631
const primaryOrigin = new TestOrigin(this.primaryDomainName);
@@ -35,3 +50,15 @@ export function defaultOrigin(domainName?: string, originId?: string): IOrigin {
3550
export function defaultOriginGroup(): IOrigin {
3651
return new TestOriginGroup('www.example.com', 'foo.example.com');
3752
}
53+
54+
export function defaultOriginWithOriginAccessControl(
55+
domainName?: string,
56+
originId?: string,
57+
originAccessControlId?: string,
58+
): IOrigin {
59+
return new TestOrigin(domainName ?? 'www.example.com', {
60+
originId,
61+
originAccessControlId:
62+
originAccessControlId ?? 'test-origin-access-control-id',
63+
});
64+
}

0 commit comments

Comments
 (0)