Skip to content

Commit 3e58e5a

Browse files
author
Daniel Bartholomae
authored
fix(cloudfront): fromOriginAccessIdentityName is a misnomer (#20772)
fixes #20141 ---- ### All Submissions: * [X] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent b0b7a32 commit 3e58e5a

File tree

5 files changed

+68
-14
lines changed

5 files changed

+68
-14
lines changed

packages/@aws-cdk/aws-cloudfront-origins/lib/s3-origin.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,6 @@ class S3BucketOrigin extends cloudfront.OriginBase {
8484
}
8585

8686
protected renderS3OriginConfig(): cloudfront.CfnDistribution.S3OriginConfigProperty | undefined {
87-
return { originAccessIdentity: `origin-access-identity/cloudfront/${this.originAccessIdentity.originAccessIdentityName}` };
87+
return { originAccessIdentity: `origin-access-identity/cloudfront/${this.originAccessIdentity.originAccessIdentityId}` };
8888
}
8989
}

packages/@aws-cdk/aws-cloudfront/lib/origin-access-identity.ts

+55-10
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,35 @@ export interface OriginAccessIdentityProps {
2020
*/
2121
export interface IOriginAccessIdentity extends cdk.IResource, iam.IGrantable {
2222
/**
23-
* The Origin Access Identity Name
23+
* The Origin Access Identity Id (physical id)
24+
* It is misnamed and superseded by the correctly named originAccessIdentityId
25+
*
26+
* @deprecated use originAccessIdentityId instead
2427
*/
2528
readonly originAccessIdentityName: string;
29+
30+
/**
31+
* The Origin Access Identity Id (physical id)
32+
* This was called originAccessIdentityName before
33+
*/
34+
readonly originAccessIdentityId: string;
2635
}
2736

2837
abstract class OriginAccessIdentityBase extends cdk.Resource {
2938
/**
30-
* The Origin Access Identity Name (physical id)
39+
* The Origin Access Identity Id (physical id)
40+
* It is misnamed and superseded by the correctly named originAccessIdentityId
41+
*
42+
* @deprecated use originAccessIdentityId instead
3143
*/
3244
public abstract readonly originAccessIdentityName: string;
45+
46+
/**
47+
* The Origin Access Identity Id (physical id)
48+
* This was called originAccessIdentityName before
49+
*/
50+
public abstract readonly originAccessIdentityId: string;
51+
3352
/**
3453
* Derived principal value for bucket access
3554
*/
@@ -45,7 +64,7 @@ abstract class OriginAccessIdentityBase extends cdk.Resource {
4564
region: '', // global
4665
account: 'cloudfront',
4766
resource: 'user',
48-
resourceName: `CloudFront Origin Access Identity ${this.originAccessIdentityName}`,
67+
resourceName: `CloudFront Origin Access Identity ${this.originAccessIdentityId}`,
4968
},
5069
);
5170
}
@@ -60,18 +79,32 @@ abstract class OriginAccessIdentityBase extends cdk.Resource {
6079
*/
6180
export class OriginAccessIdentity extends OriginAccessIdentityBase implements IOriginAccessIdentity {
6281
/**
63-
* Creates a OriginAccessIdentity by providing the OriginAccessIdentityName
82+
* Creates a OriginAccessIdentity by providing the OriginAccessIdentityId.
83+
* It is misnamed and superseded by the correctly named fromOriginAccessIdentityId.
84+
*
85+
* @deprecated use `fromOriginAccessIdentityId`
6486
*/
6587
public static fromOriginAccessIdentityName(
6688
scope: Construct,
6789
id: string,
6890
originAccessIdentityName: string): IOriginAccessIdentity {
91+
return OriginAccessIdentity.fromOriginAccessIdentityId(scope, id, originAccessIdentityName);
92+
}
93+
94+
/**
95+
* Creates a OriginAccessIdentity by providing the OriginAccessIdentityId.
96+
*/
97+
public static fromOriginAccessIdentityId(
98+
scope: Construct,
99+
id: string,
100+
originAccessIdentityId: string): IOriginAccessIdentity {
69101

70102
class Import extends OriginAccessIdentityBase {
71-
public readonly originAccessIdentityName = originAccessIdentityName;
103+
public readonly originAccessIdentityId = originAccessIdentityId;
104+
public readonly originAccessIdentityName = originAccessIdentityId;
72105
public readonly grantPrincipal = new iam.ArnPrincipal(this.arn());
73106
constructor(s: Construct, i: string) {
74-
super(s, i, { physicalName: originAccessIdentityName });
107+
super(s, i, { physicalName: originAccessIdentityId });
75108
}
76109
}
77110

@@ -93,11 +126,23 @@ export class OriginAccessIdentity extends OriginAccessIdentityBase implements IO
93126
public readonly grantPrincipal: iam.IPrincipal;
94127

95128
/**
96-
* The Origin Access Identity Name (physical id)
129+
* The Origin Access Identity Id (physical id)
130+
* It is misnamed and superseded by the correctly named originAccessIdentityId
131+
*
132+
* @attribute
133+
* @deprecated use originAccessIdentityId instead
134+
*/
135+
public get originAccessIdentityName() {
136+
return this.originAccessIdentityId;
137+
}
138+
139+
/**
140+
* The Origin Access Identity Id (physical id)
141+
* This was called originAccessIdentityName before
97142
*
98143
* @attribute
99144
*/
100-
public readonly originAccessIdentityName: string;
145+
public readonly originAccessIdentityId: string;
101146

102147
/**
103148
* CDK L1 resource
@@ -112,8 +157,8 @@ export class OriginAccessIdentity extends OriginAccessIdentityBase implements IO
112157
this.resource = new CfnCloudFrontOriginAccessIdentity(this, 'Resource', {
113158
cloudFrontOriginAccessIdentityConfig: { comment },
114159
});
115-
// physical id - OAI name
116-
this.originAccessIdentityName = this.getResourceNameAttribute(this.resource.ref);
160+
// physical id - OAI Id
161+
this.originAccessIdentityId = this.getResourceNameAttribute(this.resource.ref);
117162

118163
// Canonical user to grant access to in the S3 Bucket Policy
119164
this.cloudFrontOriginAccessIdentityS3CanonicalUserId = this.resource.attrS3CanonicalUserId;

packages/@aws-cdk/aws-cloudfront/lib/web-distribution.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,7 @@ export class CloudFrontWebDistribution extends cdk.Resource implements IDistribu
11071107
}));
11081108

11091109
s3OriginConfig = {
1110-
originAccessIdentity: `origin-access-identity/cloudfront/${originConfig.s3OriginSource.originAccessIdentity.originAccessIdentityName}`,
1110+
originAccessIdentity: `origin-access-identity/cloudfront/${originConfig.s3OriginSource.originAccessIdentity.originAccessIdentityId}`,
11111111
};
11121112
} else {
11131113
s3OriginConfig = {};

packages/@aws-cdk/aws-cloudfront/test/integ.cloudfront-s3.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const oai = new cloudfront.CfnCloudFrontOriginAccessIdentity(stack, 'OAI', {
1212
},
1313
});
1414

15-
const oaiImported = cloudfront.OriginAccessIdentity.fromOriginAccessIdentityName(
15+
const oaiImported = cloudfront.OriginAccessIdentity.fromOriginAccessIdentityId(
1616
stack,
1717
'OAIImported',
1818
oai.ref,

packages/@aws-cdk/aws-cloudfront/test/oai.test.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Template } from '@aws-cdk/assertions';
22
import * as cdk from '@aws-cdk/core';
3+
import { testDeprecated } from '@aws-cdk/cdk-build-tools';
34
import { OriginAccessIdentity } from '../lib';
45

56
describe('Origin Access Identity', () => {
@@ -61,11 +62,19 @@ describe('Origin Access Identity', () => {
6162
});
6263
});
6364

64-
test('Builds ARN of CloudFront user', () => {
65+
testDeprecated('Builds ARN of CloudFront user for fromOriginAccessIdentityName', () => {
6566
const stack = new cdk.Stack();
6667

6768
const oai = OriginAccessIdentity.fromOriginAccessIdentityName(stack, 'OAI', 'OAITest');
6869

6970
expect(oai.grantPrincipal.policyFragment.principalJson.AWS[0]).toMatch(/:iam::cloudfront:user\/CloudFront Origin Access Identity OAITest$/);
7071
});
72+
73+
test('Builds ARN of CloudFront user for fromOriginAccessIdentityId', () => {
74+
const stack = new cdk.Stack();
75+
76+
const oai = OriginAccessIdentity.fromOriginAccessIdentityId(stack, 'OAI', 'OAITest');
77+
78+
expect(oai.grantPrincipal.policyFragment.principalJson.AWS[0]).toMatch(/:iam::cloudfront:user\/CloudFront Origin Access Identity OAITest$/);
79+
});
7180
});

0 commit comments

Comments
 (0)