| 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 |
# please keep this list in alphabetical order of auth method name |
| 18 |
from ansible_collections.community.hashi_vault.plugins.module_utils._auth_method_approle import HashiVaultAuthMethodApprole |
| 19 |
from ansible_collections.community.hashi_vault.plugins.module_utils._auth_method_aws_iam import HashiVaultAuthMethodAwsIam |
| 20 |
from ansible_collections.community.hashi_vault.plugins.module_utils._auth_method_azure import HashiVaultAuthMethodAzure |
| 21 |
from ansible_collections.community.hashi_vault.plugins.module_utils._auth_method_cert import HashiVaultAuthMethodCert |
| 22 |
from ansible_collections.community.hashi_vault.plugins.module_utils._auth_method_gcp import HashiVaultAuthMethodGcp |
| 23 |
from ansible_collections.community.hashi_vault.plugins.module_utils._auth_method_jwt import HashiVaultAuthMethodJwt |
| 24 |
from ansible_collections.community.hashi_vault.plugins.module_utils._auth_method_ldap import HashiVaultAuthMethodLdap |
| 25 |
from ansible_collections.community.hashi_vault.plugins.module_utils._auth_method_none import HashiVaultAuthMethodNone |
| 26 |
from ansible_collections.community.hashi_vault.plugins.module_utils._auth_method_token import HashiVaultAuthMethodToken |
| 27 |
from ansible_collections.community.hashi_vault.plugins.module_utils._auth_method_userpass import HashiVaultAuthMethodUserpass |
| 28 |
|
| 29 |
|
| 30 |
class HashiVaultAuthenticator(): |
| 31 |
ARGSPEC = dict( |
| 32 |
auth_method=dict(type='str', default='token', choices=[ |
| 33 |
'token', |
| 34 |
'userpass', |
| 35 |
'ldap', |
| 36 |
'approle', |
| 37 |
'aws_iam', |
| 38 |
'azure', |
| 39 |
'jwt', |
| 40 |
'cert', |
| 41 |
'gcp', |
| 42 |
'none', |
| 43 |
]), |
| 44 |
mount_point=dict(type='str'), |
| 45 |
token=dict(type='str', no_log=True, default=None), |
| 46 |
token_path=dict(type='str', default=None, no_log=False), |
| 47 |
token_file=dict(type='str', default='.vault-token'), |
| 48 |
token_validate=dict(type='bool', default=False), |
| 49 |
username=dict(type='str'), |
| 50 |
password=dict(type='str', no_log=True), |
| 51 |
role_id=dict(type='str'), |
| 52 |
secret_id=dict(type='str', no_log=True), |
| 53 |
jwt=dict(type='str', no_log=True), |
| 54 |
aws_profile=dict(type='str', aliases=['boto_profile']), |
| 55 |
aws_access_key=dict(type='str', aliases=['aws_access_key_id'], no_log=False), |
| 56 |
aws_secret_key=dict(type='str', aliases=['aws_secret_access_key'], no_log=True), |
| 57 |
aws_security_token=dict(type='str', no_log=False), |
| 58 |
region=dict(type='str'), |
| 59 |
aws_iam_server_id=dict(type='str'), |
| 60 |
azure_tenant_id=dict(type='str'), |
| 61 |
azure_client_id=dict(type='str'), |
| 62 |
azure_client_secret=dict(type='str', no_log=True), |
| 63 |
azure_resource=dict(type='str', default='https://management.azure.com/'), |
| 64 |
cert_auth_private_key=dict(type='path', no_log=False), |
| 65 |
cert_auth_public_key=dict(type='path'), |
| 66 |
) |
| 67 |
|
| 68 |
def __init__(self, option_adapter, warning_callback, deprecate_callback): |
| 69 |
self._options = option_adapter |
| 70 |
self._selector = { |
| 71 |
# please keep this list in alphabetical order of auth method name |
| 72 |
# so that it's easier to scan and see at a glance that a given auth method is present or absent |
| 73 |
'approle': HashiVaultAuthMethodApprole(option_adapter, warning_callback, deprecate_callback), |
| 74 |
'aws_iam': HashiVaultAuthMethodAwsIam(option_adapter, warning_callback, deprecate_callback), |
| 75 |
'azure': HashiVaultAuthMethodAzure(option_adapter, warning_callback, deprecate_callback), |
| 76 |
'cert': HashiVaultAuthMethodCert(option_adapter, warning_callback, deprecate_callback), |
| 77 |
'gcp': HashiVaultAuthMethodGcp(option_adapter, warning_callback, deprecate_callback), |
| 78 |
'jwt': HashiVaultAuthMethodJwt(option_adapter, warning_callback, deprecate_callback), |
| 79 |
'ldap': HashiVaultAuthMethodLdap(option_adapter, warning_callback, deprecate_callback), |
| 80 |
'none': HashiVaultAuthMethodNone(option_adapter, warning_callback, deprecate_callback), |
| 81 |
'token': HashiVaultAuthMethodToken(option_adapter, warning_callback, deprecate_callback), |
| 82 |
'userpass': HashiVaultAuthMethodUserpass(option_adapter, warning_callback, deprecate_callback), |
| 83 |
} |
| 84 |
|
| 85 |
self.warn = warning_callback |
| 86 |
self.deprecate = deprecate_callback |
| 87 |
|
| 88 |
def _get_method_object(self, method=None): |
| 89 |
if method is None: |
| 90 |
method = self._options.get_option('auth_method') |
| 91 |
|
| 92 |
try: |
| 93 |
o_method = self._selector[method] |
| 94 |
except KeyError: |
| 95 |
raise NotImplementedError("auth method '%s' is not implemented in HashiVaultAuthenticator" % method) |
| 96 |
|
| 97 |
return o_method |
| 98 |
|
| 99 |
def validate(self, *args, **kwargs): |
| 100 |
method = self._get_method_object(kwargs.pop('method', None)) |
| 101 |
method.validate(*args, **kwargs) |
| 102 |
|
| 103 |
def authenticate(self, *args, **kwargs): |
| 104 |
method = self._get_method_object(kwargs.pop('method', None)) |
| 105 |
return method.authenticate(*args, **kwargs) |