配置 LDAP 认证
你可以使用轻量级目录访问协议(LDAP)来保护对 Apache® Druid 的访问。本文介绍如何使用 LDAP 以及基于 TLS 的 LDAP(LDAPS)来设置 Druid 的认证和授权。本页中的示例展示的是 Active Directory LDAP 系统的配置。
第一步是为 Druid 启用 LDAP 认证和授权。然后将 LDAP 组映射到 Druid 角色,并为这些角色分配权限。完成这些配置后,你还可以选择启用 LDAPS,使 LDAP 流量保密且安全。
前提条件
在开始为 Druid 配置 LDAP 之前,请先测试你的 LDAP 连接并执行一次示例搜索。
检查 LDAP 连接
测试你的 LDAP 连接,确认它可以使用用户凭据正常工作。在后面的步骤中,你将为 Druid 配置 LDAP 认证,并将此用户用作 bindUser。
以下示例命令测试用户 myuser@example.com 的连接。请填入你的 LDAP 服务器 IP 地址。如果你的 LDAP 实例监听的不是 389 端口,请修改端口号。
ldapwhoami -vv -H ldap://ip_address:389 -D "myuser@example.com" -W在提示时输入该用户的密码,并确认命令执行成功。如果失败,请检查以下各项:
- 确保你使用的是 LDAP 实例的正确端口。
- 检查是否有网络防火墙阻止了到 LDAP 端口的连接。
- 查看你的 LDAP 实现细节,确认是否需要在 LDAP 服务器上专门允许 LDAP 客户端。如果需要,请将 Druid Coordinator 服务器添加到允许列表中。
测试 LDAP 搜索
LDAP 连接正常后,搜索一个用户。例如,以下命令在 Active Directory 系统中搜索用户 myuser。sAMAccountName 属性是 Active Directory 特有的,包含已认证用户的身份:
ldapsearch -x -W -H ldap://ip_address:389 -D "cn=admin,dc=example,dc=com" -b "dc=example,dc=com" "(sAMAccountName=myuser)" +结果中的 memberOf 属性显示了该用户所属的组。例如,以下响应表明该用户是 mygroup 组的成员:
memberOf: cn=mygroup,ou=groups,dc=example,dc=com在后面的步骤中,你将使用这些信息把 LDAP 组映射到 Druid 角色。
INFO
Druid 使用 memberOf 属性来确定组成员关系。如果你的 LDAP 服务器不返回该属性,你可以手动将 LDAP 组映射到 Druid 角色,或者配置反向组查找来自动解析组。
为 Druid 配置 LDAP 认证
按照以下步骤将 Druid 配置为使用 LDAP 认证。配置文件的位置请参阅配置参考。
在 LDAP 系统中创建一个用户,该用户既用于 Druid 的内部通信,也用作 LDAP 初始管理员用户。更多信息请参阅安全概述。 在下面的示例中,LDAP 用户为
internal@example.com。在
common.runtime.properties文件中启用druid-basic-security扩展。在
common.runtime.properties文件中添加以下 LDAP 属性,并将其值替换为你自己的值。这些属性的详细信息请参阅 Druid Basic 安全。druid.auth.authenticatorChain=["ldap"] druid.auth.authenticator.ldap.type=basic druid.auth.authenticator.ldap.enableCacheNotifications=true druid.auth.authenticator.ldap.credentialsValidator.type=ldap druid.auth.authenticator.ldap.credentialsValidator.url=ldap://ip_address:port druid.auth.authenticator.ldap.credentialsValidator.bindUser=administrator@example.com druid.auth.authenticator.ldap.credentialsValidator.bindPassword=adminpassword druid.auth.authenticator.ldap.credentialsValidator.baseDn=dc=example,dc=com druid.auth.authenticator.ldap.credentialsValidator.userSearch=(&(sAMAccountName=%s)(objectClass=user)) druid.auth.authenticator.ldap.credentialsValidator.userAttribute=sAMAccountName druid.auth.authenticator.ldap.authorizerName=ldapauth druid.escalator.type=basic druid.escalator.internalClientUsername=internal@example.com druid.escalator.internalClientPassword=internaluserpassword druid.escalator.authorizerName=ldapauth druid.auth.authorizers=["ldapauth"] druid.auth.authorizer.ldapauth.type=basic druid.auth.authorizer.ldapauth.initialAdminUser=internal@example.com druid.auth.authorizer.ldapauth.initialAdminRole=admin druid.auth.authorizer.ldapauth.roleProvider.type=ldap请注意以下几点:
bindUser:用于连接 LDAP 的用户。它应与你测试 LDAP 搜索时使用的用户相同。userSearch:你的 LDAP 搜索语法。userAttribute:用户搜索属性。internal@example.com是你在第 1 步中创建的 LDAP 用户。在本示例中,它同时充当内部客户端用户和初始管理员用户。
INFO
在上面的示例中,Druid escalator 和 LDAP 初始管理员用户被设置为同一个用户:internal@example.com。如果 escalator 设置为其他用户,则必须先按照第 4 步和第 5 步创建组映射并分配初始角色,集群的其余部分才能正常工作。
将你的组映射保存到一个 JSON 文件中。示例文件
groupmap.json如下:{ "name": "mygroupmap", "groupPattern": "CN=mygroup,CN=Users,DC=example,DC=com", "roles": [ "readRole" ] }在本示例中,LDAP 组
mygroup映射到 Druid 角色readRole,该映射的名称为mygroupmap。使用 Druid API 根据你的 JSON 文件创建组映射并分配初始角色。以下示例使用 curl 为 LDAP 组
mygroup创建groupmap.json中定义的映射:curl -i -v -H "Content-Type: application/json" -u internal -X POST -d @groupmap.json http://localhost:8081/druid-ext/basic-security/authorization/db/ldapauth/groupMappings/mygroupmap检查组映射是否创建成功。以下示例请求列出所有组映射:
curl -i -v -H "Content-Type: application/json" -u internal -X GET http://localhost:8081/druid-ext/basic-security/authorization/db/ldapauth/groupMappings
将 LDAP 组映射到 Druid 角色
完成初始设置和映射后,你可以将更多 LDAP 组映射到 Druid 角色。LDAP 组的成员将获得对应 Druid 角色的权限。
创建 Druid 角色
要创建 Druid 角色,你可以使用 Druid REST API 向 Coordinator 进程提交 POST 请求,也可以使用 Druid 控制台。
以下示例使用 localhost 作为 Coordinator 主机、8081 作为端口。请根据你的部署情况修改这些值。
创建名为 readRole 的角色的示例请求:
curl -i -v -H "Content-Type: application/json" -u internal -X POST http://localhost:8081/druid-ext/basic-security/authorization/db/ldapauth/roles/readRole检查 Druid 是否已成功创建该角色。以下示例请求列出所有角色:
curl -i -v -H "Content-Type: application/json" -u internal -X GET http://localhost:8081/druid-ext/basic-security/authorization/db/ldapauth/roles为 Druid 角色添加权限
有了 Druid 角色之后,就可以为其添加权限。以下示例为 wikipedia 数据源添加只读访问权限。
假设有一个名为 perm.json 的文件,其中包含以下 JSON:
[
{ "resource": { "name": "wikipedia", "type": "DATASOURCE" }, "action": "READ" },
{ "resource": { "name": ".*", "type": "STATE" }, "action": "READ" },
{ "resource": {"name": ".*", "type": "CONFIG"}, "action": "READ"}
]以下请求将该 JSON 文件中的权限关联到 readRole 角色:
curl -i -v -H "Content-Type: application/json" -u internal -X POST -d@perm.json http://localhost:8081/druid-ext/basic-security/authorization/db/ldapauth/roles/readRole/permissionsDruid 用户需要 STATE 和 CONFIG 权限才能在 Druid 控制台中查看数据源。如果你只想分配查询权限,可以只使用 perm.json 文件中第一行的 READ 权限。
你还可以用正则表达式的形式提供数据源名称。例如,要授予对所有以 wiki 开头的数据源的访问权限,可以将数据源名称指定为 { "name": "wiki.*" }。
创建组映射
现在你可以将 LDAP 组映射到 Druid 角色了。以下示例请求创建一个名为 mygroupmap 的映射,它假定目录中存在一个名为 mygroup 的组。
{
"name": "mygroupmap",
"groupPattern": "CN=mygroup,CN=Users,DC=example,DC=com",
"roles": [
"readRole"
]
}以下示例请求用于配置该映射——角色映射保存在文件 groupmap.json 中。示例文件的内容请参阅为 Druid 配置 LDAP 认证。
curl -i -v -H "Content-Type: application/json" -u internal -X POST -d @groupmap.json http://localhost:8081/druid-ext/basic-security/authorization/db/ldapauth/groupMappings/mygroupmap要检查组映射是否创建成功,以下请求列出所有组映射:
curl -i -v -H "Content-Type: application/json" -u internal -X GET http://localhost:8081/druid-ext/basic-security/authorization/db/ldapauth/groupMappings以下示例请求返回 mygroupmap 组的详细信息:
curl -i -v -H "Content-Type: application/json" -u internal -X GET http://localhost:8081/druid-ext/basic-security/authorization/db/ldapauth/groupMappings/mygroupmap以下示例请求将角色 queryRole 添加到 mygroupmap 映射中:
curl -i -v -H "Content-Type: application/json" -u internal -X POST http://localhost:8081/druid-ext/basic-security/authorization/db/ldapauth/groupMappings/mygroup/roles/queryrole向 Druid 添加 LDAP 用户并分配角色
只有在以下情况下,你才需要完成这一步:
- 你的 LDAP 用户不属于任何 LDAP 组,或者
- 你希望为某个用户配置额外的 Druid 角色,而这些角色没有映射到该用户所属的 LDAP 组。
将 LDAP 用户 myuser 添加到 Druid 的示例请求:
curl -i -v -H "Content-Type: application/json" -u internal -X POST http://localhost:8081/druid-ext/basic-security/authorization/db/ldapauth/users/myuser将用户 myuser 分配给 queryRole 角色的示例请求:
curl -i -v -H "Content-Type: application/json" -u internal -X POST http://localhost:8081/druid-ext/basic-security/authorization/db/ldapauth/users/myuser/roles/queryRole启用基于 TLS 的 LDAP(LDAPS)
在 Druid 中配置好 LDAP 认证后,你还可以选择使用传输层安全(TLS,其前身为安全套接字层 SSL)技术,使 LDAP 流量保密且安全。
配置 LDAPS 可以在 Druid 和 LDAP 服务器之间建立信任。
前提条件
在开始为 Druid 设置 LDAPS 之前,你必须先为 Druid 配置 LDAP 认证。你还需要:
- 由公共证书颁发机构(CA)签发的证书,或由内部 CA 签发的自签名证书。
- 为 LDAP 服务器签发证书的 CA 的根证书。如果你使用的是常见的公共 CA,该证书可能已经在 Java truststore 中;否则,你需要导入该 CA 的证书。
为 Druid 配置 LDAPS
完成以下步骤为 Druid 设置 LDAPS。配置文件的位置请参阅配置参考。
将 LDAP 服务器的 CA 证书或自签名证书导入到新建的 LDAP 信任库(trust store),或者导入到
common.runtime.properties文件中druid.client.https.trustStorePath属性所指定的信任库。下面的示例演示了 HTTPS 客户端和 LDAP 客户端共用一个密钥库的做法,但如果你愿意,也可以为 LDAP 单独使用一个专用的信任库。
keytool -import -trustcacerts -keystore path/to/cacerts -storepass truststorepassword -alias aliasName -file path/to/certificate.cer将 path/to/cacerts 替换为你的 truststore 路径,将 truststorepassword 替换为你的 truststore 密码,将 aliasName 替换为密钥库的别名,并将 path/to/certificate.cer 替换为你的证书的位置和名称。例如:
keytool -import -trustcacerts -keystore /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/jre/lib/security/cacerts -storepass mypassword -alias myAlias -file /etc/ssl/certs/my-certificate.cer如果 CA 的根证书尚未在 Java truststore 中,请导入它:
keytool -importcert -keystore path/to/cacerts -storepass truststorepassword -alias aliasName -file path/to/certificate.cer将
path/to/cacerts替换为你的 truststore 路径,将truststorepassword替换为你的 truststore 密码,将aliasName替换为密钥库的别名,并将path/to/certificate.cer替换为你的证书的位置和名称。例如:keytool -importcert -keystore /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/jre/lib/security/cacerts -storepass mypassword -alias myAlias -file /etc/ssl/certs/my-certificate.cer在
common.runtime.properties文件的 LDAP 配置部分添加以下内容,并替换为你自己的信任库路径和密码。注意,指向信任库的属性是druid.auth.basic.ssl.trustStorePath,而不是druid.client.https.trustStorePath。无论 HTTPS 客户端和 LDAP 使用同一个信任库,还是 LDAP 使用单独的信任库,都要确保正确的属性指向你导入了 LDAP 证书的那个信任库。druid.auth.basic.ssl.trustStorePath=/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/jre/lib/security/cacerts druid.auth.basic.ssl.protocol=TLS druid.auth.basic.ssl.trustStorePassword=xxxxxx这些属性的详细信息请参阅 Druid Basic 安全。
你还可以选择在
common.runtime.properties文件中配置其他 LDAPS 属性。更多信息请参阅 Druid Basic 安全。重启 Druid。
组搜索反向查找配置
默认情况下,Druid 从 LDAP 用户条目中读取 memberOf 属性来确定组成员关系。有些 LDAP 服务器不返回 memberOf,原因可能是该功能未启用、它被存储为 Java JNDI 无法获取的操作属性,或者组成员关系只存储在组条目本身上。在这些情况下,由于找不到任何组,基于组的授权会拒绝所有请求。
要解决这个问题,可以配置反向组查找,让 Druid 搜索组条目,以找出哪些组包含该用户。在 common.runtime.properties 中添加以下属性:
druid.auth.authenticator.ldap.credentialsValidator.groupBaseDn=ou=Groups,dc=example,dc=com
druid.auth.authenticator.ldap.credentialsValidator.groupSearch=(uniqueMember=%s)其中:
groupBaseDn:存储 LDAP 组的基础 DN。groupSearch:用于查找包含某个用户的组的 LDAP 过滤器。该过滤器必须包含字面量%s占位符,它会被替换为用户的完整 DN(例如uid=myuser,ou=People,dc=example,dc=com)。对于groupOfUniqueNames,请使用(uniqueMember=%s);对于groupOfNames,请使用(member=%s)。如果过滤器不包含%s,Druid 将无法启动,因为这样的过滤器会匹配groupBaseDn下的所有组,并把所有已映射的组角色授予每个用户。
设置了这些属性后,如果用户搜索没有返回 memberOf 属性,Druid 会自动执行反向组查找,并在认证结果中填充 memberOf。授权器会照常处理这些组,无需额外配置。
INFO
如果你的 LDAP 服务器直接返回了 memberOf,则会跳过反向查找。
故障排除提示
以下是一些帮助你排查 LDAP 和 LDAPS 问题的思路。
检查 Coordinator 日志
如果 LDAP 连接无法正常工作,请检查 Coordinator 日志。详情请参阅日志。
检查 Druid escalator 配置
如果 Coordinator 正常工作而集群的其余部分不正常,请检查 escalator 配置。详情请参阅配置参考。你还可以查看其他服务的日志,了解这些服务为什么无法从 Coordinator 获取授权详情。
检查 LDAP 服务器响应时间
如果用户可以登录 Druid 控制台,但登录后的首页显示 401 错误,请检查 LDAP 服务器的响应时间。在拥有大量 LDAP 用户的大型组织中,LDAP 的响应可能较慢,从而导致连接超时。