| 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 |
import os |
| 18 |
|
| 19 |
from ansible_collections.community.hashi_vault.plugins.module_utils._hashi_vault_common import ( |
| 20 |
HashiVaultAuthMethodBase, |
| 21 |
HashiVaultValueError, |
| 22 |
) |
| 23 |
|
| 24 |
|
| 25 |
class HashiVaultAuthMethodToken(HashiVaultAuthMethodBase): |
| 26 |
'''HashiVault option group class for auth: userpass''' |
| 27 |
|
| 28 |
NAME = 'token' |
| 29 |
OPTIONS = ['token', 'token_path', 'token_file', 'token_validate'] |
| 30 |
|
| 31 |
_LATE_BINDING_ENV_VAR_OPTIONS = { |
| 32 |
'token': dict(env=['VAULT_TOKEN']), |
| 33 |
'token_path': dict(env=['HOME']), |
| 34 |
} |
| 35 |
|
| 36 |
def __init__(self, option_adapter, warning_callback, deprecate_callback): |
| 37 |
super(HashiVaultAuthMethodToken, self).__init__(option_adapter, warning_callback, deprecate_callback) |
| 38 |
|
| 39 |
def _simulate_login_response(self, token, lookup_response=None): |
| 40 |
'''returns a similar structure to a login method's return, optionally incorporating a lookup-self response''' |
| 41 |
|
| 42 |
response = { |
| 43 |
'auth': { |
| 44 |
'client_token': token |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
if lookup_response is None: |
| 49 |
return response |
| 50 |
|
| 51 |
# first merge in the entire response at the top level |
| 52 |
# but, rather than being missing, the auth field is going to be None, |
| 53 |
# so we explicitly overwrite that with our original value. |
| 54 |
response.update(lookup_response, auth=response['auth']) |
| 55 |
|
| 56 |
# then we'll merge the data field right into the auth field |
| 57 |
response['auth'].update(lookup_response['data']) |
| 58 |
|
| 59 |
# and meta->metadata needs a name change |
| 60 |
metadata = response['auth'].pop('meta', None) |
| 61 |
if metadata: |
| 62 |
response['auth']['metadata'] = metadata |
| 63 |
|
| 64 |
return response |
| 65 |
|
| 66 |
def validate(self): |
| 67 |
self.process_late_binding_env_vars(self._LATE_BINDING_ENV_VAR_OPTIONS) |
| 68 |
|
| 69 |
if self._options.get_option_default('token') is None and self._options.get_option_default('token_path') is not None: |
| 70 |
token_filename = os.path.join( |
| 71 |
self._options.get_option('token_path'), |
| 72 |
self._options.get_option('token_file') |
| 73 |
) |
| 74 |
if os.path.exists(token_filename): |
| 75 |
if not os.path.isfile(token_filename): |
| 76 |
raise HashiVaultValueError("The Vault token file '%s' was found but is not a file." % token_filename) |
| 77 |
with open(token_filename) as token_file: |
| 78 |
self._options.set_option('token', token_file.read().strip()) |
| 79 |
|
| 80 |
if self._options.get_option_default('token') is None: |
| 81 |
raise HashiVaultValueError("No Vault Token specified or discovered.") |
| 82 |
|
| 83 |
def authenticate(self, client, use_token=True, lookup_self=False): |
| 84 |
token = self._options.get_option('token') |
| 85 |
validate = self._options.get_option_default('token_validate') |
| 86 |
|
| 87 |
response = None |
| 88 |
|
| 89 |
if use_token: |
| 90 |
client.token = token |
| 91 |
|
| 92 |
if lookup_self or validate: |
| 93 |
from hvac import exceptions |
| 94 |
|
| 95 |
try: |
| 96 |
try: |
| 97 |
response = client.auth.token.lookup_self() |
| 98 |
except (NotImplementedError, AttributeError): |
| 99 |
# usually we would warn here, but the v1 method doesn't seem to be deprecated (yet?) |
| 100 |
response = client.lookup_token() # when token=None on this method, it calls lookup-self |
| 101 |
except (exceptions.Forbidden, exceptions.InvalidPath, exceptions.InvalidRequest): |
| 102 |
if validate: |
| 103 |
raise HashiVaultValueError("Invalid Vault Token Specified.") |
| 104 |
|
| 105 |
return self._simulate_login_response(token, response) |