如何排查 IAM 访问权限被拒绝或未经授权的问题?
上次更新日期:2021 年 2 月 15 日
我在尝试访问 AWS 资源,收到了“访问被拒绝”或“未经授权”错误。如何排查 AWS Identity and Access Management (IAM) 用户的权限问题?
简短描述
您可以使用 Amazon Athena 查询或 AWS 命令行界面 (AWS CLI) 来排查 IAM 权限 API 调用失败问题。
解决方法
注意:如果在运行 AWS CLI 命令时收到错误,请确保您使用的是最新的 AWS CLI 版本。
选项 1:使用 Athena 查询通过搜索 AWS CloudTrail 日志来排查 IAM 权限 API 调用失败问题
注意:开始之前,您必须启用跟踪,以记录到 Amazon Simple Storage Service (Amazon S3) 存储桶中。这是因为 Athena 使用 CloudTrail 日志文件中记录的事件,这些日志文件将传输到 Amazon S3 存储桶中进行此跟踪。
1. 遵照如何在 Amazon Athena 中自动创建表以搜索 AWS CloudTrail 日志的创建 Athena 表部分中的步骤
注意:自动创建的 Athena 表与您的 Amazon S3 存储桶在同一个 AWS 区域中。
2. 打开 Athena 控制台,选择新查询,然后选择相应对话框以清除示例查询。
3. 输入以下示例查询,然后选择运行查询。
在本示例查询中,时间格式使用 ISO 8601 基本格式,且 UTC 使用变量 Z。
注意:将 your-arn 替换为您资源的 IAM Amazon 资源名称 (ARN),并将 your-table 替换为您的表名称。
SELECT from_iso8601_timestamp(eventTime) AS "Time", useridentity.arn AS "Identity ARN", eventID AS "Event ID",
eventsource AS "Service", eventname AS "Action", errorCode AS "Error", errorMessage AS "Message"
FROM your-table
WHERE from_iso8601_timestamp(eventtime) >= from_iso8601_timestamp('2019-10-29T06:40:00Z')
AND from_iso8601_timestamp(eventtime) < from_iso8601_timestamp('2019-10-29T06:55:00Z')
AND userIdentity.arn = 'your-arn'
AND eventType = 'AwsApiCall'
AND errorCode is not null
AND (lower(errorCode) LIKE '%accessdenied%' OR lower(errorCode) LIKE '%unauthorized%')
ORDER BY eventTime desc
4. 此示例表输出列出了身份 ARN 的权限错误:
| Time | Event ID | Service | Action | Error | Message |
|-----------------------------|--------------------------------------|--------------------------|--------------|--------------|----------------------------------------------------------------------------------------------------------------------|
| 2019-10-29 06:52:45.000 UTC | 0406f0c1-47a8-4f71-8a94-18267b84042a | cloudtrail.amazonaws.com | LookupEvents | AccessDenied | User: arn:aws:iam::account:user/username is not authorized to perform: cloudtrail:LookupEvents with an explicit deny |
| 2019-10-29 06:41:48.000 UTC | 14e5e77c-f682-45e1-8c88-12d15af293dd | cloudtrail.amazonaws.com | LookupEvents | AccessDenied | User: arn:aws:iam::account:user/username is not authorized to perform: cloudtrail:LookupEvents |
注意:CloudTrail 事件输出可能需要多达 15 分钟才能传输结果。
5. 或者,通过将此行从示例查询中删除来获取所有用户的错误:
AND userIdentity.arn = 'your-arn'
6. 或者,通过将此行从示例查询中删除来获取选定时间段的所有错误:
AND (lower(errorCode) LIKE '%accessdenied%' OR lower(errorCode) LIKE '%unauthorized%')
选项 2:使用 AWS CLI 排查 IAM 权限 API 调用失败问题
注意:此 AWS CLI 脚本需要 jq 命令行 JSON 处理器。关于教程和下载说明,请参阅 JSON 输出格式。关于使用 yum 程序包的分配,请运行以下命令:
$ sudo yum install jq
1. 运行以下 AWS CLI 命令:
注意:将 your-arn 替换为您资源的 IAM ARN。
( echo "Time,Identity ARN,Event ID,Service,Action,Error,Message";
aws cloudtrail lookup-events --start-time "2019-10-29T06:40:00Z" --end-time "2019-10-29T06:55:00Z" --query "Events[*].CloudTrailEvent" --output text \
| jq -r ". | select(.userIdentity.arn == \"your-arn\" and .eventType == \"AwsApiCall\" and .errorCode != null
and (.errorCode | ascii_downcase | (contains(\"accessdenied\") or contains(\"unauthorized\"))))
| [.eventTime, .userIdentity.arn, .eventID, .eventSource, .eventName, .errorCode, .errorMessage] | @csv"
) | column -t -s'",'
注意:对 CloudTrail 的查找请求速率限制为每个账户每秒一个请求。如果您超出此限制,则会发生限流错误。
2. 此示例表输出列出了指定时间段内身份 ARN 的权限错误。
注意: 默认时间段为 90 天。
Time Event ID Service Action Error Message
2019-10-29T06:52:45Z 0406f0c1-47a8-4f71-8a94-18267b84042a cloudtrail.amazonaws.com LookupEvents AccessDenied User: arn:aws:iam::account:user/username is not authorized to perform: cloudtrail:LookupEvents with an explicit deny
2019-10-29T06:41:48Z 14e5e77c-f682-45e1-8c88-12d15af293dd cloudtrail.amazonaws.com LookupEvents AccessDenied User: arn:aws:iam::account:user/username is not authorized to perform: cloudtrail:LookupEvents
3. 或者,通过删除此行来获取所有用户的错误:
.userIdentity.arn == \"your-arn\" and
4. 或者,通过删除此行来获取选定时间段的所有错误:
and (.errorCode | ascii_downcase | (contains(\"accessdenied\") or contains(\"unauthorized\")))
排查未经授权错误
Athena 和 AWS CLI 示例输出对 CloudTrail 服务中的 LookupEvents 操作进行调用。
带有显式拒绝的 cloudtrail:LookupEvents 输出表示 IAM 策略不正确。所有 IAM 策略类型均可以返回“显式拒绝”错误。而且显式拒绝语句始终覆盖允许语句。
cloudtrail:LookupEvents 输出表示,错误由隐式拒绝造成。如果发生以下情况,会出现此输出错误:
- IAM 策略显式允许 cloudtrail:LookupEvents 操作。
- 一个身份的最大权限由 IAM 策略进行限制。
- 账户的 AWS Organizations 服务控制策略 (SCP) 缺乏显式允许。
IAM 评估的建立访问权的策略类型有:
- AWS Organizations SCP
- 基于资源的策略
- IAM 权限边界
- 会话策略
- 基于身份的策略