ユーザー権限でsystemd-credsを使う
初期化。これを実行すると /var/lib/systemd/credential.secret に秘密鍵が作られる。
systemd-creds setupところで systemd-creds setup は何もしなくてもホスト鍵が用意された。デフォルトでは--with-key=auto で、これは可能であればTPMとファイルの2つを使ってシークレットを暗号化または複合する。確証はないけど、デフォルトだと困る場合に setup を使えばいいのだろう。
シークレットを暗号化するには systemd-creds の encrypt サブコマンドを使う。
systemd-creds --user --name=secstore.secret encrypt secstore.txt ~/lib/secstore.key暗号化されたファイルは decrypt サブコマンドで複合できる。
systemd-creds --user --name=secstore.secret decrypt ~/lib/secstore.key別のユーザーを指定する場合は —uid= オプションを使う。—user オプションが指定されていて —uid が無い場合は —uid=self が暗黙的に使われる。
TPMを更新したとき
Section titled “TPMを更新したとき”再作成が必要。systemd-creds setup の再実行は必要ないので systemd-creds —user で暗号化やりなおす。
ユニットからシークレットを参照する
Section titled “ユニットからシークレットを参照する”上記の方法で secstore.key に暗号化された状態のシークレットが入るので、ユニットからシークレットを参照するとき以下どちらかのディレクティブを使う。
- LoadCredentialEncrypted
- SetCredentialEncrypted
これらのディレクティブについてはsystemdユニットファイルのディレクティブを参照してほしいが、例えば LoadCredentialEncrypted の場合(%h や %d は systemdのユニットで利用できる変数にある)、
[Service]LoadCredentialEncrypted=secstore.secret:%h/lib/secstore.keyExecStart=wc %d/secstore.secretテストしたい場合は systemd-run で確認できる。
$ systemd-run --user -P --wait -p LoadCredentialEncrypted=secstore.secret:~/lib/secstore.key -t bashRunning as unit: run-u193.servicePress ^] three times within 1s to disconnect TTY.
bash$ cat $CREDENTIALS_DIRECTORY/secstore.secretsystemd 257以前
Section titled “systemd 257以前”systemd 257以前では、ユーザーユニットから LoadCredentialEncrypted パラメータを参照するとエラーになる。ただし systemd-creds を使った複合は可能なので、ユニット内でコマンドを呼べばどうにかなる。
ExecStartPost=bash -c 'systemd-creds --user decrypt --name=secstore.secret %h/lib/secstore.key'パイプの一部として扱う場合、systemd-creds には、余計なお節介をしないように —newline=no や —no-pager などオプションを与えたほうが安全かもしれない。