蓝盾v7.1接入LDAP登录
通过蓝鲸用户中心配置 LDAP 后,存在登录失败以及用户名需要加域(与当前用户的使用习惯不符)等问题。
背景
按照 蓝盾v7.1生产部署文档 对接 LDAP 服务后用户正常同步,但是登录时报用户密码错误。
问题分析
后台查看 bk-user-api-web 日志,报错如下:
{
"levelname": "ERROR",
"asctime": "2024-05-22 15:15:30,947",
"pathname": "/app/bkuser_core/api/login/views.py",
"lineno": 205,
"funcName": "login",
"process": 530,
"thread": 140333237386568,
"request_id": "ebf27affe3f74e77b961a11df38583e9",
"exc_info": "Traceback (most recent call last):
File \"/app/bkuser_core/api/login/views.py\", line 197, in login
login_class().check(profile, password)
File \"/app/bkuser_core/categories/plugins/ldap/login.py\", line 62, in check
target_dn = self.fetch_dn(user)
File \"/app/bkuser_core/categories/plugins/ldap/login.py\", line 30, in fetch_dn
return force_str(user_info[\"raw_attributes\"][\"entryDN\"][0])
File \"/usr/local/lib/python3.6/site-packages/ldap3/utils/ciDict.py\", line 68, in __getitem__
return self._store[self._case_insensitive_keymap[self._ci_key(key)]]
KeyError: 'entrydn'"
}
报错信息很明显,在 user_info.raw_attributes 里找不到 entryDN 这个 key。即获取用户用于登陆的 login dn 失败,需要修改相关逻辑。
解决方案
修改代码
本地通过 vscode 插件连上 ldap 后,发现用户用于登陆的 login dn 的 key 应该是 dn,修改用于用户登陆的 login dn 逻辑。详细步骤如下:
- 通过部署的配置文件
environments/default/version.yaml找到部署的 bk-user 的版本为:bk-user: "1.4.14-beta.10" - 下载该包到本地
helm pull blueking/bk-user --version 1.4.14-beta.10,解压找到镜像版本:tag: "v2.5.4-beta.10" - 找到 bk-user 该 tag 源码地址:https://github.com/TencentBlueKing/bk-user/tree/v2.5.4-beta.10
- 根据日志找到对应文件
src/api/bkuser_core/categories/plugins/ldap/login.py - 修改 fetch_dn 函数的实现,将
entryDN修改为dn。
@staticmethod
def fetch_dn(user_info: dict) -> str:
return force_str(user_info["raw_attributes"]["dn"][0])
更新服务
制作镜像:
我们只需要更新 bk-user-api-web 服务所以只需要制作该服务镜像,执行命令 make build-api。
变更模版:
在 environments/default 目录下新建 bkuser-custom-values.yaml.gotmpl 文件使用新的镜像,若已存在则跳过。
api:
image:
registry: your registry
repository: bk-user-api
pullPolicy: IfNotPresent
tag: "v1.0.1"
更新服务:
helmfile -f base-blueking.yaml.gotmpl -l seq=third sync
检查以下容器镜像的变更是否符合预期:
- bk-user-api-beat
- bk-user-api-web
- bk-user-api-worker
总结
登陆失败的问题可以通过修改源码进行修复。
登陆无需加域的临时方案:将用户和组织结构信息同步至默认域,然后查找默认域。需要修改同步逻辑。