Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # (c) 2022, Brian Scholer (@briantist)
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import (absolute_import, division, print_function)
8 __metaclass__ = type
9
10 DOCUMENTATION = r'''
11 module: vault_kv2_get
12 version_added: 2.5.0
13 author:
14 - Brian Scholer (@briantist)
15 short_description: Get a secret from HashiCorp Vault's KV version 2 secret store
16 requirements:
17 - C(hvac) (L(Python library,https://hvac.readthedocs.io/en/stable/overview.html))
18 - For detailed requirements, see R(the collection requirements page,ansible_collections.community.hashi_vault.docsite.user_guide.requirements).
19 description:
20 - Gets a secret from HashiCorp Vault's KV version 2 secret store.
21 seealso:
22 - ref: community.hashi_vault.vault_kv2_get lookup <ansible_collections.community.hashi_vault.vault_kv2_get_lookup>
23 description: The official documentation for the C(community.hashi_vault.vault_kv2_get) lookup plugin.
24 - module: community.hashi_vault.vault_kv1_get
25 - module: community.hashi_vault.vault_kv2_write
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.attributes
31 - community.hashi_vault.attributes.action_group
32 - community.hashi_vault.attributes.check_mode_read_only
33 - community.hashi_vault.connection
34 - community.hashi_vault.auth
35 - community.hashi_vault.engine_mount
36 options:
37 engine_mount_point:
38 default: secret
39 path:
40 description:
41 - Vault KV path to be read.
42 - This is relative to the I(engine_mount_point), so the mount path should not be included.
43 - For kv2, do not include C(/data/) or C(/metadata/).
44 type: str
45 required: True
46 version:
47 description: Specifies the version to return. If not set the latest version is returned.
48 type: int
49 '''
50
51 EXAMPLES = r'''
52 - name: Read the latest version of a kv2 secret from Vault via the remote host with userpass auth
53 community.hashi_vault.vault_kv2_get:
54 url: https://vault:8201
55 path: hello
56 auth_method: userpass
57 username: user
58 password: '{{ passwd }}'
59 register: response
60 # equivalent API path is secret/data/hello
61
62 - name: Display the results
63 ansible.builtin.debug:
64 msg:
65 - "Secret: {{ response.secret }}"
66 - "Data: {{ response.data }} (contains secret data & metadata in kv2)"
67 - "Metadata: {{ response.metadata }}"
68 - "Full response: {{ response.raw }}"
69 - "Value of key 'password' in the secret: {{ response.secret.password }}"
70
71 - name: Read version 5 of a secret from kv2 with a different mount via the remote host
72 community.hashi_vault.vault_kv2_get:
73 url: https://vault:8201
74 engine_mount_point: custom/kv2/mount
75 path: hello
76 version: 5
77 register: response
78 # equivalent API path is custom/kv2/mount/data/hello
79
80 - name: Assert that the version returned is as expected
81 ansible.builtin.assert:
82 that:
83 - response.metadata.version == 5
84 '''
85
86 RETURN = r'''
87 raw:
88 description: The raw result of the read against the given path.
89 returned: success
90 type: dict
91 sample:
92 auth: null
93 data:
94 data:
95 Key1: value1
96 Key2: value2
97 metadata:
98 created_time: "2022-04-21T15:56:58.8525402Z"
99 custom_metadata: null
100 deletion_time: ""
101 destroyed: false
102 version: 2
103 lease_duration: 0
104 lease_id: ""
105 renewable: false
106 request_id: dc829675-9119-e831-ae74-35fc5d33d200
107 warnings: null
108 wrap_info: null
109 data:
110 description: The C(data) field of raw result. This can also be accessed via C(raw.data).
111 returned: success
112 type: dict
113 sample:
114 data:
115 Key1: value1
116 Key2: value2
117 metadata:
118 created_time: "2022-04-21T15:56:58.8525402Z"
119 custom_metadata: null
120 deletion_time: ""
121 destroyed: false
122 version: 2
123 secret:
124 description: The C(data) field within the C(data) field. Equivalent to C(raw.data.data).
125 returned: success
126 type: dict
127 sample:
128 Key1: value1
129 Key2: value2
130 metadata:
131 description: The C(metadata) field within the C(data) field. Equivalent to C(raw.data.metadata).
132 returned: success
133 type: dict
134 sample:
135 created_time: "2022-04-21T15:56:58.8525402Z"
136 custom_metadata: null
137 deletion_time: ""
138 destroyed: false
139 version: 2
140 '''
141
142 import traceback
143
144 from ansible.module_utils.common.text.converters import to_text
145
146 from ansible_collections.community.hashi_vault.plugins.module_utils._hashi_vault_module import HashiVaultModule
147 from ansible_collections.community.hashi_vault.plugins.module_utils._hashi_vault_common import HashiVaultValueError
148
149
150 def run_module():
151 argspec = HashiVaultModule.generate_argspec(
152 engine_mount_point=dict(type='str', default='secret'),
153 path=dict(type='str', required=True),
154 version=dict(type='int'),
155 )
156
157 module = HashiVaultModule(
158 argument_spec=argspec,
159 supports_check_mode=True
160 )
161
162 engine_mount_point = module.params.get('engine_mount_point')
163 path = module.params.get('path')
164 version = module.params.get('version')
165
166 module.connection_options.process_connection_options()
167 client_args = module.connection_options.get_hvac_connection_options()
168 client = module.helper.get_vault_client(**client_args)
169 hvac_exceptions = module.helper.get_hvac().exceptions
170
171 try:
172 module.authenticator.validate()
173 module.authenticator.authenticate(client)
174 except (NotImplementedError, HashiVaultValueError) as e:
175 module.fail_json(msg=to_text(e), exception=traceback.format_exc())
176
177 try:
178 raw = client.secrets.kv.v2.read_secret_version(path=path, version=version, mount_point=engine_mount_point)
179 except hvac_exceptions.Forbidden as e:
180 module.fail_json(msg="Forbidden: Permission Denied to path ['%s']." % path, exception=traceback.format_exc())
181 except hvac_exceptions.InvalidPath as e:
182 module.fail_json(
183 msg="Invalid or missing path ['%s'] with secret version '%s'. Check the path or secret version." % (path, version or 'latest'),
184 exception=traceback.format_exc()
185 )
186
187 data = raw['data']
188 metadata = data['metadata']
189 secret = data['data']
190 module.exit_json(raw=raw, data=data, secret=secret, metadata=metadata)
191
192
193 def main():
194 run_module()
195
196
197 if __name__ == '__main__':
198 main()