Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # (c) 2021, 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 = """
11 module: vault_login
12 version_added: 2.2.0
13 author:
14 - Brian Scholer (@briantist)
15 short_description: Perform a login 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 login operation against a given path in HashiCorp Vault, returning the login response, including the token.
21 seealso:
22 - ref: community.hashi_vault.vault_login lookup <ansible_collections.community.hashi_vault.vault_login_lookup>
23 description: The official documentation for the C(community.hashi_vault.vault_login) lookup plugin.
24 - ref: community.hashi_vault.vault_login_token filter <ansible_collections.community.hashi_vault.vault_login_token_filter>
25 description: The official documentation for the C(community.hashi_vault.vault_login_token) filter plugin.
26 extends_documentation_fragment:
27 - community.hashi_vault.attributes
28 - community.hashi_vault.attributes.action_group
29 - community.hashi_vault.connection
30 - community.hashi_vault.auth
31 notes:
32 - "A login is a write operation (creating a token persisted to storage), so this module always reports C(changed=True),
33 except when used with C(token) auth, because no new token is created in that case. For the purposes of Ansible playbooks however,
34 it may be more useful to set C(changed_when=false) if you're doing idempotency checks against the target system."
35 - The C(none) auth method is not valid for this module because there is no response to return.
36 - "With C(token) auth, no actual login is performed.
37 Instead, the given token's additional information is returned in a structure that resembles what login responses look like."
38 - "The C(token) auth method will only return full information if I(token_validate=True).
39 If the token does not have the C(lookup-self) capability, this will fail. If I(token_validate=False), only the token value itself
40 will be returned in the structure."
41 attributes:
42 check_mode:
43 support: partial
44 details:
45 - In check mode, this module will not perform a login, and will instead return a basic structure with an empty token.
46 However this may not be useful if the token is required for follow on tasks.
47 - It may be better to use this module with C(check_mode=false) in order to have a valid token that can be used.
48 options:
49 token_validate:
50 default: true
51 """
52
53 EXAMPLES = """
54 - name: Login and use the resulting token
55 community.hashi_vault.vault_login:
56 url: https://vault:8201
57 auth_method: userpass
58 username: user
59 password: '{{ passwd }}'
60 register: login_data
61
62 - name: Retrieve an approle role ID (token via filter)
63 community.hashi_vault.vault_read:
64 url: https://vault:8201
65 auth_method: token
66 token: '{{ login_data | community.hashi_vault.vault_login_token }}'
67 path: auth/approle/role/role-name/role-id
68 register: approle_id
69
70 - name: Retrieve an approle role ID (token via direct dict access)
71 community.hashi_vault.vault_read:
72 url: https://vault:8201
73 auth_method: token
74 token: '{{ login_data.login.auth.client_token }}'
75 path: auth/approle/role/role-name/role-id
76 register: approle_id
77
78 # GCP auth
79 - name: Login with GCP auth
80 community.hashi_vault.vault_login:
81 auth_method: gcp
82 role_id: myroleid
83 jwt: myjwt
84 url: https://vault:8200
85 register: gcp_login
86
87 - name: Read a secret using the GCP login token
88 community.hashi_vault.vault_read:
89 url: https://vault:8200
90 token: '{{ gcp_login.login.auth.client_token }}'
91 path: secret/data/foo
92 """
93
94 RETURN = """
95 login:
96 description: The result of the login against the given auth method.
97 returned: success
98 type: dict
99 contains:
100 auth:
101 description: The C(auth) member of the login response.
102 returned: success
103 type: dict
104 contains:
105 client_token:
106 description: Contains the token provided by the login operation (or the input token when I(auth_method=token)).
107 returned: success
108 type: str
109 data:
110 description: The C(data) member of the login response.
111 returned: success, when available
112 type: dict
113 """
114
115 import traceback
116
117 from ansible.module_utils.common.text.converters import to_text
118
119 from ...plugins.module_utils._hashi_vault_module import HashiVaultModule
120 from ...plugins.module_utils._hashi_vault_common import HashiVaultValueError
121
122 # we don't actually need to import hvac directly in this module
123 # because all of the hvac calls happen in module utils, but
124 # we would like to control the error message here for consistency.
125
126
127 def run_module():
128 argspec = HashiVaultModule.generate_argspec(
129 # we override this from the shared argspec in order to turn off no_log
130 # otherwise we would not be able to return the input token value
131 token=dict(type='str', no_log=False, default=None),
132
133 # we override this from the shared argspec because the default for
134 # this module should be True, which differs from the rest of the
135 # collection since 4.0.0.
136 token_validate=dict(type='bool', default=True)
137 )
138
139 module = HashiVaultModule(
140 argument_spec=argspec,
141 supports_check_mode=True
142 )
143
144 # a login is technically a write operation, using storage and resources
145 changed = True
146 auth_method = module.params.get('auth_method')
147
148 if auth_method == 'none':
149 module.fail_json(msg="The 'none' auth method is not valid for this module.")
150
151 if auth_method == 'token':
152 # with the token auth method, we don't actually perform a login operation
153 # nor change the state of Vault; it's read-only (to lookup the token's info)
154 changed = False
155
156 module.connection_options.process_connection_options()
157 client_args = module.connection_options.get_hvac_connection_options()
158 client = module.helper.get_vault_client(**client_args)
159
160 try:
161 module.authenticator.validate()
162 if module.check_mode:
163 response = {'auth': {'client_token': None}}
164 else:
165 response = module.authenticator.authenticate(client)
166 except (NotImplementedError, HashiVaultValueError) as e:
167 module.fail_json(msg=to_text(e), exception=traceback.format_exc())
168
169 module.exit_json(changed=changed, login=response)
170
171
172 def main():
173 run_module()
174
175
176 if __name__ == '__main__':
177 main()