如何取得資料以協助對 IAM 許可存取遭拒或未經授權錯誤進行疑難排解?

3 分的閱讀內容
0

我正在嘗試存取 AWS 資源,但收到「存取遭拒」或「未經授權」錯誤。如何取得資料以協助對這些 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.    輸入下列範例查詢,然後選擇執行

在此範例查詢中,時間格式使用 ISO 8601 基本格式,Z 變數表示 UTC。

**注意:**請將 your-arn 取代為資源的 IAM Amazon Resource Name (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"。IAM 隱含拒絕錯誤會包含片語 "because no <type> policy allows the <action> action"。

cloudtrail:LookupEvents with an explicit deny 輸出表示相關聯的 IAM 政策不正確。

任何這些政策類型都可能發生明確拒絕。例如,身分型政策、資源型政策、許可界限、組織 SCP 和工作階段政策。明確拒絕陳述式一律會覆寫允許陳述式。IAM 使用者身分型政策中存在明確拒絕。

cloudtrail:LookupEvents because no identity-based policy allows 輸出表示身分型政策不允許導致隱含拒絕的此 API 操作。身分型政策缺少 cloudtrail:LookupEvents API 動作的明確允許陳述式。

AWS 會評估以用來建立存取權的政策類型如下:

如需如何評估和管理 IAM 政策的其他資訊,請參閱政策評估邏輯管理 IAM 政策


相關資訊

IAM 中的政策和許可

對 IAM 政策進行疑難排解