Help us improve the AWS re:Post Knowledge Center by sharing your feedback in a brief survey. Your input can influence how we create and update our content to better support your AWS journey.
如何获取数据以帮助排查 IAM 权限的访问遭拒或未经授权的错误?
我在访问 AWS 资源时收到 "access denied" 或 "unauthorized" 错误。我需要数据来帮助排查这些 AWS Identity and Access Management (IAM) API 调用失败错误。
简短描述
使用 Amazon Athena 查询或 AWS 命令行界面 (AWS CLI) 获取 IAM API 调用失败的错误日志。然后,按照说明使用 IAM 策略对访问遭拒或未经授权的操作错误进行故障排除。
解决方法
**注意:**如果在运行 AWS CLI 命令时收到错误,请确保您使用的是最新版本的 AWS CLI。
使用 Athena 查询通过搜索 CloudTrail 日志对 IAM API 调用失败进行故障排除
**注意:**在开始之前,您必须创建跟踪以登录到 Amazon Simple Storage Service (Amazon S3) 存储桶。这是因为 Athena 将使用传送到 Amazon S3 存储桶的 AWS CloudTrail 日志文件中记录的事件进行跟踪。
1. 按照如何在 Athena 中自动创建表以搜索 AWS CloudTrail 日志?的创建 Athena 表部分中的步骤进行操作
**注意:**自动创建的 Athena 表将与您的 Amazon S3 存储桶位于同一 AWS 区域中。
2. 打开 Athena 控制台,然后选择加号 "+" 以创建新查询。
3. 输入以下示例查询,然后选择 Run(运行)。
在此示例查询中,时间格式使用 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 in an identity-based policy | | 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 because no identity-based policy allows the cloudtrail:LookupEvents action |
**注意:**CloudTrail 事件输出可能需要长达 15 分钟才能交付结果。
5. (可选)从示例查询中移除以下行,以便获取所有用户的错误:
AND userIdentity.arn = 'your-arn'
6. (可选)从示例查询中移除以下行,以便获取选定时间段内的所有错误:
AND (lower(errorCode) LIKE '%accessdenied%' OR lower(errorCode) LIKE '%unauthorized%')
使用 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 in an identity-based policy 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 because no identity-based policy allows the cloudtrail:LookupEvents action
3. (可选)移除以下行,以便获取所有用户的错误:
.userIdentity.arn == \"your-arn\" and
4. (可选)移除以下行,以便获取选定时间段内的所有错误:
and (.errorCode | ascii_downcase | (contains(\"accessdenied\") or contains(\"unauthorized\")))
对未经授权的错误进行故障排除
Athena 和上述 AWS CLI 示例输出与 LookupEvents API 调用有关。
IAM 策略包含拒绝语句,因此可用于拒绝访问,该策略会在显式拒绝和隐式拒绝的错误消息中包含特定短语。IAM 显式拒绝错误中包含短语 "with an explicit deny in a <type> policy"。IAM 隐式拒绝错误中包含短语 "because no <type> policy allows the <action> action"。
cloudtrail:LookupEvents 输出包含显式拒绝,指示关联的 IAM 策略不正确。
这些策略类型中的任何一种都可能出现显式拒绝。例如,基于身份的策略、基于资源的策略、权限边界、组织 SCP 以及会话策略。显式拒绝语句始终覆盖允许语句。显式拒绝存在于 IAM 用户基于身份的策略中。
由于基于身份的策略不允许输出,因此 cloudtrail:LookupEvents 输出会表现出基于身份的策略不允许此 API 操作,从而导致隐式拒绝。基于身份的策略对于 cloudtrail:LookupEvents API 操作缺少显式允许语句。
通过 AWS 评估以建立访问的策略类型为:
有关如何评估和管理 IAM 策略的其他信息,请参阅策略评估逻辑和管理 IAM 策略。
相关信息
- 语言
- 中文 (简体)

相关内容
AWS 官方已更新 5 个月前