Skip to content

Commit 8434294

Browse files
authored
feat(ecr): Public Gallery authorization token (#12775)
API for granting permissions to retrieve an authorization token for the [Public ECR Gallery](https://gallery.ecr.aws/), similarly to the [existing API](https://github.com/aws/aws-cdk/blob/master/packages/@aws-cdk/aws-ecr/lib/auth-token.ts) for private ECR registries. Also added a note in the README encouraging users to prefer authenticated pulls over anonymous ones to benefit from higher limits. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 1a9f2a8 commit 8434294

File tree

3 files changed

+76
-4
lines changed

3 files changed

+76
-4
lines changed

packages/@aws-cdk/aws-ecr/README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,29 @@ grants an IAM user access to call this API.
5151

5252
```ts
5353
import * as iam from '@aws-cdk/aws-iam';
54+
import * as ecr from '@aws-cdk/aws-ecr';
5455

5556
const user = new iam.User(this, 'User', { ... });
56-
iam.AuthorizationToken.grantRead(user);
57+
ecr.AuthorizationToken.grantRead(user);
5758
```
5859

60+
If you access images in the [Public ECR Gallery](https://gallery.ecr.aws/) as well, it is recommended you authenticate to the regsitry to benefit from
61+
higher rate and bandwidth limits.
62+
63+
> See `Pricing` in https://aws.amazon.com/blogs/aws/amazon-ecr-public-a-new-public-container-registry/ and [Service quotas](https://docs.aws.amazon.com/AmazonECR/latest/public/public-service-quotas.html).
64+
65+
The following code snippet grants an IAM user access to retrieve an authorization token for the public gallery.
66+
67+
```ts
68+
import * as iam from '@aws-cdk/aws-iam';
69+
import * as ecr from '@aws-cdk/aws-ecr';
70+
71+
const user = new iam.User(this, 'User', { ... });
72+
ecr.PublicGalleryAuthorizationToken.grantRead(user);
73+
```
74+
75+
This user can then proceed to login to the registry using one of the [authentication methods](https://docs.aws.amazon.com/AmazonECR/latest/public/public-registries.html#public-registry-auth).
76+
5977
## Automatically clean up repositories
6078

6179
You can set life cycle rules to automatically clean up old images from your

packages/@aws-cdk/aws-ecr/lib/auth-token.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import * as iam from '@aws-cdk/aws-iam';
22

33
/**
4-
* Authorization token to access ECR repositories via Docker CLI.
4+
* Authorization token to access private ECR repositories in the current environment via Docker CLI.
5+
*
6+
* @see https://docs.aws.amazon.com/AmazonECR/latest/userguide/registry_auth.html
57
*/
68
export class AuthorizationToken {
79
/**
@@ -18,3 +20,27 @@ export class AuthorizationToken {
1820
private constructor() {
1921
}
2022
}
23+
24+
/**
25+
* Authorization token to access the global public ECR Gallery via Docker CLI.
26+
*
27+
* @see https://docs.aws.amazon.com/AmazonECR/latest/public/public-registries.html#public-registry-auth
28+
*/
29+
export class PublicGalleryAuthorizationToken {
30+
31+
/**
32+
* Grant access to retrieve an authorization token.
33+
*/
34+
public static grantRead(grantee: iam.IGrantable) {
35+
grantee.grantPrincipal.addToPrincipalPolicy(new iam.PolicyStatement({
36+
actions: ['ecr-public:GetAuthorizationToken', 'sts:GetServiceBearerToken'],
37+
// GetAuthorizationToken only allows '*'. See https://docs.aws.amazon.com/service-authorization/latest/reference/list_amazonelasticcontainerregistry.html#amazonelasticcontainerregistry-actions-as-permissions
38+
// GetServiceBearerToken only allows '*'. See https://docs.aws.amazon.com/service-authorization/latest/reference/list_awssecuritytokenservice.html#awssecuritytokenservice-actions-as-permissions
39+
resources: ['*'],
40+
}));
41+
}
42+
43+
private constructor() {
44+
}
45+
46+
}

packages/@aws-cdk/aws-ecr/test/test.auth-token.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { expect, haveResourceLike } from '@aws-cdk/assert';
22
import * as iam from '@aws-cdk/aws-iam';
33
import { Stack } from '@aws-cdk/core';
44
import { Test } from 'nodeunit';
5-
import { AuthorizationToken } from '../lib';
5+
import { AuthorizationToken, PublicGalleryAuthorizationToken } from '../lib';
66

77
export = {
8-
'grant()'(test: Test) {
8+
'AuthorizationToken.grantRead()'(test: Test) {
99
// GIVEN
1010
const stack = new Stack();
1111
const user = new iam.User(stack, 'User');
@@ -28,4 +28,32 @@ export = {
2828

2929
test.done();
3030
},
31+
32+
'PublicGalleryAuthorizationToken.grantRead()'(test: Test) {
33+
// GIVEN
34+
const stack = new Stack();
35+
const user = new iam.User(stack, 'User');
36+
37+
// WHEN
38+
PublicGalleryAuthorizationToken.grantRead(user);
39+
40+
// THEN
41+
expect(stack).to(haveResourceLike('AWS::IAM::Policy', {
42+
PolicyDocument: {
43+
Statement: [
44+
{
45+
Action: [
46+
'ecr-public:GetAuthorizationToken',
47+
'sts:GetServiceBearerToken',
48+
],
49+
Effect: 'Allow',
50+
Resource: '*',
51+
},
52+
],
53+
},
54+
}));
55+
56+
test.done();
57+
},
58+
3159
};

0 commit comments

Comments
 (0)