| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2022, Alexander Hussey <[email protected]> |
| 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 |
Ansible Module - community.general.keyring |
| 8 |
""" |
| 9 |
|
| 10 |
from __future__ import annotations |
| 11 |
|
| 12 |
DOCUMENTATION = r""" |
| 13 |
module: keyring |
| 14 |
version_added: 5.2.0 |
| 15 |
author: |
| 16 |
- Alexander Hussey (@ahussey-redhat) |
| 17 |
short_description: Set or delete a passphrase using the Operating System's native keyring |
| 18 |
description: >- |
| 19 |
This module uses the L(keyring Python library, https://pypi.org/project/keyring/) to set or delete passphrases for a given |
| 20 |
service and username from the OS' native keyring. |
| 21 |
requirements: |
| 22 |
- keyring (Python library) |
| 23 |
- gnome-keyring (application - required for headless Gnome keyring access) |
| 24 |
- dbus-run-session (application - required for headless Gnome keyring access) |
| 25 |
extends_documentation_fragment: |
| 26 |
- community.general._attributes |
| 27 |
attributes: |
| 28 |
check_mode: |
| 29 |
support: full |
| 30 |
diff_mode: |
| 31 |
support: none |
| 32 |
options: |
| 33 |
service: |
| 34 |
description: The name of the service. |
| 35 |
required: true |
| 36 |
type: str |
| 37 |
username: |
| 38 |
description: The user belonging to the service. |
| 39 |
required: true |
| 40 |
type: str |
| 41 |
user_password: |
| 42 |
description: The password to set. |
| 43 |
type: str |
| 44 |
aliases: |
| 45 |
- password |
| 46 |
keyring_password: |
| 47 |
description: Password to unlock keyring. |
| 48 |
required: true |
| 49 |
type: str |
| 50 |
state: |
| 51 |
description: Whether the password should exist. |
| 52 |
default: present |
| 53 |
type: str |
| 54 |
choices: |
| 55 |
- present |
| 56 |
- absent |
| 57 |
""" |
| 58 |
|
| 59 |
EXAMPLES = r""" |
| 60 |
- name: Set a password for test/test1 |
| 61 |
community.general.keyring: |
| 62 |
service: test |
| 63 |
username: test1 |
| 64 |
user_password: "{{ user_password }}" |
| 65 |
keyring_password: "{{ keyring_password }}" |
| 66 |
|
| 67 |
- name: Delete the password for test/test1 |
| 68 |
community.general.keyring: |
| 69 |
service: test |
| 70 |
username: test1 |
| 71 |
user_password: "{{ user_password }}" |
| 72 |
keyring_password: "{{ keyring_password }}" |
| 73 |
state: absent |
| 74 |
""" |
| 75 |
|
| 76 |
import traceback |
| 77 |
from shlex import quote |
| 78 |
|
| 79 |
from ansible.module_utils.basic import AnsibleModule, missing_required_lib |
| 80 |
|
| 81 |
try: |
| 82 |
import keyring |
| 83 |
|
| 84 |
HAS_KEYRING = True |
| 85 |
KEYRING_IMP_ERR = None |
| 86 |
except ImportError: |
| 87 |
HAS_KEYRING = False |
| 88 |
KEYRING_IMP_ERR = traceback.format_exc() |
| 89 |
|
| 90 |
|
| 91 |
def del_passphrase(module): |
| 92 |
""" |
| 93 |
Attempt to delete a passphrase in the keyring using the Python API and fallback to using a shell. |
| 94 |
""" |
| 95 |
if module.check_mode: |
| 96 |
return None |
| 97 |
try: |
| 98 |
keyring.delete_password(module.params["service"], module.params["username"]) |
| 99 |
return None |
| 100 |
except keyring.errors.KeyringLocked: |
| 101 |
delete_argument = ( |
| 102 |
f'echo "{quote(module.params["keyring_password"])}" | gnome-keyring-daemon --unlock\n' |
| 103 |
f"keyring del {quote(module.params['service'])} {quote(module.params['username'])}\n" |
| 104 |
) |
| 105 |
dummy, dummy, stderr = module.run_command( |
| 106 |
"dbus-run-session -- /bin/bash", |
| 107 |
use_unsafe_shell=True, |
| 108 |
data=delete_argument, |
| 109 |
encoding=None, |
| 110 |
) |
| 111 |
|
| 112 |
if not stderr.decode("UTF-8"): |
| 113 |
return None |
| 114 |
return stderr.decode("UTF-8") |
| 115 |
|
| 116 |
|
| 117 |
def set_passphrase(module): |
| 118 |
""" |
| 119 |
Attempt to set passphrase in the keyring using the Python API and fallback to using a shell. |
| 120 |
""" |
| 121 |
if module.check_mode: |
| 122 |
return None |
| 123 |
try: |
| 124 |
keyring.set_password( |
| 125 |
module.params["service"], |
| 126 |
module.params["username"], |
| 127 |
module.params["user_password"], |
| 128 |
) |
| 129 |
return None |
| 130 |
except keyring.errors.KeyringLocked: |
| 131 |
set_argument = ( |
| 132 |
f'echo "{quote(module.params["keyring_password"])}" | gnome-keyring-daemon --unlock\n' |
| 133 |
f"keyring set {quote(module.params['service'])} {quote(module.params['username'])}\n{quote(module.params['user_password'])}\n" |
| 134 |
) |
| 135 |
dummy, dummy, stderr = module.run_command( |
| 136 |
"dbus-run-session -- /bin/bash", |
| 137 |
use_unsafe_shell=True, |
| 138 |
data=set_argument, |
| 139 |
encoding=None, |
| 140 |
) |
| 141 |
if not stderr.decode("UTF-8"): |
| 142 |
return None |
| 143 |
return stderr.decode("UTF-8") |
| 144 |
|
| 145 |
|
| 146 |
def get_passphrase(module): |
| 147 |
""" |
| 148 |
Attempt to retrieve passphrase from keyring using the Python API and fallback to using a shell. |
| 149 |
""" |
| 150 |
try: |
| 151 |
passphrase = keyring.get_password(module.params["service"], module.params["username"]) |
| 152 |
return passphrase |
| 153 |
except keyring.errors.KeyringLocked: |
| 154 |
pass |
| 155 |
except keyring.errors.InitError: |
| 156 |
pass |
| 157 |
except AttributeError: |
| 158 |
pass |
| 159 |
get_argument = ( |
| 160 |
f'echo "{quote(module.params["keyring_password"])}" | gnome-keyring-daemon --unlock\n' |
| 161 |
f"keyring get {quote(module.params['service'])} {quote(module.params['username'])}\n" |
| 162 |
) |
| 163 |
dummy, stdout, dummy = module.run_command( |
| 164 |
"dbus-run-session -- /bin/bash", |
| 165 |
use_unsafe_shell=True, |
| 166 |
data=get_argument, |
| 167 |
encoding=None, |
| 168 |
) |
| 169 |
try: |
| 170 |
return stdout.decode("UTF-8").splitlines()[1] # Only return the line containing the password |
| 171 |
except IndexError: |
| 172 |
return None |
| 173 |
|
| 174 |
|
| 175 |
def run_module(): |
| 176 |
""" |
| 177 |
Attempts to retrieve a passphrase from a keyring. |
| 178 |
""" |
| 179 |
result = dict( |
| 180 |
changed=False, |
| 181 |
msg="", |
| 182 |
) |
| 183 |
|
| 184 |
module_args = dict( |
| 185 |
service=dict(type="str", required=True), |
| 186 |
username=dict(type="str", required=True), |
| 187 |
keyring_password=dict(type="str", required=True, no_log=True), |
| 188 |
user_password=dict(type="str", no_log=True, aliases=["password"]), |
| 189 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 190 |
) |
| 191 |
|
| 192 |
module = AnsibleModule(argument_spec=module_args, supports_check_mode=True) |
| 193 |
module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"} |
| 194 |
|
| 195 |
if not HAS_KEYRING: |
| 196 |
module.fail_json(msg=missing_required_lib("keyring"), exception=KEYRING_IMP_ERR) |
| 197 |
|
| 198 |
passphrase = get_passphrase(module) |
| 199 |
if module.params["state"] == "present": |
| 200 |
if passphrase is not None: |
| 201 |
if passphrase == module.params["user_password"]: |
| 202 |
result["msg"] = f"Passphrase already set for {module.params['service']}@{module.params['username']}" |
| 203 |
if passphrase != module.params["user_password"]: |
| 204 |
set_result = set_passphrase(module) |
| 205 |
if set_result is None: |
| 206 |
result["changed"] = True |
| 207 |
result["msg"] = ( |
| 208 |
f"Passphrase has been updated for {module.params['service']}@{module.params['username']}" |
| 209 |
) |
| 210 |
if set_result is not None: |
| 211 |
module.fail_json(msg=set_result) |
| 212 |
if passphrase is None: |
| 213 |
set_result = set_passphrase(module) |
| 214 |
if set_result is None: |
| 215 |
result["changed"] = True |
| 216 |
result["msg"] = ( |
| 217 |
f"Passphrase has been updated for {module.params['service']}@{module.params['username']}" |
| 218 |
) |
| 219 |
if set_result is not None: |
| 220 |
module.fail_json(msg=set_result) |
| 221 |
|
| 222 |
if module.params["state"] == "absent": |
| 223 |
if not passphrase: |
| 224 |
result["result"] = f"Passphrase already absent for {module.params['service']}@{module.params['username']}" |
| 225 |
if passphrase: |
| 226 |
del_result = del_passphrase(module) |
| 227 |
if del_result is None: |
| 228 |
result["changed"] = True |
| 229 |
result["msg"] = ( |
| 230 |
f"Passphrase has been removed for {module.params['service']}@{module.params['username']}" |
| 231 |
) |
| 232 |
if del_result is not None: |
| 233 |
module.fail_json(msg=del_result) |
| 234 |
|
| 235 |
module.exit_json(**result) |
| 236 |
|
| 237 |
|
| 238 |
def main(): |
| 239 |
""" |
| 240 |
main module loop |
| 241 |
""" |
| 242 |
run_module() |
| 243 |
|
| 244 |
|
| 245 |
if __name__ == "__main__": |
| 246 |
main() |