Debian Codegraph @

ansible_collections/community/hashi_vault/plugins/lookup/vault_login.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_login
10 version_added: 2.2.0
11 author:
12 - Brian Scholer (@briantist)
13 short_description: Perform a login 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 login operation against a given path in HashiCorp Vault, returning the login response, including the token.
19 seealso:
20 - module: community.hashi_vault.vault_login
21 - ref: community.hashi_vault.vault_login_token filter <ansible_collections.community.hashi_vault.vault_login_token_filter>
22 description: The official documentation for the C(community.hashi_vault.vault_login_token) filter plugin.
23 notes:
24 - This lookup does not use the term string and will not work correctly in loops. Only a single response will be returned.
25 - "A login is a write operation (creating a token persisted to storage), so this module always reports C(changed=True),
26 except when used with C(token) auth, because no new token is created in that case. For the purposes of Ansible playbooks however,
27 it may be more useful to set C(changed_when=false) if you're doing idempotency checks against the target system."
28 - The C(none) auth method is not valid for this plugin because there is no response to return.
29 - "With C(token) auth, no actual login is performed.
30 Instead, the given token's additional information is returned in a structure that resembles what login responses look like."
31 - "The C(token) auth method will only return full information if I(token_validate=True).
32 If the token does not have the C(lookup-self) capability, this will fail. If I(token_validate=False), only the token value itself
33 will be returned in the structure."
34 extends_documentation_fragment:
35 - community.hashi_vault.connection
36 - community.hashi_vault.connection.plugins
37 - community.hashi_vault.auth
38 - community.hashi_vault.auth.plugins
39 options:
40 _terms:
41 description: This is unused and any terms supplied will be ignored.
42 type: str
43 required: false
44 token_validate:
45 default: true
46 """
47
48 EXAMPLES = """
49 - name: Set a fact with a lookup result
50 set_fact:
51 login_data: "{{ lookup('community.hashi_vault.vault_login', url='https://vault', auth_method='userpass', username=user, password=pwd) }}"
52
53 - name: Retrieve an approle role ID (token via filter)
54 community.hashi_vault.vault_read:
55 url: https://vault:8201
56 auth_method: token
57 token: '{{ login_data | community.hashi_vault.vault_login_token }}'
58 path: auth/approle/role/role-name/role-id
59 register: approle_id
60
61 - name: Retrieve an approle role ID (token via direct dict access)
62 community.hashi_vault.vault_read:
63 url: https://vault:8201
64 auth_method: token
65 token: '{{ login_data.auth.client_token }}'
66 path: auth/approle/role/role-name/role-id
67 register: approle_id
68
69 - name: Login with GCP auth
70 set_fact:
71 gcp_login: "{{ lookup('community.hashi_vault.vault_login', auth_method='gcp', role_id='myroleid', jwt='myjwt', url='https://vault:8200') }}"
72
73 - name: Read a secret using the GCP login token
74 community.hashi_vault.vault_read:
75 url: https://vault:8200
76 token: '{{ gcp_login.auth.client_token }}'
77 path: secret/data/foo
78
79 """
80
81 RETURN = """
82 _raw:
83 description:
84 - The result of the login with the given auth method.
85 type: list
86 elements: dict
87 contains:
88 auth:
89 description: The C(auth) member of the login response.
90 returned: success
91 type: dict
92 contains:
93 client_token:
94 description: Contains the token provided by the login operation (or the input token when I(auth_method=token)).
95 returned: success
96 type: str
97 data:
98 description: The C(data) member of the login response.
99 returned: success, when available
100 type: dict
101 """
102
103 from ansible.errors import AnsibleError
104 from ansible.utils.display import Display
105
106 from ...plugins.plugin_utils._hashi_vault_lookup_base import HashiVaultLookupBase
107 from ...plugins.module_utils._hashi_vault_common import HashiVaultValueError
108
109 display = Display()
110
111
112 class LookupModule(HashiVaultLookupBase):
113 def run(self, terms, variables=None, **kwargs):
114 self.set_options(direct=kwargs, var_options=variables)
115 # TODO: remove process_deprecations() if backported fix is available (see method definition)
116 self.process_deprecations()
117
118 if self.get_option('auth_method') == 'none':
119 raise AnsibleError("The 'none' auth method is not valid for this lookup.")
120
121 self.connection_options.process_connection_options()
122 client_args = self.connection_options.get_hvac_connection_options()
123 client = self.helper.get_vault_client(**client_args)
124
125 if len(terms) != 0:
126 display.warning("Supplied term strings will be ignored. This lookup does not use term strings.")
127
128 try:
129 self.authenticator.validate()
130 response = self.authenticator.authenticate(client)
131 except (NotImplementedError, HashiVaultValueError) as e:
132 raise AnsibleError(e)
133
134 return [response]