diff --git a/infrastructure/containers/docker/cli/container/logs.md b/infrastructure/containers/docker/cli/container/logs.md new file mode 100644 index 0000000..6db8606 --- /dev/null +++ b/infrastructure/containers/docker/cli/container/logs.md @@ -0,0 +1,25 @@ +# docker container logs + +## 基本構文 + +``` +docker container [OPTION]... logs CONTAINER +``` +- `CONTAINER` ... コンテナ名、もしくはコンテナID + +## 主なオプション +- `--details` ... 詳細を表示 +- `-f`(`--follow`) ... ログを出力し続ける +- `--tail ` ... ログの最後から表示する行数を指定 + +## `docker compose logs`との違い +`docker container logs`: +- コンテナ単位 +- コンテナ名、もしくはコンテナIDで指定したコンテナのログを表示 +- Composeで作成されたコンテナでも構わない + +`docker compose logs`: +- サービス単位 +- コンテナ名やコンテナIDではなく、サービス名で指定したコンテナのログを表示 +- サービス名を指定しない場合、そのcomposeプロジェクトに属する全サービスのログを出力 + diff --git a/infrastructure/containers/docker/cli/container/run.md b/infrastructure/containers/docker/cli/container/run.md new file mode 100644 index 0000000..00ae190 --- /dev/null +++ b/infrastructure/containers/docker/cli/container/run.md @@ -0,0 +1,64 @@ +# docker container run + +`docker container run`は、イメージをもとにコンテナを起動するためのコマンド + +## 基本構文 + +``` +docker container run [OPTION]... IMAGE [COMMAND [ARG]...] +``` + +## 使用例 + +### コンテナに名前を付ける + +### コンテナ終了時に、コンテナを自動削除する + +### バックグラウンドで実行 + +### 標準入力を接続 +``` +ubuntu@localhost:~$ echo hello | docker container run --rm ubuntu cat +ubuntu@localhost:~$ echo hello | docker container run --rm --interactive ubuntu cat +hello +ubuntu@localhost:~$ +``` +- `-i`を指定することで、ホストの標準入力をコンテナのPID1のプロセスの標準入力に接続する +- `-i`のロングオプションは`--interactive` +- コンテナのstdout/stderrはデフォルトでdockerクライアントのstdout/stderrに接続する + +### コンテナ内のプロセスに疑似端末を割り当てる + +実行例1: +``` +tsubanyan@localhost:~$ docker container run --rm --tty ubuntu tty +/dev/pts/0 +tsubanyan@localhost:~$ docker container run --rm ubuntu tty +not a tty +tsubanyan@localhost:~$ +``` + +実行例2: +``` +tsubanyan@localhost:~$ echo "hello" | docker container run --rm -i ubuntu cat +hello +tsubanyan@localhost:~$ echo "hello" | docker container run --rm --tty ubuntu cat +^C +^C +^C +got 3 SIGTERM/SIGINTs, forcefully exiting +tsubanyan@localhost:~$ +``` +- `-t`で疑似端末を割り当てても、`-i`なしだとホスト側からの入力はできない + +実行例3: +``` +tsubanyan@localhost:~$ docker container run --rm -it ubuntu bash +root@93ef64b84948:/# exit +exit +tsubanyan@localhost:~$ +``` +- 対話的に操作できるセッションを作りたい場合は、`-i`と`-t`の療法を指定する +- 慣習的に、`-it`という指定の仕方がよくされる + +### コンテナのポートをホストに公開する \ No newline at end of file diff --git a/infrastructure/containers/docker/compose/cli/logs.md b/infrastructure/containers/docker/compose/cli/logs.md new file mode 100644 index 0000000..23d22d8 --- /dev/null +++ b/infrastructure/containers/docker/compose/cli/logs.md @@ -0,0 +1,12 @@ +# docker compose logs + +## 基本構文 + +``` +docker compose logs [OPTIONS]... [SERVICE]... +``` +- `SERVICE`を省略すると、そのComposeプロジェクトの全サービスが対象になる + +## 主なオプション +- `-f`(`--follow`) ... 追従 +- `--tail N` ... 末尾N行だけ表示 \ No newline at end of file diff --git a/infrastructure/linux/commands/text/cat.md b/infrastructure/linux/commands/text/cat.md new file mode 100644 index 0000000..30446d5 --- /dev/null +++ b/infrastructure/linux/commands/text/cat.md @@ -0,0 +1,40 @@ +# catコマンド + +`cat`(concatenate)は、ファイルや標準入力から読み込んだデータを標準出力に書き出すコマンド + +## 使用例 + +### 最も基本的な使い方 + +``` +cat test.txt +``` + +### 複数ファイルの内容を標準出力に書き出す + +``` +cat file1.txt file2.txt +``` + +### 標準入力からデータを読み込む +標準入力をキーボードから読み込む: +``` +tsubanyan@localhost:~$ cat +hello +hello +world +world +tsubanyan@localhost:~$ +``` +- ファイルを指定せずに実行すると、標準入力からデータを読み込む +- 端末は通常カノニカルモードなので、改行文字が入力され初めて`cat`に渡る + +標準入力をファイルから読み込む: +``` +cat < test.txt +``` +- `<` ... 指定したファイルをコマンドの標準入力に接続するリダイレクト演算子 +- `cat`は標準入力から`test.txt`の内容を読み込み、標準出力に書き出す + + + diff --git a/infrastructure/linux/commands/text/echo.md b/infrastructure/linux/commands/text/echo.md new file mode 100644 index 0000000..86339cb --- /dev/null +++ b/infrastructure/linux/commands/text/echo.md @@ -0,0 +1,51 @@ +# echoコマンド + +`echo`は、指定した文字列や変数の値を標準出力に書き出すコマンド + +## 使用例 + +### 最も基本的な使い方 + +文字列を標準出力に書き出す: +``` +echo "Hello, world!" +``` + +変数の値を標準出力に書き出す: +``` +echo $HOME +``` + +例: +``` +tsubanyan@localhost:~$ echo "Hello, world!" +Hello, world! +tsubanyan@localhost:~$ echo $HOME +/home/tsubanyan +tsubanyan@localhost:~$ +``` + +### 標準出力をファイルに書き込む +ファイルを新規作成・上書き: +``` +echo "Hello" > test.txt +``` +- `>`は標準出力を指定したファイルにリダイレクトする演算子 +- ファイルが存在しない場合は新規作成し、存在する場合は上書き + +ファイルに追記: +``` +echo "World" >> test.txt +``` +- `>>`は標準出力を指定したファイルの末尾に追記する演算子 + +例: +``` +tsubanyan@localhost:~$ echo "Hello" > test.txt +tsubanyan@localhost:~$ echo "World" >> test.txt +tsubanyan@localhost:~$ cat test.txt +Hello +World +tsubanyan@localhost:~$ +``` + diff --git a/infrastructure/mail/protocols/smtp/submission.md b/infrastructure/mail/protocols/smtp/submission.md new file mode 100644 index 0000000..436a000 --- /dev/null +++ b/infrastructure/mail/protocols/smtp/submission.md @@ -0,0 +1,62 @@ +# Submission (Message Submission) + +Submission(Message Submission)は、メールクライアントからメール送信用サーバーにメールを投稿するための仕組み + +## 補足:SMTPの2つの登場機会 + +全体: +``` +Thunderbird + | + | Submission # ここと + | TCP 587 + | SMTP AUTH + v +Postfix + | + | SMTP Relay # ここ + | TCP 25 + v +Gmail SMTP Server + | + v +Gmailのメールボックス +``` + +### 1. クライアントからメールサーバへの送信 +``` +Thunderbird + | + | Submission + | TCP 587 + | SMTP AUTH + v + Postfix +``` +- メールクライアントが送信サーバにメールを提出する処理を、一般に**Message Submission**と呼ばれる +- TCP 587番ポートが標準的に使用される +- 通常SMTP AUTHによるユーザ認証を行い、STARTTLSによって通信を暗号化する +- TCP 465番ポートを使用し、接続開始時からTLSで暗号化する方式もある + +### 2. メールサーバ感の転送 +``` + Postfix + | + | SMTP + | TCP 25 + v +Gmail SMTP Server +``` +- サーバ間のメール転送では、基本的にTCP 25番ポートが使用される +- サーバ間のSMTPでもSTARTTLSによる暗号化が利用される(平文許容されることある) + + + + +## Submissionを使用しないケース + +SMTPサーバをホストするサーバ上で動作するプログラムからメールを送信する場合: +- `sendmail`コマンド ... SMTP使用しない・Submission使用しない +- `localhost:25`に接続 ... SMTP使用する・Submission使用しない +- `localhost:587`に接続 ... SMTP使用する・Submission使用する + diff --git a/infrastructure/mail/security/spf.md b/infrastructure/mail/security/spf.md new file mode 100644 index 0000000..965e7fe --- /dev/null +++ b/infrastructure/mail/security/spf.md @@ -0,0 +1,54 @@ +# SPF(Sender Policy Framework) + +SPF(Sender Policy Framework)は、送信ドメインがDNSのTXTレコードでメールの送信を許可するIPアドレスを公開し、受信側のSMTPサーバが送信元IPアドレスの正当性を検証する仕組み + +## 検証の流れ +1. **メールの送信** ... 送信側のSMTPサーバが、受信側のSMTPサーバにメールを送信 +2. **送信元ドメインの取得** ... 受信側のSMTPサーバが、エンベロープFrom(MAIL FROM)から送信元ドメインを取得 +3. **SPFレコードの取得** ... 送信元ドメインのDNSに問い合わせ、SPFレコード(TXTレコード)を取得 +4. **IPアドレスの照合** ... 実際に接続してきた送信側SMTPサーバのIPアドレスが、SPFレコードで許可されているかを確認 +5. **検証結果の判定** ... 照合結果に応じて、pass(認証成功)やfail(認証失敗)などの結果を返す + +## SPFレコードの設定例 + +特定のIPv4アドレスのみ許可する: +``` +v=spf1 ip4:192.0.2.1 -all +``` +- IPアドレスで指定する場合、`ip4:`もしくは`ip6:`をプレフィックスに用いる + +複数のIPv4アドレスを許可する: +``` +v=spf1 ip4:192.0.2.1 ip4:192:.0.0.2 -all +``` + +Aレコードに登録されたIPアドレスを許可する: +``` +v=spf1 a -all + +v=spf1 a:mail.example.com -all +``` + +MXレコードが指すIPアドレスを許可する: +``` +v=spf1 mx -all +``` + +外部のメール送信サービスを利用する場合: +``` +v=spf1 include:_spf.example.com -all +``` + +## 補足:SPFレコードの識別 + +素朴な疑問: +> SPFレコードってただのTXTレコードだから、複数のTXTレコードがあっても大丈夫なのだろうか + +複数のTXTレコードが登録されている場合: +``` +example.com. IN TXT "v=spf1 ip4:192.0.2.1 -all" +example.com. IN TXT "google-site-verification=xxxxxxxx" +example.com. IN TXT "example-verification=yyyyyyyy" +``` +- `v=spf1`で始まるレコードがSPFレコードとして識別される +- SPFレコードが複数存在する場合はNG diff --git a/infrastructure/mail/software/postfix/deployment/docker-compose.md b/infrastructure/mail/software/postfix/deployment/docker-compose.md new file mode 100644 index 0000000..edc738e --- /dev/null +++ b/infrastructure/mail/software/postfix/deployment/docker-compose.md @@ -0,0 +1,98 @@ +# PostfixのComposeプロジェクト + +## 1. ディレクトリの作成 +``` +sudo mkdir -p /opt/postfix + +sudo chown $USER:$USER /opt/postfix + +cd /opt/postfix +``` + +## 2. compose.ymlの作成 +`compose.yml`: +``` +services: + postfix: + image: boky/postfix:latest + restart: unless-stopped + + hostname: mail.unchioshi.net + + environment: + TZ: Asia/Tokyo + ALLOWED_SENDER_DOMAINS: unchioshi.net + + ports: + - "127.0.0.1:2525:25" +``` +- Dockerイメージは`boky/postfix`(送信用のSMTPリレーのイメージ)を利用 +- `ALLOWED_SENDER_DOMAINS`にはメールの送信元として許可するドメインを指定 +- コンテナのTCP 587番ポートをホスト側の2525番ポートに公開 + +## 3. コンテナの起動 +``` +docker compose up -d +``` + +## 4. 送信テスト +`swaks`をインストール: +``` +sudo apt update + +sudo apt install swaks +``` + +メール送信: +``` +swaks \ + --server localhost \ + --port 2525 \ + --from notification@unchioshi.net \ + --to t.murate1124@gmail.com \ + --header "Subject: Postfix SMTP Test" + --body "Hello from Postfix!" +``` +- `--server ` ... SMTPサーバの接続先 +- `--port ` ... SMTPサーバのポート +- `--from ` ... 送信元メールアドレス +- `--to ` ... 送信先メールアドレス +- `--header
` ... メールヘッダー +- `--body ` ... メール本文 + +## 補足:正引きと逆引きの対応を一致させ、PostfixがSMTP通信で名乗るホスト名も揃える + +正引きと逆引きの対応: +``` +tsubanyan@localhost:~$ dig +short mail.unchioshi.net A +46.250.255.203 +tsubanyan@localhost:~$ dig +short -x 46.250.255.203 +mail.unchioshi.net. +tsubanyan@localhost:~$ +``` +- 対応が取れていない場合は適宜編集する +- 正引きはゾーン情報を編集する +- 逆引きホスト名は、サーバ側の管理画面から設定 + +Postfixのホスト名を合わせる +``` +myhostname = mail.unchioshi.net +``` +- PostfixがSMTP通信で自身を識別する際のホスト名 + +``` +smtp_helo_name = mail.unchioshi.net +``` +- 外部へのSMTP接続で使用するEHLO名 +- 通常`smtp_helo_name`のデフォルト値は`$myhostname`なので明示的に設定する必要はない + +## 補足:SPF・DMARC対策 + +``` +tsubanyan@localhost:~$ dig +short unchioshi.net TXT +"v=spf1 a mx ~all" +tsubanyan@localhost:~$ dig +short _dmarc.unchioshi.net TXT +"v=DMARC1; p=none" +tsubanyan@localhost:~$ +``` +- ゾーン情報を編集して設定 \ No newline at end of file diff --git a/infrastructure/mail/software/postfix/deployment/native-installation.md b/infrastructure/mail/software/postfix/deployment/native-installation.md new file mode 100644 index 0000000..e69de29 diff --git a/software/identity/authentik/docker-compose.yml b/software/identity/authentik/docker-compose.yml new file mode 100644 index 0000000..773eeae --- /dev/null +++ b/software/identity/authentik/docker-compose.yml @@ -0,0 +1,110 @@ +# authentik + +## サーバーを立てる手順 + +``` +mkdir -p /opt/authentik +chown $USER:$USER /opt/authentik + +# 公式 compose を取得 +wget https://docs.goauthentik.io/compose.yml +# curl を使うなら: +# curl -O https://docs.goauthentik.io/compose.yml + +# .env を作成 +echo "PG_PASS=$(openssl rand -base64 36 | tr -d '\n')" >> .env +echo "AUTHENTIK_SECRET_KEY=$(openssl rand -base64 60 | tr -d '\n')" >> .env + +# 必要なら公開ポート変更 +# echo "COMPOSE_PORT_HTTP=80" >> .env +# echo "COMPOSE_PORT_HTTPS=443" >> .env + +# イメージ取得と起動 +docker compose -f compose.yml pull +docker compose -f compose.yml up -d +``` + +## 初期セットアップ +``` +http://<サーバのIPまたはホスト名>:9000/if/flow/initial-setup/ +``` +もしくは、 +``` +docker compose exec server ak changepassword akadmin +``` +- 注意:サーバ立ち上がった後に実行 +- `docker compose exec` ... 起動中コンテナの中でコマンド実行 +- `server` ... compose内のauthentik serverサービス名 +- `ak` ... authentikの管理CLIコマンド +- `changepassword` ... パスワード変更サブコマンド +- `akadmin` ... 対象ユーザー名(初期管理者) + +実行イメージ +``` +docker compose exec server ak changepassword akadmin +( 大量のログ ) +Changing password for user 'akadmin' +Password: +Password (again): +( ログ ) +Password changed successfully for user 'akadmin' +``` + +## 補足:Nginxの設定 +`/etc/nginx/sites-available/auth.unchioshi.net`: +``` +map $http_upgrade $connection_upgrade { + default upgrade; + '' close; +} + +server { + listen 443 ssl http2; + server_name auth.unchioshi.net; + + ssl_certificate /etc/letsencrypt/live/unchioshi.net/fullchain.pem; + ssl_certificate_key /etc/letsencrypt/live/unchioshi.net/privkey.pem; + + client_max_body_size 16m; + + location / { + proxy_pass http://127.0.0.1:9000; + + proxy_http_version 1.1; + + proxy_set_header Host $host; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Host $host; + + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection $connection_upgrade; + + proxy_read_timeout 300; + proxy_send_timeout 300; + } + + location /-/metrics/ { + deny all; + return 404; + } +} +``` + +サーバー証明書: +``` +ubuntu@localhost:~$ sudo certbot certificates +Saving debug log to /var/log/letsencrypt/letsencrypt.log + +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Found the following certs: + Certificate Name: unchioshi.net + Serial Number: 6a9796023640060b9b76f0b64f322bb1122 + Key Type: ECDSA + Domains: unchioshi.net auth.unchioshi.net git.unchioshi.net img.unchioshi.net wiki.unchioshi.net + Expiry Date: 2026-12-20 04:38:25+00:00 (VALID: 88 days) + Certificate Path: /etc/letsencrypt/live/unchioshi.net/fullchain.pem + Private Key Path: /etc/letsencrypt/live/unchioshi.net/privkey.pem +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +ubuntu@localhost:~$ +``` \ No newline at end of file