Skip to content

Commit

Permalink
Added option for disabling MSI autodiscover feature in azure_keyvault…
Browse files Browse the repository at this point in the history
…_secret lookup plugin (#1353)

* Added option for disabling MSI autodiscover feature

* sorting out some linting things and removed msrest module requirement from documentation

* added missing period in documentation
  • Loading branch information
nalle authored Dec 6, 2023
1 parent a867b33 commit 498bb04
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions plugins/lookup/azure_keyvault_secret.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
description: Secret of the service principal.
tenant_id:
description: Tenant id of service principal.
use_msi:
description: MSI token autodiscover, default is true.
notes:
- If version is not provided, this plugin will return the latest version of the secret.
- If ansible is running on Azure Virtual Machine with MSI enabled, client_id, secret and tenant isn't required.
Expand Down Expand Up @@ -74,7 +76,8 @@
vault_url=url,
client_id=client_id,
secret=secret,
tenant_id=tenant
tenant_id=tenant,
use_msi=false
)
}}"
Expand Down Expand Up @@ -139,22 +142,6 @@
'Metadata': 'true'
}

token = None

try:
token_res = requests.get('http://169.254.169.254/metadata/identity/oauth2/token', params=token_params, headers=token_headers, timeout=(3.05, 27))

This comment has been minimized.

Copy link
@fredericrenan

fredericrenan Jun 17, 2024

It took me a long time to realize that the extra 27 seconds in my tasks came from this timeout.
Why perform this test when loading the plugin when it cannot take into account the use_msi=false parameter ?
=> This happens for each task in a task file, even if the task does not use the plugin.

if token_res.ok:
token = token_res.json().get("access_token")
if token is not None:
TOKEN_ACQUIRED = True
else:
display.v('Successfully called MSI endpoint, but no token was available. Will use service principal if provided.')
else:
display.v("Unable to query MSI endpoint, Error Code %s. Will use service principal if provided" % token_res.status_code)
except Exception:
display.v('Unable to fetch MSI token. Will use service principal if provided.')
TOKEN_ACQUIRED = False


def lookup_secret_non_msi(terms, vault_url, kwargs):

Expand Down Expand Up @@ -187,6 +174,27 @@ class LookupModule(LookupBase):
def run(self, terms, variables, **kwargs):
ret = []
vault_url = kwargs.pop('vault_url', None)
use_msi = kwargs.pop('use_msi', True)
TOKEN_ACQUIRED = False
token = None

if use_msi:
try:
token_res = requests.get('http://169.254.169.254/metadata/identity/oauth2/token',
params=token_params,
headers=token_headers,
timeout=(3.05, 27))
if token_res.ok:
token = token_res.json().get("access_token")
if token is not None:
TOKEN_ACQUIRED = True
else:
display.v('Successfully called MSI endpoint, but no token was available. Will use service principal if provided.')
else:
display.v("Unable to query MSI endpoint, Error Code %s. Will use service principal if provided" % token_res.status_code)
except Exception:
display.v('Unable to fetch MSI token. Will use service principal if provided.')

if vault_url is None:
raise AnsibleError('Failed to get valid vault url.')
if TOKEN_ACQUIRED:
Expand Down

0 comments on commit 498bb04

Please sign in to comment.