-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Conversation
…tible with a disabled originShield
"pull in upstream changes"
There was a problem hiding this 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.
✅ 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 |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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; |
There was a problem hiding this comment.
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:
this.originShieldEnabled = props.originShieldEnabled; | |
this.originShieldEnabled = props.originShieldEnabled ?? true; |
(L136, L175 and L213 will need a change)
private renderOriginShield(originShieldRegion?: string, originShieldEnabled?: boolean ): CfnDistribution.OriginShieldProperty | undefined { | ||
if (originShieldEnabled === false) return { enabled: false }; | ||
return originShieldRegion ? { enabled: true, originShieldRegion } : undefined; |
There was a problem hiding this comment.
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:
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; |
Pull request has been modified.
@@ -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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
originShield: this.renderOriginShield(this.originShieldEnabled ?? true, this.originShieldRegion), | |
originShield: this.renderOriginShield(this.originShieldEnabled, this.originShieldRegion), |
There was a problem hiding this 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.
Pull request has been modified.
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀
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). |
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:
New Features
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