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(cloudfront): OriginShield not easily disabled once enabled on an origin #22791

Merged
merged 14 commits into from
Nov 11, 2022

Conversation

bradlead
Copy link
Contributor

@bradlead bradlead commented Nov 4, 2022

Fixes #22233. Previous PR now closed #22334.

Added new prop originShieldEnabled as suggested by @corymhall which can be set to false if the user needs to explicitly disable origin shield.

Updated unit test origin.test.ts

Added new integ test.


All Submissions:

Adding new Unconventional Dependencies:

  • This PR adds new unconventional dependencies following the process described here

New Features

  • Have you added the new feature to an integration test?
    • 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

@gitpod-io
Copy link

gitpod-io bot commented Nov 4, 2022

@github-actions github-actions bot added the beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK label Nov 4, 2022
@aws-cdk-automation aws-cdk-automation requested a review from a team November 4, 2022 20:16
@github-actions github-actions bot added bug This issue is a bug. effort/small Small work item – less than a day of effort p2 labels Nov 4, 2022
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pull request linter has failed. See the aws-cdk-automation comment below for failure reasons. If you believe this pull request should receive an exemption, please comment and provide a justification.

@aws-cdk-automation aws-cdk-automation dismissed their stale review November 7, 2022 16:40

✅ Updated pull request passes all PRLinter validations. Dissmissing previous PRLinter review.

/**
* A flag for disabling Origin Shield on the origin.
*
* @default - Origin Shield not enabled
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this default description is correct. Based on the code I think we default to something like:

  • Does originShieldRegion exists? -> YES
  • Otherwise -> NO

Not sure if true as a default is more accurate.

@@ -82,6 +82,13 @@ export interface OriginOptions {
*/
readonly originShieldRegion?: string;

/**
* A flag for disabling Origin Shield on the origin.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's be a bite more descriptive here (see also default). Explain how originShieldRegion and originShieldEnabled interact etc.

testCases: [stack],
});

app.synth();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
app.synth();
app.synth();

@@ -139,6 +148,7 @@ export abstract class OriginBase implements IOrigin {
this.customHeaders = props.customHeaders;
this.originShieldRegion = props.originShieldRegion;
this.originId = props.originId;
this.originShieldEnabled = props.originShieldEnabled;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is fine in principle. However boolean variables that might be also be undefined, can easily turn into bugs when not taken care of in if statements. Consider this code:

if (this.originShieldEnabled) {
  // do stuff when enabled
} else {
 // do stuff when disabled
}

In this case it seems to be the default is true, so the above would be broken when the value is undefined.

I'd recommend to always set an explicit default in these cases and adjust the private types accordingly:

Suggested change
this.originShieldEnabled = props.originShieldEnabled;
this.originShieldEnabled = props.originShieldEnabled ?? true;

(L136, L175 and L213 will need a change)

Comment on lines 213 to 215
private renderOriginShield(originShieldRegion?: string, originShieldEnabled?: boolean ): CfnDistribution.OriginShieldProperty | undefined {
if (originShieldEnabled === false) return { enabled: false };
return originShieldRegion ? { enabled: true, originShieldRegion } : undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally okay in principal. We avoid single line if statements. In combination with my other suggestion it could look like this:

Suggested change
private renderOriginShield(originShieldRegion?: string, originShieldEnabled?: boolean ): CfnDistribution.OriginShieldProperty | undefined {
if (originShieldEnabled === false) return { enabled: false };
return originShieldRegion ? { enabled: true, originShieldRegion } : undefined;
private renderOriginShield(originShieldEnabled: boolean, originShieldRegion?: string): CfnDistribution.OriginShieldProperty | undefined {
if (!originShieldEnabled) {
return { enabled: false };
}
return originShieldRegion ? { enabled: true, originShieldRegion } : undefined;

@mergify mergify bot dismissed mrgrain’s stale review November 11, 2022 14:48

Pull request has been modified.

@bradlead
Copy link
Contributor Author

Hey @mrgrain I've made the changes in this commit Thanks

@@ -124,7 +132,8 @@ export abstract class OriginBase implements IOrigin {
private readonly connectionTimeout?: Duration;
private readonly connectionAttempts?: number;
private readonly customHeaders?: Record<string, string>;
private readonly originShieldRegion?: string
private readonly originShieldRegion?: string;
private readonly originShieldEnabled?: (true | boolean);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private readonly originShieldEnabled?: (true | boolean);
private readonly originShieldEnabled: boolean;

@@ -162,7 +172,7 @@ export abstract class OriginBase implements IOrigin {
originCustomHeaders: this.renderCustomHeaders(),
s3OriginConfig,
customOriginConfig,
originShield: this.renderOriginShield(this.originShieldRegion),
originShield: this.renderOriginShield(this.originShieldEnabled ?? true, this.originShieldRegion),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
originShield: this.renderOriginShield(this.originShieldEnabled ?? true, this.originShieldRegion),
originShield: this.renderOriginShield(this.originShieldEnabled, this.originShieldRegion),

Copy link
Contributor

@mrgrain mrgrain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can simplify these types now. After that we should be done.

@mergify mergify bot dismissed mrgrain’s stale review November 11, 2022 15:13

Pull request has been modified.

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 5487a8c
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

Copy link
Contributor

@mrgrain mrgrain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@mergify
Copy link
Contributor

mergify bot commented Nov 11, 2022

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot merged commit 6be4cf6 into aws:main Nov 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK bug This issue is a bug. effort/small Small work item – less than a day of effort p2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

aws-cloudfront: OriginShield not easily disabled once enabled on an origin
3 participants