From 8e7f3c2791beef051677ec39044baa7830f32444 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Thu, 22 Oct 2026 13:01:46 -0400 Subject: [PATCH] fix(aws-lambda): adopt an existing ECR repository only when it carries the caller's tags --- .../aws-lambda/src/sdk/pushImageToEcr.test.ts | 20 ++++++++++++++++++ packages/aws-lambda/src/sdk/pushImageToEcr.ts | 21 ++++++++++++++++--- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/packages/aws-lambda/src/sdk/pushImageToEcr.test.ts b/packages/aws-lambda/src/sdk/pushImageToEcr.test.ts index 6559f997fe..1398b550e7 100644 --- a/packages/aws-lambda/src/sdk/pushImageToEcr.test.ts +++ b/packages/aws-lambda/src/sdk/pushImageToEcr.test.ts @@ -61,11 +61,16 @@ class FakeEcr { DescribeRepositoriesCommand(input: { repositoryNames: string[] }): unknown { return { repositories: input.repositoryNames.map((name) => ({ + repositoryArn: `arn:aws:ecr:r:1:repository/${name}`, repositoryUri: this.repositories.get(name), })), }; } + ListTagsForResourceCommand(input: { resourceArn: string }): unknown { + return { tags: this.repositoryTags.get(input.resourceArn.split("/").pop() ?? "") ?? [] }; + } + BatchCheckLayerAvailabilityCommand(input: { layerDigests: string[] }): unknown { if (input.layerDigests.length > 100) throw awsError("InvalidParameterException"); const stored = (d: string) => this.blobs.has(d) && !this.hiddenFromCheck.has(d); @@ -344,4 +349,19 @@ describe("ensureEcrRepository", () => { expect(repositoryUri).toBe(`${REGISTRY}/hyperframes-render`); expect(ecr.calls).toEqual(["CreateRepositoryCommand", "DescribeRepositoriesCommand"]); }); + + it("adopts an existing repository only when it carries the tags", async () => { + const ecr = new FakeEcr(); + const tags = [{ Key: "hyperframes:stack", Value: "s" }]; + await ensureEcrRepository({ ecr: asEcr(ecr), repositoryName: "s-video", tags }); + + const again = await ensureEcrRepository({ ecr: asEcr(ecr), repositoryName: "s-video", tags }); + expect(again.repositoryUri).toBe(`${REGISTRY}/s-video`); + + ecr.repositories.set("other-video", `${REGISTRY}/other-video`); + ecr.repositoryTags.set("other-video", [{ Key: "hyperframes:stack", Value: "other" }]); + await expect( + ensureEcrRepository({ ecr: asEcr(ecr), repositoryName: "other-video", tags }), + ).rejects.toThrow("other-video already exists without hyperframes:stack=s"); + }); }); diff --git a/packages/aws-lambda/src/sdk/pushImageToEcr.ts b/packages/aws-lambda/src/sdk/pushImageToEcr.ts index 3146346ccb..9c585d0330 100644 --- a/packages/aws-lambda/src/sdk/pushImageToEcr.ts +++ b/packages/aws-lambda/src/sdk/pushImageToEcr.ts @@ -12,6 +12,7 @@ import { DescribeRepositoriesCommand, ECRClient, InitiateLayerUploadCommand, + ListTagsForResourceCommand, PutImageCommand, type Repository, type Tag, @@ -37,11 +38,11 @@ export interface EnsureEcrRepositoryOptions { ecr?: ECRClient; region?: string; repositoryName: string; - /** Applied only when the repository is created; an existing repository keeps its tags. */ + /** Set on create. An existing repository is adopted only if it already carries every one. */ tags?: Tag[]; } -/** Create the repository, or look it up when it already exists. */ +/** Create the repository, or adopt an existing one that carries `tags`. */ export async function ensureEcrRepository( opts: EnsureEcrRepositoryOptions, ): Promise<{ repositoryUri: string }> { @@ -56,7 +57,21 @@ export async function ensureEcrRepository( const found = await ecr.send( new DescribeRepositoriesCommand({ repositoryNames: [repositoryName] }), ); - return { repositoryUri: repositoryUriOf(found.repositories?.[0], repositoryName) }; + const repository = found.repositories?.[0]; + if (tags?.length) { + const listed = await ecr.send( + new ListTagsForResourceCommand({ resourceArn: repository?.repositoryArn }), + ); + const has = new Map((listed.tags ?? []).map((tag) => [tag.Key, tag.Value])); + const missing = tags.filter((tag) => has.get(tag.Key) !== tag.Value); + if (missing.length > 0) { + const wanted = missing.map((tag) => `${tag.Key}=${tag.Value}`).join(", "); + throw new Error( + `[ensureEcrRepository] ${repositoryName} already exists without ${wanted}; refusing to adopt a repository this caller did not create`, + ); + } + } + return { repositoryUri: repositoryUriOf(repository, repositoryName) }; } function repositoryUriOf(repository: Repository | undefined, repositoryName: string): string {