

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# CloudTrail IAM 身份中心的用例
<a name="sso-cloudtrail-use-cases"></a>

IAM Identity Center 发出 CloudTrail 的事件对于各种用例来说可能很有价值。Organizations 可以使用这些事件日志来监控和审核用户在其 AWS 环境中的访问和活动。这有助于合规使用案例，因为日志会记录谁在何时访问了哪些资源的详细信息。您还可以使用这些 CloudTrail 数据进行事件调查，从而使团队能够分析用户行为并跟踪可疑行为。此外，事件历史记录可支持故障排除，让您清晰了解用户权限和配置随时间的变更情况。

以下部分介绍了支撑审计、事件调查和故障排除等工作流程的基础使用案例。

## 在 IAM 身份中心用户发起 CloudTrail 的事件中识别用户
<a name="user-session-iam-identity-center"></a>

IAM Identity Center 会发出两个 CloudTrail 字段，使您能够识别 CloudTrail事件背后的 IAM 身份中心用户，例如登录 IAM 身份中心或 AWS CLI使用 AWS 访问门户，包括管理 MFA 设备：
+ `userId` - 来自 IAM Identity Center 实例身份存储的唯一且不可变的用户标识符。
+ `identityStoreArn` - 包含该用户的身份存储的 Amazon 资源名称（ARN）。

`userID`和`identityStoreArn`字段显示在嵌套在`onBehalfOf`元素内的[https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-event-reference-user-identity.html](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-event-reference-user-identity.html)元素中，如以下示例 CloudTrail事件日志所示。此事件日志展示了 `userIdentity` 类型为“`IdentityCenterUser`”的事件中的这两个字段。您还可以在已认证 IAM Identity Center 用户的事件中找到这些字段，此类事件的 `userIdentity` 类型为“`Unknown`”。您的工作流程应支持这两种类型值。

```
"userIdentity":{
  "type":"IdentityCenterUser",
  "accountId":"111122223333",
  "onBehalfOf": {
    "userId": "544894e8-80c1-707f-60e3-3ba6510dfac1",
    "identityStoreArn": "arn:aws:identitystore::111122223333:identitystore/d-1234567890"
    },
    "credentialId" : "90e292de-5eb8-446e-9602-90f7c45044f7"
  }
```

**提示**  
我们建议您使用`userId`和`identityStoreArn`来识别 IAM Identity Center CloudTrail 事件背后的用户。`userIdentity` 元素下的 `userName` 和 `principalId` 字段已不再提供。如果您的工作流程（例如审计或事件响应）依赖于访问 `username`，则您有两个选择：  
按照 [登录事件 CloudTrail 中的用户名](username-sign-in-cloudtrail-events.md) 中的说明，从 IAM Identity Center 目录中获取用户名。
获取 IAM Identity Center 在登录事件的 `additionalEventData` 元素下发出的 `UserName`。此选项不需要访问 IAM Identity Center 目录。有关更多信息，请参阅 [登录事件 CloudTrail 中的用户名](username-sign-in-cloudtrail-events.md)。

要检索用户的详细信息，包括 `username` 字段，您需要使用用户 ID 和身份存储 ID 作为参数来查询身份存储。您可以通过 [https://docs.aws.amazon.com/singlesignon/latest/IdentityStoreAPIReference/API_DescribeUser.html](https://docs.aws.amazon.com/singlesignon/latest/IdentityStoreAPIReference/API_DescribeUser.html) API 请求或通过 CLI 执行此操作。示例 CLI 命令如下。如果您的 IAM Identity Center 实例位于 CLI 默认区域，可省略 `region` 参数。

```
aws identitystore describe-user \
--identity-store-id  d-1234567890 \
--user-id  544894e8-80c1-707f-60e3-3ba6510dfac1 \
--region your-region-id
```

要确定前面示例中 CLI 命令的身份存储 ID 值，您可以从 `identityStoreArn` 值中提取身份存储 ID。在示例 ARN `arn:aws:identitystore::111122223333:identitystore/d-1234567890` 中，身份存储 ID 为 `d-1234567890`。或者，您可以通过导航至 IAM Identity Center 控制台**设置**区域的**身份存储**选项卡查找身份存储 ID。

如果您正在自动化 IAM Identity Center 目录中的用户查询，建议您估算用户查询频率，并考虑 [IAM Identity Center 对身份存储 API 的限流阈值](limits.md#ssodirectorylimits)。缓存已获取的用户属性有助于您控制查询频率在限流阈值内。

## 关联同一用户会话内的用户事件
<a name="correlating-users-same-session"></a>

登录事件中发出的[`AuthWorkflowID`](understanding-sign-in-events.md)字段允许在 IAM Identity Center 用户会话开始之前跟踪与登录序列相关的所有 CloudTrail 事件。

对于 AWS 访问门户内部的用户操作，该`credentialId`值设置为用于请求操作的 IAM Identity Center 用户会话的 ID。您可以使用此值来识别在 AWS 访问门户中同一个经过身份验证的 IAM Identity Center 用户会话中启动 CloudTrail 的事件。

**注意**  
您无法使用 `credentialId` 将登录事件与后续事件（如 AWS 访问门户的使用）相关联。登录事件中输出的 `credentialId` 字段值仅供内部使用，建议您不要依赖该值。对于通过 OIDC 调用的 [AWS 访问门户事件](sso-info-in-cloudtrail.md#cloudtrail-events-access-portal-operations)，其输出的 `credentialId` 字段值等于访问令牌 ID。

## 在 IAM 身份中心用户发起的事件中识别用户 CloudTrail 后台会话详细信息
<a name="identifying-user-background-session-details"></a>

以下 CloudTrail 事件捕获了 OAuth 2.0 令牌交换的过程，在该过程中，将代表用户交互式会话的现有访问令牌 (the`subjectToken`) 交换为刷新令牌 (the`requestedTokenType`)。刷新令牌允许用户发起的任何长时间运行的作业在用户退出登录后，仍以该用户的权限继续运行。

对于 IAM Identity Cent [er 用户后台会话](user-background-sessions.md)，该 CloudTrail 事件包括元素`resource`中名为的附加`requestParameters`元素。`resource` 参数包含在后台运行的作业的 Amazon 资源名称（ARN）。此元素仅存在于 CloudTrail 事件记录中，不包含在 IAM Identity Center I [CreateTokenWithAM](https://docs.aws.amazon.com/singlesignon/latest/OIDCAPIReference/API_CreateTokenWithIAM.html) API 或 SDK 响应中。

```
{
  "clientId": "EXAMPLE-CLIENT-ID",
  "grantType": "urn:ietf:params:oauth:grant-type:token-exchange",
  "code": "HIDDEN_DUE_TO_SECURITY_REASONS",
  "redirectUri": "https://example.com/callback",
  "assertion": "HIDDEN_DUE_TO_SECURITY_REASONS",
  "subjectToken": "HIDDEN_DUE_TO_SECURITY_REASONS",
  "subjectTokenType": "urn:ietf:params:oauth:token-type:access_token",
  "requestedTokenType": "urn:ietf:params:oauth:token-type:refresh_token",
  "resource": "arn:aws:sagemaker:us-west-2:123456789012:training-job/my-job"
}
```

## 在 IAM Identity Center 与外部目录之间关联用户
<a name="correlating-users"></a>

IAM Identity Center 提供两个用户属性，可用于将其目录中的用户与外部目录（例如 Microsoft Active Directory 和 Okta Universal Directory）中的同一用户相关联。
+ `externalId` - IAM Identity Center 用户的外部标识符。建议您将此标识符映射到外部目录中不可变的用户标识符。请注意，IAM 身份中心不会在中发出此值。 CloudTrail
+ `username` - 客户提供的值，用户通常使用该值登录。该值可能会变更（例如通过 SCIM 更新）。请注意，当身份源为时 Directory Service，IAM Identity Center 发出的用户名与您为进行身份验证而输入的用户名 CloudTrail 相匹配。该用户名无需与 IAM Identity Center 目录中的用户名完全一致。

   如果您有权访问 CloudTrail 事件但没有 IAM Identity Center 目录的访问权限，则可以使用登录时`additionalEventData`元素下方显示的用户名。有关 `additionalEventData` 中用户名的更多详情，请参阅 [登录事件 CloudTrail 中的用户名](username-sign-in-cloudtrail-events.md)。

当身份源为 Directory Service时，这两个用户属性与外部目录中对应用户属性的映射关系在 IAM Identity Center 中定义。有关信息，请参阅 [IAM Identity Center 与外部身份提供者目录之间的属性映射](attributemappingsconcept.md)。 IdPs 在外部，使用 SCIM 的用户有自己的映射。即使您使用 IAM Identity Center 目录作为身份源，也可以使用 `externalId` 属性将安全主体与您的外部目录进行交叉引用。

以下部分说明如何根据用户的 `username` 和 `externalId` 查找 IAM Identity Center 用户。

## 通过用户名和 externalId 查看 IAM Identity Center 用户
<a name="view-username-extid"></a>

对于已知用户名，您可先通过 [https://docs.aws.amazon.com/singlesignon/latest/IdentityStoreAPIReference/API_GetUserId.html](https://docs.aws.amazon.com/singlesignon/latest/IdentityStoreAPIReference/API_GetUserId.html) API 请求获取对应的 `userId`，再发起 [https://docs.aws.amazon.com/singlesignon/latest/IdentityStoreAPIReference/API_DescribeUser.html](https://docs.aws.amazon.com/singlesignon/latest/IdentityStoreAPIReference/API_DescribeUser.html) API 请求，从 IAM Identity Center 目录中获取用户属性（如前例所示）。以下示例展示如何从身份存储中获取特定用户名对应的 `userId`。如果您的 IAM Identity Center 实例位于 CLI 默认区域，可省略 `region` 参数。

```
aws identitystore get-user-id \
    --identity-store d-9876543210 \
    --alternate-identifier '{
      "UniqueAttribute": {
      "AttributePath": "username",
      "AttributeValue": "anyuser@example.com"
        }
          }' \
    --region your-region-id
```

同理，如果已知 `externalId`，也可使用相同机制查询。将前例中的属性路径更新为 `externalId`，并将属性值设置为您要查询的具体 `externalId`。

## 在 Microsoft Active Directory（AD）中查看用户的安全标识符（SID）与 externalId
<a name="view-users-sid-ad"></a>

在某些情况下，IAM Identity Center 会在 CloudTrail 事件`principalId`字段中发出用户的 SID，例如 AWS 访问门户和 OID APIs C 发布的事件。**此类场景正逐步淘汰。**如果需从 AD 中获取唯一用户标识符，建议您的工作流程使用 AD 属性 `objectguid`。您可在 IAM Identity Center 目录的 `externalId` 属性中找到该值。但是，如果您的工作流程需要使用 SID，请从 AD 中检索该值，因为该值无法通过 IAM Identity Center 获得 APIs。

[关联同一用户会话内的用户事件在 IAM Identity Center 与外部目录之间关联用户](#correlating-users) 介绍了如何使用 `externalId` 和 `username` 字段将 IAM Identity Center 用户与外部目录中的对应用户相关联。默认情况下，IAM Identity Center 将 `externalId` 映射到 AD 中的 `objectguid` 属性，此映射关系为固定配置。IAM Identity Center 允许管理员灵活配置 `username` 的映射关系，而非默认映射到 AD 中的 `userprincipalname`。

您可在 IAM Identity Center 控制台中查看这些映射关系。导航到**设置**的**身份源**选项卡，然后在**操作**菜单中选择**管理同步**。在**管理同步**部分，选择**查看属性映射**按钮。

虽然您可以使用 IAM Identity Center 中可用的任何唯一 AD 用户标识符在 AD 中查找用户，但我们建议您在查询中使用 `objectguid`，因为它是一个不可变的标识符。以下示例展示了如何使用 Powershell 查询 Microsoft AD，以使用用户的 `objectguid` 值 `16809ecc-7225-4c20-ad98-30094aefdbca` 来检索用户。该查询的成功响应将包含用户的 SID。

```
Install-WindowsFeature -Name  RSAT-AD-PowerShell
 
  Get-ADUser `
  -Filter {objectGUID -eq  [GUID]::Parse("16809ecc-7225-4c20-ad98-30094aefdbca")} `
  -Properties *
```