

# 使用 AWS CLI 配置 IAM Identity Center 身份验证
<a name="cli-configure-sso"></a>

本主题提供的说明可帮助您了解如何使用 AWS IAM Identity Center（IAM Identity Center）配置 AWS CLI，以检索运行 AWS CLI 命令所需的凭证。可采用两种主要方式来使用 IAM Identity Center 验证用户身份，从而获取通过 `config` 文件运行 AWS CLI 命令所需的凭证：
+ **（推荐）**SSO 令牌提供程序配置。
+ 遗留的不可刷新配置。

有关使用持有者身份验证（不使用账户 ID 和角色）的信息，请参阅《Amazon CodeCatalyst 用户指南》中**的[进行设置以将 AWS CLI 与 CodeCatalyst 一起使用](https://docs.aws.amazon.com/codecatalyst/latest/userguide/set-up-cli.html)。

**注意**  
有关使用带 AWS CLI 命令的 IAM Identity Center 的指导过程，请参阅[教程：使用 IAM 身份中心在 AWS CLI 中运行 Amazon S3 命令](cli-configure-sso-tutorial.md)。

**主题**
+ [先决条件](#cli-configure-sso-prereqs)
+ [使用 `aws configure sso` 向导配置您的配置文件](#cli-configure-sso-configure)
+ [使用 `aws configure sso-session` 向导仅配置您的 `sso-session` 部分](#cli-configure-sso-session)
+ [使用 `config` 文件手动配置](#cli-configure-sso-manual)
+ [登录到 IAM Identity Center 会话](#cli-configure-sso-login)
+ [使用 IAM Identity Center 配置文件运行命令](#cli-configure-sso-use)
+ [退出 IAM Identity Center 会话](#cli-configure-sso-logout)
+ [问题排查](#cli-configure-sso-tshoot)
+ [相关资源](#cli-configure-sso-resources)

## 先决条件
<a name="cli-configure-sso-prereqs"></a>
+ 安装 AWS CLI。有关更多信息，请参阅 [安装或更新最新版本的 AWS CLI](getting-started-install.md)。
+ 您必须先有权访问 IAM Identity Center 中的 SSO 身份验证。选择下列方法之一来访问您的 AWS 凭证。

### 我尚未通过 IAM Identity Center 确立访问权限
<a name="idc-access"></a>

请按照《AWS IAM Identity Center 用户指南》**的[入门](https://docs.aws.amazon.com/singlesignon/latest/userguide/getting-started.html)中的说明操作。此过程激活 IAM Identity Center，创建管理用户并添加适当的最低权限集。

**注意**  
创建应用最低权限的权限集。除非您的雇主已为此目的创建自定义权限集，否则建议使用预定义的 `PowerUserAccess` 权限集。

退出门户并重新登录以查看您的 AWS 账户、编程访问详细信息以及 `Administrator` 和 `PowerUserAccess` 选项。在使用开发工具包时选择 `PowerUserAccess`。

### 我已通过我的雇主管理的联邦身份提供者（例如 Azure AD 或 Okta）获得对 AWS 的访问权限
<a name="federated-access"></a>

通过身份提供者的门户登录 AWS。如果您的云管理员已向您授予 `PowerUserAccess`（开发人员）权限，您将看到您有权访问的 AWS 账户和您的权限集。在您的权限集名称旁边，可以看到有关使用该权限集手动或以编程方式访问账户的选项。

自定义实现可能会产生不同的体验，例如不同的权限集名称。如果您不确定要使用哪个权限集，请联系 IT 团队以寻求帮助。

### 我已通过雇主管理的 AWS 访问门户获得对 AWS 的访问权限
<a name="accessportal-access"></a>

通过您的 AWS 访问门户登录到 AWS。如果您的云管理员已向您授予 `PowerUserAccess`（开发人员）权限，您将看到您有权访问的 AWS 账户和您的权限集。在您的权限集名称旁边，可以看到有关使用该权限集手动或以编程方式访问账户的选项。

### 我已通过我的雇主管理的联合自定义身份提供者获得对 AWS 的访问权限
<a name="customfederated-access"></a>

请联系您的 IT 团队以寻求帮助。

访问 IAM Identity Center 后，通过执行以下操作收集 IAM Identity Center 信息：

1. 收集运行 `aws configure sso` 所需的 `SSO Start URL` 和 `SSO Region` 值

   1. 在 AWS 访问门户中，选择用于开发的权限集，然后选择**访问密钥**链接。

   1. 在**获取凭证**对话框中，选择与您的操作系统匹配的选项卡。

   1. 选择 **IAM Identity Center 凭证**方法以获取 `SSO Start URL` 和 `SSO Region` 值。

1. 或者，从版本 2.22.0 开始，您可以使用发布者 URL 来代替启动 URL。发布者 URL 位于 AWS IAM Identity Center 控制台上的下列位置之一：
   + 在**控制面板**页面上，发布者 URL 位于设置摘要中。
   + 在**设置**页面上，发布者 URL 位于**身份源**设置中。

1. 有关要注册哪些范围值的信息，请参阅《IAM Identity Center 用户指南》**中的 [OAuth 2.0 访问范围](https://docs.aws.amazon.com/singlesignon/latest/userguide/customermanagedapps-saml2-oauth2.html#oidc-concept)。

## 使用 `aws configure sso` 向导配置您的配置文件
<a name="cli-configure-sso-configure"></a>

**在 AWS CLI 中配置 IAM Identity Center 配置文件：**

1. 在首选终端中运行 `aws configure sso` 命令。

------
#### [ (Recommended) IAM Identity Center ]

   创建会话名称，并提供 IAM Identity Center 启动 URL 或发布者 URL、托管 IAM Identity Center 目录的 AWS 区域和注册范围。

   ```
   $ aws configure sso
   SSO session name (Recommended): my-sso
   SSO start URL [None]: https://my-sso-portal.awsapps.com/start
   SSO region [None]: us-east-1
   SSO registration scopes [None]: sso:account:access
   ```

   要获得双栈支持，请使用双栈 SSO 起始 URL：

   ```
   $ aws configure sso
   SSO session name (Recommended): my-sso
   SSO start URL [None]: https://ssoins-1234567890abcdef.portal.us-east-1.app.aws
   SSO region [None]: us-east-1
   SSO registration scopes [None]: sso:account:access
   ```

   从版本 **2.22.0** 开始，代码交换的证明密钥（PKCE）授权默认用于 AWS CLI，并且必须在带浏览器的设备上使用。要继续使用设备授权，请附加 `--use-device-code` 选项。

   ```
   $ aws configure sso --use-device-code
   ```

------
#### [ Legacy IAM Identity Center ]

   跳过该会话名称并提供 IAM Identity Center 启动 URL 以及托管 Identity Center 目录的 AWS 区域。

   ```
   $ aws configure sso
   SSO session name (Recommended):
   SSO start URL [None]: https://my-sso-portal.awsapps.com/start
   SSO region [None]:us-east-1
   ```

   对于双栈支持：

   ```
   $ aws configure sso
   SSO session name (Recommended):
   SSO start URL [None]: https://ssoins-1234567890abcdef.portal.us-east-1.app.aws
   SSO region [None]:us-east-1
   ```

------

1. AWS CLI 将尝试打开您的默认浏览器，以便登录到您的 IAM Identity Center 账户。此过程可能会提示您允许 AWS CLI 访问您的数据。由于 AWS CLI 基于 SDK for Python 而构建，因此权限消息可能包含 `botocore` 名称的变体。
   + **如果 AWS CLI 无法打开浏览器**，则会显示手动启动登录过程的说明，具体取决于所使用的授权类型。

------
#### [ PKCE authorization ]

     从版本 2.22.0 开始，代码交换的证明密钥（PKCE）授权默认用于 AWS CLI。显示的 URL 是唯一 URL，开头为：
     + IPv4：*https://oidc.us-east-1.amazonaws.com/authorize*
     + 双栈：*https://oidc.us-east-1.api.aws/authorize*

     PKCE 授权 URL 必须在您登录到的同一台设备上打开，并且必须用于带浏览器的设备。

     ```
     Attempting to automatically open the SSO authorization page in your 
     default browser.
     If the browser does not open or you wish to use a different device to 
     authorize the request, open the following URL:
     
     https://oidc.us-east-1.amazonaws.com/authorize?<abbreviated>
     ```

------
#### [ Device authorization ]

     对于版本 2.22.0 之前的版本，AWS CLI 默认使用 OAuth 2.0 设备授权。对于更新的版本，您可以使用 `--use-device-code` 选项启用此方法。

     设备授权 URL 无需在您登录到的同一台设备上打开，并且可用于带或不带浏览器的设备。端点格式取决于您的配置：
     + IPv4：*https://device.sso.us-west-2.amazonaws.com/*
     + 双栈：*https://device.sso.us-west-2.api.aws/*

     ```
     If the browser does not open or you wish to use a different device to 
     authorize this request, open the following URL:
     https://device.sso.us-west-2.amazonaws.com/
     
     Then enter the code:
     QCFK-N451
     ```

------

1. 从所示列表中选择要使用的 AWS 账户。如果您只被授权使用一个账户，AWS CLI 会自动选择该账户并跳过提示。

   ```
   There are 2 AWS accounts available to you.
   > DeveloperAccount, developer-account-admin@example.com (123456789011) 
     ProductionAccount, production-account-admin@example.com (123456789022)
   ```

1. 从所示列表中选择要使用的 IAM 角色。如果只有一个角色可用，AWS CLI 会自动选择该角色并跳过提示。

   ```
   Using the account ID 123456789011
   There are 2 roles available to you.
   > ReadOnly
     FullAccess
   ```

1. 指定[默认输出格式](cli-configure-files.md#cli-config-output)、向其发送命令的[默认 AWS 区域](cli-configure-files.md#cli-config-region)，以及[配置文件的名称](cli-configure-files.md)。如果指定 `default` 作为配置文件名称，此配置文件将变为使用的默认配置文件。在以下示例中，用户输入默认区域、默认输出格式和配置文件的名称。

   ```
   Default client Region [None]: us-west-2<ENTER>
   CLI default output format (json if not specified) [None]: json<ENTER>
   Profile name [123456789011_ReadOnly]: my-dev-profile<ENTER>
   ```

1. 最后一条消息描述了完成的配置文件配置。现在，您可以使用此配置文件请求凭证。使用 `aws sso login` 命令请求和检索运行命令所需的凭证。有关说明，请参阅[登录到 IAM Identity Center 会话](#cli-configure-sso-login)。

### 生成的配置文件
<a name="cli-configure-sso-generated"></a>

执行这些步骤会在 `config` 文件中创建 `sso-session` 部分和命名配置文件，具体如下所示：

------
#### [ IAM Identity Center ]

```
[profile my-dev-profile]
sso_session = my-sso
sso_account_id = 123456789011
sso_role_name = readOnly
region = us-west-2
output = json

[sso-session my-sso]
sso_region = us-east-1
sso_start_url = https://my-sso-portal.awsapps.com/start
sso_registration_scopes = sso:account:access
```

对于双栈支持：

```
[profile my-dev-profile]
sso_session = my-sso
sso_account_id = 123456789011
sso_role_name = readOnly
region = us-west-2
output = json

[sso-session my-sso]
sso_region = us-east-1
sso_start_url = https://ssoins-1234567890abcdef.portal.us-east-1.app.aws
sso_registration_scopes = sso:account:access
```

------
#### [ Legacy IAM Identity Center ]

```
[profile my-dev-profile]
sso_start_url = https://my-sso-portal.awsapps.com/start
sso_region = us-east-1
sso_account_id = 123456789011
sso_role_name = readOnly
region = us-west-2
output = json
```

对于双栈支持：

```
[profile my-dev-profile]
sso_start_url = https://ssoins-1234567890abcdef.portal.us-east-1.app.aws
sso_region = us-east-1
sso_account_id = 123456789011
sso_role_name = readOnly
region = us-west-2
output = json
```

------

## 使用 `aws configure sso-session` 向导仅配置您的 `sso-session` 部分
<a name="cli-configure-sso-session"></a>

**注意**  
此配置与旧版 IAM Identity Center 不兼容。

`aws configure sso-session` 命令会更新 `~/.aws/config` 文件中的 `sso-session` 部分。运行 `aws configure sso-session` 命令并提供 IAM Identity Center 启动 URL 或发布者 URL 以及托管 IAM Identity Center 目录的 AWS 区域。

```
$ aws configure sso-session
SSO session name: my-sso
SSO start URL [None]: https://my-sso-portal.awsapps.com/start
SSO region [None]: us-east-1
SSO registration scopes [None]: sso:account:access
```

要获得双栈支持，请使用双栈 SSO 起始 URL：

```
$ aws configure sso-session
SSO session name: my-sso
SSO start URL [None]: https://ssoins-1234567890abcdef.portal.us-east-1.app.aws
SSO region [None]: us-east-1
SSO registration scopes [None]: sso:account:access
```

## 使用 `config` 文件手动配置
<a name="cli-configure-sso-manual"></a>

IAM Identity Center 配置信息存储在 `config` 文件中，可以使用文本编辑器对其进行编辑。要手动向命名配置文件中添加 IAM Identity Center 支持，必须向 `config` 文件中添加密钥和值。

### IAM Identity Center 配置文件
<a name="cli-configure-sso-manual-config"></a>

`config` 文件的 `sso-session` 部分用于对获取 SSO 访问令牌的配置变量进行分组，然后可以用来获取 AWS 凭证。使用了以下设置：
+ **（必需）**`sso\$1start\$1url`
+ **（必需）**`sso\$1region`
+ `sso\$1account\$1id`
+ `sso\$1role\$1name`
+ `sso\$1registration\$1scopes`

您可以定义 `sso-session` 部分并将其与配置文件相关联。必须在 `sso-session` 部分配置 `sso_region` 和 `sso_start_url` 设置。通常，必须在 `profile` 部分配置 `sso_account_id` 和 `sso_role_name` 设置，以便 SDK 可以请求 SSO 凭证。

以下示例将 SDK 配置为请求 SSO 凭证并支持自动令牌刷新：

```
[profile dev]
sso_session = my-sso
sso_account_id = 111122223333
sso_role_name = SampleRole

[sso-session my-sso]
sso_region = us-east-1
sso_start_url = https://my-sso-portal.awsapps.com/start
```

要获得双栈支持，请使用双栈 SSO 起始 URL 格式：

```
[profile dev]
sso_session = my-sso
sso_account_id = 111122223333
sso_role_name = SampleRole

[sso-session my-sso]
sso_region = us-east-1
sso_start_url = https://ssoins-1234567890abcdef.portal.us-east-1.app.aws
```

这也允许在多个配置文件中重复使用 `sso-session` 配置：

```
[profile dev]
sso_session = my-sso
sso_account_id = 111122223333
sso_role_name = SampleRole

[profile prod]
sso_session = my-sso
sso_account_id = 111122223333
sso_role_name = SampleRole2

[sso-session my-sso]
sso_region = us-east-1
sso_start_url = https://my-sso-portal.awsapps.com/start
```

要获得双栈支持，请使用双栈 SSO 起始 URL 格式：

```
[profile dev]
sso_session = my-sso
sso_account_id = 111122223333
sso_role_name = SampleRole

[profile prod]
sso_session = my-sso
sso_account_id = 111122223333
sso_role_name = SampleRole2

[sso-session my-sso]
sso_region = us-east-1
sso_start_url = https://ssoins-1234567890abcdef.portal.us-east-1.app.aws
```

但是，并不是所有 SSO 令牌配置场景都需要 `sso_account_id` 和 `sso_role_name`。如果您的应用程序仅使用支持持有者身份验证的 AWS 服务，则不需要传统 AWS 凭证。持有者身份验证是一种 HTTP 身份验证方案，它使用称为持有者令牌的安全令牌。在这种情况下，不需要 `sso_account_id` 和 `sso_role_name`。请参阅 AWS 服务的单独指南，以确定其是否支持持有者令牌授权。

此外，可以作为 `sso-session` 的一部分配置注册范围。范围是 OAuth 2.0 中的一种机制，用于限制应用程序对用户账户的访问。应用程序可以请求一个或多个范围，向应用程序签发的访问令牌将仅限于授予的范围。这些范围定义了为已注册的 OIDC 客户端请求授权的权限和客户端检索的访问令牌。以下示例将 `sso_registration_scopes` 设置为提供已列出账户/角色的访问权限：

```
[sso-session my-sso]
sso_region = us-east-1
sso_start_url = https://my-sso-portal.awsapps.com/start
sso_registration_scopes = sso:account:access
```

对于双栈支持：

```
[sso-session my-sso]
sso_region = us-east-1
sso_start_url = https://ssoins-1234567890abcdef.portal.us-east-1.app.aws
sso_registration_scopes = sso:account:access
```

身份验证令牌缓存到 `sso/cache` 目录下的磁盘上，文件名基于会话名称。

### 遗留的 IAM Identity Center 配置文件
<a name="cli-configure-sso-manual-legacy"></a>

**注意**  
使用遗留的不可刷新配置不支持自动令牌刷新。建议使用 SSO 令牌配置。

要手动向命名配置文件中添加 IAM Identity Center 支持，必须将以下键和值添加到 `config` 文件中的配置文件定义中。
+ `sso\$1start\$1url`
+ `sso\$1region`
+ `sso\$1account\$1id`
+ `sso\$1role\$1name`

您可以在 `.aws/config` 文件中包括有效的任何其他键和值。下面是一个 IAM Identity Center 配置文件示例：

```
[profile my-sso-profile]
sso_start_url = https://my-sso-portal.awsapps.com/start
sso_region = us-west-2
sso_account_id = 111122223333
sso_role_name = SSOReadOnlyRole
region = us-west-2
output = json
```

对于双栈支持：

```
[profile my-sso-profile]
sso_start_url = https://ssoins-1234567890abcdef.portal.us-east-1.app.aws
sso_region = us-west-2
sso_account_id = 111122223333
sso_role_name = SSOReadOnlyRole
region = us-west-2
output = json
```

要运行命令，您必须首先[登录到 IAM Identity Center 会话](#cli-configure-sso-login)来请求和检索临时凭证。

有关 `config` 和 `credentials` 文件的更多信息，请参阅[AWS CLI 中的配置和凭证文件设置](cli-configure-files.md)。

## 登录到 IAM Identity Center 会话
<a name="cli-configure-sso-login"></a>

**注意**  
登录过程可能会提示您允许 AWS CLI 访问您的数据。由于 AWS CLI 基于 SDK for Python 而构建，因此权限消息可能包含 `botocore` 名称的变体。

要检索和缓存一组 IAM Identity Center 凭证，请运行以下 AWS CLI 命令，打开您的默认浏览器并验证您的 IAM Identity Center 登录信息。

```
$ aws sso login --profile my-dev-profile
SSO authorization page has automatically been opened in your default browser. 
Follow the instructions in the browser to complete this authorization request.
Successfully logged into Start URL: https://my-sso-portal.awsapps.com/start
```

将缓存您的 IAM Identity Center 会话凭证，并且 AWS CLI 会使用它们安全地检索在配置文件中指定的用于 IAM 角色的 AWS 凭证。

### 如果 AWS CLI 无法打开您的浏览器
<a name="cli-configure-sso-login-browser"></a>

如果 AWS CLI 无法自动打开浏览器，则会显示手动启动登录过程的说明，具体取决于所使用的授权类型。

------
#### [ PKCE authorization ]

从版本 2.22.0 开始，代码交换的证明密钥（PKCE）授权默认用于 AWS CLI。显示的 URL 是唯一 URL，开头为：
+ IPv4：*https://oidc.us-east-1.amazonaws.com/authorize*
+ 双栈：*https://oidc.us-east-1.api.aws/authorize*

PKCE 授权 URL 必须在您登录到的同一台设备上打开，并且必须用于带浏览器的设备。

```
Attempting to automatically open the SSO authorization page in your 
default browser.
If the browser does not open or you wish to use a different device to 
authorize the request, open the following URL:

https://oidc.us-east-1.amazonaws.com/authorize?<abbreviated>
```

------
#### [ Device authorization ]

对于版本 2.22.0 之前的版本，AWS CLI 默认使用 OAuth 2.0 设备授权。对于更新的版本，您可以使用 `--use-device-code` 选项启用此方法。

设备授权 URL 无需在您登录到的同一台设备上打开，并且可用于带或不带浏览器的设备。

```
If the browser does not open or you wish to use a different device to 
authorize this request, open the following URL:
https://device.sso.us-west-2.amazonaws.com/

Then enter the code:
QCFK-N451
```

------

在使用 `aws sso login` 命令的 `--sso-session` 参数登录时，您还可以指定使用哪个 `sso-session` 配置文件。该 `sso-session` 选项不适用于旧版 IAM Identity Center。

```
$ aws sso login --sso-session my-dev-session
```

从版本 2.22.0 开始，默认使用 PKCE 授权。要使用设备授权进行登录，请添加 `--use-device-code` 选项。

```
$ aws sso login --profile my-dev-profile --use-device-code
```

身份验证令牌缓存到 `~/.aws/sso/cache` 目录下的磁盘上，其文件名基于 `sso_start_url`。

## 使用 IAM Identity Center 配置文件运行命令
<a name="cli-configure-sso-use"></a>

登录后，您可以使用您的凭证通过关联的命名配置文件来调用 AWS CLI 命令。以下示例显示使用配置文件的命令：

```
$ aws sts get-caller-identity --profile my-dev-profile
```

只要您登录 IAM Identity Center 并且这些缓存的凭证未过期，AWS CLI 就会在需要时自动续订过期的 AWS 凭证。但是，如果您的 IAM Identity Center 凭证过期，则必须通过再次登录您的 IAM Identity Center 账户来显式续订它们。

## 退出 IAM Identity Center 会话
<a name="cli-configure-sso-logout"></a>

使用完 IAM Identity Center 配置文件后，可以让凭证过期，或运行以下命令，删除缓存的凭证。

```
$ aws sso logout
Successfully signed out of all SSO profiles.
```

## 问题排查
<a name="cli-configure-sso-tshoot"></a>

如果您遇到 AWS CLI 使用问题，请参阅[排查 AWS CLI 错误](cli-chap-troubleshooting.md)以了解故障排除步骤。

## 相关资源
<a name="cli-configure-sso-resources"></a>

其他资源如下所示。
+ [AWS CLI 的 AWS IAM Identity Center 概念](cli-configure-sso-concepts.md)
+ [教程：使用 IAM 身份中心在 AWS CLI 中运行 Amazon S3 命令](cli-configure-sso-tutorial.md)
+ [安装或更新最新版本的 AWS CLI](getting-started-install.md)
+ [AWS CLI 中的配置和凭证文件设置](cli-configure-files.md)
+ 《AWS CLI 版本 2 中参考》中的 [https://docs.aws.amazon.com/cli/latest/reference/configure/sso.html](https://docs.aws.amazon.com/cli/latest/reference/configure/sso.html)**
+ 《AWS CLI 版本 2 中参考》中的 [https://docs.aws.amazon.com/cli/latest/reference/configure/sso-session.html](https://docs.aws.amazon.com/cli/latest/reference/configure/sso-session.html)**
+ 《AWS CLI 版本 2 中参考》中的 [https://docs.aws.amazon.com/cli/latest/reference/sso/login.html](https://docs.aws.amazon.com/cli/latest/reference/sso/login.html)**
+ 《AWS CLI 版本 2 中参考》中的 [https://docs.aws.amazon.com/cli/latest/reference/sso/logout.html](https://docs.aws.amazon.com/cli/latest/reference/sso/logout.html)**
+ 《Amazon CodeCatalyst 用户指南》**中的[设置为将 AWS CLI 与 CodeCatalyst 一起使用](https://docs.aws.amazon.com/codecatalyst/latest/userguide/set-up-cli.html)
+ 《IAM Identity Center 用户指南》**中的 [OAuth 2.0 访问范围](https://docs.aws.amazon.com/singlesignon/latest/userguide/customermanagedapps-saml2-oauth2.html#oidc-concept)
+ 《IAM Identity Center 用户指南》**中的[入门教程](https://docs.aws.amazon.com/singlesignon/latest/userguide/tutorials.html)

# AWS CLI 的 AWS IAM Identity Center 概念
<a name="cli-configure-sso-concepts"></a>

本主题介绍 AWS IAM Identity Center（IAM Identity Center）的关键概念。IAM Identity Center 是一项基于云的 IAM 服务，通过与现有身份提供者（IdP）集成，可简化多个 AWS 账户、SDK 和工具的用户访问管理。它通过集中式用户门户实现安全的单点登录、权限管理和审计，从而简化了组织的身份和访问管理。

**Topics**
+ [什么是 IAM Identity Center](#cli-configure-sso-concepts-what)
+ [术语](#cli-configure-sso-terms)
+ [IAM Identity Center 的工作原理](#cli-configure-sso-concepts-process)
+ [其他资源](#cli-configure-sso-concepts-resources)

## 什么是 IAM Identity Center
<a name="cli-configure-sso-concepts-what"></a>

IAM Identity Center 是一项基于云的身份和访问管理（IAM）服务，可帮助您集中管理对多个 AWS 账户和业务应用程序的访问。

它提供一个用户门户，授权用户可以在该门户中使用其现有的公司凭证访问 AWS 账户和获得相应权限的应用程序。这使组织能够实施一致的安全策略并简化用户访问管理。

无论您使用哪个 IdP，IAM Identity Center 都会将这些区别抽象出来。例如，您可以连接 Microsoft Azure AD，如博客文章 [IAM Identity Center 的下一个演进](https://aws.amazon.com/blogs/aws/the-next-evolution-in-aws-single-sign-on/)中所述。

**注意**  
有关使用持有者身份验证（不使用账户 ID 和角色）的信息，请参阅《Amazon CodeCatalyst 用户指南》中**的[进行设置以将 AWS CLI 与 CodeCatalyst 一起使用](https://docs.aws.amazon.com/codecatalyst/latest/userguide/set-up-cli.html)。

## 术语
<a name="cli-configure-sso-terms"></a>

使用 IAM Identity Center 时的常用术语如下所示：

**身份提供者（IdP）**  
一个身份管理系统，例如 IAM Identity Center、Microsoft Azure AD、Okta 或您自己的公司目录服务。

**AWS IAM Identity Center**  
IAM Identity Center 是 AWS 自有的 IdP 服务。SDK 和工具以前称为 AWS 单点登录，它们保留了 `sso` API 命名空间以实现向后兼容。有关更多信息，请参阅《AWS IAM Identity Center 用户指南》**中的 [IAM Identity Center 重命名](https://docs.aws.amazon.com/singlesignon/latest/userguide/what-is.html#renamed)。

**AWS 访问门户 URL、SSO 启动 URL、启动 URL**  
您组织唯一的 IAM Identity Center URL，用于访问您的授权 AWS 账户、服务和资源。

**发布者 URL**  
您组织唯一的 IAM Identity Center 发布者 URL，用于通过编程方式访问您的授权 AWS 账户、服务和资源。从版本 2.22.0 的 AWS CLI 开始，发布者 URL 可以与启动 URL 互换使用。

**联合身份验证**  
在 IAM Identity Center 和身份提供者之间建立信任以启用单点登录（SSO）的过程。

**AWS 账户**  
您通过 AWS IAM Identity Center 为用户提供访问权的 AWS 账户。

**权限集、AWS 凭证、凭证、sigv4 凭证**  
预定义的权限集合，可以将其分配给用户或组以授予对 AWS 服务 的访问权限。

**注册范围、访问范围、范围**  
范围是 OAuth 2.0 中的一种机制，用于限制应用程序对用户账户的访问。应用程序可以请求一个或多个范围，向应用程序签发的访问令牌将仅限于授予的范围。有关范围的信息，请参阅《IAM Identity Center 用户指南》**中的[访问范围](https://docs.aws.amazon.com/singlesignon/latest/userguide/customermanagedapps-saml2-oauth2.html#oidc-concept)。

**令牌、刷新令牌、访问令牌**  
令牌是在身份验证时向您颁发的临时安全凭证。这些令牌包含有关您的身份和您被授予的权限的信息。  
当您通过 IAM Identity Center 门户访问 AWS 资源或应用程序时，系统会将您的令牌提供给 AWS 以进行身份验证和授权。这使得 AWS 能够验证您的身份，并确保您具有执行所请求操作所需的权限。  
身份验证令牌缓存在磁盘上的 `~/.aws/sso/cache` 目录下，JSON 文件名基于会话名称。

**会话**  
IAM Identity Center 会话是指用户通过身份验证并获得访问 AWS 资源或应用程序的授权的时间段。当用户登录 IAM Identity Center 门户时，即会建立会话，并且该用户的令牌在指定的持续时间内有效。有关设置会话持续时间的更多信息，请参阅《AWS IAM Identity Center 用户指南》**中的[设置会话持续时间](https://docs.aws.amazon.com/singlesignon/latest/userguide/howtosessionduration.html)。  
在会话期间，只要不同 AWS 账户和应用程序的会话保持活动状态，您就可以在这些账户和应用程序之间导航，而无需重新进行身份验证。会话到期后，请重新登录以续订您的访问权限。  
IAM Identity Center 会话可帮助提供无缝的用户体验，同时还可以通过限制用户访问证书的有效性来强制实施最佳安全实践。

**具有 PKCE（代码交换的证明密钥）的授权代码授予**  
从版本 2.22.0 开始，代码交换的证明密钥（PKCE）是适用于带浏览器的设备的 OAuth 2.0 身份验证授予流程。利用 PKCE，可在桌面设备和移动设备上使用 Web 浏览器轻松安全地进行身份验证并获得对您的 AWS 资源的访问权限。这是默认授权行为。有关 PKCE 的更多信息，请参阅《AWS IAM Identity Center 用户指南》**中的[具有 PKCE 的授权代码授予](https://docs.aws.amazon.com/singlesignon/latest/userguide/customermanagedapps-saml2-oauth2.html#auth-code-grant-pkce)。

**设备授权授予**  
适用于带或不带 Web 浏览器的设备的 OAuth 2.0 身份验证授予流程。有关设置会话持续时间的更多信息，请参阅《AWS IAM Identity Center 用户指南》**中的[设备授权授予](https://docs.aws.amazon.com/singlesignon/latest/userguide/customermanagedapps-saml2-oauth2.html#device-auth-grant)。

## IAM Identity Center 的工作原理
<a name="cli-configure-sso-concepts-process"></a>

IAM Identity Center 可与组织的身份提供者（例如 IAM Identity Center、Microsoft Azure AD 或 Okta）集成。用户对该身份提供者进行身份验证，然后 IAM Identity Center 会将这些身份映射到您的 AWS 环境中的相应权限和访问权限。

以下 IAM Identity Center 工作流假设您已将 AWS CLI 配置为使用 IAM Identity Center：

1. 在首选终端中运行 `aws sso login` 命令。

1. 登录 AWS 访问门户，启动新会话。
   + 启动新会话时，您将收到刷新令牌和缓存的访问令牌。
   + 如果您已经有一个活动会话，将重用现有会话并且该会话将在现有会话过期时过期。

1. 根据您在 `config` 文件中设置的配置文件，IAM Identity Center 将采用相应的权限集，以便授予对相关 AWS 账户和应用程序的访问权限。

1. AWS CLI、SDK 和工具使用您担任的 IAM 角色调用 AWS 服务（例如创建 Amazon S3 存储桶），直到该会话过期。

1. 系统每小时检查一次来自 IAM Identity Center 的访问令牌，并使用刷新令牌自动刷新该访问令牌。
   + 如果访问令牌已过期，SDK 或工具将使用刷新令牌来尝试获取新的访问令牌。然后比较这些令牌的会话持续时间，如果刷新令牌未过期，IAM Identity Center 将提供新的访问令牌。
   + 如果刷新令牌已过期，则不会提供新的访问令牌，并且您的会话已结束。

1. 会话将在刷新令牌过期后，或者当您使用 `aws sso logout` 命令手动注销时结束。缓存的凭证将被删除。要继续使用 IAM Identity Center 访问服务，您必须使用 `aws sso login` 命令启动新会话。

## 其他资源
<a name="cli-configure-sso-concepts-resources"></a>

其他资源如下所示。
+ [使用 AWS CLI 配置 IAM Identity Center 身份验证](cli-configure-sso.md)
+ [教程：使用 IAM 身份中心在 AWS CLI 中运行 Amazon S3 命令](cli-configure-sso-tutorial.md)
+ [安装或更新最新版本的 AWS CLI](getting-started-install.md)
+ [AWS CLI 中的配置和凭证文件设置](cli-configure-files.md)
+ 《AWS CLI 版本 2 中参考》中的 [https://docs.aws.amazon.com/cli/latest/reference/configure/sso.html](https://docs.aws.amazon.com/cli/latest/reference/configure/sso.html)**
+ 《AWS CLI 版本 2 中参考》中的 [https://docs.aws.amazon.com/cli/latest/reference/configure/sso-session.html](https://docs.aws.amazon.com/cli/latest/reference/configure/sso-session.html)**
+ 《AWS CLI 版本 2 中参考》中的 [https://docs.aws.amazon.com/cli/latest/reference/sso/login.html](https://docs.aws.amazon.com/cli/latest/reference/sso/login.html)**
+ 《AWS CLI 版本 2 中参考》中的 [https://docs.aws.amazon.com/cli/latest/reference/sso/logout.html](https://docs.aws.amazon.com/cli/latest/reference/sso/logout.html)**
+ 《Amazon CodeCatalyst 用户指南》**中的[设置为将 AWS CLI 与 CodeCatalyst 一起使用](https://docs.aws.amazon.com/codecatalyst/latest/userguide/set-up-cli.html)
+ 《AWS IAM Identity Center 用户指南》**中的 [IAM Identity Center 重命名](https://docs.aws.amazon.com/singlesignon/latest/userguide/what-is.html#renamed)
+ 《IAM Identity Center 用户指南》**中的 [OAuth 2.0 访问范围](https://docs.aws.amazon.com/singlesignon/latest/userguide/customermanagedapps-saml2-oauth2.html#oidc-concept)
+ 《AWS IAM Identity Center 用户指南》**中的[设置会话持续时间](https://docs.aws.amazon.com/singlesignon/latest/userguide/howtosessionduration.html)
+ 《IAM Identity Center 用户指南》**中的[入门教程](https://docs.aws.amazon.com/singlesignon/latest/userguide/tutorials.html)

# 教程：使用 IAM 身份中心在 AWS CLI 中运行 Amazon S3 命令
<a name="cli-configure-sso-tutorial"></a>

本主题介绍如何配置 AWS CLI，通过使用当前 AWS IAM Identity Center（IAM Identity Center）验证用户身份，来获取针对 Amazon Simple Storage Service（Amazon S3）运行 AWS Command Line Interface（AWS CLI）命令的凭证。

**Topics**
+ [步骤 1：使用 IAM Identity Center 进行身份验证](#cli-configure-sso-tutorial-authentication)
+ [步骤 2：收集 IAM Identity Center 信息](#cli-configure-sso-tutorial-gather)
+ [步骤 3：创建 Amazon S3 存储桶](#cli-configure-sso-tutorial-buckets)
+ [第 4 步：安装 AWS CLI](#cli-configure-sso-tutorial-install)
+ [步骤 5：配置 AWS CLI 配置文件](#cli-configure-sso-tutorial-configure)
+ [步骤 6：登录到 IAM Identity Center](#cli-configure-sso-tutorial-login.title)
+ [步骤 7：运行 Amazon S3 命令](#cli-configure-sso-tutorial-commands)
+ [步骤 8：注销 IAM Identity Center](#cli-configure-sso-tutorial-logout)
+ [步骤 9：清理资源](#cli-configure-sso-tutorial-cleanup)
+ [问题排查](#cli-configure-sso-tutorial-tshoot)
+ [其他资源](#cli-configure-sso-tutorial-resources.title)

## 步骤 1：使用 IAM Identity Center 进行身份验证
<a name="cli-configure-sso-tutorial-authentication"></a>

获取在 IAM Identity Center 中访问 SSO 身份验证的权限。选择下列方法之一来访问您的 AWS 凭证。

### 我尚未通过 IAM Identity Center 确立访问权限
<a name="idc-access"></a>

请按照《AWS IAM Identity Center 用户指南》**的[入门](https://docs.aws.amazon.com/singlesignon/latest/userguide/getting-started.html)中的说明操作。此过程激活 IAM Identity Center，创建管理用户并添加适当的最低权限集。

**注意**  
创建应用最低权限的权限集。除非您的雇主已为此目的创建自定义权限集，否则建议使用预定义的 `PowerUserAccess` 权限集。

退出门户并重新登录以查看您的 AWS 账户、编程访问详细信息以及 `Administrator` 和 `PowerUserAccess` 选项。在使用开发工具包时选择 `PowerUserAccess`。

### 我已通过我的雇主管理的联邦身份提供者（例如 Azure AD 或 Okta）获得对 AWS 的访问权限
<a name="federated-access"></a>

通过身份提供者的门户登录 AWS。如果您的云管理员已向您授予 `PowerUserAccess`（开发人员）权限，您将看到您有权访问的 AWS 账户和您的权限集。在您的权限集名称旁边，可以看到有关使用该权限集手动或以编程方式访问账户的选项。

自定义实现可能会产生不同的体验，例如不同的权限集名称。如果您不确定要使用哪个权限集，请联系 IT 团队以寻求帮助。

### 我已通过雇主管理的 AWS 访问门户获得对 AWS 的访问权限
<a name="accessportal-access"></a>

通过您的 AWS 访问门户登录到 AWS。如果您的云管理员已向您授予 `PowerUserAccess`（开发人员）权限，您将看到您有权访问的 AWS 账户和您的权限集。在您的权限集名称旁边，可以看到有关使用该权限集手动或以编程方式访问账户的选项。

### 我已通过我的雇主管理的联合自定义身份提供者获得对 AWS 的访问权限
<a name="customfederated-access"></a>

请联系您的 IT 团队以寻求帮助。

## 步骤 2：收集 IAM Identity Center 信息
<a name="cli-configure-sso-tutorial-gather"></a>

获得对 AWS 访问权限后，通过执行以下操作收集 IAM Identity Center 信息：

1. 收集运行 `aws configure sso` 所需的 `SSO Start URL` 和 `SSO Region` 值

   1. 在 AWS 访问门户中，选择用于开发的权限集，然后选择**访问密钥**链接。

   1. 在**获取凭证**对话框中，选择与您的操作系统匹配的选项卡。

   1. 选择 **IAM Identity Center 凭证**方法以获取 `SSO Start URL` 和 `SSO Region` 值。

1. 或者，从版本 2.22.0 开始，您可以使用新的发布者 URL 来代替启动 URL。发布者 URL 位于 AWS IAM Identity Center 控制台上的下列位置之一：
   + 在**控制面板**页面上，发布者 URL 位于设置摘要中。
   + 在**设置**页面上，发布者 URL 位于**身份源**设置中。

1. 有关要注册哪些范围值的信息，请参阅《IAM Identity Center 用户指南》**中的 [OAuth 2.0 访问范围](https://docs.aws.amazon.com/singlesignon/latest/userguide/customermanagedapps-saml2-oauth2.html#oidc-concept)。

## 步骤 3：创建 Amazon S3 存储桶
<a name="cli-configure-sso-tutorial-buckets"></a>

登录到 AWS 管理控制台，然后通过以下网址打开 Amazon S3 控制台：[https://console.aws.amazon.com/s3/](https://console.aws.amazon.com/s3/)。

在本教程中，创建几个存储桶，以便稍后在列表中检索。

## 第 4 步：安装 AWS CLI
<a name="cli-configure-sso-tutorial-install"></a>

要安装 AWS CLI，请按照适用操作系统的说明操作。有关更多信息，请参阅 [安装或更新最新版本的 AWS CLI](getting-started-install.md)。

安装完成后，可以通过打开首选终端并运行以下命令来验证安装情况。这应该会显示您已安装的 AWS CLI 版本。

```
$ aws --version
```

## 步骤 5：配置 AWS CLI 配置文件
<a name="cli-configure-sso-tutorial-configure"></a>

使用以下方法之一配置您的配置文件

### 使用 `aws configure sso` 向导配置您的配置文件
<a name="li-configure-sso-tutorial-configure-wizard"></a>

`config` 文件的 `sso-session` 部分用于对获取 SSO 访问令牌的配置变量进行分组，然后可以用来获取 AWS 凭证。使用了以下设置：
+ **（必需）**`sso\$1start\$1url`
+ **（必需）**`sso\$1region`
+ `sso\$1account\$1id`
+ `sso\$1role\$1name`
+ `sso\$1registration\$1scopes`

您可以定义 `sso-session` 部分并将其与配置文件相关联。必须在 `sso-session` 部分配置 `sso_region` 和 `sso_start_url` 设置。通常，必须在 `profile` 部分配置 `sso_account_id` 和 `sso_role_name` 设置，以便 SDK 可以请求 SSO 凭证。

以下示例将 SDK 配置为请求 SSO 凭证并支持自动令牌刷新：

```
$ aws configure sso
SSO session name (Recommended): my-sso
SSO start URL [None]: https://my-sso-portal.awsapps.com/start
SSO region [None]: us-east-1
SSO registration scopes [None]: sso:account:access
```

要获得双栈支持，您可以使用双栈 SSO 起始 URL 格式：

```
$ aws configure sso
SSO session name (Recommended): my-sso
SSO start URL [None]: https://ssoins-1234567890abcdef.portal.us-east-1.app.aws
SSO region [None]: us-east-1
SSO registration scopes [None]: sso:account:access
```

从版本 2.22.0 开始，代码交换的证明密钥（PKCE）授权默认用于 AWS CLI，并且必须在带浏览器的设备上使用。要继续使用设备授权，请附加 `--use-device-code` 选项。

```
$ aws configure sso --use-device-code
```

### 使用 `config` 文件手动配置
<a name="cli-configure-sso-tutorial-configure-manual"></a>

`config` 文件的 `sso-session` 部分用于对获取 SSO 访问令牌的配置变量进行分组，然后可以用来获取 AWS 凭证。使用了以下设置：
+ **（必需）**`sso\$1start\$1url`
+ **（必需）**`sso\$1region`
+ `sso\$1account\$1id`
+ `sso\$1role\$1name`
+ `sso\$1registration\$1scopes`

您定义一个 `sso-session` 部分并将其关联到配置文件。必须在 `sso-session` 部分内设置 `sso_region` 和 `sso_start_url`。通常，`sso_account_id` 和 `sso_role_name` 必须在 `profile` 部分进行设置，以便 SDK 可以请求 SSO 凭证。

以下示例将 SDK 配置为请求 SSO 凭证并支持自动令牌刷新：

```
[profile my-dev-profile]
sso_session = my-sso
sso_account_id = 111122223333
sso_role_name = SampleRole

[sso-session my-sso]
sso_region = us-east-1
sso_start_url = https://my-sso-portal.awsapps.com/start
sso_registration_scopes = sso:account:access
```

要获得双栈支持，请使用双栈 SSO 起始 URL 格式：

```
[profile my-dev-profile]
sso_session = my-sso
sso_account_id = 111122223333
sso_role_name = SampleRole

[sso-session my-sso]
sso_region = us-east-1
sso_start_url = https://ssoins-1234567890abcdef.portal.us-east-1.app.aws
sso_registration_scopes = sso:account:access
```

身份验证令牌缓存到 `~/.aws/sso/cache` 目录下的磁盘上，文件名基于会话名称。

## 步骤 6：登录到 IAM Identity Center
<a name="cli-configure-sso-tutorial-login.title"></a>

**注意**  
登录过程可能会提示您允许 AWS CLI 访问您的数据。由于 AWS CLI 基于 SDK for Python 而构建，因此权限消息可能包含 `botocore` 名称的变体。

要检索和缓存您的 IAM Identity Center 凭证，请在 AWS CLI 中运行以下命令，打开您的默认浏览器并验证您的 IAM Identity Center 登录信息。

```
$ aws sso login --profile my-dev-profile
```

从版本 2.22.0 开始，默认使用 PKCE 授权。要使用设备授权进行登录，请添加 `--use-device-code` 选项。

```
$ aws sso login --profile my-dev-profile --use-device-code
```

## 步骤 7：运行 Amazon S3 命令
<a name="cli-configure-sso-tutorial-commands"></a>

要列出您之前创建的存储桶，请使用 [https://docs.aws.amazon.com/cli/latest/reference/s3/ls.html](https://docs.aws.amazon.com/cli/latest/reference/s3/ls.html) 命令。以下示例列出您的所有 Amazon S3 存储桶。

```
$ aws s3 ls
2018-12-11 17:08:50 my-bucket
2018-12-14 14:55:44 my-bucket2
```

## 步骤 8：注销 IAM Identity Center
<a name="cli-configure-sso-tutorial-logout"></a>

使用完您的 IAM Identity Center 配置文件后，可运行以下命令删除缓存凭证。

```
$ aws sso logout
Successfully signed out of all SSO profiles.
```

## 步骤 9：清理资源
<a name="cli-configure-sso-tutorial-cleanup"></a>

完成本教程后，请清理在本教程中创建的所有不再需要的资源，包括 Amazon S3 存储桶。

## 问题排查
<a name="cli-configure-sso-tutorial-tshoot"></a>

如果您遇到 AWS CLI 使用问题，请参阅[排查 AWS CLI 错误](cli-chap-troubleshooting.md)了解故障排除步骤。

## 其他资源
<a name="cli-configure-sso-tutorial-resources.title"></a>

其他资源如下所示。
+ [AWS CLI 的 AWS IAM Identity Center 概念](cli-configure-sso-concepts.md)
+ [使用 AWS CLI 配置 IAM Identity Center 身份验证](cli-configure-sso.md)
+ [安装或更新最新版本的 AWS CLI](getting-started-install.md)
+ [AWS CLI 中的配置和凭证文件设置](cli-configure-files.md)
+ 《AWS CLI 版本 2 中参考》中的 [https://docs.aws.amazon.com/cli/latest/reference/configure/sso.html](https://docs.aws.amazon.com/cli/latest/reference/configure/sso.html)**
+ 《AWS CLI 版本 2 中参考》中的 [https://docs.aws.amazon.com/cli/latest/reference/configure/sso-session.html](https://docs.aws.amazon.com/cli/latest/reference/configure/sso-session.html)**
+ 《AWS CLI 版本 2 中参考》中的 [https://docs.aws.amazon.com/cli/latest/reference/sso/login.html](https://docs.aws.amazon.com/cli/latest/reference/sso/login.html)**
+ 《AWS CLI 版本 2 中参考》中的 [https://docs.aws.amazon.com/cli/latest/reference/sso/logout.html](https://docs.aws.amazon.com/cli/latest/reference/sso/logout.html)**
+ 《Amazon CodeCatalyst 用户指南》**中的[设置为将 AWS CLI 与 CodeCatalyst 一起使用](https://docs.aws.amazon.com/codecatalyst/latest/userguide/set-up-cli.html)
+ 《IAM Identity Center 用户指南》**中的 [OAuth 2.0 访问范围](https://docs.aws.amazon.com/singlesignon/latest/userguide/customermanagedapps-saml2-oauth2.html#oidc-concept)
+ 《IAM Identity Center 用户指南》**中的[入门教程](https://docs.aws.amazon.com/singlesignon/latest/userguide/tutorials.html)