|
30 | 30 | description: Client id of service principal that has access to the Azure Key Vault |
31 | 31 | secret: |
32 | 32 | description: Secret of the service principal. |
33 | | - tenant_id: |
| 33 | + tenant: |
34 | 34 | description: Tenant id of service principal. |
| 35 | + aliases: |
| 36 | + - tenant_id |
35 | 37 | use_msi: |
36 | 38 | description: MSI token autodiscover, default is true. |
37 | 39 | use_cli: |
|
44 | 46 | - For enabling MSI on Azure VM, please refer to this doc https://docs.microsoft.com/en-us/azure/active-directory/managed-service-identity/ |
45 | 47 | - After enabling MSI on Azure VM, remember to grant access of the Key Vault to the VM by adding a new Acess Policy in Azure Portal. |
46 | 48 | - If MSI is not enabled on ansible host, it's required to provide a valid service principal which has access to the key vault. |
47 | | - - To authenticate via service principal, pass client_id, secret and tenant_id or set environment variables |
| 49 | + - To authenticate via service principal, pass client_id, secret and tenant or set environment variables |
48 | 50 | AZURE_CLIENT_ID, AZURE_CLIENT_SECRET and AZURE_TENANT_ID. |
49 | 51 | - Authentication via C(az login) is also supported. |
50 | 52 | - To use a plugin from a collection, please reference the full namespace, collection name, and lookup plugin name that you want to use. |
|
84 | 86 | vault_url=url, |
85 | 87 | client_id=client_id, |
86 | 88 | secret=secret, |
87 | | - tenant_id=tenant, |
| 89 | + tenant=tenant, |
88 | 90 | use_msi=false |
89 | 91 | ) |
90 | 92 | }}" |
|
122 | 124 | description: secret content string |
123 | 125 | """ |
124 | 126 |
|
| 127 | +from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMAuth |
125 | 128 | from ansible.errors import AnsibleError |
126 | 129 | from ansible.plugins.lookup import LookupBase |
127 | 130 | from ansible.utils.display import Display |
128 | 131 | try: |
129 | 132 | import logging |
130 | 133 | import requests |
131 | 134 | from azure.keyvault.secrets import SecretClient |
132 | | - from azure.identity import DefaultAzureCredential, ClientSecretCredential, AzureCliCredential |
133 | 135 | from azure.keyvault.secrets import SecretClient |
134 | 136 |
|
135 | 137 | except ImportError: |
|
142 | 144 | logger = logging.getLogger("azure.identity").setLevel(logging.ERROR) |
143 | 145 |
|
144 | 146 |
|
145 | | -def lookup_secret_non_msi(terms, vault_url, kwargs): |
146 | | - |
147 | | - client_id = kwargs['client_id'] if kwargs.get('client_id') else None |
148 | | - secret = kwargs['secret'] if kwargs.get('secret') else None |
149 | | - tenant_id = kwargs['tenant_id'] if kwargs.get('tenant_id') else None |
150 | | - |
151 | | - if all(v is not None for v in [client_id, secret, tenant_id]): |
152 | | - credential = ClientSecretCredential( |
153 | | - tenant_id=tenant_id, |
154 | | - client_id=client_id, |
155 | | - client_secret=secret, |
| 147 | +class LookupModule(LookupBase): |
| 148 | + def lookup_secret_non_msi(self, terms, vault_url): |
| 149 | + |
| 150 | + auth_source = 'auto' |
| 151 | + if self.get_option('use_cli'): |
| 152 | + auth_source = 'cli' |
| 153 | + auth_options = dict( |
| 154 | + auth_source=auth_source, |
| 155 | + client_id=self.get_option('client_id'), |
| 156 | + secret=self.get_option('secret'), |
| 157 | + tenant=self.get_option('tenant'), |
| 158 | + is_ad_resource=True |
156 | 159 | ) |
157 | | - else: |
158 | | - if kwargs.get('use_cli'): |
159 | | - credential = AzureCliCredential() |
160 | | - else: |
161 | | - credential = DefaultAzureCredential() |
162 | | - client = SecretClient(vault_url, credential) |
163 | 160 |
|
164 | | - ret = [] |
165 | | - for term in terms: |
166 | | - try: |
167 | | - secret_val = client.get_secret(term).value |
168 | | - ret.append(secret_val) |
169 | | - except Exception: |
170 | | - raise AnsibleError('Failed to fetch secret ' + term + ' from ' + vault_url + '.') |
171 | | - return ret |
| 161 | + azure_auth = AzureRMAuth(**auth_options) |
172 | 162 |
|
| 163 | + client = SecretClient(vault_url, azure_auth.azure_credential_track2) |
173 | 164 |
|
174 | | -class LookupModule(LookupBase): |
| 165 | + ret = [] |
| 166 | + for term in terms: |
| 167 | + try: |
| 168 | + secret_val = client.get_secret(term).value |
| 169 | + ret.append(secret_val) |
| 170 | + except Exception: |
| 171 | + raise AnsibleError('Failed to fetch secret ' + term + ' from ' + vault_url + '.') |
| 172 | + return ret |
175 | 173 |
|
176 | 174 | def run(self, terms, variables, **kwargs): |
| 175 | + |
| 176 | + self.set_options(direct=kwargs) |
| 177 | + |
177 | 178 | ret = [] |
178 | | - vault_url = kwargs.pop('vault_url', None) |
179 | | - use_msi = kwargs.pop('use_msi', True) |
| 179 | + vault_url = self.get_option('vault_url', None) |
| 180 | + use_msi = self.get_option('use_msi', True) |
180 | 181 | TOKEN_ACQUIRED = False |
181 | 182 | token = None |
182 | 183 |
|
183 | 184 | token_params = { |
184 | 185 | 'api-version': '2018-02-01', |
185 | | - 'resource': 'https://vault.{0}.net'.format(kwargs.get('cloud_type', 'azure')) |
| 186 | + 'resource': 'https://vault.{0}.net'.format(self.get_option('cloud_type', 'azure')) |
186 | 187 | } |
187 | 188 |
|
188 | 189 | token_headers = { |
@@ -221,4 +222,4 @@ def run(self, terms, variables, **kwargs): |
221 | 222 | raise AnsibleError('Failed to fetch secret ' + term + ' from ' + vault_url + ' via MSI endpoint.') |
222 | 223 | return ret |
223 | 224 | else: |
224 | | - return lookup_secret_non_msi(terms, vault_url, kwargs) |
| 225 | + return self.lookup_secret_non_msi(terms, vault_url) |
0 commit comments