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

aws-ecr: RepositoryPolicyText should error if includes Resource #24314

Closed
2 tasks
ahammond opened this issue Feb 24, 2023 · 11 comments
Closed
2 tasks

aws-ecr: RepositoryPolicyText should error if includes Resource #24314

ahammond opened this issue Feb 24, 2023 · 11 comments
Assignees
Labels
@aws-cdk/aws-ecr Related to Amazon Elastic Container Registry bug This issue is a bug. effort/small Small work item – less than a day of effort good first issue Related to contributions. See CONTRIBUTING.md p2

Comments

@ahammond
Copy link
Contributor

ahammond commented Feb 24, 2023

Describe the feature

When adding a resource policy to an ECR repo, if your PolicyStatment includes a Resource, it will successfully synth, but then fail to deploy.

CDK should detect the presence of the Resource key and issue an Error.

Use Case

I spent an entire day staring at a perfectly legal looking resource policy. Only to find out that my bug was this illegal resource key. Error message from Cfn wasn't very helpful:

Resource handler returned message: "Invalid parameter at 'PolicyText' failed to satisfy constraint: 'Invalid repository policy provided' (Service: Ecr, Status Code: 400,  ...

Proposed Solution

When calling myRepo.addToResourcePolicy(), check the input PolicyStatement to confirm it doesn't have a resources: key.

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

CDK version used

2.27.0

Environment details (OS name and version, etc.)

MacOS, pretty recent. Node 16

@ahammond ahammond added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Feb 24, 2023
@github-actions github-actions bot added the @aws-cdk/aws-ecr Related to Amazon Elastic Container Registry label Feb 24, 2023
@khushail
Copy link
Contributor

Thanks for reaching out @ahammond . It would be really helpful if you could share the code for reproducing this error

@khushail khushail self-assigned this Feb 25, 2023
@khushail khushail added investigating This issue is being investigated and/or work is in progress to resolve the issue. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed needs-triage This issue or PR still needs to be triaged. investigating This issue is being investigated and/or work is in progress to resolve the issue. labels Feb 25, 2023
@ahammond
Copy link
Contributor Author

Write literally any PolicyStatement that includes the traditional

resources: ['*']

And you'll see it. I can provide a snippet if necessary, but this is pretty easy to repro.

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Feb 26, 2023
@ahammond
Copy link
Contributor Author

import { App, Stack, StackProps } from 'aws-cdk-lib';
import { Repository } from 'aws-cdk-lib/aws-ecr';
import { AnyPrincipal, PolicyStatement } from 'aws-cdk-lib/aws-iam';
import { Construct } from 'constructs';

export class EcrStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    const r = new Repository(this, 'MyEcrRepo');
    r.addToResourcePolicy(new PolicyStatement({
      actions: ['ecr:GetDownloadUrlForLayer'],
      principals: [new AnyPrincipal()],
      // Every other resource policy I've ever written requires a resource.
      // They're really only good for limiting access within a resource.
      // I understand why they're allowed, but not why they're required.
      // Strangely, not only does Cfn for ECR not allow
      // us to specify a resource policy for a specific image,
      // it will straight up fail if a resource section is present at all.
      resources: ['*'], // This will break, so CDK should warn. At least until Cfn (and possibly ECR) is fixed.
    }));
  }
}

const devEnv = {
  account: process.env.CDK_DEFAULT_ACCOUNT,
  region: process.env.CDK_DEFAULT_REGION,
};

const app = new App();
new EcrStack(app, 'MyEcrStack',  { env: devEnv });

app.synth();

ahammond added a commit to ahammond/repro-ecr-policy-with-resource that referenced this issue Feb 27, 2023
@pahud
Copy link
Contributor

pahud commented Feb 27, 2023

@ahammond

I just checked the doc for ECR resource policy and yes 'resource' is not required while in other resource types such as cloudwatch logs resource policy or s3 bucket policy, the resource attribute is required.
https://docs.aws.amazon.com/AmazonECR/latest/userguide/repository-policy-examples.html

Yes I think we should add a check to avoid this error. I am making it as a p2 feature request and any PR submission will be highly appreciated.

@pahud pahud added p2 effort/small Small work item – less than a day of effort effort/medium Medium work item – several days of effort and removed effort/small Small work item – less than a day of effort labels Feb 27, 2023
@TheRealAmazonKendra TheRealAmazonKendra added bug This issue is a bug. effort/small Small work item – less than a day of effort and removed feature-request A feature should be added or improved. effort/medium Medium work item – several days of effort labels Feb 27, 2023
@TheRealAmazonKendra
Copy link
Contributor

The implementation of addToResourcePolicy here just plain shouldn't allow for providing resources. This is a bug. We'll likely need to deprecate the function and write a new one. Because there is a workaround, however, this is still a p2 and we will not have capacity to implement the fix. We welcome community contributions on this, though.

@TheRealAmazonKendra TheRealAmazonKendra added the good first issue Related to contributions. See CONTRIBUTING.md label Feb 27, 2023
@khushail khushail removed their assignment Feb 28, 2023
@ahammond
Copy link
Contributor Author

@TheRealAmazonKendra I agree it shouldn't allow providing a resource. Deprecating and replacing the function is probably a medium, but... adding a Warn on the existing function would save your customers hours of very frustrating debugging time. It's an easy fix for what it otherwise a terrible UX. And it's a very, very, small.

@TheRealAmazonKendra
Copy link
Contributor

You're totally right and we'd certainly accept a PR with that as a stopgap. Since this is a P2, however, we don't have the capacity to make the change.

@jbrathwa
Copy link

jbrathwa commented Mar 1, 2023

Is there any workaround to this?

@ahammond
Copy link
Contributor Author

ahammond commented Mar 1, 2023

Is there any workaround to this?

What I did was

  • Write a reasonable resource policy, including a resources entry. Same as I've done for countless other AWS resources.
  • Got an obscure error message.
  • Googled the error message, found nothing.
  • Found and read https://docs.aws.amazon.com/AmazonECR/latest/userguide/repository-policies.html but still didn't see the problem.
  • Opened an AWS support ticket. Chatted with the agent. They thought my policy was reasonable and correct.
  • Agent escalated internally, they also thought policy was reasonable.
  • +1hr of trying different things and agent asked me to try removing the resource.

So... sure. The workaround is finding this issue and knowing to not put a resource in your policy statement. Which... is a pretty bad user experience.

ahammond added a commit to ahammond/aws-cdk that referenced this issue Mar 1, 2023
ECR does not allow resource to be included in private repository resource policies.
CFN largely swallows the error message.
Most resources require or at least allow a resource in their policies, so we should at least warn.

See issue aws#24314
ahammond added a commit to ahammond/aws-cdk that referenced this issue Mar 1, 2023
ECR does not allow resource to be included in private repository resource policies.
CFN largely swallows the error message.
Most resources require or at least allow a resource in their policies, so we should at least warn.

See issue aws#24314
ahammond added a commit to ahammond/aws-cdk that referenced this issue Mar 6, 2023
ECR does not allow resource to be included in private repository resource policies.
CFN largely swallows the error message.
Most resources require or at least allow a resource in their policies, so we should at least warn.

See issue aws#24314
mergify bot pushed a commit that referenced this issue Apr 4, 2023
ECR does not allow resource to be included in private repository resource policies.
CFN largely swallows the error message.
Most resources require or at least allow a resource in their policies, so we should at least warn.

See issue #24314
@paulhcsun paulhcsun self-assigned this Dec 5, 2023
@paulhcsun
Copy link
Contributor

Closed by #24401

Copy link

github-actions bot commented Dec 5, 2023

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-ecr Related to Amazon Elastic Container Registry bug This issue is a bug. effort/small Small work item – less than a day of effort good first issue Related to contributions. See CONTRIBUTING.md p2
Projects
None yet
Development

No branches or pull requests

6 participants