

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# Amazon Cognito 登入 AWS CloudTrail
<a name="logging-using-cloudtrail"></a>

Amazon Cognito 已與 服務整合 AWS CloudTrail，此服務提供由 Amazon Cognito AWS 中的使用者、角色或服務所採取之動作的記錄。CloudTrail 將 Amazon Cognito 的 API 呼叫子集擷取為事件，包括來自 Amazon Cognito 主控台的呼叫，以及來自對 Amazon Cognito API 操作發出的程式碼呼叫。如果您建立追蹤，就可以選擇將 CloudTrail 事件傳遞至 Amazon S3 儲存貯體，包括 Amazon Cognito 的事件。即使您未設定追蹤，依然可以透過 CloudTrail 主控台中的 **Event history (事件歷史記錄)** 檢視最新事件。您可以利用 CloudTrail 所收集的資訊來判斷向 Amazon Cognito 發出的請求，以及發出請求的 IP 地址、人員、時間和其他詳細資訊。

若要進一步了解 CloudTrail，包括如何設定及啟用，請參閱[《AWS CloudTrail 使用者指南》](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/)。

您也可以針對特定 CloudTrail 事件建立 Amazon CloudWatch 警示。例如，您可以設定 CloudWatch 在身分集區組態變更時觸發警示。如需詳細資訊，請參閱[建立 CloudTrail 事件的 CloudWatch 警示：範例](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudwatch-alarms-for-cloudtrail.html)。

**Topics**
+ [Amazon Cognito 傳送至 CloudTrail 的資訊](#amazon-cognito-info-in-cloudtrail)
+ [使用 Amazon CloudWatch Logs Insights 分析 Amazon Cognito CloudTrail 事件](#analyzingcteventscwinsight)
+ [Amazon Cognito 事件範例](understanding-amazon-cognito-entries.md)

## Amazon Cognito 傳送至 CloudTrail 的資訊
<a name="amazon-cognito-info-in-cloudtrail"></a>

當您建立 時，CloudTrail 會開啟 AWS 帳戶。當 Amazon Cognito 中發生支援的事件活動時，該活動會與**事件歷史記錄**中的其他 AWS 服務事件一起記錄在 CloudTrail 事件中。您可以在 AWS 帳戶中檢視、搜尋和下載最近的事件。如需詳細資訊，請參閱[使用 CloudTrail 事件歷史記錄檢視事件](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/view-cloudtrail-events.html)。

若要持續記錄您 AWS 帳戶中的事件，包括 Amazon Cognito 的事件，請建立追蹤。CloudTrail 追蹤將日誌檔案傳遞至 Amazon S3 儲存貯體。根據預設，當您在主控台建立線索時，線索會套用到所有 區域。線索會記錄 AWS 分割區中所有區域的事件，並將日誌檔案傳送到您指定的 Amazon S3 儲存貯體。此外，您可以設定其他 AWS 服務，以進一步分析和處理 CloudTrail 日誌中所收集的事件資料。如需詳細資訊，請參閱：
+ [建立追蹤的概觀](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-create-and-update-a-trail.html)
+ [CloudTrail 支援的服務和整合](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-aws-service-specific-topics.html#cloudtrail-aws-service-specific-topics-list)
+ [設定 CloudTrail 的 Amazon SNS 通知](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/getting_notifications_top_level.html)
+ [接收多個區域的 CloudTrail 日誌檔案](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/receive-cloudtrail-log-files-from-multiple-regions.html)和[接收多個帳戶的 CloudTrail 日誌檔案](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-receive-logs-from-multiple-accounts.html)

每一筆事件或日誌專案都會包含產生請求者的資訊。身分資訊可協助您判斷下列事項：
+ 該請求是否使用根或 IAM 使用者憑證提出。
+ 提出該請求時，是否使用了特定角色或聯合身分使用者的暫時安全憑證。
+ 請求是否由其他 AWS 服務提出。

如需詳細資訊，請參閱 [CloudTrail userIdentity 元素](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-event-reference-user-identity.html)。

**中的機密資料 AWS CloudTrail**  
由於使用者集區和身分集區會處理使用者資料，因此 Amazon Cognito 會使用值 `HIDDEN_DUE_TO_SECURITY_REASONS` 隱藏 CloudTrail 事件中的某些私有欄位。如需 Amazon Cognito 未填入事件的欄位範例，請參閱 [Amazon Cognito 事件範例](understanding-amazon-cognito-entries.md)。Amazon Cognito 只會隱藏某些常包含使用者資訊的欄位，例如密碼和權杖。Amazon Cognito 不會對您填入 API 請求中非私有欄位的個人識別資訊執行任何自動偵測或遮蔽。

### 使用者集區事件
<a name="user-pools-cloudtrail-events"></a>

Amazon Cognito 支援將 [User Pool Actions](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_Operations.html) (使用者集區動作) 頁面上列出的所有動作記錄為 CloudTrail 日誌檔案中的事件。Amazon Cognito 會將使用者集區事件記錄到 CloudTrail 做為*管理事件*。

Amazon Cognito 使用者集區 CloudTrail 項目中的 `eventType` 欄位會告訴您應用程式是向 [Amazon Cognito 使用者集區 API](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/Welcome.html) 還是為 [ OpenID Connect、SAML 2.0 或受管登入頁面提供資源的端點](cognito-userpools-server-contract-reference.md)提出請求。API 請求具有 `AwsApiCall` `eventType`，端點請求具有 `AwsServiceEvent` `eventType`。

Amazon Cognito 會將下列請求記錄到您的受管登入服務，做為 CloudTrail 中的事件。

------
#### [ Hosted UI (classic) events ]


**CloudTrail 中的託管 UI （傳統） 事件**  

| 作業 | Description | 
| --- | --- | 
| Login\_GET, CognitoAuthentication | 使用者檢視或提交憑證至您的 [登入端點](login-endpoint.md)。 | 
| OAuth2\_Authorize\_GET, Beta\_Authorize\_GET | 使用者檢視您的 [授權端點](authorization-endpoint.md)。 | 
| OAuth2Response\_GET, OAuth2Response\_POST | 使用者向您的 /oauth2/idpresponse 端點提交 IdP 權杖。 | 
| SAML2Response\_POST, Beta\_SAML2Response\_POST | 使用者向您的 /saml2/idpresponse 端點提交 IdP SAML 斷言。 | 
| Login\_OIDC\_SAML\_POST | 使用者在您的 [登入端點](login-endpoint.md) 輸入使用者名稱以及相匹配的 [IdP 識別碼](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-integrating-3rd-party-saml-providers.html)。 | 
| Token\_POST, Beta\_Token\_POST | 使用者向您的 [權杖端點](token-endpoint.md) 提交授權碼。 | 
| Signup\_GET, Signup\_POST | 使用者向您的 /signup 端點提交註冊資訊。 | 
| Confirm\_GET, Confirm\_POST | 使用者在託管 UI 中提交確認碼。 | 
| ResendCode\_POST | 使用者在託管 UI 中提交重新發送確認碼的請求。 | 
| ForgotPassword\_GET, ForgotPassword\_POST | 使用者向您的 /forgotPassword 端點提交重設密碼的請求。 | 
| ConfirmForgotPassword\_GET, ConfirmForgotPassword\_POST | 使用者向確認其 ForgotPassword 請求的您的 /confirmForgotPassword 端點提交代碼。 | 
| ResetPassword\_GET, ResetPassword\_POST | 使用者在託管的 UI 中提交新密碼。 | 
| Mfa\_GET, Mfa\_POST | 使用者在託管的 UI 中提交多重要素驗證 (MFA) 代碼。 | 
| MfaOption\_GET, MfaOption\_POST | 使用者在託管的 UI 中選擇他們偏好的 MFA 方法。 | 
| MfaRegister\_GET, MfaRegister\_POST | 使用者在託管的 UI 中提交多重要素驗證 (MFA) 代碼。 | 
| Logout | 使用者登出您的 /logout 端點。 | 
| SAML2Logout\_POST | 使用者登出您的 /saml2/logout 端點。 | 
| Error\_GET | 使用者在託管的 UI 中檢視錯誤頁面。 | 
| UserInfo\_GET, UserInfo\_POST | 使用者或 IdP 與您的 [userInfo 端點](userinfo-endpoint.md) 交換資訊。 | 
| Confirm\_With\_Link\_GET | 使用者根據 Amazon Cognito 在電子郵件中傳送的連結提交確認。 | 
| Event\_Feedback\_GET | 使用者向 Amazon Cognito 提交有關[威脅防護](cognito-user-pool-settings-threat-protection.md)事件的意見回饋。 | 

------
#### [ Managed login events ]


**CloudTrail 中的受管登入事件**  

| 作業 | Description | 
| --- | --- | 
| login\_POST | 使用者提交登入資料到您的 [登入端點](login-endpoint.md)。 | 
| login\_continue\_POST | 已經登入一次的使用者選擇再次登入。 | 
| forgotPassword\_POST | 使用者重設密碼。 | 
| selectChallenge\_POST | 使用者在提交使用者名稱或登入資料後回應身分驗證挑戰。 | 
| confirmUser\_GET | 使用者會在[確認或驗證電子郵件訊息](signing-up-users-in-your-app.md)中開啟連結。 | 
| mfa\_back\_POST | 使用者會在 MFA 提示後選擇返回按鈕。 | 
| mfa\_options\_POST | 使用者選取 MFA 選項。 | 
| mfa\_phone\_register\_POST | 使用者提交要註冊為 MFA 因素的電話號碼。此操作會導致 Amazon Cognito 將 MFA 代碼傳送到其電話號碼。 | 
| mfa\_phone\_verify\_POST | 使用者提交傳送到其電話號碼的 MFA 代碼。 | 
| mfa\_phone\_resendCode\_POST | 使用者提交請求，以將 MFA 代碼重新傳送至其電話號碼。 | 
| mfa\_totp\_POST | 使用者提交 TOTP MFA 代碼。 | 
| signup\_POST | 使用者提交資訊到您的/signup受管登入頁面。 | 
| signup\_confirm\_POST | 使用者從電子郵件或簡訊提交確認碼。 | 
| verifyCode\_POST | 使用者提交一次性密碼 (OTP) 以進行無密碼身分驗證。 | 
| passkeys\_add\_POST | 使用者提交註冊新通行金鑰登入資料的請求。 | 
| passkeys\_add\_GET | 使用者導覽至可註冊通行金鑰的頁面。 | 
| login\_passkey\_POST | 使用者使用通行金鑰登入。 | 

------

**注意**  
Amazon Cognito 會在 CloudTrail 日誌中，記錄特定使用者請求的 `UserSub` 而不是 `UserName`。您可以呼叫 `ListUsers` API並使用子使用者篩選條件，找到特定 `UserSub` 的使用者。

### 身分集區事件
<a name="identity-pools-cloudtrail-events"></a>

**資料事件**

Amazon Cognito 會將下列 Amazon Cognito 身分事件記錄至 CloudTrail 作為資料事件。[資料事件](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/logging-data-events-with-cloudtrail.html#logging-data-events)是 CloudTrail 預設不會記錄的大容量資料平面 API 操作。資料事件需支付額外的費用。
+ [https://docs.aws.amazon.com/cognitoidentity/latest/APIReference/API_GetCredentialsForIdentity.html](https://docs.aws.amazon.com/cognitoidentity/latest/APIReference/API_GetCredentialsForIdentity.html)
+ [https://docs.aws.amazon.com/cognitoidentity/latest/APIReference/API_GetId.html](https://docs.aws.amazon.com/cognitoidentity/latest/APIReference/API_GetId.html)
+ [https://docs.aws.amazon.com/cognitoidentity/latest/APIReference/API_GetOpenIdToken.html](https://docs.aws.amazon.com/cognitoidentity/latest/APIReference/API_GetOpenIdToken.html)
+ [https://docs.aws.amazon.com/cognitoidentity/latest/APIReference/API_GetOpenIdTokenForDeveloperIdentity.html](https://docs.aws.amazon.com/cognitoidentity/latest/APIReference/API_GetOpenIdTokenForDeveloperIdentity.html)
+ [https://docs.aws.amazon.com/cognitoidentity/latest/APIReference/API_UnlinkIdentity.html](https://docs.aws.amazon.com/cognitoidentity/latest/APIReference/API_UnlinkIdentity.html)

若要為這些 API 操作產生 CloudTrail 日誌，您必須在您的線索中啟動資料事件，並為 **Cognito 身分集區**選擇事件選取器。如需詳細資訊，請參閱《AWS CloudTrail 使用者指南》**中的[記錄資料事件](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/logging-data-events-with-cloudtrail.html)。

您也可以使用下列 CLI 命令，將身分集區事件選取器新增至線索。

```
aws cloudtrail put-event-selectors --trail-name {{<trail name>}} --advanced-event-selectors \
"{\
   \"Name\": \"Cognito Selector\",\
   \"FieldSelectors\": [\
      {\
         \"Field\": \"eventCategory\",\
         \"Equals\": [\
            \"Data\"\
         ]\
      },\
      {\
         \"Field\": \"resources.type\",\
         \"Equals\": [\
            \"AWS::Cognito::IdentityPool\"\
         ]\
      }\
   ]\
}"
```

**管理事件**

Amazon Cognito 會將剩餘的 Amazon Cognito 身分池 API 操作記錄為管理事件。根據預設，CloudTrail 會記錄管理事件 API 操作。

如需 Amazon Cognito 記錄到 CloudTrail 的 Amazon Cognito 身分池 API 操作清單，請參閱 [Amazon Cognito 身分池 API 參考](https://docs.aws.amazon.com/cognitoidentity/latest/APIReference/API_Operations.html)。

**Amazon Cognito Sync**

Amazon Cognito 將所有 Amazon Cognito Sync API 操作記錄為管理事件。如需 Amazon Cognito 記錄到 CloudTrail 的 Amazon Cognito Sync API 操作清單，請參閱 [Amazon Cognito Sync API 參考](https://docs.aws.amazon.com/cognitosync/latest/APIReference/API_Operations.html)。

## 使用 Amazon CloudWatch Logs Insights 分析 Amazon Cognito CloudTrail 事件
<a name="analyzingcteventscwinsight"></a>

您可以使用 Amazon CloudWatch Logs Insights 搜尋和分析您的 Amazon Cognito CloudTrail 事件。當您將追蹤設定為將事件傳送到 CloudWatch Logs 時，CloudTrail 只會傳送符合您追蹤設定的事件。

若要查詢或研究 Amazon Cognito CloudTrail 事件，請在 CloudTrail 主控台中，確定您在追蹤設定中選取**管理事件**選項，以便您可以監控對 AWS 資源執行的管理操作。想找出您帳戶中的錯誤、異常活動或異常使用者行為時，您可以選擇性選取追蹤設定中的 **Insights 事件** 選項。

### Amazon Cognito 查詢範例
<a name="analyzingcteventscwinsight-samplequeries"></a>

您可以在 Amazon CloudWatch 主控台中使用下列查詢。

**一般查詢**

尋找最近新增的 25 個日誌事件。

```
fields @timestamp, @message | sort @timestamp desc | limit 25
| filter eventSource = "cognito-idp.amazonaws.com"
```

取得最近新增的 25 個日誌事件 (包含例外狀況) 清單。

```
fields @timestamp, @message | sort @timestamp desc | limit 25
| filter eventSource = "cognito-idp.amazonaws.com" and @message like /Exception/
```

**例外狀況和錯誤查詢**

使用錯誤碼 `NotAuthorizedException` 及 Amazon Cognito 使用者集區 `sub`，尋找最近新增的 25 個日誌事件。

```
fields @timestamp, additionalEventData.sub as user | sort @timestamp desc | limit 25
| filter eventSource = "cognito-idp.amazonaws.com" and errorCode= "NotAuthorizedException"
```

使用 `sourceIPAddress` 和相應 `eventName`，尋找記錄數量。

```
filter eventSource = "cognito-idp.amazonaws.com"
| stats count(*) by sourceIPAddress, eventName
```

尋找觸發 `NotAuthorizedException` 錯誤的前 25 個 IP 地址。

```
filter eventSource = "cognito-idp.amazonaws.com" and errorCode= "NotAuthorizedException"
| stats count(*) as count by sourceIPAddress, eventName
| sort count desc | limit 25
```

尋找呼叫 `ForgotPassword` API 的前 25 個 IP 地址。

```
filter eventSource = "cognito-idp.amazonaws.com" and eventName = 'ForgotPassword'
| stats count(*) as count by sourceIPAddress
| sort count desc | limit 25
```