| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# (c) 2023, Tom Kivlin (@tomkivlin) |
| 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 = """ |
| 11 |
module: vault_list |
| 12 |
version_added: 4.1.0 |
| 13 |
author: |
| 14 |
- Tom Kivlin (@tomkivlin) |
| 15 |
short_description: Perform a list operation against HashiCorp Vault |
| 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 |
- Performs a generic list operation against a given path in HashiCorp Vault. |
| 21 |
seealso: |
| 22 |
- ref: community.hashi_vault.vault_list lookup <ansible_collections.community.hashi_vault.vault_list_lookup> |
| 23 |
description: The official documentation for the C(community.hashi_vault.vault_list) lookup plugin. |
| 24 |
extends_documentation_fragment: |
| 25 |
- community.hashi_vault.attributes |
| 26 |
- community.hashi_vault.attributes.action_group |
| 27 |
- community.hashi_vault.attributes.check_mode_read_only |
| 28 |
- community.hashi_vault.connection |
| 29 |
- community.hashi_vault.auth |
| 30 |
options: |
| 31 |
path: |
| 32 |
description: Vault path to be listed. |
| 33 |
type: str |
| 34 |
required: true |
| 35 |
""" |
| 36 |
|
| 37 |
EXAMPLES = """ |
| 38 |
- name: List kv2 secrets from Vault via the remote host with userpass auth |
| 39 |
community.hashi_vault.vault_list: |
| 40 |
url: https://vault:8201 |
| 41 |
path: secret/metadata |
| 42 |
# For kv2, the path needs to follow the pattern 'mount_point/metadata' or 'mount_point/metadata/path' to list all secrets in that path |
| 43 |
auth_method: userpass |
| 44 |
username: user |
| 45 |
password: '{{ passwd }}' |
| 46 |
register: secret |
| 47 |
|
| 48 |
- name: Display the secrets found at the path provided above |
| 49 |
ansible.builtin.debug: |
| 50 |
msg: "{{ secret.data.data['keys'] }}" |
| 51 |
# Note that secret.data.data.keys won't work as 'keys' is a built-in method |
| 52 |
|
| 53 |
- name: List access policies from Vault via the remote host |
| 54 |
community.hashi_vault.vault_list: |
| 55 |
url: https://vault:8201 |
| 56 |
path: sys/policies/acl |
| 57 |
register: policies |
| 58 |
|
| 59 |
- name: Display the policy names |
| 60 |
ansible.builtin.debug: |
| 61 |
msg: "{{ policies.data.data['keys'] }}" |
| 62 |
# Note that secret.data.data.keys won't work as 'keys' is a built-in method |
| 63 |
""" |
| 64 |
|
| 65 |
RETURN = """ |
| 66 |
data: |
| 67 |
description: The raw result of the list against the given path. |
| 68 |
returned: success |
| 69 |
type: dict |
| 70 |
""" |
| 71 |
|
| 72 |
import traceback |
| 73 |
|
| 74 |
from ansible.module_utils.common.text.converters import to_text |
| 75 |
|
| 76 |
from ansible_collections.community.hashi_vault.plugins.module_utils._hashi_vault_module import HashiVaultModule |
| 77 |
from ansible_collections.community.hashi_vault.plugins.module_utils._hashi_vault_common import HashiVaultValueError |
| 78 |
|
| 79 |
|
| 80 |
def run_module(): |
| 81 |
argspec = HashiVaultModule.generate_argspec( |
| 82 |
path=dict(type='str', required=True), |
| 83 |
) |
| 84 |
|
| 85 |
module = HashiVaultModule( |
| 86 |
argument_spec=argspec, |
| 87 |
supports_check_mode=True |
| 88 |
) |
| 89 |
|
| 90 |
path = module.params.get('path') |
| 91 |
|
| 92 |
module.connection_options.process_connection_options() |
| 93 |
client_args = module.connection_options.get_hvac_connection_options() |
| 94 |
client = module.helper.get_vault_client(**client_args) |
| 95 |
hvac_exceptions = module.helper.get_hvac().exceptions |
| 96 |
|
| 97 |
try: |
| 98 |
module.authenticator.validate() |
| 99 |
module.authenticator.authenticate(client) |
| 100 |
except (NotImplementedError, HashiVaultValueError) as e: |
| 101 |
module.fail_json(msg=to_text(e), exception=traceback.format_exc()) |
| 102 |
|
| 103 |
try: |
| 104 |
data = client.list(path) |
| 105 |
except hvac_exceptions.Forbidden as e: |
| 106 |
module.fail_json(msg="Forbidden: Permission Denied to path '%s'." % path, exception=traceback.format_exc()) |
| 107 |
|
| 108 |
if data is None: |
| 109 |
module.fail_json(msg="The path '%s' doesn't seem to exist." % path) |
| 110 |
|
| 111 |
module.exit_json(data=data) |
| 112 |
|
| 113 |
|
| 114 |
def main(): |
| 115 |
run_module() |
| 116 |
|
| 117 |
|
| 118 |
if __name__ == '__main__': |
| 119 |
main() |