Debian Codegraph @

ansible_collections/community/hashi_vault/plugins/modules/vault_kv1_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_kv1_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 1 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 1 secret store.
21 seealso:
22 - ref: community.hashi_vault.vault_kv1_get lookup <ansible_collections.community.hashi_vault.vault_kv1_get_lookup>
23 description: The official documentation for the C(community.hashi_vault.vault_kv1_get) lookup plugin.
24 - module: community.hashi_vault.vault_kv2_get
25 - name: KV1 Secrets Engine
26 description: Documentation for the Vault KV secrets engine, version 1.
27 link: https://www.vaultproject.io/docs/secrets/kv/kv-v1
28 extends_documentation_fragment:
29 - community.hashi_vault.attributes
30 - community.hashi_vault.attributes.action_group
31 - community.hashi_vault.attributes.check_mode_read_only
32 - community.hashi_vault.connection
33 - community.hashi_vault.auth
34 - community.hashi_vault.engine_mount
35 options:
36 engine_mount_point:
37 default: kv
38 path:
39 description:
40 - Vault KV path to be read.
41 - This is relative to the I(engine_mount_point), so the mount path should not be included.
42 type: str
43 required: True
44 '''
45
46 EXAMPLES = r'''
47 - name: Read a kv1 secret from Vault via the remote host with userpass auth
48 community.hashi_vault.vault_kv1_get:
49 url: https://vault:8201
50 path: hello
51 auth_method: userpass
52 username: user
53 password: '{{ passwd }}'
54 register: response
55 # equivalent API path is kv/hello
56
57 - name: Display the results
58 ansible.builtin.debug:
59 msg:
60 - "Secret: {{ response.secret }}"
61 - "Data: {{ response.data }} (same as secret in kv1)"
62 - "Metadata: {{ response.metadata }} (response info in kv1)"
63 - "Full response: {{ response.raw }}"
64 - "Value of key 'password' in the secret: {{ response.secret.password }}"
65
66 - name: Read a secret from kv1 with a different mount via the remote host
67 community.hashi_vault.vault_kv1_get:
68 url: https://vault:8201
69 engine_mount_point: custom/kv1/mount
70 path: hello
71 register: response
72 # equivalent API path is custom/kv1/mount/hello
73
74 - name: Display the results
75 ansible.builtin.debug:
76 msg:
77 - "Secret: {{ response.secret }}"
78 - "Data: {{ response.data }} (same as secret in kv1)"
79 - "Metadata: {{ response.metadata }} (response info in kv1)"
80 - "Full response: {{ response.raw }}"
81 '''
82
83 RETURN = r'''
84 raw:
85 description: The raw result of the read against the given path.
86 returned: success
87 type: dict
88 sample:
89 auth: null
90 data:
91 Key1: value1
92 Key2: value2
93 lease_duration: 2764800
94 lease_id: ""
95 renewable: false
96 request_id: e99f145f-f02a-7073-1229-e3f191057a70
97 warnings: null
98 wrap_info: null
99 data:
100 description: The C(data) field of raw result. This can also be accessed via C(raw.data).
101 returned: success
102 type: dict
103 sample:
104 Key1: value1
105 Key2: value2
106 secret:
107 description: The C(data) field of the raw result. This is identical to C(data) in the return values.
108 returned: success
109 type: dict
110 sample:
111 Key1: value1
112 Key2: value2
113 metadata:
114 description: This is a synthetic result. It is the same as C(raw) with C(data) removed.
115 returned: success
116 type: dict
117 sample:
118 auth: null
119 lease_duration: 2764800
120 lease_id: ""
121 renewable: false
122 request_id: e99f145f-f02a-7073-1229-e3f191057a70
123 warnings: null
124 wrap_info: null
125 '''
126
127 import traceback
128
129 from ansible.module_utils.common.text.converters import to_text
130
131 from ansible_collections.community.hashi_vault.plugins.module_utils._hashi_vault_module import HashiVaultModule
132 from ansible_collections.community.hashi_vault.plugins.module_utils._hashi_vault_common import HashiVaultValueError
133
134
135 def run_module():
136 argspec = HashiVaultModule.generate_argspec(
137 engine_mount_point=dict(type='str', default='kv'),
138 path=dict(type='str', required=True),
139 )
140
141 module = HashiVaultModule(
142 argument_spec=argspec,
143 supports_check_mode=True
144 )
145
146 engine_mount_point = module.params.get('engine_mount_point')
147 path = module.params.get('path')
148
149 module.connection_options.process_connection_options()
150 client_args = module.connection_options.get_hvac_connection_options()
151 client = module.helper.get_vault_client(**client_args)
152 hvac_exceptions = module.helper.get_hvac().exceptions
153
154 try:
155 module.authenticator.validate()
156 module.authenticator.authenticate(client)
157 except (NotImplementedError, HashiVaultValueError) as e:
158 module.fail_json(msg=to_text(e), exception=traceback.format_exc())
159
160 try:
161 raw = client.secrets.kv.v1.read_secret(path=path, mount_point=engine_mount_point)
162 except hvac_exceptions.Forbidden as e:
163 module.fail_json(msg="Forbidden: Permission Denied to path ['%s']." % path, exception=traceback.format_exc())
164 except hvac_exceptions.InvalidPath as e:
165 if 'Invalid path for a versioned K/V secrets engine' in to_text(e):
166 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."
167 else:
168 msg = "Invalid or missing path ['%s']."
169
170 module.fail_json(msg=msg % (path,), exception=traceback.format_exc())
171
172 metadata = raw.copy()
173 data = metadata.pop('data')
174 module.exit_json(raw=raw, data=data, secret=data, metadata=metadata)
175
176
177 def main():
178 run_module()
179
180
181 if __name__ == '__main__':
182 main()