Use LDAP store PostgreSQL Connection parameter & client use it with .pg_service.conf

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
简介:
前面几篇BLOG谈了一下PostgreSQL的用户密码认证可以通过LDAP 来做AUTH.
客户端提交用户和密码,
PostgreSQL server根据提供客户端的用户, 以及pg_hba.conf中的配置, 到LDAP server查找匹配条目.
如果找到了匹配的话, 根据客户端提供的密码在LDAP server进行认证.
客户端只和PostgreSQL server交互, 认证部分由PostgreSQL server和LDAP server完成. 所以PostgreSQL server编译时需要--with-ldap.
本文要讲的是LDAP的另一个用法, 用来存储客户端连接数据库的连接信息.
例如psql -h 172.16.3.150 -p 1818 -U digoal -d digoal
这里的 -h 172.16.3.150 -p 1818 -U digoal -d digoal 存储到LDAP里面.
客户端psql通过LDAP获取到连接信息后再去连接数据库, 因此这里的客户端需要配置--with-ldap, 而服务端不需要.
首先将连接信息导入LDAP, 这里用到core.schema里面的objectclass  groupOfUniqueNames, 因为连接信息存储多个, 所以存储到description中, uniqueMember存储1项即可.
[root@db-172-16-3-150 ~]# cat digoal_db.ldif 
dn: cn=digoal,ou=People,dc=my-domain,dc=com
objectclass: top
objectclass: groupOfUniqueNames
cn: digoal
description: sslmode=allow
description: user=digoal
description: dbname=digoal
description: port=1818
description: host=172.16.3.150
uniqueMember: host=172.16.3.150

[root@db-172-16-3-150 ~]# ldapadd -vv -x -w 123321 -D "cn=Manager,dc=my-domain,dc=com" -f digoal_db.ldif
ldap_initialize( <DEFAULT> )
add objectclass:
        top
        groupOfUniqueNames
add cn:
        digoal
add description:
        sslmode=allow
        user=digoal
        dbname=digoal
        port=1818
        host=172.16.3.150
add uniqueMember:
        host=172.16.3.150
adding new entry "cn=digoal,ou=People,dc=my-domain,dc=com"
modify complete

查找这个DN正常.
[root@db-172-16-3-150 ~]# slapcat -s "cn=digoal,ou=People,dc=my-domain,dc=com"
bdb_db_open: warning - no DB_CONFIG file found in directory /var/lib/ldap: (2).
Expect poor performance for suffix "dc=my-domain,dc=com".
dn: cn=digoal,ou=People,dc=my-domain,dc=com
objectClass: top
objectClass: groupOfUniqueNames
cn: digoal
description: sslmode=allow
description: user=digoal
description: dbname=digoal
description: port=1818
description: host=172.16.3.150
uniqueMember: host=172.16.3.150
structuralObjectClass: groupOfUniqueNames
entryUUID: 2119866a-848b-1033-8f3c-c1c6b9bc50eb
creatorsName: cn=Manager,dc=my-domain,dc=com
createTimestamp: 20140610013437Z
entryCSN: 20140610013437.788394Z#000000#000#000000
modifiersName: cn=Manager,dc=my-domain,dc=com
modifyTimestamp: 20140610013437Z

修改数据库的pg_hba.conf, 允许客户端连接
cd $PGDATA
vi pg_hba.conf
host all all 0.0.0.0/0 md5
pg_ctl reload

查看客户端是否加了--with-ldap配置项.
pg93@db-172-16-3-39-> pg_config --configure
'--prefix=/home/pg93/pgsql9.3.1' '--with-pgport=1999' '--with-perl' '--with-tcl' '--with-python' '--with-openssl' '--with-pam' '--with-ldap' '--with-libxml' '--with-libxslt' '--enable-thread-safety' '--with-wal-blocksize=16' '--enable-dtrace' '--enable-debug'
配置~/.pg_service.conf, 注意格式,   ldap://host:port/dn?attributes?scope?filter?extensions 

pg93@db-172-16-3-39-> cat .pg_service.conf 
[mydb]
ldap://172.16.3.150:389/cn=digoal,ou=People,dc=my-domain,dc=com?description?sub?cn=digoal

使用psql连接, 会先到LDAPserver找到attributes的值作为连接项, 连接到目标数据库.
pg93@db-172-16-3-39-> psql service=mydb
Password: 
psql (9.3.1)
Type "help" for help.
digoal=> 

如果客户端未配置--with-ldap, 那么在.pg_service.conf中使用ldap uri是会报语法错误的.
pg94@db-172-16-3-39-> pg_config --configure
'--prefix=/home/pg94/pgsql9.4devel' '--with-pgport=2999' '--with-perl' '--with-tcl' '--with-python' '--with-openssl' '--with-pam' '--without-ldap' '--with-libxml' '--with-libxslt' '--enable-thread-safety' '--with-wal-blocksize=16' '--enable-dtrace'
pg94@db-172-16-3-39-> cat .pg_service.conf
[mydb]
ldap://172.16.3.150:389/cn=digoal,ou=People,dc=my-domain,dc=com?description?sub?cn=digoal
pg94@db-172-16-3-39-> psql service=mydb
psql: syntax error in service file "/home/pg94/.pg_service.conf", line 2

在LDAP server中存储数据库的连接信息, 对于需要修改数据库连接配置的场景, 只需要修改LDAP, 而不需要修改客户端的配置, 方便管理.

[参考]
4.  src/interfaces/libpq/fe-connect.c

/*
 *              ldapServiceLookup
 *
 * Search the LDAP URL passed as first argument, treat the result as a
 * string of connection options that are parsed and added to the array of
 * options passed as second argument.
 *
 * LDAP URLs must conform to RFC 1959 without escape sequences.
 *      ldap://host:port/dn?attributes?scope?filter?extensions
 *
 * Returns
 *      0 if the lookup was successful,
 *      1 if the connection to the LDAP server could be established but
 *        the search was unsuccessful,
 *      2 if a connection could not be established, and
 *      3 if a fatal error occurred.
 *
 * An error message is returned in the third argument for return codes 1 and 3.
 */
static int
ldapServiceLookup(const char *purl, PQconninfoOption *options,
                                  PQExpBuffer errorMessage)
{
        int                     port = LDAP_DEF_PORT,
                                scope,
                                rc,
                                msgid,
                                size,
                                state,
                                oldstate,
                                i;
        bool            found_keyword;
        char       *url,
                           *hostname,
                           *portstr,
                           *endptr,
                           *dn,
                           *scopestr,

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
关系型数据库 PostgreSQL
PostgreSQL - ERROR: could not determine data type of parameter $1
PostgreSQL - ERROR: could not determine data type of parameter $1
938 0
|
数据库 索引 关系型数据库
|
26天前
|
关系型数据库 分布式数据库 数据库
成都晨云信息技术完成阿里云PolarDB数据库产品生态集成认证
近日,成都晨云信息技术有限责任公司(以下简称晨云信息)与阿里云PolarDB PostgreSQL版数据库产品展开产品集成认证。测试结果表明,晨云信息旗下晨云-站群管理系统(V1.0)与阿里云以下产品:开源云原生数据库PolarDB PostgreSQL版(V11),完全满足产品兼容认证要求,兼容性良好,系统运行稳定。
|
1月前
|
关系型数据库 分布式数据库 数据库
PolarDB常见问题之数据库不能自己减少节点如何解决
PolarDB是阿里云推出的下一代关系型数据库,具有高性能、高可用性和弹性伸缩能力,适用于大规模数据处理场景。本汇总囊括了PolarDB使用中用户可能遭遇的一系列常见问题及解答,旨在为数据库管理员和开发者提供全面的问题指导,确保数据库平稳运行和优化使用体验。
|
1月前
|
缓存 关系型数据库 分布式数据库
PolarDB常见问题之数据库cpu突然飙高如何解决
PolarDB是阿里云推出的下一代关系型数据库,具有高性能、高可用性和弹性伸缩能力,适用于大规模数据处理场景。本汇总囊括了PolarDB使用中用户可能遭遇的一系列常见问题及解答,旨在为数据库管理员和开发者提供全面的问题指导,确保数据库平稳运行和优化使用体验。
|
2月前
|
关系型数据库 分布式数据库 数据库
阿里云PolarDB登顶2024中国数据库流行榜:技术实力与开发者影响力
近日,阿里云旗下的自研云原生数据库PolarDB在2024年中国数据库流行度排行榜中夺冠,并刷新了榜单总分纪录,这一成就引起了技术圈的广泛关注。这一成就源于PolarDB在数据库技术上的突破与创新,以及对开发者和用户的实际需求的深入了解体会。那么本文就来分享一下关于数据库流行度排行榜的影响力以及对数据库选型的影响,讨论PolarDB登顶的关键因素,以及PolarDB“三层分离”新版本对开发者使用数据库的影响。
74 3
阿里云PolarDB登顶2024中国数据库流行榜:技术实力与开发者影响力
|
1月前
|
关系型数据库 分布式数据库 数据库
PolarDB PostgreSQL版:Oracle兼容的高性能数据库
PolarDB PostgreSQL版是一款高性能的数据库,具有与Oracle兼容的特性。它采用了分布式架构,可以轻松处理大量的数据,同时还支持多种数据类型和函数,具有高可用性和可扩展性。它还提供了丰富的管理工具和性能优化功能,为企业提供了可靠的数据存储和处理解决方案。PolarDB PostgreSQL版在数据库领域具有很高的竞争力,可以满足各种企业的需求。
|
1天前
|
关系型数据库 OLAP 分布式数据库
「杭州*康恩贝」4月26日PolarDB开源数据库沙龙,开启报名!
4月26日周五,PolarDB开源社区联合康恩贝将共同举办开源数据库技术沙龙,本次沙龙我们邀请了众多数据库领域的专家,期待大家的参与!
|
11天前
|
运维 关系型数据库 分布式数据库
「合肥 * 讯飞」4 月 19 日 PolarDB 开源数据库沙龙,报名中!
4月19日周五,PolarDB开源社区联合科大讯飞共同举办开源数据库技术沙龙,本次沙龙我们邀请了众多数据库领域的专家,期待大家的参与!
「合肥 * 讯飞」4 月 19 日 PolarDB 开源数据库沙龙,报名中!