LDAP基礎:1:使用docker快速搭建OpenLdap伺服器
LDAP是Lightweight Directory Access Protocol的縮寫,提供LDAP服務的軟體有很多商業上獲得成功的,其中以MS的AD和Redhat的NDS(Netscape directory server)使用最為廣泛,而開源領域則是OpenLdap了,這篇文章使用docker版本的OpenLdap來搭建環境並進行確認。
映象
本文使用osixia/openldap來進行演示,
拉取映象:docker pull osixia/openldap:1.2.2
執行
docker run --name my-openldap-container --detach osixia/openldap:1.2.2
結果確認
這樣我們就可以進入到執行起來的容器來確認相關資訊了
版本資訊
可以看到OpenLDAP的版本相關資訊,為本年度5月23日釋出的版本
[[email protected] ~]# docker ps |grep ldap
e444229214a5 osixia/openldap:1.2.2 "/container/tool/run" 24 hours ago Up 24 hours 389/tcp, 636/tcp my-openldap-container
[ [email protected] ~]# docker exec -it my-openldap-container sh
# ldapsearch -VV
ldapsearch: @(#) $OpenLDAP: ldapsearch (May 23 2018 04:25:19) $
Debian OpenLDAP Maintainers <[email protected]>
(LDAP library: OpenLDAP 20444)
#
測試配置檔案
使用slaptest可以確認配置檔案資訊是否正確
# slaptest config file testing succeeded #
使用者資訊(容器內查詢)
slapcat查詢
可以使用slapcat來檢視相關的使用者資訊,預設方式下已經有cn=admin的使用者資訊,詳細結果如下
# slapcat -v
# id=00000001
dn: dc=example,dc=org
objectClass: top
objectClass: dcObject
objectClass: organization
o: Example Inc.
dc: example
structuralObjectClass: organization
entryUUID: f8f7b81c-6a92-1038-9a4d-81d6b41cd327
creatorsName: cn=admin,dc=example,dc=org
createTimestamp: 20181022221004Z
entryCSN: 20181022221004.555004Z#000000#000#000000
modifiersName: cn=admin,dc=example,dc=org
modifyTimestamp: 20181022221004Z
# id=00000002
dn: cn=admin,dc=example,dc=org
objectClass: simpleSecurityObject
objectClass: organizationalRole
cn: admin
description: LDAP administrator
userPassword:: e1NTSEF9RTBJMTlqaW1BTVlaZ0h2aEhPbm5YVVdWRmQxaEtBRnM=
structuralObjectClass: organizationalRole
entryUUID: f8f818b6-6a92-1038-9a4e-81d6b41cd327
creatorsName: cn=admin,dc=example,dc=org
createTimestamp: 20181022221004Z
entryCSN: 20181022221004.557498Z#000000#000#000000
modifiersName: cn=admin,dc=example,dc=org
modifyTimestamp: 20181022221004Z
#
ldapsearch查詢
更為常見的方式這是使用ldapsearch來進行查詢
# ldapsearch -x -H ldap://localhost -b dc=example,dc=org -D "cn=admin,dc=example,dc=org" -w admin
# extended LDIF
#
# LDAPv3
# base <dc=example,dc=org> with scope subtree
# filter: (objectclass=*)
# requesting: ALL
#
# example.org
dn: dc=example,dc=org
objectClass: top
objectClass: dcObject
objectClass: organization
o: Example Inc.
dc: example
# admin, example.org
dn: cn=admin,dc=example,dc=org
objectClass: simpleSecurityObject
objectClass: organizationalRole
cn: admin
description: LDAP administrator
userPassword:: e1NTSEF9RTBJMTlqaW1BTVlaZ0h2aEhPbm5YVVdWRmQxaEtBRnM=
# search result
search: 2
result: 0 Success
# numResponses: 3
# numEntries: 2
#
當然也可以使用如下方式進行查詢
ldapsearch -x -H ldap://`hostname` -b dc=example,dc=org -D "cn=admin,dc=example,dc=org" -w admin
另外這裡的hostname也可以換成127.0.0.1,也可以使用容器分配的IP(172.17.0.3)
使用者資訊(容器外查詢)
上面的對使用者資訊的確認是在容器內進行的,在宿主機上對其進行的訪問則需要保證宿主機上也有諸如ldapsearch的連線方式,本文由於宿主機為centos7.4,所以這裡只需要yum install openldap-clients即可。
[[email protected] ~]# ldapsearch -x -H ldap://172.17.0.3 -b dc=example,dc=org -D "cn=admin,dc=example,dc=org" -w admin
# extended LDIF
#
# LDAPv3
# base <dc=example,dc=org> with scope subtree
# filter: (objectclass=*)
# requesting: ALL
#
# example.org
dn: dc=example,dc=org
objectClass: top
objectClass: dcObject
objectClass: organization
o: Example Inc.
dc: example
# admin, example.org
dn: cn=admin,dc=example,dc=org
objectClass: simpleSecurityObject
objectClass: organizationalRole
cn: admin
description: LDAP administrator
userPassword:: e1NTSEF9RTBJMTlqaW1BTVlaZ0h2aEhPbm5YVVdWRmQxaEtBRnM=
# search result
search: 2
result: 0 Success
# numResponses: 3
# numEntries: 2
[[email protected] ~]#
LDAP一般用於SSO的單點登入,所以其他機器能夠連線進行驗證是最基礎的,這篇文章為了簡單演示,只是使用了容器的IP進行連線驗證。
參考內容
https://github.com/osixia/docker-openldap
http://www.openldap.org/doc/