Cannot get credentials from systemd store directory when run from command line #1180

Open
opened 2025-11-11 08:34:40 +00:00 by thecodehen · 8 comments

What I'm trying to do and why

I'm trying to run borgamtic list in the command line with borgmatic configs that have systemd credentials in it. The credentials are stored in a custom directory /etc/credstore.encrypted/borgmatic but borgmatic list returns "Failed to read encrypted credential data: No such file or directory" errors.

I've followed the instructions here for creating multiple credentials under a systemd credentials directory.

Steps to reproduce

The relevant config should be:

encryption_passphrase: '{credential systemd borgmatic_pw1}'

zfs:

I am using the zfs hook. The file list in the systemd credstore:

root@ubuntu:/etc/credstore.encrypted/borgmatic# tree
.
├── pw1
└── pw2

1 directory, 2 files

Actual behavior

Running the config through systemd is fine and exists successfully. However, running borgmatic list fails.

root@ubuntu:~# borgmatic list
Failed to read encrypted credential data: No such file or directory
/etc/borgmatic.d/config.yaml: /etc/borgmatic.d/config.yaml: Error getting local Borg version
/etc/borgmatic.d/config.yaml: Command '('systemd-creds', 'decrypt', '/etc/credstore.encrypted/borgmatic_pw1')' returned non-zero exit status 1.
/etc/borgmatic.d/config.yaml: Error running configuration
/etc/borgmatic.d/config.yaml: An error occurred

summary:
An error occurred
/etc/borgmatic.d/config.yaml: Error getting local Borg version
Command '('systemd-creds', 'decrypt', '/etc/credstore.encrypted/borgmatic_pw1')' returned non-zero exit status 1.
Error running configuration

Expected behavior

borgmatic list succeeds.

Other notes / implementation ideas

Seems like there are some edge cases in interpreting the credstore name in the config not considered in the code.

borgmatic version

2.0.11

borgmatic installation method

pipx

Borg version

1.4.1

Python version

No response

Database version (if applicable)

No response

Operating system and version

Ubuntu 24.04.3 LTS

### What I'm trying to do and why I'm trying to run `borgamtic list` in the command line with borgmatic configs that have systemd credentials in it. The credentials are stored in a custom directory `/etc/credstore.encrypted/borgmatic` but `borgmatic list` returns "Failed to read encrypted credential data: No such file or directory" errors. I've followed the instructions [here](https://torsion.org/borgmatic/reference/configuration/credentials/systemd/) for creating multiple credentials under a systemd credentials directory. ### Steps to reproduce The relevant config should be: ``` encryption_passphrase: '{credential systemd borgmatic_pw1}' zfs: ``` I am using the zfs hook. The file list in the systemd credstore: ``` root@ubuntu:/etc/credstore.encrypted/borgmatic# tree . ├── pw1 └── pw2 1 directory, 2 files ``` ### Actual behavior Running the config through systemd is fine and exists successfully. However, running `borgmatic list` fails. ``` root@ubuntu:~# borgmatic list Failed to read encrypted credential data: No such file or directory /etc/borgmatic.d/config.yaml: /etc/borgmatic.d/config.yaml: Error getting local Borg version /etc/borgmatic.d/config.yaml: Command '('systemd-creds', 'decrypt', '/etc/credstore.encrypted/borgmatic_pw1')' returned non-zero exit status 1. /etc/borgmatic.d/config.yaml: Error running configuration /etc/borgmatic.d/config.yaml: An error occurred summary: An error occurred /etc/borgmatic.d/config.yaml: Error getting local Borg version Command '('systemd-creds', 'decrypt', '/etc/credstore.encrypted/borgmatic_pw1')' returned non-zero exit status 1. Error running configuration ``` ### Expected behavior `borgmatic list` succeeds. ### Other notes / implementation ideas Seems like there are some edge cases in interpreting the credstore name in the config not considered in the code. ### borgmatic version 2.0.11 ### borgmatic installation method pipx ### Borg version 1.4.1 ### Python version _No response_ ### Database version (if applicable) _No response_ ### Operating system and version Ubuntu 24.04.3 LTS
Owner

Thanks for filing this! So there's a feature that falls back to using systemd-creds to load a credential when running borgmatic outside of systemd, but it appears that the feature was never tested along with the instructions in the documentation for creating multiple credentials! And therefore the path loaded by borgmatic's systemd-creds invocation is incorrect.

One solution I can think of is to change the documentation to recommend storing borgmatic credentials directly in /etc/credstore.encrypted/ without the borgmatic/ subdirectory ... and with the borgmatic_... prefix. That way, the credential name used in borgmatic's config file would match up with its filesystem path.

A different solution would be for borgmatic to take a credential name like borgmatic_pw1 and replace the _ with a / to make the credential path. That would allow you to leave everything in the borgmatic/ subdirectory, but it's potentially a little less intuitive and a little more brittle.

Thoughts?

Thanks for filing this! So there's a feature that falls back to using `systemd-creds` to load a credential when running borgmatic outside of systemd, but it appears that the feature was never tested along with the instructions in the documentation for creating multiple credentials! And therefore the path loaded by borgmatic's `systemd-creds` invocation is incorrect. One solution I can think of is to change the documentation to recommend storing borgmatic credentials directly in `/etc/credstore.encrypted/` without the `borgmatic/` subdirectory ... and *with* the `borgmatic_...` prefix. That way, the credential name used in borgmatic's config file would match up with its filesystem path. A different solution would be for borgmatic to take a credential name like `borgmatic_pw1` and replace the `_` with a `/` to make the credential path. That would allow you to leave everything in the `borgmatic/` subdirectory, but it's potentially a little less intuitive and a little more brittle. Thoughts?
Author

I think whatever the case, there should only be only one place that stores credentials. For the documentation solution, it seems like there would be a problem if someone stores credentials in both /etc/credstore.encrypted/borgmatic/pw1 and /etc/credstore.encrypted/borgmatic_pw1 where the systemd instance would read the credential from the former file and the cli instance would read the credential from the latter, causing problems and confusion in the future.

I would prefer the second solution, but also we need to add more documentation on how the name is parsed, such as: there cannot be any underscores in the directory name. There may be a better way to store the directory info and parse it as well. I’m not sure how the folder name is being parsed now though, is it being done by borgmatic or systemd? Can you have sub folders in the credential name, e.g., borgmatic/host1/pw1?

If you point me to some relevant parts of the codebase, maybe I can take a stab at this bug as well.

I think whatever the case, there should only be only one place that stores credentials. For the documentation solution, it seems like there would be a problem if someone stores credentials in both `/etc/credstore.encrypted/borgmatic/pw1` and `/etc/credstore.encrypted/borgmatic_pw1` where the `systemd` instance would read the credential from the former file and the cli instance would read the credential from the latter, causing problems and confusion in the future. I would prefer the second solution, but also we need to add more documentation on how the name is parsed, such as: there cannot be any underscores in the directory name. There may be a better way to store the directory info and parse it as well. I’m not sure how the folder name is being parsed now though, is it being done by borgmatic or systemd? Can you have sub folders in the credential name, e.g., borgmatic/host1/pw1? If you point me to some relevant parts of the codebase, maybe I can take a stab at this bug as well.
Owner

I think whatever the case, there should only be only one place that stores credentials. For the documentation solution, it seems like there would be a problem if someone stores credentials in both /etc/credstore.encrypted/borgmatic/pw1 and /etc/credstore.encrypted/borgmatic_pw1 where the systemd instance would read the credential from the former file and the cli instance would read the credential from the latter, causing problems and confusion in the future.

Yeah, that seems problematic.

I would prefer the second solution, but also we need to add more documentation on how the name is parsed, such as: there cannot be any underscores in the directory name. There may be a better way to store the directory info and parse it as well.

There's already a encrypted_credentials_directory option under borgmatic's systemd: option, so in theory the user can already override that path. It does occur to me that adding the following to your configuration would almost work:

systemd:
    encrypted_credentials_directory: /etc/credstore.encrypted/borgmatic

Except then the credential names don't quite match up with their filenames (e.g. pw1 vs borgmatic_pw1). But that could be addressed by renaming the credentials (and then updating the documentation accordingly).

I’m not sure how the folder name is being parsed now though, is it being done by borgmatic or systemd? Can you have sub folders in the credential name, e.g., borgmatic/host1/pw1?

If you point me to some relevant parts of the codebase, maybe I can take a stab at this bug as well.

Here's the relevant code: borgmatic/hooks/credential/systemd.py ... When you borgmatic list outside of systemd, that path is constructed entirely by borgmatic. And sub-folders won't work currently, because there's a single encrypted_credentials_directory used when you run borgmatic outside of systemd (and in fact when you run borgmatic inside a systemd service as well).

Let me know what you decide to do!

> I think whatever the case, there should only be only one place that stores credentials. For the documentation solution, it seems like there would be a problem if someone stores credentials in both `/etc/credstore.encrypted/borgmatic/pw1` and `/etc/credstore.encrypted/borgmatic_pw1` where the `systemd` instance would read the credential from the former file and the cli instance would read the credential from the latter, causing problems and confusion in the future. Yeah, that seems problematic. > I would prefer the second solution, but also we need to add more documentation on how the name is parsed, such as: there cannot be any underscores in the directory name. There may be a better way to store the directory info and parse it as well. There's already a `encrypted_credentials_directory` option under borgmatic's `systemd:` option, so in theory the user can already override that path. It does occur to me that adding the following to your configuration would *almost* work: ```yaml systemd: encrypted_credentials_directory: /etc/credstore.encrypted/borgmatic ``` Except then the credential names don't quite match up with their filenames (e.g. `pw1` vs `borgmatic_pw1`). But that could be addressed by renaming the credentials (and then updating the documentation accordingly). > I’m not sure how the folder name is being parsed now though, is it being done by borgmatic or systemd? Can you have sub folders in the credential name, e.g., borgmatic/host1/pw1? > > If you point me to some relevant parts of the codebase, maybe I can take a stab at this bug as well. Here's the relevant code: [borgmatic/hooks/credential/systemd.py](https://projects.torsion.org/borgmatic-collective/borgmatic/src/branch/main/borgmatic/hooks/credential/systemd.py#L43) ... When you `borgmatic list` outside of systemd, that path is constructed entirely by borgmatic. And sub-folders won't work currently, because there's a single `encrypted_credentials_directory` used when you run borgmatic outside of systemd (and in fact when you run borgmatic inside a systemd service as well). Let me know what you decide to do!

Check out issue #1123
"Use systemd credential only when running as a systemd unit, defer to manual entry otherwise"

Check out issue #1123 "Use systemd credential only when running as a systemd unit, defer to manual entry otherwise"
Owner

Just checking in... @thecodehen, did you plan to submit a PR for this? Either way, please let me know. Thanks!

Just checking in... @thecodehen, did you plan to submit a PR for this? Either way, please let me know. Thanks!
Author

Yep, I think I have a fix by checking if the name has '_' and adding that to the directory path. Is there any branch naming conventions?

Yep, I think I have a fix by checking if the name has '_' and adding that to the directory path. Is there any branch naming conventions?
Author

Ok this is my first time submitting a PR here--let me know any feedback!

Ok this is my first time submitting a PR here--let me know any feedback!
Owner

Yep, I think I have a fix by checking if the name has '_' and adding that to the directory path. Is there any branch naming conventions?

No real branch naming conventions. I generally try to go for descriptive names, but that's not always practical depending on the complexity of the issue.

> Yep, I think I have a fix by checking if the name has '_' and adding that to the directory path. Is there any branch naming conventions? No real branch naming conventions. I generally try to go for descriptive names, but that's not always practical depending on the complexity of the issue.
Sign in to join this conversation.
No milestone
No assignees
3 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
borgmatic-collective/borgmatic#1180
No description provided.