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(cloudfront): s3 origin access control L2 construct #30663

Closed
wants to merge 14 commits into from

Conversation

gracelu0
Copy link
Contributor

Issue # (if applicable)

Closes #21771 .

Reason for this change

See RFC

Description of changes

See RFC

Description of how you validated changes

Unit tests and integration tests

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@aws-cdk-automation aws-cdk-automation requested a review from a team June 25, 2024 18:27
@github-actions github-actions bot added effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2 labels Jun 25, 2024
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Jun 25, 2024
@gracelu0 gracelu0 marked this pull request as draft June 25, 2024 18:28
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 5e5ffd5
  • Result: FAILED
  • Build Logs (available for 30 days)

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

Copy link
Contributor

@GavinZZ GavinZZ left a comment

Choose a reason for hiding this comment

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

Reviewed half of the changes. Left some questions.

});
```

If the feature flag is not enabled (i.e. set to `false`), an origin access identity will be created by default.
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
If the feature flag is not enabled (i.e. set to `false`), an origin access identity will be created by default.
If the feature flag is not enabled (`undefined` or set to `false`), an origin access identity will be created by default.

Or something along the line.

@@ -15,6 +28,44 @@ export interface S3OriginProps extends cloudfront.OriginProps {
* @default - An Origin Access Identity will be created.
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we update the @default docstring?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, will specify the feature flag behaviour for creating OAI by default


/**
* An optional Origin Access Control
* @default - An Origin Access Control will be created.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: maybe mention that OAC will be created when FF is enabeld.

private originAccessIdentity!: cloudfront.IOriginAccessIdentity;
abstract class S3BucketOrigin extends cloudfront.OriginBase {
public static withAccessIdentity(bucket: s3.IBucket, props: S3OriginProps = {}): S3BucketOrigin {
return new (class OriginAccessIdentity extends S3BucketOrigin {
Copy link
Contributor

Choose a reason for hiding this comment

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

This nested class looks weird to me. Can you instead split them out?

abstract class S3BucketOrigin extends cloudfront.OriginBase {
   ...
}

class OriginAccessIdentity extends S3BucketOrigin {
   ...
}

class OriginAccessControl extends S3BucketOrigin {
  ...
}

And on line 90~100, you can do

    else if (props.originAccessControl) {
      this.origin = new OriginAccessControl(bucket, props);
    } else if (props.originAccessIdentity) {
      this.origin = new OriginAccessIdentity(bucket, props);
    } else {
      this.origin = FeatureFlags.of(bucket.stack)
        .isEnabled(cxapi.CLOUDFRONT_USE_ORIGIN_ACCESS_CONTROL_BY_DEFAULT) ?
        OriginAccessControl(bucket, props) :
        new OriginAccessIdentity(bucket, props);
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was one of the suggested changes from the RFC (see feedback). I agree the structure looks a bit unconventional, but what I like is that the API is very clear on lines 90-100:

else if (props.originAccessControl) {
      this.origin = S3BucketOrigin.withAccessControl(bucket, props);
    } else if (props.originAccessIdentity) {
      this.origin = S3BucketOrigin.withAccessIdentity(bucket, props);
    } else {
      this.origin = FeatureFlags.of(bucket.stack)
        .isEnabled(cxapi.CLOUDFRONT_USE_ORIGIN_ACCESS_CONTROL_BY_DEFAULT) ?
        S3BucketOrigin.withAccessControl(bucket, props) :
        S3BucketOrigin.withAccessIdentity(bucket, props);
    }

since it is a S3 origin with either OAI or OAC configured

} else if (props.originAccessIdentity) {
this.origin = S3BucketOrigin.withAccessIdentity(bucket, props);
} else {
this.origin = FeatureFlags.of(bucket.stack)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should you check current stack or bucket stack for cross stack usage? IMO ideally it doesn't make a difference since the Feature flag is app-level not stack-level. Just want to confirm.

Comment on lines +122 to +123
If your bucket previously used OAI, there will be a best-effort attempt to remove both the policy statement
that allows access to the OAI and the origin access identity itself.
Copy link
Contributor

Choose a reason for hiding this comment

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

Curious if the same bucket can be used by two differnt CloudFront distributions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, the same S3 origin can be used by multiple CloudFront distributions. Hmm might be tricky then to only remove the OAI associated with the distribution in the current stack if we don't have the OAI ID, is that your concern?

@gracelu0 gracelu0 closed this Jul 4, 2024
@watany-dev
Copy link
Contributor

@gracelu0 @GavinZZ
I am one of the developers eagerly awaiting the implementation of this Construct. If you are too busy to continue, I would be delighted to fork this and take over the work.

@gracelu0
Copy link
Contributor Author

@gracelu0 @GavinZZ I am one of the developers eagerly awaiting the implementation of this Construct. If you are too busy to continue, I would be delighted to fork this and take over the work.

Hi @watany-dev , we are continuing to work on this feature! There was some additional feedback on the RFC, so we will open a new PR for this once the RFC is approved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution/core This is a PR that came from AWS. effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cloudfront: Support Origin Access Control
4 participants