Debian Codegraph @

ansible_collections/community/general/plugins/modules/systemd_creds_decrypt.py

raw
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_decrypt
11 short_description: C(systemd)'s C(systemd-creds decrypt) plugin
12 description:
13 - This module decrypts input using C(systemd)'s C(systemd-creds decrypt).
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 validate the embedded credential name.
32 type: str
33 newline:
34 description:
35 - Whether to add a trailing newline character to the end of the output, if not present.
36 type: bool
37 default: false
38 secret:
39 description:
40 - The secret to decrypt.
41 type: str
42 required: true
43 timestamp:
44 description:
45 - The timestamp to use to validate the V(not-after) timestamp that was used during encryption.
46 - Takes a timestamp specification in the format described in V(systemd.time(7\)).
47 type: str
48 transcode:
49 description:
50 - Whether to transcode the output before returning it.
51 type: str
52 choices: [base64, unbase64, hex, unhex]
53 user:
54 description:
55 - A user name or numeric UID when decrypting from a specific user context.
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: Decrypt secret
65 community.general.systemd_creds_decrypt:
66 name: db
67 secret: "WhQZht+JQJax1aZemmGLxmAAAA..."
68 register: decrypted_secret
69
70 - name: Print the decrypted secret
71 ansible.builtin.debug:
72 msg: "{{ decrypted_secret }}"
73 """
74
75 RETURN = r"""
76 value:
77 description:
78 - The decrypted secret.
79 - Note that Ansible only supports returning UTF-8 encoded strings. If the decrypted secret is binary data, or a string
80 encoded in another way, use O(transcode=base64) or O(transcode=hex) to circument this restriction. You then need to
81 decode the data when using it, for example using the P(ansible.builtin.b64decode#filter) filter.
82 type: str
83 returned: always
84 sample: "access_token"
85 """
86
87
88 from ansible.module_utils.basic import AnsibleModule
89
90
91 def main():
92 """Decrypt secret using systemd-creds."""
93 module = AnsibleModule(
94 argument_spec=dict(
95 name=dict(type="str"),
96 newline=dict(type="bool", default=False),
97 secret=dict(type="str", required=True, no_log=True),
98 timestamp=dict(type="str"),
99 transcode=dict(type="str", choices=["base64", "unbase64", "hex", "unhex"]),
100 user=dict(type="str"),
101 ),
102 supports_check_mode=True,
103 )
104
105 cmd = module.get_bin_path("systemd-creds", required=True)
106
107 name = module.params["name"]
108 newline = module.params["newline"]
109 secret = module.params["secret"]
110 timestamp = module.params["timestamp"]
111 transcode = module.params["transcode"]
112 user = module.params["user"]
113
114 decrypt_cmd = [cmd, "decrypt"]
115 if name:
116 decrypt_cmd.append(f"--name={name}")
117 else:
118 decrypt_cmd.append("--name=")
119 decrypt_cmd.append(f"--newline={'yes' if newline else 'no'}")
120 if timestamp:
121 decrypt_cmd.append(f"--timestamp={timestamp}")
122 if transcode:
123 decrypt_cmd.append(f"--transcode={transcode}")
124 if user:
125 decrypt_cmd.append(f"--uid={user}")
126 decrypt_cmd.extend(["-", "-"])
127
128 rc, stdout, stderr = module.run_command(decrypt_cmd, data=secret, binary_data=True)
129
130 module.exit_json(
131 changed=False,
132 value=stdout,
133 rc=rc,
134 stderr=stderr,
135 )
136
137
138 if __name__ == "__main__":
139 main()