| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2024, Ansible Project |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: systemd_creds_encrypt |
| 11 |
short_description: C(systemd)'s C(systemd-creds encrypt) plugin |
| 12 |
description: |
| 13 |
- This module encrypts input using C(systemd)'s C(systemd-creds encrypt). |
| 14 |
author: |
| 15 |
- Thomas Sjögren (@konstruktoid) |
| 16 |
version_added: '10.2.0' |
| 17 |
extends_documentation_fragment: |
| 18 |
- community.general._attributes |
| 19 |
attributes: |
| 20 |
check_mode: |
| 21 |
support: full |
| 22 |
details: |
| 23 |
- This action does not modify state. |
| 24 |
diff_mode: |
| 25 |
support: N/A |
| 26 |
details: |
| 27 |
- This action does not modify state. |
| 28 |
options: |
| 29 |
name: |
| 30 |
description: |
| 31 |
- The credential name to embed in the encrypted credential data. |
| 32 |
type: str |
| 33 |
not_after: |
| 34 |
description: |
| 35 |
- The time when the credential shall not be used anymore. |
| 36 |
- Takes a timestamp specification in the format described in V(systemd.time(7\)). |
| 37 |
type: str |
| 38 |
pretty: |
| 39 |
description: |
| 40 |
- Pretty print the output so that it may be pasted directly into a unit file. |
| 41 |
type: bool |
| 42 |
default: false |
| 43 |
secret: |
| 44 |
description: |
| 45 |
- The secret to encrypt. |
| 46 |
type: str |
| 47 |
required: true |
| 48 |
timestamp: |
| 49 |
description: |
| 50 |
- The timestamp to embed into the encrypted credential. |
| 51 |
- Takes a timestamp specification in the format described in V(systemd.time(7\)). |
| 52 |
type: str |
| 53 |
user: |
| 54 |
description: |
| 55 |
- A user name or numeric UID to encrypt the credential for. |
| 56 |
- If set to the special string V(self) it sets the user to the user of the calling process. |
| 57 |
- Requires C(systemd) 256 or later. |
| 58 |
type: str |
| 59 |
notes: |
| 60 |
- C(systemd-creds) requires C(systemd) 250 or later. |
| 61 |
""" |
| 62 |
|
| 63 |
EXAMPLES = r""" |
| 64 |
- name: Encrypt secret |
| 65 |
become: true |
| 66 |
community.general.systemd_creds_encrypt: |
| 67 |
name: db |
| 68 |
not_after: +48hr |
| 69 |
secret: access_token |
| 70 |
register: encrypted_secret |
| 71 |
|
| 72 |
- name: Print the encrypted secret |
| 73 |
ansible.builtin.debug: |
| 74 |
msg: "{{ encrypted_secret }}" |
| 75 |
""" |
| 76 |
|
| 77 |
RETURN = r""" |
| 78 |
value: |
| 79 |
description: The Base64 encoded encrypted secret. |
| 80 |
type: str |
| 81 |
returned: always |
| 82 |
sample: "WhQZht+JQJax1aZemmGLxmAAAA..." |
| 83 |
""" |
| 84 |
|
| 85 |
from ansible.module_utils.basic import AnsibleModule |
| 86 |
|
| 87 |
|
| 88 |
def main(): |
| 89 |
"""Encrypt secret using systemd-creds.""" |
| 90 |
module = AnsibleModule( |
| 91 |
argument_spec=dict( |
| 92 |
name=dict(type="str"), |
| 93 |
not_after=dict(type="str"), |
| 94 |
pretty=dict(type="bool", default=False), |
| 95 |
secret=dict(type="str", required=True, no_log=True), |
| 96 |
timestamp=dict(type="str"), |
| 97 |
user=dict(type="str"), |
| 98 |
), |
| 99 |
supports_check_mode=True, |
| 100 |
) |
| 101 |
|
| 102 |
cmd = module.get_bin_path("systemd-creds", required=True) |
| 103 |
|
| 104 |
name = module.params["name"] |
| 105 |
not_after = module.params["not_after"] |
| 106 |
pretty = module.params["pretty"] |
| 107 |
secret = module.params["secret"] |
| 108 |
timestamp = module.params["timestamp"] |
| 109 |
user = module.params["user"] |
| 110 |
|
| 111 |
encrypt_cmd = [cmd, "encrypt"] |
| 112 |
if name: |
| 113 |
encrypt_cmd.append(f"--name={name}") |
| 114 |
else: |
| 115 |
encrypt_cmd.append("--name=") |
| 116 |
if not_after: |
| 117 |
encrypt_cmd.append(f"--not-after={not_after}") |
| 118 |
if pretty: |
| 119 |
encrypt_cmd.append("--pretty") |
| 120 |
if timestamp: |
| 121 |
encrypt_cmd.append(f"--timestamp={timestamp}") |
| 122 |
if user: |
| 123 |
encrypt_cmd.append(f"--uid={user}") |
| 124 |
encrypt_cmd.extend(["-", "-"]) |
| 125 |
|
| 126 |
rc, stdout, stderr = module.run_command(encrypt_cmd, data=secret, binary_data=True) |
| 127 |
|
| 128 |
module.exit_json( |
| 129 |
changed=False, |
| 130 |
value=stdout, |
| 131 |
rc=rc, |
| 132 |
stderr=stderr, |
| 133 |
) |
| 134 |
|
| 135 |
|
| 136 |
if __name__ == "__main__": |
| 137 |
main() |