LoginSignup
1
0

MattermostのNginxのログにユーザーIDを埋め込む

Last updated at Posted at 2024-03-25

毎度、ググっても出てこない小ネタを取り扱っております。
本記事は個人的な見解であり、筆者の所属するいかなる団体にも関係ございません。

0. はじめに

Mattermostを社内で利用しておりますが、アクセスログがイケていません。
何がイケてないかというと、アクセスログにMattermostのユーザーIDが入ってないのですね。
これだと誰がアクセスしたログなのか全く分かりません。

そこで、MattermostのNginxのログにユーザーIDを埋め込んだ、
という話になります。

1. 通常のNginxのログ

通常のMattermostのNginxのアクセスログは以下のような感じです。(IPアドレスはダミーです)

126.88.999.387 - - [25/Mar/2024:00:01:21 +0900] "POST /api/v4/users/status/ids HTTP/2.0" 200 818 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36" "-"
126.88.999.387 - - [25/Mar/2024:00:01:21 +0900] "POST /api/v4/users/ids?since=1711292401478 HTTP/2.0" 200 2 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36" "-"
126.88.999.387 - - [25/Mar/2024:00:01:21 +0900] "POST /api/v4/users/status/ids HTTP/2.0" 200 818 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36" "-"
138.64.888.345 - - [25/Mar/2024:00:01:29 +0900] "GET /api/v4/users/me/sessions HTTP/2.0" 200 1486 "-" "Mattermost Mobile/2.14.0+6000509 (Android; 14; Pixel 7a)" "-"
113.35.555.667 - - [25/Mar/2024:00:01:32 +0900] "POST /api/v4/users/status/ids HTTP/2.0" 200 986 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36" "-"
133.35.555.667 - - [25/Mar/2024:00:01:42 +0900] "POST /api/v4/users/status/ids HTTP/2.0" 200 711 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36" "-"

本来は、2つ目のハイフンのところにユーザーIDが入ってほしいところですが、残念ながら入りません。
これでは、誰がアクセスしたのか全然分かりません。

2. $cookie_MMUSERIDをログフォーマットに追加する

$cookie_MMUSERIDをログフォーマットに追加するとcookieに入っているユーザーIDをログに出力する事ができます。どこに入れても良いのですが、別のログファイルにするのも面倒ですし、mainのログフォーマットに追記してしまいましょう。

  • 変更前
/etc/nginx/nginx.conf
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
  • 変更後
/etc/nginx/nginx.conf
    log_format  main  '$remote_addr - $remote_user $cookie_MMUSERID [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

$remote_userの後に$cookie_MMUSERIDを追加しました。

3. configをチェックしてreload

設定を変えたらチェックしましょう。

sudo nginx -t

以下のように出れば問題ないはずです。

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

念のため、sudo nginx -Tで出力される設定も見ておくとよいでしょう。

以下のコマンドでreloadします。
(リロードだけで大丈夫です)

sudo systemctl reload nginx

4. まとめ

以外に上記の設定の話が日本語ではググっても出てきませんでした。
セキュリティー的には気をつけた方が良いと思うのですが、どうなんでしょうね。

5. 参考資料

[TIP] Capturing Mattermost User IDs with Nginx - Troubleshooting - Mattermost Discussion Forums
https://forum.mattermost.com/t/tip-capturing-mattermost-user-ids-with-nginx/10382

6. 蛇足

ちなみにですが、ChatGPT(GPT-4)、Gemini(Pro)、Claud3(Opus)、Perprexity(Pro)、全部嘘ばっかりでした。

聞いたプロンプト

Mattermostのomnibusインストールで、利用者のIDをnginxのaccessログに埋め込む方法を教えてください。
1
0
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
1
0