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
1060 0
|
数据库 索引 关系型数据库
|
8天前
|
SQL 关系型数据库 数据库
关系型数据库选择合适的数据库管理系统
【5月更文挑战第5天】关系型数据库选择合适的数据库管理系统
261 2
关系型数据库选择合适的数据库管理系统
|
8天前
|
关系型数据库 MySQL BI
关系型数据库选择合适的数据库管理系统
【5月更文挑战第4天】关系型数据库选择合适的数据库管理系统
183 4
关系型数据库选择合适的数据库管理系统
|
1天前
|
关系型数据库 分布式数据库 数据库
【PolarDB 开源】PolarDB 性能调优实录:提升数据库集群吞吐量的技巧
【5月更文挑战第22天】PolarDB 性能调优关键点包括硬件资源配置、数据库参数调整、索引优化、分区策略、事务优化及性能监控。创建高效索引如`CREATE INDEX idx_name ON table_name (column_name);`,根据业务场景选择分区方式,调整事务隔离级别以提升并发性能。监控 CPU、内存等指标,定期维护数据库,结合业务特点综合调优,从而提升数据库集群吞吐量。这些技巧有助于发挥PolarDB潜力,支持业务高效运行。
16 5
|
3天前
|
关系型数据库 分布式数据库 PolarDB
【PolarDB开源】PolarDB开源之旅:从零开始搭建分布式数据库集群
【5月更文挑战第20天】PolarDB,阿里云自研的云原生分布式数据库,因其高性能、高可用和易用性备受瞩目。本文指导如何搭建PolarDB集群:准备硬件和软件环境,从GitHub克隆源码,构建Docker镜像,部署控制节点和计算节点,最后验证集群状态。通过开源,PolarDB旨在推动数据库技术进步,邀请用户一同探索其潜力,共创未来。
32 4
|
6天前
|
Cloud Native 关系型数据库 分布式数据库
祝贺!阿里云PolarDB斩获数据库国际顶会ICDE 2024工业赛道最佳论文
阿里云斩获国际顶会ICDE 2024最佳论文,0.5秒实现数据库跨机实例迁移。
祝贺!阿里云PolarDB斩获数据库国际顶会ICDE 2024工业赛道最佳论文
|
6天前
|
关系型数据库 数据库 数据安全/隐私保护
使用PostgreSQL进行高级数据库管理
【5月更文挑战第17天】本文介绍了使用PostgreSQL进行高级数据库管理,涵盖性能调优、安全性加强和备份恢复。性能调优包括索引优化、查询优化、分区和硬件配置调整;安全性涉及权限管理、加密及审计监控;备份恢复则讨论了物理备份、逻辑备份和持续归档。通过这些实践,可提升PostgreSQL的性能和安全性,确保数据资源的有效管理。
|
8天前
|
存储 Cloud Native 关系型数据库
PolarDB-X 是面向超高并发、海量存储和复杂查询场景设计的云原生分布式数据库系统
【5月更文挑战第14天】PolarDB-X 是面向超高并发、海量存储和复杂查询场景设计的云原生分布式数据库系统
116 2