Skip to content

Conversation

@mks-h
Copy link

@mks-h mks-h commented Aug 2, 2025

SUMMARY

Add support for specifying the output file in systemd_creds_encrypt, since it is the only way to create an encrypted systemd credential stored in a file for use with LoadCredentialEncrypted option in systemd service files. Writing the regular output of systemd_creds_encrypt into a file using the ansible.builtin.copy module does not work, since the output is different to what systemd-creds writes to the file.

Adds the dest option to control the "output" argument of the underlying "systemd-creds" command.

ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

systemd_creds_encrypt

ADDITIONAL INFORMATION
     --name=name
           When specified with the encrypt command controls the credential name to embed in the encrypted credential data. If not
           specified, the name is chosen automatically from the filename component of the specified output path. If specified as
           empty string no credential name is embedded in the encrypted credential, and no verification of credential name is done
           when the credential is decrypted.

@ansibullbot
Copy link
Collaborator

@ansibullbot ansibullbot added feature This issue/PR relates to a feature request integration tests/integration module module new_contributor Help guide this first time contributor plugins plugin (any type) tests tests needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR labels Aug 2, 2025
@mks-h mks-h force-pushed the creds_encrypt_into_file branch from b686724 to 154a588 Compare August 2, 2025 14:24
@ansibullbot ansibullbot removed the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR label Aug 2, 2025
@felixfontein felixfontein added check-before-release PR will be looked at again shortly before release and merged if possible. backport-11 Automatically create a backport for the stable-10 branch labels Aug 2, 2025
Copy link
Collaborator

@felixfontein felixfontein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution!

This parameter changes the behavior of the module quite a lot: before the module simply encrypted a secret and returned it. It was up to the user to decide whether the encryption needs to be done, and there is no idempotency check.

With a dest parameter, the common expectation for an Ansible module is that the module performs an idempotency check. This is not done by the module right now. Also check mode is not respected: the destination file is always written, even in check mode.

This likely needs some more changes to the module and to the documentation.

@mks-h mks-h force-pushed the creds_encrypt_into_file branch 2 times, most recently from 8f2b059 to 6d85d71 Compare August 3, 2025 06:40
@ansibullbot

This comment was marked as outdated.

@ansibullbot ansibullbot added the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR label Aug 3, 2025
@mks-h mks-h force-pushed the creds_encrypt_into_file branch from a4cc451 to 4801bc1 Compare August 3, 2025 11:59
@ansibullbot ansibullbot removed the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR label Aug 3, 2025
@mks-h mks-h force-pushed the creds_encrypt_into_file branch from 4801bc1 to 8e3fa98 Compare August 3, 2025 12:56
@mks-h
Copy link
Author

mks-h commented Aug 3, 2025

The last commit is implemented wrong, I'll be redoing it. I'm currently blocked on this bug in Fedora — I can't verify whether systemd-creds cat (or list) command gives any useful metadata about options used to create the credential (such as timestamp, not-after, user). I'll continue on maybe next weekend by trying to run these commands in containers of other distributions.

Copy link
Collaborator

@russoz russoz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @mks-h

Thanks for your contribution! I second @felixfontein concerns about idempotency, but also adding a couple of comments here.

Comment on lines +169 to 166
if name is not None:
encrypt_cmd.append("--name=" + name)
else:
elif not dest:
encrypt_cmd.append("--name=")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be interesting to add one (or more) testcase that expose what happens when name="".

@ansibullbot ansibullbot added the stale_ci CI is older than 7 days, rerun before merging label Aug 14, 2025
that:
- "encrypted_dest_again.changed"
fail_msg: "The task didn't change, but should have"
success_msg: "The task changed, as it should have"
Copy link
Contributor

@konstruktoid konstruktoid Aug 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps add an decryption test/options as well? That's https://github.com/ansible-collections/community.general/blob/main/plugins/modules/systemd_creds_decrypt.py on the other hand

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mks-h mks-h force-pushed the creds_encrypt_into_file branch from 8e3fa98 to 2f308b2 Compare August 31, 2025 05:27
@ansibullbot ansibullbot removed the stale_ci CI is older than 7 days, rerun before merging label Aug 31, 2025
Copy link
Collaborator

@russoz russoz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @mks-h thanks for your continued work on this.

Left another couple of comments, and @konstruktoid also left comments in the test, but I must confess I did not delve into those. If you could be as kind as to take a look :-)

Other than these minor things, it is looking good to me.

Comment on lines +35 to +39
When using the O(dest) option, you can set this to an empty
string ("") to prevent C(systemd-creds encrypt) from using
filename from O(dest) as the credential name. Conversely,
leave out the O(name) option to let C(systemd-creds encrypt)
use the filename from O(dest) as the credential name.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this note might be better placed in dest below - both cases described required that dest is set.

Also, maybe a more affirmative wording might help. Something like (inserted be low in dest's description):

- When this is set and O(name=""), then the credential_name is XXXXX (not clear to me the right answer here)
- When this is set and O(name) is not passed, then credential name will take the value of O(dest).
- When this is set and O(name) is also set with a non-empty name, then credential name will take the value of O(name) (???? will it???)

Or similar - take this with a pinch of salt.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think?

@mks-h mks-h force-pushed the creds_encrypt_into_file branch from 2f308b2 to 74e2aa1 Compare August 31, 2025 15:52
@mks-h
Copy link
Author

mks-h commented Aug 31, 2025

The only way I can prove idempotence is to create a new secret each time, and see if the resulting file differs from the existing one. This should result in the files being the same so long as the "timestamp" is explicitly provided. If it isn't, each encryption will result in a different file, since the default is current time.

@felixfontein
Copy link
Collaborator

How about decrypting the encrypted file and comparing its contents with the original file?

In any case, I'm wondering whether this should better be a new module (systemd_creds_encrypt_file) instead of a new part of the systemd_creds_encrypt module, since the new functionality behaves quite different from the existing.

@mks-h
Copy link
Author

mks-h commented Aug 31, 2025

How about decrypting the encrypted file and comparing its contents with the original file?

This only verifies the contents, not the attributes. There's no way to read the embedded attributes from a secret. Plus, I'd say it is safer to not decrypt sensitive data — the principle of least privilege, in a way.

In any case, I'm wondering whether this should better be a new module (systemd_creds_encrypt_file) instead of a new part of the systemd_creds_encrypt module, since the new functionality behaves quite different from the existing.

The current functionality doesn't change, it only extends to work with more options of the original command.

@felixfontein
Copy link
Collaborator

In any case, I'm wondering whether this should better be a new module (systemd_creds_encrypt_file) instead of a new part of the systemd_creds_encrypt module, since the new functionality behaves quite different from the existing.

The current functionality doesn't change, it only extends to work with more options of the original command.

There is a very fundamental change: before the module was doing an action. Now it is managing a file. That's quite different.

@mks-h
Copy link
Author

mks-h commented Sep 3, 2025

Or it can still just do an action, if you don't use the dest option.

Either way, I'm afraid I'm out of ideas on how to verify that the existing secret was created with the same parameters. The credentials may contain salt, because their hashes are different each time you create one — even if you specify all the same parameters explicitly.

@ansibullbot ansibullbot added the stale_ci CI is older than 7 days, rerun before merging label Sep 11, 2025
@felixfontein felixfontein removed the backport-11 Automatically create a backport for the stable-10 branch label Oct 13, 2025
@ansibullbot ansibullbot added needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR labels Oct 29, 2025
@felixfontein
Copy link
Collaborator

Please note that this PR now has a conflict since we reformatted all code in the collection (see #10999).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

check-before-release PR will be looked at again shortly before release and merged if possible. feature This issue/PR relates to a feature request integration tests/integration module module needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR new_contributor Help guide this first time contributor plugins plugin (any type) stale_ci CI is older than 7 days, rerun before merging tests tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants