Skip to content

Commit

Permalink
fix(cli): cross account docker image assets upload no longer works (#…
Browse files Browse the repository at this point in the history
…14816)

Fixes #14815

Cross account docker images fail to upload when using modern stack and assumed roles.

Similar to the fix in afd7045 only perform the account lookup when required


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
jmortlock authored Jun 4, 2021
1 parent a12db62 commit 14fbb11
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/cdk-assets/lib/private/handlers/container-images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ export class ContainerImageAssetHandler implements IAssetHandler {
public async publish(): Promise<void> {
const destination = await replaceAwsPlaceholders(this.asset.destination, this.host.aws);
const ecr = await this.host.aws.ecrClient(destination);
const account = (await this.host.aws.discoverCurrentAccount()).accountId;
const account = async () => (await this.host.aws.discoverCurrentAccount())?.accountId;
const repoUri = await repositoryUri(ecr, destination.repositoryName);

if (!repoUri) {
throw new Error(`No ECR repository named '${destination.repositoryName}' in account ${account}. Is this account bootstrapped?`);
throw new Error(`No ECR repository named '${destination.repositoryName}' in account ${await account()}. Is this account bootstrapped?`);
}

const imageUri = `${repoUri}:${destination.imageTag}`;
Expand Down
12 changes: 12 additions & 0 deletions packages/cdk-assets/test/docker-images.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ describe('with a complete manifest', () => {
}));
});

test('Displays an error if the ECR repository cannot be found', async () => {
aws.mockEcr.describeImages = mockedApiFailure('RepositoryNotFoundException', 'Repository not Found');

await expect(pub.publish()).rejects.toThrow('Error publishing: Repository not Found');
});

test('successful run does not need to query account ID', async () => {
aws.mockEcr.describeImages = mockedApiResult({ /* No error == image exists */ });
await pub.publish();
expect(aws.discoverCurrentAccount).not.toHaveBeenCalled();
});

test('upload docker image if not uploaded yet but exists locally', async () => {
aws.mockEcr.describeImages = mockedApiFailure('ImageNotFoundException', 'File does not exist');
aws.mockEcr.getAuthorizationToken = mockedApiResult({
Expand Down

0 comments on commit 14fbb11

Please sign in to comment.