| 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_info |
| 8 |
""" |
| 9 |
|
| 10 |
from __future__ import annotations |
| 11 |
|
| 12 |
DOCUMENTATION = r""" |
| 13 |
module: keyring_info |
| 14 |
version_added: 5.2.0 |
| 15 |
author: |
| 16 |
- Alexander Hussey (@ahussey-redhat) |
| 17 |
short_description: Get 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 retrieve passphrases for a given service |
| 20 |
and username from the OS' native keyring. |
| 21 |
requirements: |
| 22 |
- keyring (Python library) |
| 23 |
- gnome-keyring (application - required for headless Linux keyring access) |
| 24 |
- dbus-run-session (application - required for headless Linux keyring access) |
| 25 |
extends_documentation_fragment: |
| 26 |
- community.general._attributes |
| 27 |
- community.general._attributes.info_module |
| 28 |
options: |
| 29 |
service: |
| 30 |
description: The name of the service. |
| 31 |
required: true |
| 32 |
type: str |
| 33 |
username: |
| 34 |
description: The user belonging to the service. |
| 35 |
required: true |
| 36 |
type: str |
| 37 |
keyring_password: |
| 38 |
description: Password to unlock keyring. |
| 39 |
required: true |
| 40 |
type: str |
| 41 |
""" |
| 42 |
|
| 43 |
EXAMPLES = r""" |
| 44 |
- name: Retrieve password for service_name/user_name |
| 45 |
community.general.keyring_info: |
| 46 |
service: test |
| 47 |
username: test1 |
| 48 |
keyring_password: "{{ keyring_password }}" |
| 49 |
register: test_password |
| 50 |
|
| 51 |
- name: Display password |
| 52 |
ansible.builtin.debug: |
| 53 |
msg: "{{ test_password.passphrase }}" |
| 54 |
""" |
| 55 |
|
| 56 |
RETURN = r""" |
| 57 |
passphrase: |
| 58 |
description: A string containing the password. |
| 59 |
returned: success and the password exists |
| 60 |
type: str |
| 61 |
sample: Password123 |
| 62 |
""" |
| 63 |
|
| 64 |
import traceback |
| 65 |
from shlex import quote |
| 66 |
|
| 67 |
from ansible.module_utils.basic import AnsibleModule, missing_required_lib |
| 68 |
|
| 69 |
try: |
| 70 |
import keyring |
| 71 |
|
| 72 |
HAS_KEYRING = True |
| 73 |
KEYRING_IMP_ERR = None |
| 74 |
except ImportError: |
| 75 |
HAS_KEYRING = False |
| 76 |
KEYRING_IMP_ERR = traceback.format_exc() |
| 77 |
|
| 78 |
|
| 79 |
def _alternate_retrieval_method(module): |
| 80 |
get_argument = ( |
| 81 |
f'echo "{quote(module.params["keyring_password"])}" | gnome-keyring-daemon --unlock\n' |
| 82 |
f"keyring get {quote(module.params['service'])} {quote(module.params['username'])}\n" |
| 83 |
) |
| 84 |
dummy, stdout, dummy = module.run_command( |
| 85 |
"dbus-run-session -- /bin/bash", |
| 86 |
use_unsafe_shell=True, |
| 87 |
data=get_argument, |
| 88 |
encoding=None, |
| 89 |
) |
| 90 |
try: |
| 91 |
return stdout.decode("UTF-8").splitlines()[1] |
| 92 |
except IndexError: |
| 93 |
return None |
| 94 |
|
| 95 |
|
| 96 |
def run_module(): |
| 97 |
""" |
| 98 |
Attempts to retrieve a passphrase from a keyring. |
| 99 |
""" |
| 100 |
result = dict(changed=False, msg="") |
| 101 |
|
| 102 |
module_args = dict( |
| 103 |
service=dict(type="str", required=True), |
| 104 |
username=dict(type="str", required=True), |
| 105 |
keyring_password=dict(type="str", required=True, no_log=True), |
| 106 |
) |
| 107 |
|
| 108 |
module = AnsibleModule(argument_spec=module_args, supports_check_mode=True) |
| 109 |
module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"} |
| 110 |
|
| 111 |
if not HAS_KEYRING: |
| 112 |
module.fail_json(msg=missing_required_lib("keyring"), exception=KEYRING_IMP_ERR) |
| 113 |
try: |
| 114 |
passphrase = keyring.get_password(module.params["service"], module.params["username"]) |
| 115 |
except keyring.errors.KeyringLocked: |
| 116 |
pass |
| 117 |
except keyring.errors.InitError: |
| 118 |
pass |
| 119 |
except AttributeError: |
| 120 |
pass |
| 121 |
|
| 122 |
if passphrase is None: |
| 123 |
passphrase = _alternate_retrieval_method(module) |
| 124 |
|
| 125 |
if passphrase is not None: |
| 126 |
result["msg"] = f"Successfully retrieved password for {module.params['service']}@{module.params['username']}" |
| 127 |
result["passphrase"] = passphrase |
| 128 |
if passphrase is None: |
| 129 |
result["msg"] = f"Password for {module.params['service']}@{module.params['username']} does not exist." |
| 130 |
module.exit_json(**result) |
| 131 |
|
| 132 |
|
| 133 |
def main(): |
| 134 |
""" |
| 135 |
main module loop |
| 136 |
""" |
| 137 |
run_module() |
| 138 |
|
| 139 |
|
| 140 |
if __name__ == "__main__": |
| 141 |
main() |