-
Notifications
You must be signed in to change notification settings - Fork 23
PBM-1268 Securely Store CLI Credentials using systemd #348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
faf8831
b64558e
de45d28
c65721d
97d4af4
0ceb2b8
77ab1b1
c9388cb
a61e122
1139738
db640c0
79184c1
97075f7
bfe771b
69131bb
2de73dc
f085235
4790bcd
9ff2c65
e2242ff
3c5b7b9
c30b52c
bd1b2ab
b5a6dc9
4bd9902
7edfaed
f902c6c
46db4f5
5bf1ba4
19bc213
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| # Secure credential management with systemd | ||
|
Check warning on line 1 in docs/install/secure-credentials-systemd.md
|
||
|
|
||
| ## Overview | ||
|
|
||
| This guide describes how to securely store the **MongoDB connection URI** (`mongodb-uri`) for `pbm-agent` using **systemd service credentials**. The same technique can be applied to other sensitive PBM credentials, such as object storage credentials in the PBM configuration. | ||
|
Check warning on line 5 in docs/install/secure-credentials-systemd.md
|
||
|
|
||
| By default, credentials are often stored in plaintext in environment variables or configuration files. This introduces security risks such as credential leakage and unauthorized access. | ||
|
Check warning on line 7 in docs/install/secure-credentials-systemd.md
|
||
|
|
||
| ## Why use systemd credentials? | ||
|
Check warning on line 9 in docs/install/secure-credentials-systemd.md
|
||
|
|
||
| Storing credentials in plaintext significantly increases the risk of compromise. Secrets placed in configuration files or environment variables can be exposed through: | ||
|
Check warning on line 11 in docs/install/secure-credentials-systemd.md
|
||
|
|
||
| - File access | ||
| - Process inspection (e.g., `ps`, `/proc`) | ||
|
Check failure on line 14 in docs/install/secure-credentials-systemd.md
|
||
| - Unauthorized system access. | ||
|
|
||
| **`systemd` credentials** mitigate these risks by: | ||
|
|
||
| - Encrypting credentials at rest | ||
| - Decrypting them only at service runtime | ||
| - Storing them in a temporary, non-swappable directory | ||
| - Restricting access to the service itself | ||
|
|
||
|
|
||
| ## Prerequisites | ||
|
|
||
| - systemd version 250 or higher. | ||
|
Check warning on line 27 in docs/install/secure-credentials-systemd.md
|
||
|
|
||
| To check the installed `systemd` version on your system, run: | ||
|
|
||
| ```bash | ||
| systemctl --version | ||
| ``` | ||
|
|
||
| ??? example "Output" | ||
|
|
||
| ```sh | ||
| systemd 252 (252.5-2ubuntu3) | ||
| +PAM +AUDIT +SELINUX ... | ||
| ``` | ||
|
|
||
| The following operating systems meet this requirement: | ||
|
|
||
| - RHEL/OL/Rocky Linux 9 | ||
| - Ubuntu 24.04 | ||
| - Debian 12 | ||
| - Amazon Linux 2023 | ||
|
|
||
| - Root or sudo privileges | ||
|
Check warning on line 49 in docs/install/secure-credentials-systemd.md
|
||
| - (Optional) TPM2 support for hardware-backed encryption | ||
| - Kernel 5.4+ | ||
|
|
||
|
|
||
| ## Procedure | ||
|
|
||
| Here are the steps to integrate PBM with systemd's [System and service credentials :octicons-link-external-16:](https://systemd.io/CREDENTIALS/) | ||
|
Check warning on line 56 in docs/install/secure-credentials-systemd.md
|
||
| {.power-number} | ||
|
|
||
| 1. Create a **temporary PBM agent YAML config file** containing the `mongodb-uri` key with your connection string: | ||
|
|
||
| ```sh | ||
| echo "mongodb-uri: mongodb://pbmuser:secretpwd@localhost:27017/?authSource=admin" > pbm_uri.yaml | ||
| ``` | ||
|
|
||
| 2. Encrypt the credential using the local system key (and Trusted Platform Module version 2.0 if available): | ||
|
|
||
| ```sh | ||
| systemd-creds encrypt --name=pbm_connection.yaml pbm_uri.yaml pbm_connection.yaml.cred | ||
| ``` | ||
|
|
||
| 3. Securely delete the plain text file: | ||
|
|
||
| ```sh | ||
| shred -u pbm_uri.yaml | ||
| ``` | ||
|
|
||
| 4. Edit the systemd unit file (for example, `/lib/systemd/system/pbm-agent.service` on Debian/Ubuntu or `/usr/lib/systemd/system/pbm-agent.service` on RHEL-based distributions). In the `[Service]` section, add the `LoadCredentialEncrypted` and `PrivateMounts` directives, and update `ExecStart` to pass the decrypted credential file to `pbm-agent`: | ||
|
Check warning on line 77 in docs/install/secure-credentials-systemd.md
|
||
|
|
||
| ``` | ||
| [Service] | ||
| LoadCredentialEncrypted=pbm_connection.yaml:/path/to/pbm_connection.yaml.cred | ||
| PrivateMounts=yes | ||
| ExecStart=/usr/bin/pbm-agent -f %d/pbm_connection.yaml | ||
|
||
| ``` | ||
rasika-chivate marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 5. Reload the systemd manager configuration: | ||
|
Check warning on line 86 in docs/install/secure-credentials-systemd.md
|
||
|
|
||
| ```sh | ||
| sudo systemctl daemon-reload | ||
| ``` | ||
|
|
||
| 6. Restart the PBM agent: | ||
|
|
||
| ```sh | ||
| sudo systemctl restart pbm-agent | ||
| ``` | ||
|
|
||
| ??? info "What happens under the hood" | ||
| Systemd automatically decrypts the credential during service startup and places it in a temporary, non-swappable directory. | ||
|
Check warning on line 99 in docs/install/secure-credentials-systemd.md
|
||
|
|
||
| - The path to the decrypted plaintext is stored in the `$CREDENTIALS_DIRECTORY` environment variable. | ||
| - In the example above, the PBM agent will read the YAML configuration file at `%d/pbm_connection.yaml`, which contains the `mongodb-uri` setting with its connection string. | ||
|
|
||
|
|
||
| ## How to verify? | ||
|
|
||
| Run the following command to ensure the service can see the credential. This file is only accessible while the service is running and is stored in a secure, temporary directory. | ||
|
|
||
| ```sh | ||
| sudo cat /run/credentials/pbm-agent.service/pbm_connection.yaml | ||
| ``` | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this be fixed according to Vale's suggestion?