如何获取数据以帮助排查 IAM 权限访问遭到拒绝或未经授权的问题?

3 分钟阅读
0

我在尝试访问 AWS 资源,收到了“access denied(访问遭到拒绝)”或“unauthorized(未经授权)”错误。如何获取数据来帮助排查这些 AWS Identity and Access Management(IAM)API 调用失败问题?

简短描述

您可以使用 Amazon Athena 查询或 AWS Command Line Interface(AWS CLI)来获取 IAM API 调用失败的错误日志。然后,按照以下说明使用 IAM 策略排查访问遭到拒绝或未授权操作错误

解决方法

**注意:**如果在运行 AWS CLI 命令时收到错误,请确保您使用的是最新的 AWS CLI 版本

选项 1:使用 Athena 查询通过搜索 CloudTrail 日志来排查 IAM API 调用失败问题

**注意:**开始之前,您必须创建跟踪,以记录到 Amazon Simple Storage Service(Amazon S3)存储桶中。这是因为 Athena 使用 AWS CloudTrail 日志文件中记录的事件,这些日志文件将传输到 Amazon S3 存储桶中进行此跟踪。

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%')

选项 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 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 示例输出与 CloudTrail LookupEvents API 调用相关。

因包含 Deny 语句而拒绝访问的 IAM 策略在错误消息中包含特定短语,用于显式和隐式拒绝。IAM 显式拒绝错误包含短语“with an explicit deny in a <type> policy(在“<type>”策略中使用显式拒绝)”。IAM 隐式拒绝错误包含短语“because no <type> policy allows the <action> action(因为没有“<type>”策略允许该“<action>”操作)”。

带有显式拒绝的 cloudtrail:LookupEvents 输出表示关联的 IAM 策略不正确。

这些策略类型中的任何一种都可能发生显式拒绝。例如,基于身份的策略、基于资源的策略、权限边界、企业 SCP 和会话策略。显式拒绝语句始终覆盖允许语句。显式拒绝存在于 IAM 用户基于身份的策略中。

cloudtrail:LookupEvents because no identity-based policy allows 输出表示基于身份的策略不允许此 API 操作,从而导致隐式拒绝。基于身份的策略缺少针对 cloudtrail:LookupEvents API 操作的显式允许语句。

AWS 评估的建立访问权的策略类型有:

有关如何评估和管理 IAM 策略的其他信息,请参阅策略评估逻辑管理 IAM 策略


相关信息

IAM 中的策略和权限

排查 IAM 策略问题

相关视频

AWS 官方
AWS 官方已更新 2 年前