はじめに

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

環境

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

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

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

構築

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

1
2
3
├── compose.yml
└── mailpit-data
    └── authfile

compose.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
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

volumes に ユーザー名とパスワードを記載いた認証用のファイルをマウントする。   ※ https://hub.docker.com/r/axllent/mailpit の下部に記載の Setting Mailpit options の通りに、/data/にマウント。

authfile

Mailpit supports multiple users & passwords in a single password file (plain text file), and the passwords encoded in the following formats: ・Plain text ・SSHA ・MD5Crypt ・APR1Crypt ・SHA ・Bcrypt ・Crypt with SHA-256 and SHA-512

https://mailpit.axllent.org/docs/configuration/passwords/

使用できるエンコードは上記となる。 今回は、Bcryptを使用する。

下記のツールを利用してユーザー名、パスワードを作成できる。 https://rakko.tools/tools/20/

authfileは以下で設定。
ユーザー名: user
パスワード: password

1
user:$2y$10$8ugWB3dbfyYsmC7APYlZte7jtFq6XiCts1IFtizfyWK6rsds.cpE6

※接頭辞については、Linuxshadowファイルと同じっぽい。   https://www.server-memo.net/centos-settings/system/passwd_shadow.html

SHA-256を使う場合であれば、下記のようなコマンドで生成できる。(-5はSHA-256, -6はSHA-512)

1
openssl passwd -5 -salt=rja5hy8u password

実行すると以下が出力される。

1
$5$rja5hy8u$O1JmEV3HAGpRB4ujGpZW2QEsgXveDFdxwLlFCgkvXC1

ちなみに、以下の書式となっている。

1
$5$saltstring$hashedpassword

コンテナの立ち上げ

1
docker compose up -d

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

試す

http://localhost:8025 にアクセスをし、BASIC認証が表示されることを確認する。   basic-auth-01

ユーザー名 (user) とパスワード (password)を入力して、mailpitの画面に遷移できれば確認はOK

basic-auth-01

参考

おわりに

パスワードの書式についても知れたので良かった。
Linuxの/etc/passwd, /etc/shadowについても勉強していきたい。