目の前に僕らの道がある

勉強会とか、技術的にはまったことのメモ

ECSのタグ付け認可とアカウント単位のオプトアウトの廃止

ECSのタグ付け認可とは

ECS関連のリソース作成時にリソースタグを付けることができます。その際 ecs:tagResource の権限が必要となります。なお、リソースタグを設定しないECSリソース作成の際は権限不要です。

この権限の有無のチェックをタグ付け認可と言います。

具体的にECSリソースの作成のアクションは以下の通りです。

  • CreateCapacityProvider
  • CreateCluster
  • CreateService
  • CreateTaskSet
  • RegisterContainerInstance
  • RegisterTaskDefinition
  • RunTask
  • StartTask

タグ付け認可の仕組みは2023年4月18日に導入されました。しかしながら従来からECSリソースを作成する際にタグ付けしていたAWSアカウントに関しては影響があるため、アカウントレベルでタグ付け認可の機能を無効(オプトアウト)することができました。つまりアカウントレベルで無効にしていれば ecs:tagResource の権限がなくてもタグ付けをすることが可能でした。

しかしながらアカウント単位のオプトアウト設定は2024年3月9日に廃止されます。

アカウント単位のオプトアウトの廃止

タグ付け認可におけるタイムラインは以下のとおりです

  • 2023年4月18日 タグ付け認可の導入とアカウント単位での有効化設定の導入
  • 2024年2月9日- 2月28日 新規アカウントおよび影響を受けないアカウントに関してデフォルトでタグ付け認可の有効化が行われる
  • 2024年2月29日 アカウント単位で有効にしている場合、無効に変更できなくなる
  • 2024年3月29日 すべてのアカウントでタグ付け認可が有効になり、アカウント単位での設定が不可能になる

現時点(2024/03/20)であまり時間がありません。

現在タグ付け認可に影響あるAWSアカウントに関しては、Personal Health Dashboadに以下のような通知が来ているはずです。

▼ElasticContainerService security notification (クリックで展開)▼

English follows Japanese | 英語のメッセージは日本語の後にございます

お客様のアカウントにて過去 1 年以内に ecs:TagResource の許可無しに ECS リソースの作成時にタグを付けていることが判明したため、ご連絡差し上げます。Amazon ECS は、2023 年 4 月 18 日にリソース作成のタグ付け認証を導入しました [1]。新規および既存のお客様は、ECS Console または API の ECS アカウント設定ページを使用して、この新機能の使用をオプトインする必要があります。このセキュリティ制御により、ECS リソースの作成時にタグをつけることをユーザーに拒否または許可できます。2024 年 3 月 29 日以降もお客様の IAM プリンシパルが新しく作成された ECS リソースに引き続きタグを適用できるように、IAM ポリシーを更新して ecs:TagResource アクションを明示的に許可することを強くお勧めします。


2024 年 2 月 9 日以降、AWS コンソール の ECS アカウント設定ページにて tagResourceAuthorization アカウント設定を明示的に off に設定していないすべてのお客様のアカウントは、自動的にこの設定にオプトインされました。お客様の AWS アカウントは一時的に許可リストに載せているため、2024 年 3 月 29 日まではタグリソース認証の off の動作が継続されます。

2024 年 3 月 8 日、現在オプトインしているアカウントが tagResourceAuthorization をオプトアウトする機能を削除し、タグをサポートするすべての ECS リソースの作成に際して ecs:TagResource IAM 権限の使用を強制するようにしました。

最終的に 2024 年 3 月 29 日をもってお客様のアカウントを許可リストから削除し、tagResourceAuthorization を有効化します。呼び出し元のプリンシパルの IAM ポリシーに ecs:TagResource アクションを含めずにタグをつけて ECS リソースを作成しようとすると、「AccessDenied」メッセージが表示されます。この変更は CreateCapacityProvider, CreateCluster, CreateService, CreateTaskSet, RegisterContainerInstance, RunTask, StartTask, および RegisterTaskDefinition の API に影響を及ぼします。

ecs:TagResource を使用しない拒否レスポンスの例

以下は、ecs:CreateCluster アクションを付与している IAM ポリシーの一部です。ecs:TagResource アクションは含まれていません。tagResourceAuthorization アカウント設定がオンの場合、リクエスト例では以下の AccessDenied 例外が返されます。

# IAM ポリシー

“Statement”: [
{
“Sid”: “AllowCreateCluster”,
“Effect”: “Allow”,
“Action”: [
“ecs:CreateCluster”
],
“Resource”: “*”
}
]


# クラスター作成のリクエスト

aws ecs create-cluster --cluster-name MyCluster --tags key=key1,value=value1

# タグ付けの拒否されたレスポンス

An error occurred (AccessDeniedException) when calling the CreateCluster operation:
User: is not authorized to perform: ecs:TagResource on resource: cluster/MyCluster because no identity-based policy allows the ecs:TagResource action

必要なアクション:

IAM プリンシパルが 2024 年 3 月 29 日以降も新しく作成された ECS リソースに引き続きタグを適用できるように、IAM ポリシーに次のステートメントを追加することを強くお勧めします。

すべての ECS リソースの作成時にタグ付けを許可
以下の説明に従って ecs:TagResource アクションを追加すると、ECS リソースの作成中にタグ付けが可能になります [2]。

“Statement”: [
{
“Sid”: “AllowTagging”,
“Effect”: “Allow”,
“Action”: [
“ecs:TagResource”
],
“Resource”: “*”
}
]

単一の ECS リソースタイプ (ECS クラスタ) の作成時にタグ付けを許可

条件ステートメント ecs:CreateAction を使用すると、タグ付けを特定の ECS API に制限できます。以下の例では、ECS CreateCluster API でのみタグ付けへのアクセスを許可します。タグ付きの ECS RunTask API へのリクエストは、拒否判定になります [2]。

“Statement”: [
{
“Sid”: “AllowClusterTagging”,
“Effect”: “Allow”,
“Action”: [
“ecs:TagResource”
],
“Resource”: “*”,
“Condition”: {
“StringEquals”: {
“ecs:CreateAction” : “CreateCluster”
}
}
}
]

タイムライン:

2024 年 2 月 9 日(完了)- タグ付け認証はデフォルトで on になっています。これには、ホワイトリストに登録されているアカウントは含まれません。tagResourceAuthorization アカウント設定の on/off を切り替えることも可能であり、ポリシーへの準拠をテストいただけます。

2024 年 3 月 8 日 - タグ付け認証を on にすると、off にすることはできなくなります。この日まではアカウント設定を切り替えることができますので、その間に IAM ポリシーをテストすることをお勧めします。

2024 年 3 月 29 日 - すべての AWS アカウントでタグ付け認証が有効になります。アカウントレベルの設定は使用されなくなり、AWS コンソールの ECS アカウント設定ページから削除されます。

ご質問やご不明点等ございましたら、AWS サポート [3] までお問い合わせください。

[1] https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-account-settings.html#tag-resources-setting
[2] https://docs.aws.amazon.com/AmazonECS/latest/developerguide/supported-iam-actions-tagging.html
[3] https://aws.amazon.com/support

---

We are contacting you because we identified that your account has tagged ECS resources upon creation, within the past year, without the ecs:TagResource permission. Amazon ECS introduced tagging authorization for resource creation on April 18, 2023 [1]. New and existing customers must opt-in to use this new feature by using the ECS Account Settings page in the ECS Console or API. This security control allows users to deny or allow tagging ECS resources when they are created. We strongly recommend you update your IAM policies to explicitly allow the ecs:TagResource action so that your IAM principals continue applying tags to newly created ECS resources on or after March 29, 2024.

From February 9, 2024, all customer accounts which have not explicitly set the tagResourceAuthorization account setting to “off” in the ECS Account Settings page in the AWS Console were automatically opted into the setting. We have temporarily allow-listed your AWS account so you will continue to have the “off” behavior for tagResourceAuthorization until March 29, 2024.

On March 8, 2024, we removed the ability for currently opted-in accounts to opt-out of tagging authorization and enforced the creation of all ECS resources that support tags to use the ecs:TagResource IAM permission.

Finally on March 29, 2024, we will remove your account from the allow-list and activate tagResourceAuthorization. You will experience an "AccessDenied" message if you attempt to create tagged ECS resources without including the ecs:TagResource action in the IAM policy of the calling principal. This change will affect the following APIs: CreateCapacityProvider, CreateCluster, CreateService, CreateTaskSet, RegisterContainerInstance, RunTask, StartTask, and RegisterTaskDefinition.


Example Deny Response without ecs:TagResource

The following is part of an IAM policy that is granting the ecs:CreateCluster Action. It does not include the ecs:TagResource Action. When tagResourceAuthorization Account setting is on, the example request would return the AccessDeniedException below.


# IAM Policy
“Statement”: [
{
“Sid”: “AllowCreateCluster”,
“Effect”: “Allow”,
“Action”: [
“ecs:CreateCluster”
],
“Resource”: “*”
}
]

# Create Cluster Request
aws ecs create-cluster --cluster-name MyCluster --tags key=key1,value=value1

# Tagging Denied Response
An error occurred (AccessDeniedException) when calling the CreateCluster operation:
User: is not authorized to perform: ecs:TagResource on resource: cluster/MyCluster because no identity-based policy allows the ecs:TagResource action

Required Action:

To ensure your IAM principals continue applying tags to newly created ECS resources on or after March 29, 2024, we strongly recommend adding the following statement(s) to your IAM policies:

Allow Tagging during creation for all ECS Resources
Adding the ecs:TagResource Action as described below would Allow tagging during ECS resource creation [2].


“Statement”: [
{
“Sid”: “AllowTagging”,
“Effect”: “Allow”,
“Action”: [
“ecs:TagResource”
],
“Resource”: “*”
}
]

Allow Tagging during creation for single ECS Resource Type (ECS Cluster)

Using the Conditional statement ecs:CreateAction allow you to limit the tagging to a specific ECS API. The example below grants access to tagging only on the ECS create-cluster API. A request to the ECS API run-task with tags would result in a Deny decision [2].


“Statement”: [
{
“Sid”: “AllowClusterTagging”,
“Effect”: “Allow”,
“Action”: [
“ecs:TagResource”
],
“Resource”: “*”,
“Condition”: {
“StringEquals”: {
“ecs:CreateAction” : “CreateCluster”
}
}
}
]

Timeline:

February 9, 2024 (Completed) - Tagging Authorization is “on” by default. This excludes your account which is allowlisted. The tagResourceAuthorization account setting can be turned on/off to help test your policy compliance.

March 8, 2024 - Tagging Authorization can no longer be turned “off” once it is turned “on”. It is recommended that you test your IAM policies before this date while you are able to toggle the account setting.

March 29, 2024 - Tagging Authorization will be turned on for all AWS accounts. The account level setting will no longer be used and will be removed from the ECS Account Settings page in the AWS Console.

If you have any questions, please contact AWS Support [3].

[1] https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-account-settings.html#tag-resources-setting
[2] https://docs.aws.amazon.com/AmazonECS/latest/developerguide/supported-iam-actions-tagging.html
[3] https://aws.amazon.com/support

通知が来ているアカウントは29日までに対応する必要があります。

確認

aws ecs list-account-settings --effective-settings --name tagResourceAuthorization を実行すると以下のような表示になると思います。ここがonであれば、すでにアカウント単位で有効になってるので影響がありません。(ただし、タグ付きのリソースを新規作成する際には権限が足りないとエラーになる可能性はあります)

ここがoffになっている場合、タグ付け認可が無効になってるので3月29日以降影響を受ける可能性があります。

% aws ecs list-account-settings --effective-settings --name tagResourceAuthorization
{
    "settings": [
        {
            "name": "tagResourceAuthorization",
            "value": "on",
            "principalArn": "arn:aws:iam::xxxxxxxxxxxx:root"
        }
    ]
}

影響がある例

ユースケースにもよりますが、タグ付け認可に関連する操作は以下のようなものが考えられるかと思います

  • インフラ担当者によるECSリソース構築
  • 開発担当者(またはCI/CD)によるECSサービスのデプロイ

前者に関しては、PowerUser相当の強い権限を付与されていることが多くここが問題になることはほとんどど無いかとは思います。

後者の特にCI/CDによるデプロイに問題となることがありえます。一般的に非人間ユーザで目的が明確であれば、最小権限の原則に則り、 ecs:TagResource が付与されていない可能性があります。

トライアンドエラーで権限を付与した場合、過去にうまく動いたためそのままの権限で使い続けている可能性もあります。その場合影響がある可能性あります。

デプロイ時のタスク定義登録の際、タスク定義内に従来なかったtagsの記述を新規追加した際にResgisterTaskDefinitionでエラーになるという事例を私は経験しました。 タスク定義にtagsがないときはタグ付け認可は実行されないのでそのまま成功していたため、ecs:TagResource が必要なことに気づいていませんでした。 エラーとしては以下のような記述になるので、タグ付け認可の機能の存在を知っていて冷静に読み解けば、ecs:TagResource が足りていないことに気づけると思います。

An error occurred (AccessDeniedException) when calling the RegisterTaskDefinition operation: User: arn:aws:sts::xxxx:assumed-role/deploy-github-actions/GitHubActions is not authorized to perform: ecs:TagResource on resource: arn:aws:ecs:ap-northeast-1:xxxx:task-definition/ecs-service because no identity-based policy allows the ecs:TagResource action

対応

まずECSサービスを利用しているIAM RoleとIAM Policyを洗い出します。

その上でそれらが以下のアクションを許可している場合、ecs:TagResource を追加してあげます。

  • CreateCapacityProvider
  • CreateCluster
  • CreateService
  • CreateTaskSet
  • RegisterContainerInstance
  • RegisterTaskDefinition
  • RunTask
  • StartTask

私の場合は、ECSサービスデプロイ用のポリシーに以下のStatementを追加しました。それぞれ適切な記述を足していただけたらと思います。

この場合タスク定義を登録する際にタグ付け認可を通すような許可を追加しています。

        {
            "Action": "ecs:TagResource",
            "Condition": {
                "StringEquals": {
                    "ecs:CreateAction": "RegisterTaskDefinition"
                }
            },
            "Effect": "Allow",
            "Resource": "arn:aws:ecs:ap-northeast-1:xxxxxx:task-definition/yyyyyyyyyyyyyyy",
            "Sid": "RegisterTaskDefinitionWithTag"
        },

まとめ

タグ付け認可について説明しました。タグ付け認可は2024年3月29日に強制的に全アカウントで有効になります。時間が少ないですが、影響受ける可能性があるかどうかチェックしてハマらないようにしましょう。

また、これまでタグ付けしてなかったリソースにタグ付けする際にタグ付け認可に引っかかる可能性があります。

デプロイやリソース作成の際にnot authorized to perform: ecs:TagResource と言われたらこの記事を思い出していただけたらと思います。

それでは良いECSライフを!

関連リソース