

# Setting up permissions for Amazon ECR Public premium repository pull through cache

With pull through cache, you can cache images from Amazon ECR Public premium repositories in your Amazon ECR private registry. To pull premium images, pull through cache uses the identity of the account that holds the AWS Marketplace subscription, called the entitled account. The account that owns the pull through cache rule and the cached repositories is called the cache account.

The permissions that you need depend on how the two accounts relate:
+ **Same account** – The cache account is the entitled account. Pull through cache uses its service-linked role, and you don't need to create any IAM roles. For more information, see [Amazon ECR service-linked role for pull through cache](slr-pullthroughcache.md).
+ **Cross account** – Both accounts are in the `aws` partition. Create a custom role in the cache account and an upstream role in the entitled account. Pull through cache assumes the custom role, and the custom role assumes the upstream role.
+ **Cross partition** – The cache account is in AWS GovCloud (US) (`aws-us-gov`) or the AWS European Sovereign Cloud (`aws-eusc`), and the entitled account is in the `aws` partition. The custom role gets a signed web identity token from AWS STS, and the upstream role trusts that token through an IAM OIDC identity provider in the entitled account.

After you create the roles, specify both role ARNs when you create the pull through cache rule. For more information, see [Creating a pull through cache rule in Amazon ECR](pull-through-cache-creating-rule.md).

The examples in this topic use `111122223333` for the cache account and `444455556666` for the entitled account.

**Topics**
+ [Creating the custom role in the cache account](#pull-through-cache-premium-custom-role)
+ [Adding an identity provider in the entitled account (cross partition only)](#pull-through-cache-premium-oidc-provider)
+ [Creating the upstream role in the entitled account](#pull-through-cache-premium-upstream-role)

## Creating the custom role in the cache account
<a name="pull-through-cache-premium-custom-role"></a>

The custom role needs a trust policy that lets pull through cache assume it, and a permissions policy that lets it reach the entitled account.

**Trust policy**

The following trust policy identifies Amazon ECR pull through cache as the service principal that can assume the role. The conditions limit this to requests made for your cache account. For a cross-partition setup, use the cache account's partition in `aws:SourceArn`, for example `arn:aws-us-gov:ecr:...`.

```JSON
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service": "pullthroughcache.ecr.amazonaws.com"
            },
            "Action": "sts:AssumeRole",
            "Condition": {
                "StringEquals": {
                    "aws:SourceAccount": "{{111122223333}}"
                },
                "ArnLike": {
                    "aws:SourceArn": "arn:aws:ecr:{{region}}:{{111122223333}}:*"
                }
            }
        }
    ]
}
```

**Permissions policy for a cross-account setup**

The following policy lets the custom role assume the upstream role in the entitled account.

```JSON
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "sts:AssumeRole",
            "Resource": "arn:aws:iam::{{444455556666}}:role/{{example-upstream-role}}"
        }
    ]
}
```

**Permissions policy for a cross-partition setup**

The following policy lets the custom role get a web identity token for the Amazon ECR Public premium audience. Before you use it, turn on outbound identity federation in the cache account with the `aws iam enable-outbound-web-identity-federation` command.

```JSON
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "sts:GetWebIdentityToken",
            "Resource": "*",
            "Condition": {
                "ForAllValues:StringEquals": {
                    "sts:IdentityTokenAudience": "https://ecr-premium.aws.com"
                }
            }
        }
    ]
}
```

**Note**  
Use the `ForAllValues:StringEquals` condition operator, as shown. With `StringEquals`, the condition doesn't match and AWS STS denies the token request.

## Adding an identity provider in the entitled account (cross partition only)
<a name="pull-through-cache-premium-oidc-provider"></a>

For a cross-partition setup, the entitled account must trust tokens issued for the cache account. In the cache account, run `aws iam get-outbound-web-identity-federation-info` and note the `IssuerIdentifier` URL. Then, in the entitled account, create an IAM OIDC identity provider for that URL with `https://ecr-premium.aws.com` as the audience.

```
aws iam create-open-id-connect-provider \
     --url https://{{issuer-host}} \
     --client-id-list https://ecr-premium.aws.com
```

In the following examples, {{issuer-host}} is the `IssuerIdentifier` URL without `https://`.

## Creating the upstream role in the entitled account
<a name="pull-through-cache-premium-upstream-role"></a>

The upstream role must be in the `aws` partition. It needs a trust policy that allows only your custom role, and a permissions policy that lets it pull premium images.

**Permissions policy**

The following policy is the same for cross-account and cross-partition setups.

```JSON
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ecr-public:GetAuthorizationToken",
                "ecr-public:GetPremiumImage",
                "sts:GetServiceBearerToken"
            ],
            "Resource": "*"
        }
    ]
}
```

**Trust policy for a cross-account setup**

```JSON
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::{{111122223333}}:role/{{example-custom-role}}"
            },
            "Action": "sts:AssumeRole"
        }
    ]
}
```

**Trust policy for a cross-partition setup**

The following trust policy accepts only tokens from your identity provider, for the Amazon ECR Public premium audience, whose subject is your custom role. Use the cache account's partition in the `sub` value.

```JSON
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Federated": "arn:aws:iam::{{444455556666}}:oidc-provider/{{issuer-host}}"
            },
            "Action": "sts:AssumeRoleWithWebIdentity",
            "Condition": {
                "StringEquals": {
                    "{{issuer-host}}:aud": "https://ecr-premium.aws.com",
                    "{{issuer-host}}:sub": "arn:{{aws-us-gov}}:iam::{{111122223333}}:role/{{example-custom-role}}"
                }
            }
        }
    ]
}
```

If the cache account's Region is an opt-in Region, also turn on that Region in the entitled account. Pull through cache calls AWS STS in the cache Region.