Debian Codegraph @

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

raw
1 # (c) 2022, Brian Scholer (@briantist)
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 = r'''
9 name: vault_kv2_get
10 version_added: 2.5.0
11 author:
12 - Brian Scholer (@briantist)
13 short_description: Get a secret from HashiCorp Vault's KV version 2 secret store
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 - Gets a secret from HashiCorp Vault's KV version 2 secret store.
19 seealso:
20 - module: community.hashi_vault.vault_kv2_get
21 - ref: community.hashi_vault.vault_kv1_get lookup <ansible_collections.community.hashi_vault.vault_kv1_get_lookup>
22 description: The official documentation for the C(community.hashi_vault.vault_kv1_get) lookup plugin.
23 - module: community.hashi_vault.vault_kv1_get
24 - ref: community.hashi_vault Lookup Guide <ansible_collections.community.hashi_vault.docsite.lookup_guide>
25 description: Guidance on using lookups in C(community.hashi_vault).
26 - name: KV2 Secrets Engine
27 description: Documentation for the Vault KV secrets engine, version 2.
28 link: https://www.vaultproject.io/docs/secrets/kv/kv-v2
29 extends_documentation_fragment:
30 - community.hashi_vault.connection
31 - community.hashi_vault.connection.plugins
32 - community.hashi_vault.auth
33 - community.hashi_vault.auth.plugins
34 - community.hashi_vault.engine_mount
35 - community.hashi_vault.engine_mount.plugins
36 options:
37 _terms:
38 description:
39 - Vault KV path(s) to be read.
40 - These are relative to the I(engine_mount_point), so the mount path should not be included.
41 type: str
42 required: True
43 engine_mount_point:
44 default: secret
45 version:
46 description: Specifies the version to return. If not set the latest version is returned.
47 type: int
48 '''
49
50 EXAMPLES = r'''
51 - name: Read a kv2 secret with the default mount point
52 ansible.builtin.set_fact:
53 response: "{{ lookup('community.hashi_vault.vault_kv2_get', 'hello', url='https://vault:8201') }}"
54 # equivalent API path in 3.x.x is kv/data/hello
55 # equivalent API path in 4.0.0+ is secret/data/hello
56
57 - name: Display the results
58 ansible.builtin.debug:
59 msg:
60 - "Secret: {{ response.secret }}"
61 - "Data: {{ response.data }} (contains secret data & metadata in kv2)"
62 - "Metadata: {{ response.metadata }}"
63 - "Full response: {{ response.raw }}"
64 - "Value of key 'password' in the secret: {{ response.secret.password }}"
65
66 - name: Read version 5 of a kv2 secret with a different mount point
67 ansible.builtin.set_fact:
68 response: "{{ lookup('community.hashi_vault.vault_kv2_get', 'hello', version=5, engine_mount_point='custom/kv2/mount', url='https://vault:8201') }}"
69 # equivalent API path is custom/kv2/mount/data/hello
70
71 - name: Assert that the version returned is as expected
72 ansible.builtin.assert:
73 that:
74 - response.metadata.version == 5
75
76 - name: Perform multiple kv2 reads with a single Vault login, showing the secrets
77 vars:
78 paths:
79 - hello
80 - my-secret/one
81 - my-secret/two
82 ansible.builtin.debug:
83 msg: "{{ lookup('community.hashi_vault.vault_kv2_get', *paths, auth_method='userpass', username=user, password=pwd)['secret'] }}"
84
85 - name: Perform multiple kv2 reads with a single Vault login in a loop
86 vars:
87 paths:
88 - hello
89 - my-secret/one
90 - my-secret/two
91 ansible.builtin.debug:
92 msg: '{{ item }}'
93 loop: "{{ query('community.hashi_vault.vault_kv2_get', *paths, auth_method='userpass', username=user, password=pwd) }}"
94
95 - name: Perform multiple kv2 reads with a single Vault login in a loop (via with_), display values only
96 vars:
97 ansible_hashi_vault_auth_method: userpass
98 ansible_hashi_vault_username: '{{ user }}'
99 ansible_hashi_vault_password: '{{ pwd }}'
100 ansible_hashi_vault_engine_mount_point: special/kv2
101 ansible.builtin.debug:
102 msg: '{{ item.values() | list }}'
103 with_community.hashi_vault.vault_kv2_get:
104 - hello
105 - my-secret/one
106 - my-secret/two
107 '''
108
109 RETURN = r'''
110 _raw:
111 description:
112 - The result of the read(s) against the given path(s).
113 type: list
114 elements: dict
115 contains:
116 raw:
117 description: The raw result of the read against the given path.
118 returned: success
119 type: dict
120 sample:
121 auth: null
122 data:
123 data:
124 Key1: value1
125 Key2: value2
126 metadata:
127 created_time: "2022-04-21T15:56:58.8525402Z"
128 custom_metadata: null
129 deletion_time: ""
130 destroyed: false
131 version: 2
132 lease_duration: 0
133 lease_id: ""
134 renewable: false
135 request_id: dc829675-9119-e831-ae74-35fc5d33d200
136 warnings: null
137 wrap_info: null
138 data:
139 description: The C(data) field of raw result. This can also be accessed via C(raw.data).
140 returned: success
141 type: dict
142 sample:
143 data:
144 Key1: value1
145 Key2: value2
146 metadata:
147 created_time: "2022-04-21T15:56:58.8525402Z"
148 custom_metadata: null
149 deletion_time: ""
150 destroyed: false
151 version: 2
152 secret:
153 description: The C(data) field within the C(data) field. Equivalent to C(raw.data.data).
154 returned: success
155 type: dict
156 sample:
157 Key1: value1
158 Key2: value2
159 metadata:
160 description: The C(metadata) field within the C(data) field. Equivalent to C(raw.data.metadata).
161 returned: success
162 type: dict
163 sample:
164 created_time: "2022-04-21T15:56:58.8525402Z"
165 custom_metadata: null
166 deletion_time: ""
167 destroyed: false
168 version: 2
169 '''
170
171 from ansible.errors import AnsibleError
172 from ansible.utils.display import Display
173
174 from ansible_collections.community.hashi_vault.plugins.plugin_utils._hashi_vault_lookup_base import HashiVaultLookupBase
175 from ansible_collections.community.hashi_vault.plugins.module_utils._hashi_vault_common import HashiVaultValueError
176
177 display = Display()
178
179
180 class LookupModule(HashiVaultLookupBase):
181 def run(self, terms, variables=None, **kwargs):
182 ret = []
183
184 self.set_options(direct=kwargs, var_options=variables)
185 # TODO: remove process_deprecations() if backported fix is available (see method definition)
186 self.process_deprecations()
187
188 self.connection_options.process_connection_options()
189 client_args = self.connection_options.get_hvac_connection_options()
190 client = self.helper.get_vault_client(**client_args)
191 hvac_exceptions = self.helper.get_hvac().exceptions
192
193 version = self._options_adapter.get_option_default('version')
194 engine_mount_point = self._options_adapter.get_option('engine_mount_point')
195
196 try:
197 self.authenticator.validate()
198 self.authenticator.authenticate(client)
199 except (NotImplementedError, HashiVaultValueError) as e:
200 raise AnsibleError(e)
201
202 for term in terms:
203 try:
204 raw = client.secrets.kv.v2.read_secret_version(path=term, version=version, mount_point=engine_mount_point)
205 except hvac_exceptions.Forbidden as e:
206 raise AnsibleError("Forbidden: Permission Denied to path ['%s']." % term) from e
207 except hvac_exceptions.InvalidPath as e:
208 raise (
209 AnsibleError("Invalid or missing path ['%s'] with secret version '%s'. Check the path or secret version." % (term, version or 'latest'))
210 ) from e
211
212 data = raw['data']
213 metadata = data['metadata']
214 secret = data['data']
215
216 ret.append(dict(raw=raw, data=data, secret=secret, metadata=metadata))
217
218 return ret