Debian Codegraph @

ansible_collections/community/hashi_vault/plugins/module_utils/_auth_method_jwt.py

raw
1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2021 Brian Scholer (@briantist)
3 # Simplified BSD License (see LICENSES/BSD-2-Clause.txt or https://opensource.org/licenses/BSD-2-Clause)
4 # SPDX-License-Identifier: BSD-2-Clause
5
6 '''Python versions supported: >=3.8'''
7
8 # FOR INTERNAL COLLECTION USE ONLY
9 # The interfaces in this file are meant for use within the community.hashi_vault collection
10 # and may not remain stable to outside uses. Changes may be made in ANY release, even a bugfix release.
11 # See also: https://github.com/ansible/community/issues/539#issuecomment-780839686
12 # Please open an issue if you have questions about this.
13
14 from __future__ import absolute_import, division, print_function
15 __metaclass__ = type
16
17 from ansible_collections.community.hashi_vault.plugins.module_utils._hashi_vault_common import HashiVaultAuthMethodBase
18
19
20 class HashiVaultAuthMethodJwt(HashiVaultAuthMethodBase):
21 '''HashiVault option group class for auth: jwt'''
22
23 NAME = 'jwt'
24 OPTIONS = ['jwt', 'role_id', 'mount_point']
25
26 def __init__(self, option_adapter, warning_callback, deprecate_callback):
27 super(HashiVaultAuthMethodJwt, self).__init__(option_adapter, warning_callback, deprecate_callback)
28
29 def validate(self):
30 self.validate_by_required_fields('role_id', 'jwt')
31
32 def authenticate(self, client, use_token=True):
33 params = self._options.get_filled_options(*self.OPTIONS)
34 params['role'] = params.pop('role_id')
35
36 if 'mount_point' in params:
37 params['path'] = params.pop('mount_point')
38
39 try:
40 response = client.auth.jwt.jwt_login(**params)
41 except (NotImplementedError, AttributeError):
42 raise NotImplementedError("JWT authentication requires HVAC version 0.10.5 or higher.")
43
44 # must manually set the client token with JWT login
45 # see https://github.com/hvac/hvac/issues/644
46 # fixed in https://github.com/hvac/hvac/pull/746
47 # but we do it manually to maintain compatibilty with older hvac versions.
48 if use_token:
49 client.token = response['auth']['client_token']
50
51 return response