一、创建用户和组
必须先创建用户,没有用户无法创建组
1、创建用户
在初始化过程中创建的People创建一个子条目
样板选择默认
ObjectClasses 选择 inetOrgPerson
RDN 选择 User Name(uid)
- cn:zhangsan
- sn:zhangsan
- displayName:张三
- 电子邮件:zhansgan@163.com
- ou:People
- 密码:xxxxxx
- User Name:zhangsan
注意:
1、如果要自定义添加属性可以在上方选择增加新的属性自行更改
2、密码储存方式默认为Clear,在这种方式下,密码将以明文的形式存储在 LDAP 服务器上。这意味着,如果有人能够访问到 LDAP 数据库,他们将能够直接看到用户的密码。推荐使用哈希或加密的方式来存储密码,以确保密码的安全性,这里我们选择使用sha,即使用SHA算法来存储密码的哈希值,当用户设置密码时,密码会被SHA算法处理,生成一个固定长度的散列值。这个散列值被存储在数据库中,而不是原始密码本身。当验证用户身份时,系统会再次对输入的密码进行SHA处理,并与存储的散列值进行比较。如果两者匹配,则验证通过。
同理,创建李四、王五
2、创建组
组可根据实际情况的不同可分为两种逻辑,第一种是以应用分组,例如分为gitlab组、jenkins组等等,第二种是按照部门分组,例如分为开发部、产品部、运维部等等,这里我们使用第一种方式
在初始化过程中创建的Group创建一个子条目
样板选择默认
ObjectClasses选择groupOfUniqueNames
RDN选择cn(cn)
- cn:jumpserver
- uniqueMember:点击放大镜,在目录树中选择人员
使用命令查看人员是否被正确添加导组中,能正确显示出信息即可
# ldapsearch -LL -Y EXTERNAL -H ldapi:/// "(cn=jumpserver)" -b dc=gaoyufu,dc=net uniqueMember
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
version: 1
dn: cn=jumpserver,ou=Group,dc=gaoyufu,dc=net
uniqueMember: uid=lisi,ou=people,dc=gaoyufu,dc=net
二、对接应用
1、GitLab 对接LDAP
官方文档:https://docs.gitlab.com/ee/administration/auth/ldap/index.html
GitLab 默认LDAP 配置没有启用,需要更改配置
# vim /etc/gitlab/gitlab.rb
gitlab_rails['ldap_enabled'] = true
# gitlab_rails['prevent_ldap_sign_in'] = false
gitlab_rails['ldap_servers'] = YAML.load <<-'EOS'
main: # 'main' is the GitLab 'provider ID' of this LDAP server
label: 'openLDAP'
host: '192.168.1.10'
port: 389
uid: 'sn'
bind_dn: 'cn=admin,dc=gaoyufu,dc=net'
password: '123456'
encryption: 'plain' # "start_tls" or "simple_tls" or "plain"
verify_certificates: false
smartcard_auth: false
active_directory: false
allow_username_or_email_login: true
lowercase_usernames: false
block_auto_created_users: true
base: 'ou=People,dc=gaoyufu,dc=net'
user_filter: '(memberOf=cn=gitlab,ou=Groups,dc=gaoyufu,dc=net)'
EOS
# gitlab-ctl reconfigure
参数 | 说明 |
---|---|
label | 这是 LDAP 服务器的标签,用于在 GitLab 中标识它 |
host | LDAP 服务器的地址 |
port | LDAP 服务器的端口 |
uid | 用于从 LDAP 服务器中提取用户名的属性 |
bind_dn | 用于查询 LDAP 目录的用户的 DN(Distinguished Name) |
password | 用于绑定到 LDAP 服务器的用户的密码 |
encryption | LDAP 的加密类型。这里设置为 "plain",表示不使用加密。如果你的 LDAP 服务器支持纯 SSL/TLS 连接,请选择 encryption: "simple_tls" ,如果你的 LDAP 服务器支持 STARTTLS 扩展,请选择 encryption: "start_tls" 。 |
verify_certificates | 是否验证 LDAP 服务器的证书 |
smartcard_auth | 是否启用智能卡认证 |
active_directory | 是否这是一个 Active Directory 服务器 |
allow_username_or_email_login | 是否允许用户使用其用户名或电子邮件地址登录 |
lowercase_usernames | 是否将用户名转换为小写 |
block_auto_created_users | 是否阻止自动创建的用户 |
base | 在 LDAP 目录中搜索用户的起始点。这通常是一个组织单元 (OU) 的 DN |
user_filter | 查询过滤器 |
命令检查gitlab能否读取到openLDAP信息
# gitlab-rake gitlab:ldap:check
Checking LDAP ...
LDAP: ... Server: ldapmain
LDAP authentication... Success
LDAP users with access to your GitLab server (only showing the first 100 results)
DN: uid=lisi,ou=people,dc=gaoyufu,dc=net sn: lisi
Checking LDAP ... Finished
打开登录页面发现多了openLDAP登录方式
注意:当用户第一次使用LDAP登录GitLab时,如果其LDAP电子邮件地址是现有GitLab用户的电子邮件地址时,那么LDAP DN用户将与现有gitlab用户相关联。如果在GitLab的数据库中没有找到LDAP电子邮件属性,就会创建一个新用户。
换句话说,如果现有的GitLab用户希望自己启用LDAP登录,那么他们应该检查他们的GitLab电子邮件地址是否匹配LDAP电子邮件地址,然后通过他们的LDAP凭证登录GitLab。
如果提示Your account is pending approval from your GitLab administrator and hence blocked. Please contact your GitLab administrator if you think this is an error.,表示新用户等待审核,请联系管理员在用户管理中手动批准
2、Jumpserver 对接LDAP
官方文档:https://docs.jumpserver.org/zh/v3/guide/system/authentication/ldap/
https://kb.fit2cloud.com/?p=96
系统设置-认证设置-LDAP
启用LDAP认证
填写LDAP地址、绑定DN、密码和用户OU,用户过滤器和用户属性映射默认不动,如果要添加组信息,可以在用户用户属性映射修改如下,用户组的命名为 AD +LDAP 中新建的用户组名
{
"username": "cn",
"name": "displayName",
"email": "mail",
"groups": "memberOf"
}
注:新增 “groups” 属性时,需要在 “email” 属性后添加 “,”。
设置完成后一定要记得点击提交才会保存设置,然后点击用户导入-同步用户,选择需要的用户进行导入,这时用户就会出现在用户管理中
如果需要限制登录 JumpServer 的用户只允许已存在于用户列表中的用户登录,也就是只允许LDAP中存在的用户登陆,不允许之前数据库中存在的用户登陆,请在系统设置-安全设置-其他中选择仅已存在用户登录
3、Grafana 对接LDAP
1、更新grafana配置文件
# vim grafana.ini
#################################### Auth LDAP ##########################
[auth.ldap]
# 启用 LDAP
enabled = true
config_file = /etc/grafana/ldap.toml
allow_sign_up = true
# 禁用组织角色同步,手动分配组织和角色
skip_org_role_sync = true
# LDAP background sync (Enterprise only)
# At 1 am every day
sync_cron = "0 1 * * *"
active_sync_enabled = true
启用 LDAP 后,默认行为是在 LDAP 身份验证成功后自动创建 Grafana 用户
2、添加ldap配置
# vim ldap.toml
[[servers]]
# Ldap server host (specify multiple hosts space separated)
host = "192.168.1.10"
# Default port is 389 or 636 if use_ssl = true
port = 389
# Set to true if LDAP server should use an encrypted TLS connection (either with STARTTLS or LDAPS)
use_ssl = false
# If set to true, use LDAP with STARTTLS instead of LDAPS
start_tls = false
# The value of an accepted TLS cipher. By default, this value is empty. Example value: ["TLS_AES_256_GCM_SHA384"])
# For a complete list of supported ciphers and TLS versions, refer to: https://go.dev/src/crypto/tls/cipher_suites.go
tls_ciphers = []
# This is the minimum TLS version allowed. By default, this value is empty. Accepted values are: TLS1.1, TLS1.2, TLS1.3.
min_tls_version = ""
# set to true if you want to skip SSL cert validation
ssl_skip_verify = false
# set to the path to your root CA certificate or leave unset to use system defaults
# root_ca_cert = "/path/to/certificate.crt"
# Authentication against LDAP servers requiring client certificates
# client_cert = "/path/to/client.crt"
# client_key = "/path/to/client.key"
# Search user bind dn
bind_dn = "cn=admin,dc=gaoyufu,dc=net"
bind_password = "123456"
# Timeout in seconds. Applies to each host specified in the 'host' entry (space separated).
timeout = 10
# User search filter, for example "(cn=%s)" or "(sAMAccountName=%s)" or "(uid=%s)"
# Allow login from email or username, example "(|(sAMAccountName=%s)(userPrincipalName=%s))"
search_filter = "(cn=%s)"
# An array of base dns to search through
search_base_dns = ["dc=gaoyufu,dc=net"]
# group_search_filter = "(&(objectClass=posixGroup)(memberUid=%s))"
# group_search_filter_user_attribute = "distinguishedName"
# group_search_base_dns = ["ou=groups,dc=grafana,dc=org"]
# Specify names of the LDAP attributes your LDAP uses
[servers.attributes]
member_of = "memberOf"
name = "displayname"
surname = "sn"
username = "cn"
email = "mail"
如果要组织角色一起同步,除了skip_org_role_sync = flase
外,在ldap.toml中添加以下内容,group_dn根据实际情况修改
[[servers.group_mappings]]
group_dn = "cn=superadmins,dc=grafana,dc=org"
org_role = "Admin"
grafana_admin = true # Available in Grafana v5.3 and above
[[servers.group_mappings]]
group_dn = "cn=admins,dc=grafana,dc=org"
org_role = "Admin"
[[servers.group_mappings]]
group_dn = "cn=users,dc=grafana,dc=org"
org_role = "Editor"
[[servers.group_mappings]]
group_dn = "*"
org_role = "Viewer"
设置完成后重启grafana
4、Jenkins 对接LDAP
官方文档:https://plugins.jenkins.io/ldap/
首先,需要在Jenkins系统中安装LDAP插件(默认已安装),然后进入系统管理 ->全局安全配置页面中选择LDAP,配置以下信息:
server:
ldap://10.0.0.101:389
root DN
dc=demo,dc=com
User search base:
ou=People
Group search base:
cn=jenkins
Group membership
nemberOf=cn=jenkins,ou=Group,
Manager DN:
cn=admin,dc=demo,dc=com
Manager Password:
123456
-
**Server:**LDAP服务器的地址,通常是一个IP地址或域名
-
root DN:这里的root DN只是指搜索的根,并非LDAP服务器的root dn。由于LDAP数据库的数据组织结构类似一颗大树,而搜索是递归执行的,理论上,我们如果从子节点(而不是根节点)开始搜索,因为缩小了搜索范围那么就可以获得更高的性能。这里的root DN指的就是这个子节点的DN,当然也可以不填,表示从LDAP的根节点开始搜索
-
User search base:这个配置也是为了缩小LDAP搜索的范围,例如Jenkins系统只允许ou为Admin下的用户才能登陆,那么你这里可以填写ou=Admin,这是一个相对的值,相对于上边的root DN,例如你上边的root DN填写的是dc=domain,dc=com,那么user search base这里填写了ou=Admin,那么登陆用户去LDAP搜索时就只会搜索ou=Admin,dc=domain,dc=com下的用户了
-
User search filter:这个配置定义登陆的“用户名”对应LDAP中的哪个字段,如果你想用LDAP中的uid作为用户名来登录,那么这里可以配置为uid={0}({0}会自动的替换为用户提交的用户名),如果你想用LDAP中的mail作为用户名来登录,那么这里就需要改为mail={0}。在测试的时候如果提示你user xxx does not exist,而你确定密码输入正确时,就要考虑下输入的用户名是不是这里定义的这个值了
-
Group search base:参考上边User search base解释
-
Group search filter:这个配置允许你将过滤器限制为所需的objectClass来提高搜索性能,也就是说可以只搜索用户属性中包含某个objectClass的用户,这就要求你对你的LDAP足够了解,一般我们也不配置
-
Group membership:这个参数通常允许您选择一种方法来确定用户所属的组。例如,您可以选择“从用户条目中的属性获取组成员资格”或“从组的成员属性中获取组成员资格”。这些选项决定了Jenkins如何查询LDAP服务器以获取用户的组成员资格信息。
-
Manager DN:这个配置在你的LDAP服务器不允许匿名访问的情况下用来做认证(详细的认证过程参考文章LDAP落地实战(二):SVN集成OpenLDAP认证中关于LDAP服务器认证过程的讲解),通常DN为cn=admin,dc=domain,dc=com这样
-
Manager Password:上边配置dn的密码
-
Display Name LDAP attribute:配置用户的显示名称,一般为显示名称就配置为uid,如果你想显示其他字段属性也可以这里配置,例如mail
-
Email Address LDAP attribute:配置用户Email对应的字段属性,一般没有修改过的话都是mail,除非你用其他的字段属性来标识用户邮箱,这里可以配置
5、Nexus 对接LDAP
官方文档:https://help.sonatype.com/repomanager3/nexus-repository-administration/user-authentication/ldap
Nexus 3 | OpenLDAP |
---|---|
3.38.1 | 1.2.4 |
1、Nexus开启认证Realm
2、配置LDAP
输入LDAP配置信息结束后点击Verify connection进行验证,出现右上角绿色信息表示验证通过,然后点击Next进行下一步
- Name:为新配置输入一个唯一的名称
- LDAP server address:输入LDAP服务器的协议*、*主机名和端口。
- Protocol:此下拉列表中的有效值为“ldap”和“ldaps”,它们对应于SSL上的轻型目录访问协议和轻型目录访问协定。
- Hostname:LDAP服务器的主机名或IP地址
- Port:端口389是“ldap”协议的默认端口,端口636是“ldaps”的默认端口。
- Search base DN:要添加到连接URL的LDAP位置。通常为LDAP最顶层。
- Authentication method: 身份验证方法
- Simple Authentication:简单的身份验证由用户名和密码组成。对于不使用安全“ldaps”协议的生产部署,不建议使用简单身份验证,因为它通过网络发送明文密码。
- Anonymous Authentication:匿名身份验证
- Digest-MD5:这是对CRAM-MD5身份验证方法的改进
- CRAM-MD5:基于HMAC-MD5 MAC算法的质询-响应认证方法(CRAM)。
- Username or DN:LDAP的管理员用户的用户名或DN(可分辨名称)。它用于连接到LDAP服务器
- Password:上面配置的用户名或DN的密码
- Configuration template:用户和组的配置模板。根据您的模板选择,其余字段输入将根据适当的用户和组模板要求进行调整。这些模板是对Active Directory、Generic Ldap Server、Posix with Dynamic Groups和Posix with Static Groups等服务器上使用的典型配置的建议。这些值仅为建议,必须根据LDAP服务器配置根据您的具体需要进行调整。
- User relative DN:此DN是相对于搜索基础DN的,例如,如果您的用户都包含在“ou=users,dc=sonateype,dc=com”中,并且您指定了“dc=sonatype,dc=com”的搜索库DN,则使用值“ou=user”
- User subtree:如果为true,请选中此框。如果为False,则取消选中。如果设置为true,则可以检索指定基本DN下的整个树中的用户条目。如果设置为false,则只能检索在基本DN及其子节点中的用户条目
- Object class:它为用户指定对象类。常见值为“inetOrgPerson”、“person”、”user“或”posixAccount“
- User filter:这允许您配置一个筛选器来限制对用户记录的搜索,这里我们使用member模块进行过滤,配置为(&(memberOf=cn=nexus,ou=Group,dc=example,dc=com))
- User ID attribute:这是上面指定的对象类的属性,它从LDAP服务器为用户提供标识符
- Real name attribute:这是提供用户真实名称的Object类的属性
- Email attribute:这是提供用户电子邮件地址的Object类的属性
- Password attribute:它可以用于配置Object类,该类提供密码(“userPassword”)。如果此字段为空,则将根据与LDAP服务器的绑定对用户进行身份验证。密码属性是可选的。如果未进行配置,身份验证将作为绑定到LDAP服务器的方式进行。否则,这是提供用户密码的Object类的属性
- Group member attribute:指定指定组成员的对象类的属性。一个示例值是“uniqueMember”
输入LDAP配置信息结束后点击Verify user mapping进行验证,出现右上角绿色信息且出现用户目录表示验证通过,然后点击Create创建完成。
3、赋予用户权限
分配Nexus管理员的角色"nx-admin"给LDAP上的一个用户,作为nexus新的管理员。然后将admin用户禁用。
6、confluence 对接LDAP
官方文档:https://confluence.atlassian.com/conf85/connecting-to-an-ldap-directory-1283360866.html
参数 | 说明 | |
---|---|---|
服务器设置 | 名称 | LDAP服务器名称 |
目录类型 | OpenLDAP | |
主机名 | LDAP 的服务器主机名 | |
端口 | 默认389,当选择使用SSL时为636 | |
用户名 | cn =用户,dc =域,dc =名称 | |
密码 | 登陆密码 | |
LDAP模式 | 基本DN | dc =域,dc =名称 |
附加用户DN | 填写限制用户搜索范围的值,类似ou=People,不填的话从基础 DN开始搜索 | |
附加组DN | 限制用户组搜索范围的值,如cn=Confluence,ou=Group | |
LDAP权限 | 只读 | 适用于对安全性要求较高,且不希望通过Confluence修改用户和用户组信息的场景 |
只读,本地组 | 适用于需要在Confluence中管理本地组,但不需要修改LDAP服务器上用户和用户组信息的场景 | |
读/写 | 适用于需要在Confluence中管理和修改用户和用户组信息的场景 | |
高级设置 | SSL安全协议 | 选择SSL且使用自签名证书时可以禁止检查SSL安全协议 |
组模式设置 | 组对象类 | 默认为groupOfUniqueNames,要注意LDAP是否开启member插件,组ObjectClasses是否为groupOfUniqueNames |
7、Sonarqube 对接LDAP
官方文档:https://docs.sonarsource.com/sonarqube/latest/instance-administration/authentication/ldap/
1、Sonarqube安装LDAP插件
配置--> 应用市场
2、修改配置文件
如果sonarqube的部署实例是使用Dockers的话,则可通过环境变量的方式注入以下配置
# vim /opt/sonarqube/conf/sonar.properties
# LDAP configuration
# General Configuration
sonar.security.realm=LDAP
ldap.url=ldap://192.168.1.10:389
ldap.bindDn=cn=admin,dc=example,dc=com
ldap.bindPassword=123456
# User Configuration
ldap.user.baseDn=ou=People,dc=example,dc=com
ldap.user.request=(&(objectClass=inetOrgPerson)(uid={login}))
ldap.user.realNameAttribute=sn
ldap.user.emailAttribute=mail
# Group Configuration
ldap.group.baseDn=ou=Group,dc=example,dc=com
ldap.group.request=(&(memberOf=cn=sonarqube,Group,dc=example,dc=com)(cn={0}))
重启Sonarqube,启动过程中如果出现以下日志,则证明LDAP连接成功
INFO org.sonar.INFO Security realm: LDAP ...
INFO o.s.p.l.LdapContextFactory Test LDAP connection: OK
3、登录验证
4、权限控制
将admin用户的管理员权限删除,赋予另一个用户
8、harbor 对接LDAP
官方文档:https://goharbor.io/docs/2.10.0/administration/configure-authentication/ldap-auth/
在配置管理中将认证模式从数据库更改为LDAP
注意:仅当数据库中没有添加本地用户时,才可以将认证方式从数据库更改为LDAP。如果至少有1个用户不在admin
Harbor数据库中,则无法更改认证模式。
如果 LDAP/AD 服务器使用自签名或不受信任的证书,请取消选中LDAP 检查证书
9、openvpn 对接LDAP
https://www.gaoyufu.cn/archives/openvpn#2%E9%85%8D%E7%BD%AEldap%E8%AE%A4%E8%AF%81
评论区