跳转到主要内容

使用Shibboleth IDP为AWS进行SAML登录

项目描述

==============
AWS SAML登录
==============

.. image:: https://travis-ci.org/zalando/aws-saml-login.svg?branch=master
:target: https://travis-ci.org/zalando/aws-saml-login
:alt:构建状态

.. image:: https://coveralls.io/repos/zalando/aws-saml-login/badge.svg
:target: https://coveralls.io/r/zalando/aws-saml-login
:alt:代码覆盖率

.. image:: https://img.shields.io/pypi/dw/aws-saml-login.svg
:目标: https://pypi.python.org/pypi/aws-saml-login/
:替代: PyPI 下载

.. 图像:: https://img.shields.io/pypi/v/aws-saml-login.svg
:目标: https://pypi.python.org/pypi/aws-saml-login/
:替代: 最新 PyPI 版本

.. 图像:: https://img.shields.io/pypi/l/aws-saml-login.svg
:目标: https://pypi.python.org/pypi/aws-saml-login/
:替代: 许可证

此 Python 包提供了一些辅助函数,允许通过使用 `Shibboleth 身份提供者`_ 进行联合登录时,从 STS_ (安全令牌服务) 以编程方式检索临时 AWS 凭证。目前它只支持 Shibboleth IDP。

该实现依赖于对 Shibboleth 重定向页面(HTML 表单)和 AWS 角色选择页面的 HTML 解析。

此包需要 Python 3.4。

安装
============

.. 代码块:: bash

$ sudo pip3 install --upgrade aws-saml-login

用法
=====

.. 代码块:: python

from aws_saml_login import authenticate, assume_role, write_aws_credentials, get_boto3_session

## 对身份提供者进行认证
saml_xml, roles = authenticate('https://shibboleth-idp.example.org', user, password)

print(roles)

## 在这里只使用第一个角色,我们可能会显示一个用户对话框来选择一个
first_role = roles[0]

provider_arn, role_arn, account_name = first_role

## 获取临时 AWS 凭证
key_id, secret, session_token = assume_role(saml_xml, provider_arn, role_arn)

## 写入到 ~/.aws/credentials
write_aws_credentials('default', key_id, secret, session_token)

## 获取 boto3 会话
session = get_boto3_session(key_id, secret, session_token)
ec2 = session.resource('ec2', 'eu-west-1')
iam = session.client('iam')

## 获取默认区域 eu-central-1 的 boto3 会话
session = get_boto3_session(key_id, secret, session_token, 'eu-central-1')
ec2 = session.resource('ec2')

## 获取前 5 个角色的会话
sessions = {}
for role in roles[:5]
provider_arn, role_arn, account_name = role
key_id, secret, session_token, expiration = assume_role(saml_xml, provider_arn, role_arn)
sessions['{} {}'.format(account_name,role_arn.split(':')[-1])] = get_boto3_session(key_id, secret, session_token)

for key in sessions.keys()
print('Key: {} / AccountAlias: {}')
.format(key,
sessions[key].client('iam').list_account_aliases()['AccountAliases']))
## 可以使用 AWS SDK(例如 boto)来调用 AWS 端点

.. _STS: http://docs.aws.amazon.com/STS/latest/UsingSTS/Welcome.html
.. _Shibboleth IDP: http://shibboleth.net/products/identity-provider.html


Shibboleth 配置
========================

.. 代码块:: xml

<rp:RelyingPartyGroup ...>
...
<!-- ========================================== -->
<!-- 元数据配置 -->
<!-- ========================================== -->
<!-- 元数据提供者组合其他元数据提供者 -->
<metadata:MetadataProvider id="ShibbolethMetadata" xsi:type="metadata:ChainingMetadataProvider">
...
<metadata:MetadataProvider id="amazon-webservices" xsi:type="metadata:FileBackedHTTPMetadataProvider"
metadataURL="https://signin.aws.amazon.com/static/saml-metadata.xml"
backingFile="shibboleth-idp/metadata/amazon-webservices.xml">
</metadata:MetadataProvider>
...
</metadata:MetadataProvider>
...
<rp:RelyingParty id="urn:amazon:webservices"
provider="https://myidp.example.org/shibboleth"
defaultSigningCredentialRef="IdPCredential">
<rp:ProfileConfiguration xsi:type="saml:SAML2SSOProfile" includeAttributeStatement="true"
assertionLifetime="PT5M" assertionProxyCount="0"
signResponses="never" signAssertions="always"
encryptAssertions="never" encryptNameIds="never"/>
</rp:RelyingParty>
...
</rp:RelyingPartyGroup>

<resolver:AttributeResolver ...>
...
<!-- ========================================== -->
<!-- AWS 连接器 -->
<!-- ========================================== -->
<resolver:AttributeDefinition id="awsRoles" xsi:type="ad:Mapped" sourceAttributeID="memberof">
<resolver:Dependency ref="corpLDAP"/>
<resolver:AttributeEncoder
xsi:type="enc:SAML2String"
name="https://aws.amazon.com/SAML/Attributes/Role"
friendlyName="Role" />
<ad:ValueMap>
<ad:ReturnValue>arn:aws:iam::$2:saml-provider/Shibboleth,arn:aws:iam::$2:role/Shibboleth-$1</ad:ReturnValue>
<ad:SourceValue ignoreCase="true">cn=([^,]*),ou=Roles,ou=[^,]*?([0-9]+),ou=AWS.*</ad:SourceValue>
</ad:ValueMap>
</resolver:AttributeDefinition>

<resolver:AttributeDefinition id="awsRoleSessionName" xsi:type="ad:Simple" sourceAttributeID="uid">
<resolver:Dependency ref="corpLDAP"/>
<resolver:AttributeEncoder
xsi:type="enc:SAML2String"
name="https://aws.amazon.com/SAML/Attributes/RoleSessionName"
friendlyName="RoleSessionName" />
</resolver:AttributeDefinition>
...
</resolver:AttributeResolver>

<afp:AttributeFilterPolicyGroup ...>
...
<afp:AttributeFilterPolicy id="afP_aws">
<afp:PolicyRequirementRule xsi:type="basic:AttributeRequesterString" value="urn:amazon:webservices" />
<afp:AttributeRule attributeID="transientId">
<afp:PermitValueRule xsi:type="basic:ANY"/>
</afp:AttributeRule>
<afp:AttributeRule attributeID="awsRoles">
<afp:PermitValueRule xsi:type="basic:ANY"/>
</afp:AttributeRule>
<afp:AttributeRule attributeID="awsRoleSessionName">
<afp:PermitValueRule xsi:type="basic:ANY"/>
</afp:AttributeRule>
</afp:AttributeFilterPolicy>
...
</afp:AttributeFilterPolicyGroup>

要登录,您必须使用 Unsolicited/SSO URL 打开正确的 providerId
https://myidp.example.org/profile/SAML2/Unsolicited/SSO?providerId=urn:amazon:webservices


许可证
=======

版权所有 © 2015 Zalando SE

根据 Apache 许可证 2.0 版(以下简称“许可证”);
除非遵守许可证,否则不得使用此文件。
您可以在以下地址获取许可证副本:

https://apache.ac.cn/licenses/LICENSE-2.0

除非适用法律要求或书面同意,否则在许可证下分发的软件
按“原样”基础分发,
不提供任何明示或暗示的保证或条件。
有关许可证下管理权限和
限制的具体语言,请参阅许可证。

项目详情


下载文件

下载适合您平台的应用程序。如果您不确定选择哪一个,请了解有关 安装包 的更多信息。

源代码分发

aws-saml-login-1.0.11.tar.gz (8.2 kB 查看哈希值)

上传时间 源代码

构建分发

aws_saml_login-1.0.11-py3-none-any.whl (10.4 kB 查看哈希值)

上传时间 Python 3

支持者