Skip to content

Commit

Permalink
Merge pull request #49 from salt-extensions/bug_keyvault
Browse files Browse the repository at this point in the history
Fix TypeError for ManagedIdentityCredential when using service principal credentials
  • Loading branch information
nicholasmhughes authored Mar 15, 2024
2 parents 49102d0 + 7b96fa6 commit c9150b5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog/46.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed TypeError for ManagedIdentityCredential when using service principal credentials.
17 changes: 13 additions & 4 deletions src/saltext/azurerm/utils/azurerm.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,19 @@ def _determine_auth(**kwargs):
)

try:
credentials = DefaultAzureCredential(authority=authority, **kwargs)
if "client_id" in kwargs and "tenant" in kwargs and "secret" in kwargs:
credentials = get_identity_credentials(**kwargs)
else:
kwargs.pop("client_id")
credentials = DefaultAzureCredential(authority=authority, **kwargs)
except ClientAuthenticationError:
raise SaltInvocationError( # pylint: disable=raise-missing-from
"Unable to determine credentials. "
"A subscription_id with username and password, "
"or client_id, secret, and tenant or a profile with the "
"required parameters populated"
)

try:
subscription_id = salt.utils.stringutils.to_str(kwargs["subscription_id"])
except KeyError:
Expand Down Expand Up @@ -174,7 +179,6 @@ def get_client(client_type, **kwargs):
base_url=cloud_env.endpoints.resource_manager,
user_agent_policy=user_agent,
)

return client


Expand Down Expand Up @@ -332,7 +336,6 @@ def get_identity_credentials(**kwargs):
for keyword, value in kwarg_map.items():
if kwargs.get(keyword):
os.environ[value] = kwargs[keyword]

try:
if kwargs.get("cloud_environment") and kwargs.get("cloud_environment").startswith("http"):
authority = kwargs["cloud_environment"]
Expand All @@ -341,10 +344,16 @@ def get_identity_credentials(**kwargs):
KnownAuthorities, kwargs.get("cloud_environment", "AZURE_PUBLIC_CLOUD")
)
log.debug("AUTHORITY: %s", authority)

except AttributeError as exc:
log.error('Unknown authority presented for "cloud_environment": %s', exc)
authority = KnownAuthorities.AZURE_PUBLIC_CLOUD

credential = DefaultAzureCredential(authority=authority)
try:
credential = DefaultAzureCredential(authority=authority)
except ClientAuthenticationError:
raise SaltInvocationError( # pylint: disable=raise-missing-from
"Unable to determine credentials. "
)

return credential

0 comments on commit c9150b5

Please sign in to comment.