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

feat(static-website): add support for Origin Access Control (OAC) #660

Closed
wants to merge 1 commit into from

Conversation

agdimech
Copy link
Contributor

@agdimech agdimech commented Nov 23, 2023

This PR exposes a new OriginAccessControl construct which is responsible for updating the Distribution and Bucket to configure OAC. As part of this is does the following:

  1. Removes any and all references/resources/policies for OAI.
  2. Creates an OAC resource
  3. Uses a custom resource to update the bucket and optional kms key resource policies which solves an issue related to cyclic dependencies (distro needs bucket but bucket policy needs distro).
  4. Works for SSE and KMS customer keys.

The StaticWebsite construct now also accepts an optional originAccessType which by default uses OAI :( but can be configured to use OAC (this is needed to preserve backward compat).

Note: This implementation is a stop-gap until aws/aws-cdk#21771 is resolved properly. Eventually, the OriginAccessControl component in here will be deprecated in favor of whatever CDK L2 construct(s) come along.

Fixes #49

};

switch (event.RequestType) {
case "Create":
Copy link
Contributor Author

@agdimech agdimech Nov 23, 2023

Choose a reason for hiding this comment

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

Think it's worth adding Update to ensure these policies are present in the event a user manually removes them?

Delete shouldn't be needed as that will be handled by the delete handler for BucketPolicy

Copy link
Member

Choose a reason for hiding this comment

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

Yeah probably useful! If doing update you might need to add IDs to the policy statements you add and replace if they exist to avoid continually growing the bucket/key policies :)

Copy link
Member

@cogwirrel cogwirrel left a comment

Choose a reason for hiding this comment

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

Looks great! Nice one :)

const stack = Stack.of(this);
this.oac = new CfnOriginAccessControl(this, `${id}-OAC`, {
originAccessControlConfig: {
name: `${stack.stackName}_OAC`,
Copy link
Member

Choose a reason for hiding this comment

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

If we need to hardcode a name it's best to include something like this.node.addr.slice(-8) for some additional uniqueness, eg if we need to add 2 websites in the same stack :)

arnFormat: ArnFormat.SLASH_RESOURCE_NAME,
});
const onEventHandler = new NodejsFunction(this, "UpdatePolicy", {
entry: path.join(__dirname, "update_policy_handler/index.ts"),
Copy link
Member

Choose a reason for hiding this comment

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

Will this still be .ts when used as a lib?

};

switch (event.RequestType) {
case "Create":
Copy link
Member

Choose a reason for hiding this comment

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

Yeah probably useful! If doing update you might need to add IDs to the policy statements you add and replace if they exist to avoid continually growing the bucket/key policies :)

Copy link

This pull request is now marked as stale because it hasn't seen activity for a while. Add a comment or it will be closed soon. If you wish to exclude this issue from being marked as stale, add the "backlog" label.

@github-actions github-actions bot added the stale label Dec 11, 2023
Copy link

Closing this pull request as it hasn't seen activity for a while. Please add a comment @mentioning a maintainer to reopen. If you wish to exclude this issue from being marked as stale, add the "backlog" label.

@github-actions github-actions bot closed this Dec 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE] Support zero-config KMS encryption for static-website.
2 participants