linuxfabrik.lfops.systemd_credential¶
Manage a systemd credential in the system or a user's credential store
Synopsis¶
- Stores a secret as a systemd credential, so that a service can load it with
LoadCredentialEncrypted=orLoadCredential=and read it from$CREDENTIALS_DIRECTORY/<name>. - With systemd 251 or newer, the secret is encrypted with
systemd-creds encryptand stored in/etc/credstore.encrypted/<name>, whereLoadCredentialEncrypted=<name>finds it without a path. - With an older systemd (for example RHEL 8 or Ubuntu 22.04), the secret is stored in plain text in
/etc/credstore/<name>, readable by root only. A consumer that reads${CREDENTIALS_DIRECTORY:-/etc/credstore}/<name>works with both. - Idempotent: an encrypted credential is decrypted on the host and compared with content. It is only encrypted again if the content differs or the file cannot be decrypted, for example after the host key was lost.
systemd-creds encryptproduces a different file on every call, so comparing files would always report a change. - The secret is passed to
systemd-credson stdin, never on the command line, and the module never returns it. - Switching between the two stores, for example after an upgrade from RHEL 8 to RHEL 9, removes the credential from the store that is no longer used.
- With scope=user, the module runs as the user whose services load the credential (
become_user), encrypts it withsystemd-creds encrypt --userand stores it in~/.config/credstore.encrypted/<name>, owned by that user. This needs systemd 256 or newer; there is no plain text fallback. With systemd 257, the per-user service manager cannot load such a credential withLoadCredentialEncrypted=(it decrypts in-process and has no access to the host key, systemd 258 askssystemd-creds.socketinstead), so the service decrypts it itself, for example inExecStartPre=withsystemd-creds decrypt --user --name=<name> <path> <file below the runtime directory>. - The store directory is kept at mode
0700and the credential at0600, both owned by root (by the user with scope=user). A symlink in place of the store directory makes the module fail, a symlink or other non-regular file in place of the credential is replaced. Files are written to a temporary file in the store, synced and renamed, so a service never loads a half-written credential.
Available since LFOps 10.0.0.
Requirements¶
- systemd.
systemd-credsfor the encrypted store.
Mandatory Parameters¶
name
- Name of the credential, also used as the file name in the credential store and embedded in the encrypted file. Letters, digits,
_,-and.only. - Type: String.
Optional Parameters¶
content
- The secret. Stored byte for byte as given, without adding a trailing newline. Required and must not be empty with state=present.
- Type: String.
scope
systemstores the credential for system services in/etc/credstore.encrypted/(or/etc/credstore/).userstores it for the services of the user the module runs as, for example rootless Podman containers, in~/.config/credstore.encrypted/.- Type: String. One of
system,user. - Default:
system
state
- Whether the credential should exist.
absentremoves it from both stores. - Type: String. One of
absent,present. - Default:
present
Examples¶
- name: 'Store the dump login as a systemd credential'
linuxfabrik.lfops.systemd_credential:
name: 'mariadb-dump-login.cnf'
content: |
[client]
user = "mariadb-dump"
password = "linuxfabrik"
register: 'dump_login_credential_result'
# The unit loads the credential from the store the module chose. The template contains:
# {% if dump_login_credential_result['encrypted'] %}
# LoadCredentialEncrypted=mariadb-dump-login.cnf
# {% endif %}
# and the script reads ${CREDENTIALS_DIRECTORY:-/etc/credstore}/mariadb-dump-login.cnf
- name: 'Deploy /etc/systemd/system/mariadb-dump.service'
ansible.builtin.template:
src: 'etc/systemd/system/mariadb-dump.service.j2'
dest: '/etc/systemd/system/mariadb-dump.service'
owner: 'root'
group: 'root'
mode: 0o644
- name: 'Remove the credential'
linuxfabrik.lfops.systemd_credential:
name: 'mariadb-dump-login.cnf'
state: 'absent'
# For the services of a rootless user, e.g. a Podman container run by "whiteboard"
- name: 'Store the JWT secret as a user-scoped systemd credential'
linuxfabrik.lfops.systemd_credential:
name: 'jwt-secret-key'
content: 'linuxfabrik'
scope: 'user'
become: true
become_user: 'whiteboard'
Return Values¶
encrypted
- Whether the credential is stored encrypted (systemd 251 or newer, always with scope=user). Use it to decide between
LoadCredentialEncrypted=and the plain fallback. - Type: Bool.
- Returned: always.
- Sample:
True
path
- Path of the credential in the credential store. Empty with state=absent.
- Type: String.
- Returned: always.
- Sample:
/etc/credstore.encrypted/mariadb-dump-login.cnf
scope
- The scope the credential is stored in.
- Type: String.
- Returned: always.
- Sample:
system
systemd_version
- Major version of the running systemd service manager.
- Type: Number.
- Returned: always.
- Sample:
252
Notes¶
- A service reads its credentials only when it starts. Restart it when the module reports a change.
- The encrypted credential is bound to the host key
/var/lib/systemd/credential.secretand, if the host has one, to its TPM2 chip. On a host without a TPM2 chip, a full copy of the disk is enough to decrypt it. - With a TPM2 chip,
systemd-credsalso binds the credential to the Secure Boot state (PCR 7). After a firmware, shim or Secure Boot update, the service cannot decrypt it and fails to start until the module runs again and re-encrypts it. - Check mode is supported. Diff mode is not, since a diff would reveal the secret.
- Has to run as root with scope=system, and as the user, not root, with scope=user.
- The version of the running service manager decides between the two stores, since it is the one that loads the credential.
Authors¶
- Linuxfabrik GmbH, Zurich, Switzerland, https://www.linuxfabrik.ch