跳转到主要内容

JupyterHub的LDAP验证器

项目描述

ldapauthenticator

TravisCI (.com) build status Latest PyPI version Latest conda-forge version GitHub Discourse Gitter

JupyterHub的简单LDAP验证插件

安装

您可以使用以下命令从pip安装它

pip install jupyterhub-ldapauthenticator

...或者使用conda

conda install -c conda-forge jupyterhub-ldapauthenticator

注销用户

如果您对JupyterHub的认证设置进行了任何更改,更改了允许登录的用户组(例如更改allowed_groups或甚至只是开启LDAPAuthenticator),您必须更改jupyterhub的cookie密钥,否则之前登录但未注销的用户将继续能够登录!

您可以通过删除 jupyterhub_cookie_secret 文件来实现。请注意,这将使所有当前登录的用户注销。

用法

您可以在 jupyter_config.py 中添加以下行来启用此认证器

c.JupyterHub.authenticator_class = 'ldapauthenticator.LDAPAuthenticator'

必需配置

在使用 LDAP 认证器之前,必须至少设置以下两个配置选项

LDAPAuthenticator.server_address

联系 LDAP 服务器的地址。只需使用纯主机名或 IP 地址,无需端口号或协议前缀。

LDAPAuthenticator.lookup_dnLDAPAuthenticator.bind_dn_template

为了认证用户,我们需要绑定到 LDAP 服务器的相应 DN。DN 可以通过以下方式获得:

  1. 设置 bind_dn_template,这是一个用于从可读用户名生成用户全 DN 的字符串模板列表,或者
  2. lookup_dn 设置为 True,这将对用户名进行反向查找以获取用户的 DN。这是因为某些 LDAP 服务器,如 Active Directory,并不总是使用真实的 DN 进行绑定。
lookup_dn = False

如果 lookup_dn = False,则 bind_dn_template 必须是非空模板列表,用于指定用户所属的组。例如,如果您的 LDAP 数据库中的某些用户 DN 为 uid=Yuvipanda,ou=people,dc=wikimedia,dc=org,而其他用户 DN 为 uid=Mike,ou=developers,dc=wikimedia,dc=org(其中 YuvipandaMike 是用户名),则您会设置此配置项为:

c.LDAPAuthenticator.bind_dn_template = [
    "uid={username},ou=people,dc=wikimedia,dc=org",
    "uid={username},ou=developers,dc=wikimedia,dc=org",
]

不要忘记在设置配置参数时使用前缀 c.!JupyterHub 使用 traitlets 进行配置,其中 c 代表 配置对象

{username} 将扩展为用户提供用户名。

lookup_dn = True
c.LDAPAuthenticator.lookup_dn = True

如果未显式配置 bind_dn_template(即空列表),则将使用从用户名查找动态获取的 DN 值。如果已配置 bind_dn_template,则它将像在 lookup_dn = False 情况下一样使用。

{username} 将扩展为 LDAP 查找返回的 LDAP 对象的完整路径。例如,在 Active Directory 系统上,{username} 可能扩展为类似 CN=First M. Last,OU=An Example Organizational Unit,DC=EXAMPLE,DC=COM 的内容。

此外,当使用 lookup_dn = True 时,需要 user_search_baseuser_attributelookup_dn_user_dn_attributelookup_dn_search_filter 选项,尽管它们的默认值可能对您的用例足够。

可选配置

LDAPAuthenticator.allowed_groups

允许登录的 LDAP 组成员。这必须设置为空列表 [](默认值,以禁用)或设置为包含尝试登录的当前用户 member 属性的完整 DN 列表。

例如,要仅允许组 researcheroperations 中的人员访问,

c.LDAPAuthenticator.allowed_groups = [
    "cn=researcher,ou=groups,dc=wikimedia,dc=org",
    "cn=operations,ou=groups,dc=wikimedia,dc=org",
]

LDAPAuthenticator.valid_username_regex

在将用户名发送到 LDAP 之前,所有用户名都将与此正则表达式进行匹配。这既是一种过滤无效用户名的简单方法,也是防止 LDAP 注入攻击的保护措施。

默认情况下,它查找正则表达式 ^[a-z][.a-z0-9_-]*$,这是大多数 shell 用户名验证器所做的。

LDAPAuthenticator.use_ssl

布尔值,用于指定在联系 LDAP 服务器时是否使用 SSL 加密。如果设置为 False(默认值),LDAPAuthenticator 将尝试使用 StartTLS 升级连接。将其设置为 True 以开始 SSL 连接。

LDAPAuthenticator.server_port

用于联系 LDAP 服务器的端口号。如果没有使用 SSL,默认为 389;如果使用 SSL,则为 636。

LDAPAuthenticator.user_search_base

仅在 lookup_dn=True 时使用。定义在目录中查找用户的搜索基准。

c.LDAPAuthenticator.user_search_base = 'ou=People,dc=example,dc=com'

LDAPAuthenticator.user_attribute

仅在 lookup_dn=True 时使用。定义在您的目录中存储用户用户名的属性。

# Active Directory
c.LDAPAuthenticator.user_attribute = 'sAMAccountName'

# OpenLDAP
c.LDAPAuthenticator.user_attribute = 'uid'

LDAPAuthenticator.lookup_dn_search_filter

如果将 lookup_dn 设置为 True,则如何查询 LDAP 以进行用户名查找。默认值 '({login_attr}={login})' 应该适用于大多数用例。

LDAPAuthenticator.lookup_dn_search_user, LDAPAuthenticator.lookup_dn_search_password

如果将 lookup_dn 设置为 True,则用于用户查找的技术账户。如果 lookup_dn_search_user 和 lookup_dn_search_password 都为 None,则将执行匿名 LDAP 查询。

LDAPAuthenticator.lookup_dn_user_dn_attribute

如果将 lookup_dn 设置为 True,则包含用于构建 DN 字符串的用户名所需的属性。有关此属性的使用方法,请参阅 user_search_base。对于大多数 LDAP 服务器,这是用户名。对于 Active Directory,它是 cn。

LDAPAuthenticator.escape_userdn

如果设置为 True,则在 LDAP 身份验证时对用户dn中的特殊字符进行转义。在某些 LDAP 服务器上,如果用户dn包含像 '(', ')', '' 这样的字符,并且没有进行转义,则身份验证可能会失败。

LDAPAuthenticator.auth_state_attributes

登录后要获取的属性的可选列表。如果找到这些属性,则将作为 auth_state 返回。

LDAPAuthenticator.use_lookup_dn_username

如果设置为 True(默认值),则在 lookup_dn 为 True 时返回构建 DN 字符串时使用的用户名。

在针对 AD 服务器在 Linux 机器上进行身份验证时,这可能会返回与提供的 UNIX 用户名不同的内容。在这种情况下,将此选项设置为 False 可能是解决方案。

兼容性

此代码已在 OpenLDAP 服务器上进行了测试,客户端运行 Python 3.4。欢迎验证此代码与其他 LDAP 设置的良好工作,以及错误报告和补丁,以使其与其他 LDAP 设置兼容!

Active Directory 集成

请使用以下选项进行 AD 集成。这在两种情况下特别有用

  • LDAP 搜索需要有效的用户帐户才能查询用户数据库
  • DN 不包含登录,而是其他字段,如 CN(实际登录位于 sAMAccountName 中,我们需要查找 CN)
c.LDAPAuthenticator.lookup_dn = True
c.LDAPAuthenticator.lookup_dn_search_filter = '({login_attr}={login})'
c.LDAPAuthenticator.lookup_dn_search_user = 'ldap_search_user_technical_account'
c.LDAPAuthenticator.lookup_dn_search_password = 'secret'
c.LDAPAuthenticator.user_search_base = 'ou=people,dc=wikimedia,dc=org'
c.LDAPAuthenticator.user_attribute = 'sAMAccountName'
c.LDAPAuthenticator.lookup_dn_user_dn_attribute = 'cn'
c.LDAPAuthenticator.escape_userdn = False
c.LDAPAuthenticator.bind_dn_template = '{username}'

在上面的设置中,首先将使用技术帐户 ldap_search_user_technical_account 对具有 sAMAccountName=login 的用户进行 LDAP 搜索。然后使用找到的 CN 值构建 DN。

关于本地用户创建的配置说明

目前,LDAPAuthenticator 不支持本地用户创建,因为这不够安全,因为没有为这些创建的用户提供清理方法。因此,在 LDAP 中被禁用的用户将能够访问这些用户很长时间。

作为替代,Linux 对将 LDAP 直接集成到系统用户设置提供了良好的支持,并且用户可以使用 PAM(PAM 不仅在 JupyterHub 中支持,在 ssh 和许多其他工具中也支持)进行登录。您可以在 http://www.tldp.org/HOWTO/archived/LDAP-Implementation-HOWTO/pamnss.html 以及网络上大量其他文档上找到有关如何设置 LDAP 为您的系统提供用户帐户的信息。这些方法非常广泛使用,更加安全,文档也更为丰富。我们建议您使用它们,而不是让 JupyterHub 使用 LDAPAuthenticator 创建本地帐户。

问题 #19 提供了关于本地用户创建的附加讨论。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分发

jupyterhub-ldapauthenticator-1.3.2.tar.gz (10.6 kB 查看哈希值)

上传时间

构建分发

jupyterhub_ldapauthenticator-1.3.2-py3-none-any.whl (11.2 kB 查看哈希值)

上传时间 Python 3

由...

AWSAWS 云计算和安全赞助商 DatadogDatadog 监控 FastlyFastly CDN GoogleGoogle 下载分析 MicrosoftMicrosoft PSF赞助商 PingdomPingdom 监控 SentrySentry 错误日志 StatusPageStatusPage 状态页