当前位置:   article > 正文

PostgreSQL开启openssl(通讯加密)_如何查看pg数据库是否开启ssl

如何查看pg数据库是否开启ssl

上一节安装了数据库,这节我们开启openssl,并开启密码复杂度检查模块

【没有开启ssl,使用wireshark工具对抓取的包解析,传输的sql会被解析到,开启ssl 抓包时sql会加密

       Server.key server.crt位置都处于$PGDATA下,为保证安全性,可以将其放置于不同的位置,权限为0600,对于postgres用户可读可写,其他用户都没有任何权限

目录

创建根CSR文件

创建根CRT文件

创建服务器CSR文件

创建服务器CRT文件

配置数据库 启用ssl

重新启动数据库,检查ssl是否已经开启

创建ssl扩展

使用ssl方式登录数据库

检查当前是否为ssl模式

启用密码check机制

修改配置文件  在最后一行添加预加载库

重启数据库,验证简单密码是否可以创建


创建根CSR文件

  1. [postgres@localhost ~]$ cd ~
  2. [postgres@localhost ~]$ mkdir openssl
  3. [postgres@localhost ~]$ cd openssl/
  4. [postgres@localhost openssl]$ openssl req -new -nodes -text -out root.csr -keyout root.key -subj "/CN=test1"

查看会生成两个文件(.key和.csr)

 注意:-subj  参数内容 必须以/开头

删除其他用户的权限

[postgres@localhost openssl]$ chmod og-rwx root.key

创建根CRT文件

修改openssl配置文件   修改默认路径如下

  1. [postgres@localhost openssl]$ cp /etc/pki/tls/openssl.cnf /home/postgres/openssl/
  2. [postgres@localhost openssl]$ vi openssl.cnf

 生成CRT文件

[postgres@localhost openssl]$ openssl x509 -req -in root.csr -text -days 3650 -extfile /home/postgres/openssl/openssl.cnf -extensions v3_ca -signkey root.key -out root.crt

创建服务器CSR文件

[postgres@localhost openssl]$ openssl req -new -nodes -text -out server.csr -keyout server.key -subj "/CN=test1"

删除其他用户的权限

[postgres@localhost openssl]$ chmod og-rwx server.key

创建服务器CRT文件

[postgres@localhost openssl]$ openssl x509 -req -in server.csr -text -days 365 -CA root.crt -CAkey root.key -CAcreateserial -out server.crt

配置数据库 启用ssl

修改配置文件

[postgres@localhost openssl]$ vi $PGDATA/postgresql.conf
  1. ssl = on
  2. #ssl_ca_file = ''
  3. ssl_cert_file = '/home/postgres/openssl/server.crt'
  4. #ssl_crl_file = ''
  5. ssl_key_file = '/home/postgres/openssl/server.key'

说明:crt和key这些文件 默认路径是在$PGDATA,为了安全 我们新建了目录,在自定义的目录生成 ,所以这里密钥的路径要改一下。

对于密钥文件的处理,我们做了两步,一个是修改了密钥的默认路径,一个是修改了key的权限,都是为了安全考虑

重新启动数据库,检查ssl是否已经开启

  1. [postgres@localhost openssl]$ pg_ctl restart
  2. [postgres@localhost openssl]$ psql
  3. postgres=# show ssl;
  4. ssl
  5. -----
  6. on
  7. (1 row)
  8. postgres=# show ssl_key_file ;
  9. ssl_key_file
  10. -----------------------------------
  11. /home/postgres/openssl/server.key
  12. (1 row)
  13. postgres=# show ssl_cert_file;
  14. ssl_cert_file
  15. -----------------------------------
  16. /home/postgres/openssl/server.crt
  17. (1 row)

创建ssl扩展

postgres=# create extension sslinfo;

使用ssl方式登录数据库

[postgres@localhost openssl]$ psql "host=localhost user=postgres dbname=postgres password=123456 sslmode=require"

检查当前是否为ssl模式

  1. postgres=# select ssl_is_used();
  2. ssl_is_used
  3. -------------
  4. t
  5. (1 row)

启用密码check机制

上一节已经编译过所有模块了,如果没有编译,需要先将模块编译出来并且放到lib库

  1. [postgres@bogon passwordcheck]$ cd /home/postgres/postgresql-12.0/contrib/passwordcheck
  2. [postgres@bogon passwordcheck]$ make
  3. [postgres@bogon passwordcheck]$ make install
  4. [postgres@bogon passwordcheck]$ cp passwordcheck.so /home/postgres/install/lib/

修改配置文件  在最后一行添加预加载库

[postgres@localhost openssl]$ vi $PGDATA/postgresql.conf 

重启数据库,验证简单密码是否可以创建

  1. [postgres@localhost openssl]$ pg_ctl restart
  2. [postgres@localhost openssl]$ psql
  3. psql (12.0)
  4. Type "help" for help.
  5. postgres=# create user user1 with password '123456';
  6. 2022-07-05 12:11:49.250 EDT [23839] ERROR: password is too short
  7. 2022-07-05 12:11:49.250 EDT [23839] STATEMENT: create user user1 with password '123456';
  8. ERROR: password is too short

 

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号