PostgreSQL配置SSL安全連接

環境說明

  • PostgreSQL 9.4 docker容器

配置步驟

服務器端證書配置

服務器端需生成三個文件: root.crt(根證書)server.crt(服務器證書)server.key(服務器私鑰)

生成服務器私鑰

$ cd /var/lib/postgresql/data
$ openssl genrsa -des3 -out server.key 2048
Generating RSA private key, 2048 bit long modulus
...........................................................................+++++
.....+++++
e is 65537 (0x010001)
Enter pass phrase for server.key:
Verifying - Enter pass phrase for server.key:
# 此處需輸入密碼,並需要再次確認輸入密碼

服務器私鑰生成後,需移除密碼,否則數據庫重啓時會出現異常

$ openssl rsa -in server.key -out server.key
Enter pass phrase for server.key:
writing RSA key

生成服務器證書

$ openssl req -new -key server.key -days 2650 -out server.crt -x509
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:
Locality Name (eg, city) []:
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:localhost
Common Name (e.g. server FQDN or YOUR name) []:localhost
Email Address []:
# 此處可以一路enter,不用輸入

生成根證書

由於沒有公證機構提供,只能使用自簽名證書,因此可以將服務器證書作爲根證書

$ cp server.crt root.crt

服務器端配置

postgresql的ssl配置默認是關閉的,需更改配置文件進行開啓

$ vi /var/lib/postgresql/data/postgresql.conf
ssl=on
ssl_ca_file='root.crt'
ssl_key_file='server.key'
ssl_cert_file='server.crt'

還需要更改服務器的pg_hba.conf文件禁止用戶以非SSL連接數據庫

$ vi /var/lib/postgresql/data/pg_hba.conf
# host all all all md5
hostssl all all 0.0.0.0/0 cert

然後重啓postgresql

客戶端證書配置

客戶端需要三個文件: root.crt(根證書)postgresql.crt(客戶端證書)postgresql.key(客戶端私鑰)

生成客戶端私鑰

$ cd /var/lib/postgresql/data
$ openssl genrsa -des3 -out postgresql.key 2048
Generating RSA private key, 2048 bit long modulus
...........................................................................+++++
.....+++++
e is 65537 (0x010001)
Enter pass phrase for postgresql.key:
Verifying - Enter pass phrase for postgresql.key:
# 此處需輸入密碼,並需要再次確認輸入密碼

客戶端私鑰生成後,可不移除密碼,爲簡便操作,此處進行移除

$ openssl rsa -in postgresql.key -out postgresql.key
Enter pass phrase for postgresql.key:
writing RSA key

生成客戶端csr文件

$ openssl req -new -key postgresql.key -out postgresql.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:
Locality Name (eg, city) []:
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:postgres
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

Common Name (e.g. server FQDN or YOUR name) []:postgres該項必須設置爲要連接postgresql數據庫的用戶名,否則會默認使用當前計算機的用戶名,導致證書使用時,認證失敗。

生成客戶端證書

$ openssl x509 -req -days 3650 -in postgresql.csr -CA root.crt -CAkey server.key -out postgresql.crt -CAcreateserial
Signature ok
subject=C = CN, ST = Some-State, O = Internet Widgits Pty Ltd, CN = postgres
Getting CA Private Key

測試連接

root.crtpostgresql.crtpostgresql.key拷貝到客戶端主機上行,然後使用navicat驗證連接:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章