如何使用 AD FS 向 Active Directory 用户授予访问 API 或 AWS CLI 的权限?

3 分钟阅读
0

我已使用联合身份验证为 Active Directory 用户配置了对 AWS 管理控制台的访问权限。如何使用 Active Directory 联合身份验证服务 (AD FS) 向用户授予对 AWS 命令行界面 (AWS CLI) 的相同访问权限?

简短描述

如果您已允许 SAML 2.0 联合身份用户访问 AWS 管理控制台,则需要编程访问权限的用户还必须有访问密钥和私有密钥。

要获取 AWS Identity and Access Management (IAM) 用户的访问密钥 ID 和秘密访问密钥,您可以:

配置 AWS CLI

-或者-

获取临时凭证,供联合身份用户访问 AWS CLI。

向联合身份用户授予访问权限之前,您必须:

**注意:**如果您为 Directory 用户开启 Multi-Factor Authentication (MFA),则此解决方案不兼容。

解决方法

如果您的身份提供商 (IdP) 已配置为使用集成 Windows 身份验证 (IWA)、NTLM 或 Kerberos(AD FS 2.0 的默认设置),请参阅解决方案 1:如果您的 IdP 配置为使用基于表单的身份验证(AD FS 3.0 和 4.0 的默认设置),请参阅解决方案 2。

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

解决方案 1:将 PowerShell 用于使用 IWA 的 AD FS (PowerShell 2.0)

1.    运行以下命令,以导入 Windows PowerShell 模块:

> Import-Module "C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell\AWSPowerShell.psd1"

2.    运行如下命令,以为 AD FS 终端节点设置变量:

> $Endpoint = 'https://adfs.example.com/adfs/ls/IdpInitiatedSignOn.aspx?loginToRp=urn:amazon:webservices'

**注意:**其中包括 AD FS 登录页面的完整 URL 以及 AWS 的登录统一资源名称 (URN)。 3.    运行如下命令,以设置 SAML 终端节点:

> $EndpointName = Set-AWSSamlEndpoint -Endpoint "$Endpoint" -StoreAs 'ADFS-Login' -AuthenticationType NTLM

**注意:**默认情况下,AD FS 2.0 AuthenticationType 设置为 NTLM。如果在上述 AWS 工具 Cmdlet 中没有为 AuthenticationType 指定值,则 AWS 工具会默认使用 Kerberos。

4.    通过以下方法之一使用存储的终端节点设置对 AD FS IdP 进行身份验证,以获得用户可以代入的角色列表:

使用当前已登录工作站的用户的凭证。

> Set-AWSSamlRoleProfile -StoreAs 'SAMLUser' -EndpointName $EndpointName

-或者-

指定 Active Directory 用户的凭证。

> $Credential = Get-Credential -Message "Enter the domain credentials for the endpoint"
> Set-AWSSamlRoleProfile -EndpointName $EndpointName -NetworkCredential $credential -StoreAs 'SAMLUser'

5.    如果具有多个角色,系统会提示您选择要担任的角色。在您的终端会话中输入字母字符,如下所示:

SAMLUser[A] A - 123456789012:role/ADFS-DevAdmin [B] B - 123456789012:role/ADFS-DevReadOnly [?] Help (default is "A"): A

6.    运行如下所示的命令,确认用户可以使用联合凭证和指定的配置文件来访问 AWS CLI:

Get-IAMSAMLProviderList -ProfileName SAMLUser

解决方案 2:将 Python 用于使用基于表单的身份验证的 AD FS(AD FS 3.0 和 4.0 的默认设置)

1.    将以下模块安装到 Python:

pip install --upgrade boto beautifulsoup4 requests

2.    使用 SAML 2.0 实施适用于联合 API/CLI 访问的常规解决方案,然后从此链接博文的第 4 步中下载脚本。

3.    打开脚本并设置您的首选区域和输出格式。将 adfs.example.com 替换为您的 URL,然后输入 AD FS 服务器的完全限定域名 (FQDN)。

region = 'eu-west-1'
    outputformat = 'json'
    awsconfigfile = '/.aws/credentials'
    idpentryurl = 'https://adfs.example.com/adfs/ls/IdpInitiatedSignOn.aspx?loginToRp=urn:amazon:webservices'

**注意:**如果 AWS 凭证文件有不同的文件路径,请指定该文件路径。

4.    保存更改,运行文件,然后在显示以下字段时填写字段:

bob@Ubuntu64:~$ ./working_samlapi.py
   Username: bob@example.com
   Password: ***********

   Please choose the role you would like to assume:
   [ 0 ]:  arn:aws:iam::123456789012:role/ADFS-DevAdmin
   [ 1 ]:  arn:aws:iam::123456789012:role/ADFS-DevReadOnly

   Selection:  0

   ----------------------------------------------------------------
   Your new access key pair has been stored in the AWS configuration file /home/ec2-user/.aws/credentials under the saml profile.
   Note that it will expire at 2018-03-14T14:57:45Z.
   After this time, you may safely rerun this script to refresh your access key pair.
   To use this credential, call the AWS CLI with the --profile option (e.g. aws --profile saml ec2 describe-instances).
   ----------------------------------------------------------------

5.    成功联合之后,使用新配置的 SAML 配置文件和命令中的 --profile 参数运行命令。

bob@Ubuntu64:~$ aws iam list-saml-providers --profile saml
            
{
  "SAMLProviderList": [
    {
      "CreateDate": "2018-03-14T13:28:24Z",
      "ValidUntil": "2118-03-14T13:28:23Z",
      "Arn": "arn:aws:iam::123456789012:saml-provider/adfs"
    }
  ]
}

相关信息

使用 AWS Tools for PowerShell 配置联合身份

单点登录

相关视频

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