Debian Codegraph @

ansible_collections/community/general/plugins/lookup/keyring.py

raw
1 # Copyright (c) 2016, Samuel Boucher <[email protected]>
2 # Copyright (c) 2017 Ansible Project
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6 from __future__ import annotations
7
8 DOCUMENTATION = r"""
9 name: keyring
10 author:
11 - Samuel Boucher (!UNKNOWN) <[email protected]>
12 requirements:
13 - keyring (python library)
14 short_description: Grab secrets from the OS keyring
15 description:
16 - Allows you to access data stored in the OS provided keyring/keychain.
17 options:
18 _terms:
19 description:
20 - List of pairs of service and user name.
21 - Every entry must be of the form V(servicename username).
22 type: list
23 elements: str
24 """
25
26 EXAMPLES = r"""
27 - name: output secrets to screen (BAD IDEA)
28 ansible.builtin.debug:
29 msg: "Password: {{item}}"
30 with_community.general.keyring:
31 - 'servicename username'
32
33 - name: access mysql with password from keyring
34 community.mysql.mysql_db:
35 login_password: "{{ lookup('community.general.keyring', 'mysql joe') }}"
36 login_user: joe
37 """
38
39 RETURN = r"""
40 _raw:
41 description: Secrets stored.
42 type: list
43 elements: str
44 """
45
46 HAS_KEYRING = True
47
48 from ansible.errors import AnsibleError
49 from ansible.utils.display import Display
50
51 from ansible_collections.community.general.plugins.plugin_utils._lookup import check_for_wrong_terms
52
53 try:
54 import keyring
55 except ImportError:
56 HAS_KEYRING = False
57
58 from ansible.plugins.lookup import LookupBase
59
60 display = Display()
61
62
63 class LookupModule(LookupBase):
64 def run(self, terms, variables=None, **kwargs):
65 if not HAS_KEYRING:
66 raise AnsibleError("Can't LOOKUP(keyring): missing required python library 'keyring'")
67
68 self.set_options(var_options=variables, direct=kwargs)
69 check_for_wrong_terms(self, direct=kwargs)
70
71 display.vvvv(f"keyring: {keyring.get_keyring()}")
72 ret = []
73 for term in terms:
74 (servicename, username) = (term.split()[0], term.split()[1])
75 display.vvvv(f"username: {username}, servicename: {servicename} ")
76 password = keyring.get_password(servicename, username)
77 if password is None:
78 raise AnsibleError(f"servicename: {servicename} for user {username} not found")
79 ret.append(password.rstrip())
80 return ret