Skip to content

Commit c0c3d19

Browse files
fix: ecr policy warning always throws (#25041)
A change recently added a warning when the policy added to a Repository resource policy. Check length of array instead of existence of array Closes #25028 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 91553e5 commit c0c3d19

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ export class Repository extends RepositoryBase {
669669
* It will fail if a resource section is present at all.
670670
*/
671671
public addToResourcePolicy(statement: iam.PolicyStatement): iam.AddToResourcePolicyResult {
672-
if (statement.resources) {
672+
if (statement.resources.length) {
673673
Annotations.of(this).addWarning('ECR resource policy does not allow resource statements.');
674674
}
675675
if (this.policyDocument === undefined) {

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

+16
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,22 @@ describe('repository', () => {
386386
Annotations.fromStack(stack).hasWarning('*', 'ECR resource policy does not allow resource statements.');
387387
});
388388

389+
test('does not warn if repository policy does not have resources', () => {
390+
// GIVEN
391+
const app = new cdk.App();
392+
const stack = new cdk.Stack(app, 'my-stack');
393+
const repo = new ecr.Repository(stack, 'Repo');
394+
395+
// WHEN
396+
repo.addToResourcePolicy(new iam.PolicyStatement({
397+
actions: ['ecr:*'],
398+
principals: [new iam.AnyPrincipal()],
399+
}));
400+
401+
// THEN
402+
Annotations.fromStack(stack).hasNoWarning('*', 'ECR resource policy does not allow resource statements.');
403+
});
404+
389405
test('default encryption configuration', () => {
390406
// GIVEN
391407
const app = new cdk.App();

0 commit comments

Comments
 (0)