Debian Codegraph @

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

raw
1 # (c) 2021, 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 = """
9 name: vault_read
10 version_added: 1.4.0
11 author:
12 - Brian Scholer (@briantist)
13 short_description: Perform a read 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 read operation against a given path in HashiCorp Vault.
19 seealso:
20 - module: community.hashi_vault.vault_read
21 - ref: community.hashi_vault.hashi_vault lookup <ansible_collections.community.hashi_vault.hashi_vault_lookup>
22 description: The official documentation for the C(community.hashi_vault.hashi_vault) lookup plugin.
23 extends_documentation_fragment:
24 - community.hashi_vault.connection
25 - community.hashi_vault.connection.plugins
26 - community.hashi_vault.auth
27 - community.hashi_vault.auth.plugins
28 options:
29 _terms:
30 description: Vault path(s) to be read.
31 type: str
32 required: True
33 """
34
35 EXAMPLES = """
36 - name: Read a kv2 secret
37 ansible.builtin.debug:
38 msg: "{{ lookup('community.hashi_vault.vault_read', 'secret/data/hello', url='https://vault:8201') }}"
39
40 - name: Retrieve an approle role ID
41 ansible.builtin.debug:
42 msg: "{{ lookup('community.hashi_vault.vault_read', 'auth/approle/role/role-name/role-id', url='https://vault:8201') }}"
43
44 - name: Perform multiple reads with a single Vault login
45 vars:
46 paths:
47 - secret/data/hello
48 - auth/approle/role/role-one/role-id
49 - auth/approle/role/role-two/role-id
50 ansible.builtin.debug:
51 msg: "{{ lookup('community.hashi_vault.vault_read', *paths, auth_method='userpass', username=user, password=pwd) }}"
52
53 - name: Perform multiple reads with a single Vault login in a loop
54 vars:
55 paths:
56 - secret/data/hello
57 - auth/approle/role/role-one/role-id
58 - auth/approle/role/role-two/role-id
59 ansible.builtin.debug:
60 msg: '{{ item }}'
61 loop: "{{ query('community.hashi_vault.vault_read', *paths, auth_method='userpass', username=user, password=pwd) }}"
62
63 - name: Perform multiple reads with a single Vault login in a loop (via with_)
64 vars:
65 ansible_hashi_vault_auth_method: userpass
66 ansible_hashi_vault_username: '{{ user }}'
67 ansible_hashi_vault_password: '{{ pwd }}'
68 ansible.builtin.debug:
69 msg: '{{ item }}'
70 with_community.hashi_vault.vault_read:
71 - secret/data/hello
72 - auth/approle/role/role-one/role-id
73 - auth/approle/role/role-two/role-id
74 """
75
76 RETURN = """
77 _raw:
78 description:
79 - The raw result of the read against the given path.
80 type: list
81 elements: dict
82 """
83
84 from ansible.errors import AnsibleError
85 from ansible.utils.display import Display
86
87 from ansible_collections.community.hashi_vault.plugins.plugin_utils._hashi_vault_lookup_base import HashiVaultLookupBase
88 from ansible_collections.community.hashi_vault.plugins.module_utils._hashi_vault_common import HashiVaultValueError
89
90 display = Display()
91
92
93 class LookupModule(HashiVaultLookupBase):
94 def run(self, terms, variables=None, **kwargs):
95 ret = []
96
97 self.set_options(direct=kwargs, var_options=variables)
98 # TODO: remove process_deprecations() if backported fix is available (see method definition)
99 self.process_deprecations()
100
101 self.connection_options.process_connection_options()
102 client_args = self.connection_options.get_hvac_connection_options()
103 client = self.helper.get_vault_client(**client_args)
104 hvac_exceptions = self.helper.get_hvac().exceptions
105
106 try:
107 self.authenticator.validate()
108 self.authenticator.authenticate(client)
109 except (NotImplementedError, HashiVaultValueError) as e:
110 raise AnsibleError(e)
111
112 for term in terms:
113 try:
114 data = client.read(term)
115 except hvac_exceptions.Forbidden:
116 raise AnsibleError("Forbidden: Permission Denied to path '%s'." % term)
117
118 if data is None:
119 raise AnsibleError("The path '%s' doesn't seem to exist." % term)
120
121 ret.append(data)
122
123 return ret