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

Cannot reference EcrImage by digest instead of tag #5082

Closed
statik opened this issue Nov 18, 2019 · 2 comments · Fixed by #13299
Closed

Cannot reference EcrImage by digest instead of tag #5082

statik opened this issue Nov 18, 2019 · 2 comments · Fixed by #13299
Assignees
Labels
@aws-cdk/aws-ecr Related to Amazon Elastic Container Registry effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. good first issue Related to contributions. See CONTRIBUTING.md p1

Comments

@statik
Copy link
Contributor

statik commented Nov 18, 2019

ecs.ContainerImage.fromEcrRepository() does not allow constructing an image reference by digest instead of tag (although the comments reference the ECR support for specifying an image by digest).

Reproduction Steps

import cdk = require("@aws-cdk/core");
import * as ecr from "@aws-cdk/aws-ecr";
import * as ecs from "@aws-cdk/aws-ecs";
const myApp = new cdk.App();
export class DemoStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props: cdk.StackProps) {
    super(scope, id, props);

    const ecrRepoName = "test-repo";
    const ecrRepo = ecr.Repository.fromRepositoryAttributes(
      stack,
      ecrRepoName,
      {
        repositoryArn: `arn:aws:ecr:us-west-2:0123456789:repository/${ecrRepoName}`,
        repositoryName: ecrRepoName
      }
    );
    const image = ecs.ContainerImage.fromEcrRepository(
      ecrRepo,
      "sha256:cb8444cafe12a57bff212d1aaf1ab9063233f701760dcf5c08d5e98f4933a04"
    );
    console.log(image.imageName);
    // Got:  0123456789.dkr.ecr.us-west-2.${Token[AWS::URLSuffix.1]}/test-repo:sha256:cb8444cafe12a57bff212d1aaf1ab9063233f701760dcf5c08d5e98f4933a04
    // Need: 0123456789.dkr.ecr.us-west-2.${Token[AWS::URLSuffix.1]}/test-repo@sha256:cb8444cafe12a57bff212d1aaf1ab9063233f701760dcf5c08d5e98f4933a04
  }
}
new DemoStack(myApp, "demostack", {});

Error Log

The printed image name contains a : separating the image name and the digest. The current implementation of EcrImage only support using tags, while ECR allows specifying either a tag or an image digest. See

* The image name. Images in Amazon ECR repositories can be specified by either using the full registry/repository:tag or
* registry/repository@digest.
*
* For example, 012345678910.dkr.ecr.<region-name>.amazonaws.com/<repository-name>:latest or
* 012345678910.dkr.ecr.<region-name>.amazonaws.com/<repository-name>@sha256:94afd1f2e64d908bc90dbca0035a5b567EXAMPLE.
*/
for context.

Environment

  • CLI Version : 1.16.3
  • Framework Version: 1.16.3
  • OS : MacOS Mojave 10.14.6
  • Language : Typescript

Other

I'd like to be able to specify a digest instead of a tag when calling ecs.ContainerImage.fromEcrRepository.

I'm using a workaround of defining my own image class, but this seems like something that we should fix in aws-cdk itself. I'm not sure what the API should look like for allowing the caller to specify either a tag or a digest, so filing this bug first. I'd be happy to prepare a PR for this if there is agreement on what the API should be.

Workaround

import * as cdk from "@aws-cdk/core";
import * as ecs from "@aws-cdk/aws-ecs";
import * as ecr from "@aws-cdk/aws-ecr";
export class DigestibleEcrImage extends ecs.ContainerImage {
  /**
   * The image name. Images in Amazon ECR repositories can be specified by either using the full registry/repository:tag or
   * registry/repository@digest.
   *
   * 012345678910.dkr.ecr.<region-name>.amazonaws.com/<repository-name>@sha256:94afd1f2e64d908bc90dbca0035a5b567EXAMPLE.
   */
  public readonly imageName: string;

  constructor(
    private readonly repository: ecr.IRepository,
    private readonly digest: string
  ) {
    super();

    this.imageName = `${this.repository.repositoryUriForTag()}@${digest}`;
  }

  public bind(
    _scope: cdk.Construct,
    containerDefinition: ecs.ContainerDefinition
  ): ecs.ContainerImageConfig {
    this.repository.grantPull(
      containerDefinition.taskDefinition.obtainExecutionRole()
    );

    return {
      imageName: this.imageName
    };
  }
}

This is 🐛 Bug Report

@statik statik added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Nov 18, 2019
@SomayaB SomayaB added the @aws-cdk/aws-ecr Related to Amazon Elastic Container Registry label Nov 19, 2019
@rix0rrr
Copy link
Contributor

rix0rrr commented Nov 19, 2019

We can detect whether the supposed "tag" starts with "sha256:" and then switch to digest mode. There is no point in supporting both methods, so I don't see why this wouldn't work.

Then the people will arrive that want to use a digest from a CfnParameter, so we'll have to add an optional 3rd enum parameter that indicates the type of identifier (TAG or DIGEST). If specified, we'll use that to determine : or @. If unspecified AND the parameter is a token, we'll have to fail. If unspecified and the parameter is not a Token, we can autodetect.

This seems like a low-complexity change that mostly does "what you want it to do" out of the box, and I think it won't cause any problems in the future.

@rix0rrr rix0rrr added feature-request A feature should be added or improved. and removed bug This issue is a bug. labels Nov 19, 2019
@SomayaB SomayaB removed the needs-triage This issue or PR still needs to be triaged. label Nov 26, 2019
@rix0rrr rix0rrr added the good first issue Related to contributions. See CONTRIBUTING.md label Nov 27, 2019
@rix0rrr rix0rrr assigned MrArnoldPalmer and unassigned rix0rrr Jan 23, 2020
@MrArnoldPalmer MrArnoldPalmer added p1 effort/small Small work item – less than a day of effort labels Feb 12, 2020
@mergify mergify bot closed this as completed in #13299 Feb 26, 2021
mergify bot pushed a commit that referenced this issue Feb 26, 2021
)

Fixes #5082


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@github-actions
Copy link

⚠️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 effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. good first issue Related to contributions. See CONTRIBUTING.md p1
Projects
None yet
4 participants