azure-powershell icon indicating copy to clipboard operation
azure-powershell copied to clipboard

Import-AzureRMContext does not show any subscription

Open juancrl opened this issue 8 years ago • 4 comments

Description

When importing credentials from JSON file, I cannot enumerate my subscriptions

The script uses multiple Contexts/subscriptions, but in this version only one is active. (Logon is performed again when needed). Multiple contexts are saved in JSON files. Since AzureRM 6.0 it worked perfectly, but a recent change (probably 6.7.0) broke it.

Module Version

6.7.0

Script/Steps for Reproduction

Minimum repro code, as suggested by @markcowl :

  Clear-AzureRMContext
  Disable-AzureRMContextAutosave -Scope CurrentUser 
  $File = "C:\Bats\logon\jc_arm.json"
  Import-AzureRMContext -Path $File -verbose
  Get-AzureRMSubscription          #  -WarningAction SilentlyContinue

Prior to the change, I got some warnings related to old test directories, but also the two subscriptions existing, which I can select from. Now I'm getting just the warnings (and no subscriptions)

WARNING: Unable to acquire token for tenant 'a856b482-8519-4324-96aa-93aad8b23b6a' ... WARNING: Unable to acquire token for tenant 'd626b9b9-92ab-45dc-a2d4-9016dfbcc96b'

Test 1 : Removing the JSON file, creating it again (Connect-AzureRM +Save-AzureRMContext), then Get-AzureRMSubscription, I can see my 2 subscriptions. As I'm reading my own words, I believe it's just an interactive logon, the JSON file is not read in this case.

Debug Output

Test 2. Running the repro code with Get-AzureRMSubscription -Debug. During the Token Acquisition phase, Interesting ErrorCode: user_interaction_required

DEBUG: Microsoft.IdentityModel.Clients.ActiveDirectory Verbose: 1 : DEBUG: 08/21/2018 07:57:36: 733cfda7-05cf-4265-a806-d1936eb2819e - TokenCache: Looking up cache for a token... DEBUG: Microsoft.IdentityModel.Clients.ActiveDirectory Information: 2 : DEBUG: 08/21/2018 07:57:36: 733cfda7-05cf-4265-a806-d1936eb2819e - TokenCache: No matching token was found in the cache DEBUG: Microsoft.IdentityModel.Clients.ActiveDirectory Verbose: 1 : DEBUG: 08/21/2018 07:57:36: - WindowsFormsWebAuthenticationDialogBase: Navigating to 'https://login.microsoftonline.com/a856b482-8519-4324-96aa-93aad8b23b6a/oauth2/authorize?resource=https://management.core.windows.net/&client_id=1950a258-227b-4e31-a9cf-717495945fc2&response_type=code&haschro me=1&redirect_uri=urn:ietf:wg:oauth:2.0:oob&[email protected]&client-request-id=733cfda7-05cf-4265-a806-d1936eb2819e&prompt=attempt_none&x-client-SKU=.NET&x-client-Ver=2.28.3.860&x-cl ient-CPU=x64&x-client-OS=Microsoft Windows NT 10.0.17134.0&site_id=501358&display=popup'. DEBUG: Microsoft.IdentityModel.Clients.ActiveDirectory Verbose: 1 : DEBUG: 08/21/2018 07:57:37: - WindowsFormsWebAuthenticationDialogBase: Navigating to 'https://login.microsoftonline.com/a856b482-8519-4324-96aa-93aad8b23b6a/oauth2/authorize?resource=https://management.core.windows.net/&client_id=1950a258-227b-4e31-a9cf-717495945fc2&response_type=code&haschro me=1&redirect_uri=urn:ietf:wg:oauth:2.0:oob&[email protected]&client-request-id=733cfda7-05cf-4265-a806-d1936eb2819e&prompt=attempt_none&x-client-SKU=.NET&x-client-Ver=2.28.3.860&x-cl ient-CPU=x64&x-client-OS=Microsoft Windows NT 10.0.17134.0&site_id=501358&display=popup&sso_nonce=AQABAAAAAADXzZ3ifr-GRbDT45zNSEFEI_eE0-_e2EO9SxZvI2SUHHyCfGqa1VfY6-ZOfm9TK8YcoesE94JoPuyfCAcmeFJgraZH-74D-mrmN_vJWRUCbCAA&mscrid=733cfda7-05cf-4265-a806-d193 6eb2819e'. DEBUG: Microsoft.IdentityModel.Clients.ActiveDirectory Error: 4 : DEBUG: 08/21/2018 07:57:39: 733cfda7-05cf-4265-a806-d1936eb2819e - d__0: Microsoft.IdentityModel.Clients.ActiveDirectory.AdalException: user_interaction_required: One of two conditions was encountered: 1. The PromptBehavior.Never flag was passed, but the constraint could not be honored, because user interaction was required. 2. An error occurred during a silent web authentication that prevented the http authentication flow from completing in a short enough time frame at Microsoft.IdentityModel.Clients.ActiveDirectory.AcquireTokenInteractiveHandler.VerifyAuthorizationResult() at Microsoft.IdentityModel.Clients.ActiveDirectory.AcquireTokenInteractiveHandler.PreTokenRequest() at Microsoft.IdentityModel.Clients.ActiveDirectory.AcquireTokenHandlerBase.d__0.MoveNext() ErrorCode: user_interaction_required WARNING: Unable to acquire token for tenant 'a856b482-8519-4324-96aa-93aad8b23b6a'

juancrl avatar Aug 21 '18 07:08 juancrl

Hi Maddie, Please check https://github.com/Azure/azure-powershell/issues/7110, I've found a workaround which seems to work with 6.8.1. Now it wold be great to find a more logical/evident way to fix this issue... and probably document which .NET object can be used to see the cache again (as the previously used method now always returns $null) jc

juancrl avatar Sep 07 '18 21:09 juancrl

$cache= [Microsoft.IdentityModel.Clients.ActiveDirectory.TokenCache]::DefaultShared $cache.ReadItems()

Now empty in 6.7.x and 6.8.x

juancrl avatar Sep 07 '18 21:09 juancrl

@juancrl TokenCache.DefautShared is not the place that we look by default for tokens, we use [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.TokenCache

However, this is not public-facing PowerShell API, and so, not recommended as a mechanism for getting data, as the specifics may change in the future.

markcowl avatar Oct 03 '18 18:10 markcowl

Thanks ! I was now using (Get-AzureRMContext).TokenCache which probably points to the same place...

juancrl avatar Oct 03 '18 18:10 juancrl