Debian Codegraph @

ansible_collections/community/hashi_vault/plugins/lookup/vault_kv1_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_kv1_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 1 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 1 secret store.
19 seealso:
20 - module: community.hashi_vault.vault_kv1_get
21 - ref: community.hashi_vault.vault_kv2_get lookup <ansible_collections.community.hashi_vault.vault_kv2_get_lookup>
22 description: The official documentation for the C(community.hashi_vault.vault_kv2_get) lookup plugin.
23 - module: community.hashi_vault.vault_kv2_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: KV1 Secrets Engine
27 description: Documentation for the Vault KV secrets engine, version 1.
28 link: https://www.vaultproject.io/docs/secrets/kv/kv-v1
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: kv
45 '''
46
47 EXAMPLES = r'''
48 - name: Read a kv1 secret with the default mount point
49 ansible.builtin.set_fact:
50 response: "{{ lookup('community.hashi_vault.vault_kv1_get', 'hello', url='https://vault:8201') }}"
51 # equivalent API path is kv/hello
52
53 - name: Display the results
54 ansible.builtin.debug:
55 msg:
56 - "Secret: {{ response.secret }}"
57 - "Data: {{ response.data }} (same as secret in kv1)"
58 - "Metadata: {{ response.metadata }} (response info in kv1)"
59 - "Full response: {{ response.raw }}"
60 - "Value of key 'password' in the secret: {{ response.secret.password }}"
61
62 - name: Read a kv1 secret with a different mount point
63 ansible.builtin.set_fact:
64 response: "{{ lookup('community.hashi_vault.vault_kv1_get', 'hello', engine_mount_point='custom/kv1/mount', url='https://vault:8201') }}"
65 # equivalent API path is custom/kv1/mount/hello
66
67 - name: Display the results
68 ansible.builtin.debug:
69 msg:
70 - "Secret: {{ response.secret }}"
71 - "Data: {{ response.data }} (same as secret in kv1)"
72 - "Metadata: {{ response.metadata }} (response info in kv1)"
73 - "Full response: {{ response.raw }}"
74 - "Value of key 'password' in the secret: {{ response.secret.password }}"
75
76 - name: Perform multiple kv1 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_kv1_get', *paths, auth_method='userpass', username=user, password=pwd)['secret'] }}"
84
85 - name: Perform multiple kv1 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_kv1_get', *paths, auth_method='userpass', username=user, password=pwd) }}"
94
95 - name: Perform multiple kv1 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.builtin.debug:
101 msg: '{{ item.values() | list }}'
102 with_community.hashi_vault.vault_kv1_get:
103 - hello
104 - my-secret/one
105 - my-secret/two
106 '''
107
108 RETURN = r'''
109 _raw:
110 description:
111 - The result of the read(s) against the given path(s).
112 type: list
113 elements: dict
114 contains:
115 raw:
116 description: The raw result of the read against the given path.
117 returned: success
118 type: dict
119 sample:
120 auth: null
121 data:
122 Key1: value1
123 Key2: value2
124 lease_duration: 2764800
125 lease_id: ""
126 renewable: false
127 request_id: e99f145f-f02a-7073-1229-e3f191057a70
128 warnings: null
129 wrap_info: null
130 data:
131 description: The C(data) field of raw result. This can also be accessed via C(raw.data).
132 returned: success
133 type: dict
134 sample:
135 Key1: value1
136 Key2: value2
137 secret:
138 description: The C(data) field of the raw result. This is identical to C(data) in the return values.
139 returned: success
140 type: dict
141 sample:
142 Key1: value1
143 Key2: value2
144 metadata:
145 description: This is a synthetic result. It is the same as C(raw) with C(data) removed.
146 returned: success
147 type: dict
148 sample:
149 auth: null
150 lease_duration: 2764800
151 lease_id: ""
152 renewable: false
153 request_id: e99f145f-f02a-7073-1229-e3f191057a70
154 warnings: null
155 wrap_info: null
156 '''
157
158 from ansible.errors import AnsibleError
159 from ansible.utils.display import Display
160
161 from ansible_collections.community.hashi_vault.plugins.plugin_utils._hashi_vault_lookup_base import HashiVaultLookupBase
162 from ansible_collections.community.hashi_vault.plugins.module_utils._hashi_vault_common import HashiVaultValueError
163
164 display = Display()
165
166
167 class LookupModule(HashiVaultLookupBase):
168 def run(self, terms, variables=None, **kwargs):
169 ret = []
170
171 self.set_options(direct=kwargs, var_options=variables)
172 # TODO: remove process_deprecations() if backported fix is available (see method definition)
173 self.process_deprecations()
174
175 self.connection_options.process_connection_options()
176 client_args = self.connection_options.get_hvac_connection_options()
177 client = self.helper.get_vault_client(**client_args)
178 hvac_exceptions = self.helper.get_hvac().exceptions
179
180 engine_mount_point = self._options_adapter.get_option('engine_mount_point')
181
182 try:
183 self.authenticator.validate()
184 self.authenticator.authenticate(client)
185 except (NotImplementedError, HashiVaultValueError) as e:
186 raise AnsibleError(e)
187
188 for term in terms:
189 try:
190 raw = client.secrets.kv.v1.read_secret(path=term, mount_point=engine_mount_point)
191 except hvac_exceptions.Forbidden as e:
192 raise AnsibleError("Forbidden: Permission Denied to path ['%s']." % term) from e
193 except hvac_exceptions.InvalidPath as e:
194 if 'Invalid path for a versioned K/V secrets engine' in str(e):
195 msg = "Invalid path for a versioned K/V secrets engine ['%s']. If this is a KV version 2 path, use community.hashi_vault.vault_kv2_get."
196 else:
197 msg = "Invalid or missing path ['%s']."
198
199 raise AnsibleError(msg % (term,)) from e
200
201 metadata = raw.copy()
202 data = metadata.pop('data')
203
204 ret.append(dict(raw=raw, data=data, secret=data, metadata=metadata))
205
206 return ret