Debian Codegraph @

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

raw
1 # -*- coding: utf-8 -*-
2 # Copyright (c) 2022 Junrui Chen (@jchenship)
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 (
18 HashiVaultAuthMethodBase,
19 HashiVaultValueError,
20 )
21
22
23 class HashiVaultAuthMethodAzure(HashiVaultAuthMethodBase):
24 '''HashiVault auth method for Azure'''
25
26 NAME = 'azure'
27 OPTIONS = [
28 'role_id',
29 'jwt',
30 'mount_point',
31 'azure_tenant_id',
32 'azure_client_id',
33 'azure_client_secret',
34 'azure_resource',
35 ]
36
37 def __init__(self, option_adapter, warning_callback, deprecate_callback):
38 super(HashiVaultAuthMethodAzure, self).__init__(
39 option_adapter, warning_callback, deprecate_callback
40 )
41
42 def validate(self):
43 params = {
44 'role': self._options.get_option_default('role_id'),
45 'jwt': self._options.get_option_default('jwt'),
46 }
47 if not params['role']:
48 raise HashiVaultValueError(
49 'role_id is required for azure authentication.'
50 )
51
52 # if mount_point is not provided, it will use the default value defined
53 # in hvac library (e.g. `azure`)
54 mount_point = self._options.get_option_default('mount_point')
55 if mount_point:
56 params['mount_point'] = mount_point
57
58 # if jwt exists, use provided jwt directly, otherwise trying to get jwt
59 # from azure service principal or managed identity
60 if not params['jwt']:
61 azure_tenant_id = self._options.get_option_default('azure_tenant_id')
62 azure_client_id = self._options.get_option_default('azure_client_id')
63 azure_client_secret = self._options.get_option_default('azure_client_secret')
64
65 # the logic of getting azure scope is from this function
66 # https://github.com/Azure/azure-cli/blob/azure-cli-2.39.0/src/azure-cli-core/azure/cli/core/auth/util.py#L72
67 # the reason we expose resource instead of scope is resource is
68 # more aligned with the vault azure auth config here
69 # https://www.vaultproject.io/api-docs/auth/azure#resource
70 azure_resource = self._options.get_option('azure_resource')
71 azure_scope = azure_resource + "/.default"
72
73 try:
74 import azure.identity
75 except ImportError:
76 raise HashiVaultValueError(
77 "azure-identity is required for getting access token from azure service principal or managed identity."
78 )
79
80 if azure_client_id and azure_client_secret:
81 # service principal
82 if not azure_tenant_id:
83 raise HashiVaultValueError(
84 'azure_tenant_id is required when using azure service principal.'
85 )
86 azure_credentials = azure.identity.ClientSecretCredential(
87 azure_tenant_id, azure_client_id, azure_client_secret
88 )
89 elif azure_client_id:
90 # user assigned managed identity
91 azure_credentials = azure.identity.ManagedIdentityCredential(
92 client_id=azure_client_id
93 )
94 else:
95 # system assigned managed identity
96 azure_credentials = azure.identity.ManagedIdentityCredential()
97
98 params['jwt'] = azure_credentials.get_token(azure_scope).token
99
100 self._auth_azure_login_params = params
101
102 def authenticate(self, client, use_token=True):
103 params = self._auth_azure_login_params
104 response = client.auth.azure.login(use_token=use_token, **params)
105 return response