Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Unit test S3BucketOrigin.withOriginAccessControl with custom props + removed originAccessControlId from OriginBase #31186

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,170 @@ import { Annotations, Template } from '../../assertions';
import * as cloudfront from '../../aws-cloudfront/index';
import * as origins from '../../aws-cloudfront-origins';
import * as s3 from '../../aws-s3/index';
import { Stack } from '../../core';
import { Duration, Stack } from '../../core';

describe('S3BucketOrigin', () => {
describe('withOriginAccessControl', () => {
describe('when passing custom props', () => {
let stack: Stack;
let bucket: s3.Bucket;
let origin: cloudfront.IOrigin;
let template: Template;

beforeAll(() => {
stack = new Stack();
bucket = new s3.Bucket(stack, 'MyBucket');
origin = origins.S3BucketOrigin.withOriginAccessControl(bucket, {
originAccessControl: new cloudfront.S3OriginAccessControl(stack, 'MyOac'),
originAccessLevels: [cloudfront.AccessLevel.WRITE, cloudfront.AccessLevel.READ],
originPath: '/pathA',
connectionTimeout: Duration.seconds(10),
connectionAttempts: 2,
customHeaders: { headerA: 'headerAValue' },
originShieldRegion: 'ca-central-1',
originShieldEnabled: true,
originId: 'originIdA',
});

new cloudfront.Distribution(stack, 'MyDistributionA', {
defaultBehavior: { origin: origin },
});

template = Template.fromStack(stack);
});

it('should match expected template resources', () => {
expect(template.toJSON().Resources).toEqual({
MyBucketF68F3FF0: {
Type: 'AWS::S3::Bucket',
UpdateReplacePolicy: 'Retain',
DeletionPolicy: 'Retain',
},
MyBucketPolicyE7FBAC7B: {
Type: 'AWS::S3::BucketPolicy',
Properties: {
Bucket: {
Ref: 'MyBucketF68F3FF0',
},
PolicyDocument: {
Statement: [
{
Action: [
's3:PutObject',
's3:GetObject',
],
Condition: {
StringEquals: {
'AWS:SourceArn': {
'Fn::Join': [
'',
[
'arn:',
{
Ref: 'AWS::Partition',
},
':cloudfront::',
{
Ref: 'AWS::AccountId',
},
':distribution/',
{
Ref: 'MyDistributionA2150CE0F',
},
],
],
},
},
},
Effect: 'Allow',
Principal: {
Service: 'cloudfront.amazonaws.com',
},
Resource: {
'Fn::Join': [
'',
[
{
'Fn::GetAtt': [
'MyBucketF68F3FF0',
'Arn',
],
},
'/*',
],
],
},
Sid: 'GrantCloudFrontOACAccessToS3Origin',
},
],
Version: '2012-10-17',
},
},
},
MyOacAA788594: {
Type: 'AWS::CloudFront::OriginAccessControl',
Properties: {
OriginAccessControlConfig: {
Name: 'MyOac',
OriginAccessControlOriginType: 's3',
SigningBehavior: 'always',
SigningProtocol: 'sigv4',
},
},
},
MyDistributionA2150CE0F: {
Type: 'AWS::CloudFront::Distribution',
Properties: {
DistributionConfig: {
DefaultCacheBehavior: {
CachePolicyId: '658327ea-f89d-4fab-a63d-7e88639e58f6',
Compress: true,
TargetOriginId: 'originIdA',
ViewerProtocolPolicy: 'allow-all',
},
Enabled: true,
HttpVersion: 'http2',
IPV6Enabled: true,
Origins: [
{
ConnectionAttempts: 2,
ConnectionTimeout: 10,
DomainName: {
'Fn::GetAtt': [
'MyBucketF68F3FF0',
'RegionalDomainName',
],
},
Id: 'originIdA',
OriginAccessControlId: {
'Fn::GetAtt': [
'MyOacAA788594',
'Id',
],
},
OriginCustomHeaders: [
{
HeaderName: 'headerA',
HeaderValue: 'headerAValue',
},
],
OriginPath: '/pathA',
OriginShield: {
Enabled: true,
OriginShieldRegion: 'ca-central-1',
},
S3OriginConfig: {
OriginAccessIdentity: '',
},
},
],
},
},
},
});
});
});

describe('when attaching to a multiple distribution', () => {
let stack: Stack;
let bucket: s3.Bucket;
Expand Down
3 changes: 0 additions & 3 deletions packages/aws-cdk-lib/aws-cloudfront/lib/origin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ export abstract class OriginBase implements IOrigin {
private readonly originShieldRegion?: string;
private readonly originShieldEnabled: boolean;
private readonly originId?: string;
private readonly originAccessControlId?: string;

protected constructor(domainName: string, props: OriginProps = {}) {
validateIntInRangeOrUndefined('connectionTimeout', 1, 10, props.connectionTimeout?.toSeconds());
Expand All @@ -161,7 +160,6 @@ export abstract class OriginBase implements IOrigin {
this.originShieldRegion = props.originShieldRegion;
this.originId = props.originId;
this.originShieldEnabled = props.originShieldEnabled ?? true;
this.originAccessControlId = props.originAccessControlId;
}

/**
Expand All @@ -186,7 +184,6 @@ export abstract class OriginBase implements IOrigin {
s3OriginConfig,
customOriginConfig,
originShield: this.renderOriginShield(this.originShieldEnabled, this.originShieldRegion),
originAccessControlId: this.originAccessControlId,
},
};
}
Expand Down
Loading