-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Core] az logout
: Remove service principal access tokens from token cache
#29441
Conversation
️✔️AzureCLI-FullTest
|
Hi @jiasli, |
️✔️AzureCLI-BreakingChangeTest
|
Core |
# TODO: As MSAL provides no interface to get all service principals in its token cache, this method can't | ||
# clear all service principals' access tokens from MSAL token cache. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left a comment at AzureAD/microsoft-authentication-library-for-python#666 (comment).
Luckily, when az account clear
is called, it internally calls logout_all_users
which deletes the whole token cache file, bypassing this issue.
def logout_service_principal(self, client_id): | ||
# If client_id is a username, it is ignored | ||
|
||
# Step 1: Remove SP from MSAL token cache | ||
# Note that removing SP access tokens shouldn't rely on SP store | ||
cca = ConfidentialClientApplication(client_id, **self._msal_app_kwargs) | ||
cca.remove_tokens_for_client() | ||
|
||
# Step 2: Remove SP from SP store | ||
self._service_principal_store.remove_entry(client_id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I originally wrote the code as
def logout_service_principal(self, client_id):
try:
entry = self._service_principal_store.load_entry(client_id, self.tenant_id)
except CLIError as ex:
logger.info('%s %s is possibly a user account.', ex, client_id)
return
# Step 1: Remove service principal access tokens from MSAL token cache.
sp_auth = ServicePrincipalAuth(entry)
cred = ServicePrincipalCredential(sp_auth, **self._msal_app_kwargs)
cred.remove_tokens_for_client()
# Step 2: Remove service principal secrets
self._service_principal_store.remove_entry(client_id)
Then I realized removing an SP from token cache doesn't require client_credential
, so there is no need to read from self._service_principal_store
and create a fully-functioning ServicePrincipalCredential
.
There is one edge case caused by the "subscription ID as primary key" issue (#15005). If sp1 and sp2 have access to the same subscription, after running To solve that, the user must either
The same happens to user accounts too. |
Related command
az logout
Description
Based on AzureAD/microsoft-authentication-library-for-python#666 from MSAL 1.27.0 (adopted by #28556).
Testing Guide