Skip to content

Commit cc2275c

Browse files
rix0rrrElad Ben-Israel
authored andcommitted
fix(ecr): repository grant uses correct resource ARN (#3220)
When granting to a cross-account principal the repository would use a self-reference to obtain the right ARN to use in its own resource policy, which can obviously never work. The solution is to use a '*' resource ARN. Fixes #2473.
1 parent 3319fe5 commit cc2275c

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

packages/@aws-cdk/aws-ecr/lib/repository.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ export abstract class RepositoryBase extends Resource implements IRepository {
179179
grantee,
180180
actions,
181181
resourceArns: [this.repositoryArn],
182+
resourceSelfArns: ['*'],
182183
resource: this,
183184
});
184185
}

packages/@aws-cdk/aws-ecr/test/test.repository.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expect, haveResource, haveResourceLike, ResourcePart } from '@aws-cdk/assert';
22
import iam = require('@aws-cdk/aws-iam');
33
import cdk = require('@aws-cdk/core');
4-
import { RemovalPolicy } from '@aws-cdk/core';
4+
import { RemovalPolicy, Stack } from '@aws-cdk/core';
55
import { Test } from 'nodeunit';
66
import ecr = require('../lib');
77

@@ -352,6 +352,35 @@ export = {
352352
"DeletionPolicy": "Delete"
353353
}, ResourcePart.CompleteDefinition));
354354
test.done();
355-
}
355+
},
356+
357+
'grant adds appropriate resource-*'(test: Test) {
358+
// GIVEN
359+
const stack = new Stack();
360+
const repo = new ecr.Repository(stack, 'TestHarnessRepo');
361+
362+
// WHEN
363+
repo.grantPull(new iam.AnyPrincipal());
364+
365+
// THEN
366+
expect(stack).to(haveResource('AWS::ECR::Repository', {
367+
"RepositoryPolicyText": {
368+
"Statement": [
369+
{
370+
"Action": [
371+
"ecr:BatchCheckLayerAvailability",
372+
"ecr:GetDownloadUrlForLayer",
373+
"ecr:BatchGetImage"
374+
],
375+
"Effect": "Allow",
376+
"Principal": "*",
377+
"Resource": "*",
378+
}
379+
],
380+
"Version": "2012-10-17"
381+
}
382+
}));
356383

384+
test.done();
385+
},
357386
};

0 commit comments

Comments
 (0)