Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ecr): authorization token retrieval grants #11783

Merged
merged 3 commits into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/@aws-cdk/aws-ecr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ repository.onImageScanCompleted('ImageScanComplete')
.addTarget(...)
```

### Authorization Token

Besides the Amazon ECR APIs, ECR also allows the Docker CLI or a language-specific Docker library to push and pull
images from an ECR repository. However, the Docker CLI does not support native IAM authentication methods and
additional steps must be taken so that Amazon ECR can authenticate and authorize Docker push and pull requests.
More information can be found at at [Registry Authentication](https://docs.aws.amazon.com/AmazonECR/latest/userguide/Registries.html#registry_auth).

A Docker authorization token can be obtained using the `GetAuthorizationToken` ECR API. The following code snippets
grants an IAM user access to call this API.

```ts
import * as iam from '@aws-cdk/aws-iam';

const user = new iam.User(this, 'User', { ... });
AuthorizationToken.grantRead(user);
```

### Automatically clean up repositories

Expand Down
20 changes: 20 additions & 0 deletions packages/@aws-cdk/aws-ecr/lib/auth-token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as iam from '@aws-cdk/aws-iam';

/**
* Authorization token to access ECR repositories via Docker CLI.
*/
export class AuthorizationToken {
/**
* Grant access to retrieve an authorization token.
*/
public static grantRead(grantee: iam.IGrantable) {
grantee.grantPrincipal.addToPrincipalPolicy(new iam.PolicyStatement({
actions: ['ecr:GetAuthorizationToken'],
// GetAuthorizationToken only allows '*'. See https://docs.aws.amazon.com/service-authorization/latest/reference/list_amazonelasticcontainerregistry.html#amazonelasticcontainerregistry-actions-as-permissions
resources: ['*'],
nija-at marked this conversation as resolved.
Show resolved Hide resolved
}));
}

private constructor() {
}
}
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-ecr/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './ecr.generated';

export * from './repository';
export * from './lifecycle';
export * from './auth-token';
31 changes: 31 additions & 0 deletions packages/@aws-cdk/aws-ecr/test/test.auth-token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { expect, haveResourceLike } from '@aws-cdk/assert';
import * as iam from '@aws-cdk/aws-iam';
import { Stack } from '@aws-cdk/core';
import { Test } from 'nodeunit';
import { AuthorizationToken } from '../lib';

export = {
'grant()'(test: Test) {
// GIVEN
const stack = new Stack();
const user = new iam.User(stack, 'User');

// WHEN
AuthorizationToken.grantRead(user);

// THEN
expect(stack).to(haveResourceLike('AWS::IAM::Policy', {
PolicyDocument: {
Statement: [
{
Action: 'ecr:GetAuthorizationToken',
Effect: 'Allow',
Resource: '*',
},
],
},
}));

test.done();
},
};