PostgreSQL LDAP认证之 simple bind配置
背景
在企业中,如果要使用统一的账户管理,可以考虑LDAP或者WINDOWS AD域进行管理。
PostgreSQL数据库也支持LDAP的认证手段,支持simple bind和search bind。
本文介绍一下PostgreSQL的simple bind配置方法。
上一篇BLOG介绍了OpenLDAP Server的配置
PostgreSQL LDAP认证介绍
本文将要讲一下ldap client的配置, 以及PostgreSQL的ldap认证方法.
ldap认证方法指的是通过ldap进行认证, 而非数据库本身的用户密码进行认证.
首先要确保PostgreSQL编译时带上了–with-ldap选项,因为认证过程实际上是PG代理用户向LDAP发起的认证请求.
例如以下数据库不可用使用ldap认证, 因为带了–without-ldap
postgres@db6-> pg_config --configure
'--prefix=/app/pgsql' '--with-pgport=1921' '--with-wal-segsize=64' '--with-perl' '--with-python' '--with-openssl' '--with-pam' '--without-ldap' '--with-libxml' '--with-libxslt' '--enable-thread-safety'
被认证的用户必须在ldap服务端存在, 同时在数据库中也要存在, 但是数据库中的用户密码不被用作校验, 所以不需要指定密码.
甚至可以指定数据库中的密码的到期日, 因为数据库中的密码不被用作校验, 所以限制数据库的密码到期不会限制用户是否能用.
测试环境
OpenLDAP server
172.16.3.150:389
PostgreSQL Server
172.16.3.39:1999
psql client
172.16.3.176
配置过程
在ldap中创建一个用户
1. 生成用户密码
[root@db-172-16-3-150 ~]# slappasswd
New password: 111111
Re-enter new password: 111111
{SSHA}YrmLO/6mSZOkR+fCbtIbWHOAMoju9cCz
2. 创建ldif
[root@db-172-16-3-150 ~]# vi user01.ldif
dn: uid=new,ou=People,dc=my-domain,dc=com
uid: new
cn: new
objectClass: account
objectClass: posixAccount
objectClass: top
objectClass: shadowAccount
loginShell: /bin/bash
uidNumber: 503
gidNumber: 500
homeDirectory: /home/new
userPassword: {SSHA}YrmLO/6mSZOkR+fCbtIbWHOAMoju9cCz
3. 导入ldap
[root@db-172-16-3-150 ~]# ldapadd -x -W -D "cn=Manager,dc=my-domain,dc=com" -f user01.ldif
4. 我们这里新增的用户在ldap中的DN是
uid=new,ou=People,dc=my-domain,dc=com
PostgreSQL LDAP认证模式文档参考
PostgreSQL支持2种ldap的认证模式, simple bind和search
参考以下链接了解两种认证模式在pg_hba.conf中的配置方法.
http://www.postgresql.org/docs/9.4/static/auth-methods.html#AUTH-LDAP
配置PostgreSQL simple bind LDAP认证
配置数据库的pg_hba.conf, 我这里用simple bind的写法.
写法如下
DN = prefix username suffix
解释:
prefix和suffix对应
ldapprefix
ldapsuffix
username对应客户端提交的用户名.
所以知道openldap中配置的DN的话, pg_hba.conf配置就非常简单了.
认证的话就是用到这个DN下的userPassword.
因为pg_hba.conf是顺序匹配的, 所以为了方便, 直接在文件的最前面加一条
host all new 0.0.0.0/0 ldap ldapserver=172.16.3.150 ldapport=389 ldapprefix="uid=" ldapsuffix=",ou=People,dc=my-domain,dc=com"
pg93@db-172-16-3-39-> pg_ctl reload
server signaled
查看postgresql csvlog确保pg_hba.conf的修改生效.
2014-06-06 15:52:13.528 CST,,,5229,,53917265.146d,3,,2014-06-06 15:48:53 CST,,0,LOG,00000,"received SIGHUP, reloading configuration files",,,,,,,,"SIGHUP_handler, postmaster.c:2294",""
当数据库中不存在 new用户时, 使用111111这个密码登陆报角色不存在的错误.
postgres@db5-> psql -h 172.16.3.39 -p 1999 -U new postgres
Password for user new:
psql: FATAL: role "new" does not exist
在数据库中创建这个用户, 不需要指定密码.
[root@db-172-16-3-39 postgresql-9.3.1]# su - pg93
pg93@db-172-16-3-39-> psql
psql (9.3.1)
Type "help" for help.
digoal=# create role new nosuperuser login;
CREATE ROLE
现在可以用LDAP认证登陆了
postgres@db5-> psql -h 172.16.3.39 -p 1999 -U new postgres
Password for user new:
psql (9.1.3, server 9.3.1)
WARNING: psql version 9.1, server version 9.3.
Some psql features might not work.
Type "help" for help.
postgres=>
修改用户的密码有效时间, 不会影响ldap认证
digoal=# alter role new valid until '1970-01-01';
ALTER ROLE
digoal=# \du+ new
List of roles
Role name | Attributes | Member of | Description
-----------+---------------------------------------------+-----------+-------------
new | Password valid until 1970-01-01 00:00:00+08 | {} |
digoal=#
postgres@db5-> psql -h 172.16.3.39 -p 1999 -U new postgres
Password for user new:
psql (9.1.3, server 9.3.1)
WARNING: psql version 9.1, server version 9.3.
Some psql features might not work.
Type "help" for help.
postgres=>
修改用户在数据库中的密码, 不影响ldap密码认证.
postgres=> alter role new encrypted password 'abc';
ALTER ROLE
postgres@db5-> psql -h 172.16.3.39 -p 1999 -U new postgres
Password for user new: 111111
psql (9.1.3, server 9.3.1)
WARNING: psql version 9.1, server version 9.3.
Some psql features might not work.
Type "help" for help.
postgres=>
当然, 使用数据库中的密码是无法认证的,因为走的是LDAP认证。
postgres@db5-> psql -h 172.16.3.39 -p 1999 -U new postgres
Password for user new: abc
psql: FATAL: LDAP authentication failed for user "new"
参考
1. http://www.postgresql.org/docs/9.4/static/auth-methods.html#AUTH-LDAP
2. man slapd-mdb, slapd-sql ….
3. http://w.gdu.me/wiki/sysadmin/OpenLDAPconfig.html
4. http://blog.christophersmart.com/articles/openldap-how-to-fedora/
5. http://directory.fedoraproject.org/wiki/Main_Page
6. https://access.redhat.com/site/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Deployment_Guide/ch-Directory_Servers.html
7. https://access.redhat.com/site/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Deployment_Guide/ch-Configuring_Authentication.html
8. http://quark.humbug.org.au/publications/ldap/ldap_tut.html
9. https://access.redhat.com/site/documentation/en-US/Red_Hat_Directory_Server/9.0/index.html
10. http://www.zytrax.com/books/ldap/index.html
11. http://grokbase.com/t/postgresql/pgsql-general/086x32w1ew/ldap-authentication
12. http://www-06.ibm.com/jp/linux/tech/doc/attachments/openldap_install_v1.3.pdf
13. http://www.cnblogs.com/gaojian/p/3336657.html
14. https://secure.sqlmanager.net/en/articles/postgresql/654
15. http://itc.musc.edu/wiki/PostgreSQL
16. http://paginas.fe.up.pt/~jvv/PERL/manual/site/lib/URI/ldap.html
17. http://www.openldap.org/doc/admin24/backends.html