はじめに

mailpitにBASIC認証をつけたい場面があったのでメモ書き。

環境

1
2
Docker version 27.1.1, build 6312585
Docker Compose version v2.29.1-desktop.1

BASIC認証はサポートされているのか

上記ページを見ると、BASIC認証の設定方法が記載されているので設定は可能なようだ。

暗号アルゴリズムの参考

公開鍵,秘密鍵,CSR,証明書の作成

秘密鍵の作成

1
openssl genrsa -out private.key 2048

公開鍵の作成

1
openssl rsa -in private.key -pubout -out public.key
1
writing RSA key

が出て、public.keyが出力されていればOK

CSRの作成

1
openssl req -new -key private.key -out server.csr

適当に作成する↓

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
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]:
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) []: localhost
Email Address []:

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

証明書作成

1年で証明書を作成する。

1
openssl x509 -days 365 -req -sha256 -signkey private.key < server.csr > server.crt
1
2
Certificate request self-signature ok
subject=C = AU, ST = Some-State, O = Internet Widgits Pty Ltd, CN = localhost

構築

最終的なファイルツリーは以下のような想定をする。

1
2
3
4
5
6
7
├── compose.yml
└── mailpit-data
    ├── authfile
    ├── private.key
    ├── public.key
    ├── server.crt
    └── server.csr

compose.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
services:
  mailpit:
    image: axllent/mailpit:v1.20.1
    container_name: mailpit
    restart: unless-stopped
    ports:
      - 8025:8025
    volumes:
      - ./mailpit-data:/data
    environment:
      - MP_UI_AUTH_FILE=/data/authfile
      - MP_UI_TLS_CERT=/data/server.crt
      - MP_UI_TLS_KEY=/data/private.key

volumes に 証明書をマウントして /data/に入れておく。

コンテナの立ち上げ

1
docker compose up -d

でコンテナを立ち上げる。

試す

http://localhost:8025 にアクセスをする。
access-mailpit-01

Client sent an HTTP request to an HTTPS server. となり、HTTPでのアクセスは上記の画面になる。

https://localhost:8025 にアクセスをする。
access-mailpit-02 access-mailpit-03

httpsでのアクセスができているのでOK

参考

おわりに

BASIC認証はつけたけど、httpのアクセスとなっていたのでhttpsでのアクセスをできるようにした。
mailpitは基本的にローカルで利用するので,公開することはないけど知っておきたいのでやってみた。