Use systemd credential only when running as a systemd unit, defer to manual entry otherwise #1123
Labels
No labels
blocked
breaking
bug
data loss
design finalized
good first issue
new feature area
question / support
security
waiting for response
No milestone
No assignees
3 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
borgmatic-collective/borgmatic#1123
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
What I'd like to do and why
Borgmatic allows users to pass systemd credential instead of manually entering the repo passphrase by adding
encryption_passphrase: "{credential systemd borgmatic.pw}" to the config.yaml file. Now, when systemd invokes borgmatic, it pulls the repo password from a secure systemd credential file. However, when the user manually invokes Borgmatic, it will fail since it can't pull the password since it not running as a systemd unit. Ideally, the user would still be able to manually enter a password when manually invoking Borgmatic.
This issue is noted in the documentation under "provide-your-passwords":
"Be aware that when using this systemd {credential ...} feature, you may no longer be able to run certain borgmatic actions outside of the systemd service, as the credentials are only available from within the context of that service. So for instance, borgmatic list necessarily relies on the encryption_passphrase in order to access the Borg repository, but list shouldn't need to load any credentials for your database or monitoring hooks."
Other notes / implementation ideas
Adding the following three lines to borgmatic.hooks.credential.systemd.load_credential before checking for the credentials_directory is a crude check to see if the command is running as a systemd unit or from a user command:
is_systemd_unit = os.environ.get("INVOCATION_ID") is not None
if not is_systemd_unit:
return None
The code checks for an INVOCATION_ID environment variable, which is set by systemd. If the environment variable is defined, the process is probably a systemd unit. If the environment variable is not set, the process is not a systemd unit, the load_credential hook returns None before trying to read the credential file, and then defers to user input for the repo password
I've just run into the same issue! Is there a known work-around, so I can run
borgmatic listand validate my backups are working in the meantime?Just to share a workaround I figured out: instead of using
encryption_passphrase: "{credential systemd borgmatic.pw}", you can use:This decrypts the password and outputs it to stdout. I've tested it manually (e.g. with
borgmatic list) and via systemd (withsystemctl start borgmatic.service. Will double check it runs fine on its own tomorrow morning, but it's looking promising!(Note that the location of the credential file may differ on your system so just double check)
Thanks for sharing the workaround. I may be able to update the systemd hook so that it automatically falls back to
systemd-creds decryptwhenCREDENTIALS_DIRECTORYis not set. This would have the benefit of not requiring the user to type in any credentials manually (as it sounds like is the case with theINVOCATION_IDcheck), but the downside is that it would probably only work when run as root. (Or I suppose it could detect whether it's run as root and add--useras appropriate, but that would only work if the credential was created as the same non-root user.)Another caveat is that it would either have to hardcode the
/etc/credstore.encrypted/path (or make that default overridable with a configuration option).Okay, this is implemented in main and will be part of the next release! As part of this feature, I added configuration options to override the default
systemd-credscommand and/or the/etc/credstore.encrypteddirectory. Please let me know how it works for you in practice.Wow, amazing, thank you @witten! I'll try it out and let you know how I get on.
Released in borgmatic 2.0.9!