(一)准备SSL根认证文件、私钥、证书
直接上脚本吧。
#!/bin/bash
# gen root cert
openssl genrsa -out ca.key 2048
openssl req -x509 -new -nodes -key ca.key -sha256 -days 36500 -out ca.pem -subj "/C=CN/ST=Beijing/L=Beijing/O=vmware/OU=cibg/CN=vmware.com"
# gen server ssl
openssl genrsa -out server.key 2048
openssl rsa -in server.key -pubout -out server.pub
openssl req -new -key server.key -out server.csr -subj "/C=CN/ST=Beijing/L=Beijing/O=vmware/OU=cibg/CN=127.0.0.1"
openssl x509 -req -in server.csr -CA ca.pem -CAkey ca.key -CAcreateserial -out server.pem -days 36500 -sha256
# gen client ssl
openssl genrsa -out postgres.key 2048
openssl rsa -in postgres.key -pubout -out postgres.pub
openssl req -new -key postgres.key -out postgres.csr -subj "/C=CN/ST=Beijing/L=Beijing/O=vmware/OU=cibg/CN=127.0.0.1"
openssl x509 -req -in postgres.csr -CA ca.pem -CAkey ca.key -CAcreateserial -out postgres.pem -days 36500 -sha256
# rm useless file
rm -rf server.pub server.csr postgres.pub postgres.csr ca.key
既然是双向认证,先我们得有个脚本使用openssl生成server端和client端的pem和key。结果如下,我们随机写一个错误postgres_w.pem用于测试。
$ ssl pwd
/home/hyongtao/ssl
$ ssl ls -l
total 28
-rw-r--r--. 1 hyongtao hyongtao 1338 May 11 02:04 ca.pem
-rw-rw-r--. 1 hyongtao hyongtao 983 May 11 01:57 gen_ssl.sh
-rw-------. 1 hyongtao hyongtao 1679 May 11 02:04 postgres.key
-rw-r--r--. 1 hyongtao hyongtao 1229 May 11 02:04 postgres.pem
-rw-r--r--. 1 hyongtao hyongtao 1229 May 11 01:41 postgres_w.pem (随便改出来的错误pem用于测试)
-rw-------. 1 hyongtao hyongtao 1675 May 11 02:04 server.key
-rw-r--r--. 1 hyongtao hyongtao 1229 May 11 02:04 server.pem
$ ssl openssl x509 -noout -text -in ca.pem
Certificate:
Data:
Version: 3 (0x2) (确实是TLS1.3)
Serial Number:
7f:4e:be:72:a5:49:5d:68:83:35:39:52:4f:da:e5:2d:f5:1a:77:71
Signature Algorithm: sha256WithRSAEncryption
Issuer: C = CN, ST = Beijing, L = Beijing, O = vmware, OU = cibg, CN = vmware.com
(2)数据库端的准备工作
创建一个用户hyongtao-ssl
hyongtao=# CREATE USER "hyongtao-ssl" WITH PASSWORD '123456' LOGIN;
CREATE ROLE
修改pg_hba.conf文件, 若最后为md5则为单向认证, 若最后为md5 clientcert=1则为双向认证。本文只测试双向认证
hostssl all hyongtao-ssl 0.0.0.0/0 md5 clientcert=1
修改postgresql.conf文件, 打开ssl, 双向认证中的服务器端认证在此处配置。
ssl = on
ssl_ca_file = '/home/hyongtao/ssl/ca.pem'
ssl_cert_file = '/home/hyongtao/ssl/server.pem'
#ssl_crl_file = ''
ssl_key_file = '/home/hyongtao/ssl/server.key'
双向认证中的客户端认证在环境变量中配置。
export PGSSLROOTCERT=/home/hyongtao/ssl/ca.pem;
export PGSSLCERT=/home/hyongtao/ssl/postgres.pem;
export PGSSLKEY=/home/hyongtao/ssl/postgres.key;
我们使用最严格的ssl verify策略来测试。
export PGSSLMODE=verify-full;
(3)结果测试
$ psql -U hyongtao-ssl -d hyongtao -h 127.0.0.1
Password for user hyongtao-ssl:
psql (12.12)
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.
hyongtao=>
可以看出用户hyongtao-ssl连接数据库hyongtao使用的是TLSv1.3。
随后我们给一个错误的export PGSSLCERT=/home/hyongtao/ssl/postgres_w.pem, 报错了。
$ psql -U hyongtao-ssl -d hyongtao -h 127.0.0.1
psql: error: FATAL: connection requires a valid client certificate
FATAL: no pg_hba.conf entry for host "127.0.0.1", user "hyongtao-ssl", database "hyongtao", SSL off
本文为本机上的SSL配置,后文为更接近用户实际情况的,跨机器访问的SSL配置。
最后修改时间:2024-05-14 22:02:19
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




