简体   繁体   English

使用 Python Lambda 函数的 AWS ECR 图像标签

[英]AWS ECR Image Tags using Python Lambda Function

I am using a python lambda function to add an image tag to ECR images using boto3.我正在使用 python lambda 函数使用 boto3 将图像标签添加到 ECR 图像。 The following code works and adds the desired tag to the specified image.以下代码有效并将所需的标签添加到指定的图像。 However, targeting a different image with a different test event removes the previously applied tag from the last image.但是,使用不同的测试事件定位不同的图像会从最后一个图像中删除先前应用的标签。 I have tried 3 different repos, as well as cross account and local account lambda functions.我尝试了 3 个不同的存储库,以及跨账户和本地账户 lambda 函数。

For example, these are three image tags within repository "test-repo":例如,这些是存储库“test-repo”中的三个图像标签:

  1. 1.0.0.1 1.0.0.1
  2. 1.0.0.2 1.0.0.2
  3. 1.0.0.3 1.0.0.3

I run the test event for 1.0.0.1, and the following tags are now present:我运行 1.0.0.1 的测试事件,现在出现以下标签:

  1. DELETEON_2020-03-06, 1.0.0.1 DELETEON_2020-03-06, 1.0.0.1
  2. 1.0.0.2 1.0.0.2
  3. 1.0.0.3 1.0.0.3

When I run the test event for any other image, in this example 1.0.0.2, this is what happens:当我为任何其他图像运行测试事件时,在此示例 1.0.0.2 中,会发生以下情况:

  1. 1.0.0.1 1.0.0.1
  2. DELETEON_2020-03-06, 1.0.0.2 DELETEON_2020-03-06, 1.0.0.2
  3. 1.0.0.3 1.0.0.3

I would expect the code to apply the tag to 1.0.0.1, and then when I run it for 1.0.0.2, it just adds the tag to that image as well.我希望代码将标记应用于 1.0.0.1,然后当我为 1.0.0.2 运行它时,它也只是将标记添加到该图像。 I don't see why it is removing the previously applied tag.我不明白它为什么要删除以前应用的标签。 I need to be able to apply the DELETEON tag to all identified vulnerable images.我需要能够将 DELETEON 标记应用于所有已识别的易受攻击的图像。 Is there something I am not seeing or understanding about the boto3 ECR methods, ECR itself, or perhaps this is a bug?关于 boto3 ECR 方法、ECR 本身,是否有我没有看到或理解的内容,或者这可能是一个错误?

    import json
    import boto3
    import datetime
    from datetime import timedelta

    def lambda_handler(event, context):
        acct = event['account']
        date = datetime.date.today()
        repo = event['detail']['repository-name']
        digest = event['detail']['image-digest']
        imagetag = event['detail']['image-tags'][0]
        client = boto3.client('ecr')
        dayint = datetime.date.today() + datetime.timedelta(days=3)
        deletetag = dayint.strftime("%Y-%m-%d")

        response = client.batch_get_image(
            registryId=acct,
            repositoryName=repo,
            imageIds=[
                {
                    'imageDigest': digest,
                    'imageTag': imagetag
                }
            ]
        )
        putresponse = client.put_image(
                    registryId=acct,
                    repositoryName=repo,
                    imageManifest=response['images'][0]['imageManifest'],
                    imageTag='DELETEON_' + deletetag
                )

Here is the sample test event (I switch out the image-digest and image-tag to target different images in the same repo):这是示例测试事件(我将 image-digest 和 image-tag 切换为针对同一 repo 中的不同图像):

{
  "version": "0",
  "id": "1111111111-22222222222-3333333333333",
  "detail-type": "ECR Image Scan",
  "source": "aws.ecr",
  "account": "111111111111",
  "time": "2020-02-14T22:41:19Z",
  "region": "us-east-1",
  "resources": [
    "arn:aws:ecr:us-east-1:111111111111:repository/test-repo"
  ],
  "detail": {
    "scan-status": "COMPLETE",
    "repository-name": "test-repo",
    "image-digest": "sha256:111111111111111111111111111111111111111111111111111",
    "image-tags": [
      "1.0.0.1"
    ],
    "finding-severity-counts": {
      "HIGH": 12,
      "MEDIUM": 46,
      "INFORMATIONAL": 84,
      "LOW": 72,
      "UNDEFINED": 6
    }
  }
}

In your example, the tags have exactly the same value: DELETEON_2020-03-06 .在您的示例中,标签具有完全相同的值: DELETEON_2020-03-06 They are the same tag.他们是同一个标签。 In a Docker repo, a tag can only point to one thing.在 Docker 存储库中,标签只能指向一件事。 Tags are unique identifiers.标签是唯一标识符。 You can't have the same tag pointing to multiple Docker images.您不能让相同的标签指向多个 Docker 镜像。 If that was allowed, then what would happen when you ran the command: docker run my-image:DELETEON_2020-03-06 ?如果允许,那么当您运行以下命令时会发生什么docker run my-image:DELETEON_2020-03-06

When you are apply that tag to one image, and it already points to another image, Docker is automatically moving that tag.当您将该标签应用到一个图像,并且它已经指向另一个图像时,Docker 会自动移动该标签。 Thus Docker tags are not appropriate for this specific use case.因此 Docker 标签不适合这个特定用例。 I think your best bet for storing these delete dates is going to be in a separate location outside of ECR, for example a DynamoDB table.我认为您最好将这些删除日期存储在 ECR 之外的单独位置,例如 DynamoDB 表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM