4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Docker + PostgreSQLでSSL対応

Posted at

目的

自分用のメモとして記事に残す。

検証環境

VPS

$ cat /proc/version
Linux version 3.10.0-957.1.3.el7.x86_64 
$ docker -v
Docker version 18.03.1-ce, build 9ee9f40
$ docker-compose -v
docker-compose version 1.11.1, build 7c5d5e4
$ psql -V
psql (PostgreSQL) 11.4 (Debian 11.4-1.pgdg90+1)

ツール

  • PgAdmin 4.2
  • WireShark 2.4.0

docker-compose.ymlの設定

version: '2'
services:
    postgres:
        image: postgres:11.4
        container_name: postgres11.4
        ports:
            - 5432:5432
        volumes:
            - ./docker/postgres/init.d:/docker-entrypoint-initdb.d
            - ./docker/postgres/pgdata:/var/lib/postgresql/data
        environment:
            POSTGRES_USER: test
            POSTGRES_PASSWORD: test
            POSTGRES_INITDB_ARGS: "--encoding=UTF-8

SSL接続前(Digest認証)

SSL接続前後のパケットの違いを比較するため、
WireSharkでパケットを確認する。
Digest認証(加工後).png

SSL接続

証明書作成

1.Dockerのコンテナにアクセスし、証明書を作成するディレクトリに移動

$ docker exec -it [コンテナID] bash
$ cd /var/lib/postgresql/data

2.秘密鍵、証明書発行要求、証明書を作成

$ openssl genrsa 2048 > server.key
$ openssl req -new -key server.key > server.csr
$ openssl x509 -days 36500 -req -signkey server.key < server.csr > server.crt 

3.作成したファイルの権限を変更

$ chown postgres:postgres server.*
$ chmod 600 server.key

PostgreSQLの設定ファイルを変更

1.SSL接続の設定を追加

pg_hba.conf
# TYPE  DATABASE   USER       ADDRESS            METHOD
# IPv4 local connections:
host    all        all        127.0.0.1/32       md5
hostssl all        all        0.0.0.0/0          md5 ←追加

2.SSLを有効化

postgresql.conf
# - SSL -

ssl = on ←コメント解除

設定を有効化

Dockerのコンテナから抜けて、再度コンテナを立ち上げる。

$ docker-compose down
$ docker-compose up -d

パケット確認

WireSharkでパケットを確認し、SSL(TLS)接続していることを確認する。
SSL認証(加工後).png

参考サイト

以下の記事を参考にさせて頂きました。
PostgreSQLを自己証明書でSSLに対応する

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?