Debian Codegraph @

ansible_collections/community/hashi_vault/plugins/lookup/vault_list.py

raw
1 # (c) 2023, Tom Kivlin (@tomkivlin)
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 from __future__ import (absolute_import, division, print_function)
6 __metaclass__ = type
7
8 DOCUMENTATION = """
9 name: vault_list
10 version_added: 4.1.0
11 author:
12 - Tom Kivlin (@tomkivlin)
13 short_description: Perform a list operation against HashiCorp Vault
14 requirements:
15 - C(hvac) (L(Python library,https://hvac.readthedocs.io/en/stable/overview.html))
16 - For detailed requirements, see R(the collection requirements page,ansible_collections.community.hashi_vault.docsite.user_guide.requirements).
17 description:
18 - Performs a generic list operation against a given path in HashiCorp Vault.
19 seealso:
20 - module: community.hashi_vault.vault_list
21 extends_documentation_fragment:
22 - community.hashi_vault.connection
23 - community.hashi_vault.connection.plugins
24 - community.hashi_vault.auth
25 - community.hashi_vault.auth.plugins
26 options:
27 _terms:
28 description: Vault path(s) to be listed.
29 type: str
30 required: true
31 """
32
33 EXAMPLES = """
34 - name: List all secrets at a path
35 ansible.builtin.debug:
36 msg: "{{ lookup('community.hashi_vault.vault_list', 'secret/metadata', url='https://vault:8201') }}"
37 # For kv2, the path needs to follow the pattern 'mount_point/metadata' or 'mount_point/metadata/path' to list all secrets in that path
38
39 - name: List access policies
40 ansible.builtin.debug:
41 msg: "{{ lookup('community.hashi_vault.vault_list', 'sys/policies/acl', url='https://vault:8201') }}"
42
43 - name: Perform multiple list operations with a single Vault login
44 vars:
45 paths:
46 - secret/metadata
47 - sys/policies/acl
48 ansible.builtin.debug:
49 msg: "{{ lookup('community.hashi_vault.vault_list', *paths, auth_method='userpass', username=user, password=pwd) }}"
50
51 - name: Perform multiple list operations with a single Vault login in a loop
52 vars:
53 paths:
54 - secret/metadata
55 - sys/policies/acl
56 ansible.builtin.debug:
57 msg: '{{ item }}'
58 loop: "{{ query('community.hashi_vault.vault_list', *paths, auth_method='userpass', username=user, password=pwd) }}"
59
60 - name: Perform list operations with a single Vault login in a loop (via with_)
61 vars:
62 ansible_hashi_vault_auth_method: userpass
63 ansible_hashi_vault_username: '{{ user }}'
64 ansible_hashi_vault_password: '{{ pwd }}'
65 ansible.builtin.debug:
66 msg: '{{ item }}'
67 with_community.hashi_vault.vault_list:
68 - secret/metadata
69 - sys/policies/acl
70
71 - name: Create fact consisting of list of dictionaries each with secret name (e.g. username) and value of a key (e.g. 'password') within that secret
72 ansible.builtin.set_fact:
73 credentials: >-
74 {{
75 credentials
76 | default([]) + [
77 {
78 'username': item,
79 'password': lookup('community.hashi_vault.vault_kv2_get', item, engine_mount_point='vpn-users').secret.password
80 }
81 ]
82 }}
83 loop: "{{ query('community.hashi_vault.vault_list', 'vpn-users/metadata')[0].data['keys'] }}"
84 no_log: true
85
86 - ansible.builtin.debug:
87 msg: "{{ credentials }}"
88
89 - name: Create the same as above without looping, and only 2 logins
90 vars:
91 secret_names: >-
92 {{
93 query('community.hashi_vault.vault_list', 'vpn-users/metadata')
94 | map(attribute='data')
95 | map(attribute='keys')
96 | flatten
97 }}
98 secret_values: >-
99 {{
100 lookup('community.hashi_vault.vault_kv2_get', *secret_names, engine_mount_point='vpn-users')
101 | map(attribute='secret')
102 | map(attribute='password')
103 | flatten
104 }}
105 credentials_dict: "{{ dict(secret_names | zip(secret_values)) }}"
106 ansible.builtin.set_fact:
107 credentials_dict: "{{ credentials_dict }}"
108 credentials_list: "{{ credentials_dict | dict2items(key_name='username', value_name='password') }}"
109 no_log: true
110
111 - ansible.builtin.debug:
112 msg:
113 - "Dictionary: {{ credentials_dict }}"
114 - "List: {{ credentials_list }}"
115
116 - name: List all userpass users and output the token policies for each user
117 ansible.builtin.debug:
118 msg: "{{ lookup('community.hashi_vault.vault_read', 'auth/userpass/users/' + item).data.token_policies }}"
119 loop: "{{ query('community.hashi_vault.vault_list', 'auth/userpass/users')[0].data['keys'] }}"
120 """
121
122 RETURN = """
123 _raw:
124 description:
125 - The raw result of the read against the given path.
126 type: list
127 elements: dict
128 """
129
130 from ansible.errors import AnsibleError
131 from ansible.utils.display import Display
132
133 from ansible_collections.community.hashi_vault.plugins.plugin_utils._hashi_vault_lookup_base import HashiVaultLookupBase
134 from ansible_collections.community.hashi_vault.plugins.module_utils._hashi_vault_common import HashiVaultValueError
135
136 display = Display()
137
138
139 class LookupModule(HashiVaultLookupBase):
140 def run(self, terms, variables=None, **kwargs):
141 ret = []
142
143 self.set_options(direct=kwargs, var_options=variables)
144 # TODO: remove process_deprecations() if backported fix is available (see method definition)
145 self.process_deprecations()
146
147 self.connection_options.process_connection_options()
148 client_args = self.connection_options.get_hvac_connection_options()
149 client = self.helper.get_vault_client(**client_args)
150 hvac_exceptions = self.helper.get_hvac().exceptions
151
152 try:
153 self.authenticator.validate()
154 self.authenticator.authenticate(client)
155 except (NotImplementedError, HashiVaultValueError) as e:
156 raise AnsibleError(e)
157
158 for term in terms:
159 try:
160 data = client.list(term)
161 except hvac_exceptions.Forbidden:
162 raise AnsibleError("Forbidden: Permission Denied to path '%s'." % term)
163
164 if data is None:
165 raise AnsibleError("The path '%s' doesn't seem to exist." % term)
166
167 ret.append(data)
168
169 return ret