|
45 | 45 | import java.io.IOException; |
46 | 46 | import java.net.HttpURLConnection; |
47 | 47 | import java.net.MalformedURLException; |
| 48 | + |
48 | 49 | import java.net.Proxy; |
49 | 50 | import java.net.Proxy.Type; |
50 | 51 | import java.net.URI; |
@@ -500,45 +501,63 @@ private Mono<AccessToken> getAccessTokenFromPowerShell(TokenRequestContext reque |
500 | 501 | throw LOGGER.logExceptionAsError(ex); |
501 | 502 | } |
502 | 503 | return Mono.defer(() -> { |
503 | | - String azAccountsCommand = "Import-Module Az.Accounts -MinimumVersion 2.2.0 -PassThru"; |
504 | | - return powershellManager.runCommand(azAccountsCommand).flatMap(output -> { |
505 | | - if (output.contains("The specified module 'Az.Accounts' with version '2.2.0' was not loaded " |
506 | | - + "because no valid module file")) { |
| 504 | + String sep = System.lineSeparator(); |
| 505 | + |
| 506 | + String command = "$ErrorActionPreference = 'Stop'" + sep |
| 507 | + + "[version]$minimumVersion = '2.2.0'" + sep |
| 508 | + + "" + sep |
| 509 | + + "$m = Import-Module Az.Accounts -MinimumVersion $minimumVersion -PassThru -ErrorAction SilentlyContinue" + sep |
| 510 | + + "" + sep |
| 511 | + + "if (! $m) {" + sep |
| 512 | + + " Write-Output 'VersionTooOld'" + sep |
| 513 | + + " exit" + sep |
| 514 | + + "}" + sep |
| 515 | + + "" + sep |
| 516 | + + "$useSecureString = $m.Version -ge [version]'2.17.0'" + sep |
| 517 | + + "" + sep |
| 518 | + + "$params = @{" + sep |
| 519 | + + " 'WarningAction'='Ignore'" + sep |
| 520 | + + " 'ResourceUrl'='" + scope + "'" + sep |
| 521 | + + "}" + sep |
| 522 | + + "" + sep |
| 523 | + + "if ($useSecureString) {" + sep |
| 524 | + + " $params['AsSecureString'] = $true" + sep |
| 525 | + + "}" + sep |
| 526 | + + "" + sep |
| 527 | + + "$token = Get-AzAccessToken @params" + sep |
| 528 | + + "$customToken = New-Object -TypeName psobject" + sep |
| 529 | + + "" + sep |
| 530 | + + "$customToken | Add-Member -MemberType NoteProperty -Name Token -Value ($useSecureString -eq $true ? (ConvertFrom-SecureString -AsPlainText $token.Token) : $token.Token)" + sep |
| 531 | + + "$customToken | Add-Member -MemberType NoteProperty -Name ExpiresOn -Value $token.ExpiresOn" + sep |
| 532 | + + "" + sep |
| 533 | + + "return $customToken | ConvertTo-Json"; |
| 534 | + return powershellManager.runCommand(command).flatMap(output -> { |
| 535 | + if (output.contains("VersionTooOld")) { |
507 | 536 | return Mono.error(LoggingUtil.logCredentialUnavailableException(LOGGER, options, |
508 | 537 | new CredentialUnavailableException("Az.Account module with version >= 2.2.0 is not installed. " |
509 | | - + "It needs to be installed to use Azure PowerShell " |
510 | | - + "Credential."))); |
| 538 | + + "It needs to be installed to use Azure PowerShell " |
| 539 | + + "Credential."))); |
511 | 540 | } |
512 | 541 |
|
513 | | - LOGGER.verbose("Az.accounts module was found installed."); |
514 | | - String command = "Get-AzAccessToken -ResourceUrl '" |
515 | | - + scope |
516 | | - + "' | ConvertTo-Json"; |
517 | | - LOGGER.verbose("Azure Powershell Authentication => Executing the command `{}` in Azure " |
518 | | - + "Powershell to retrieve the Access Token.", command); |
| 542 | + if (output.contains("Run Connect-AzAccount to login")) { |
| 543 | + return Mono.error(LoggingUtil.logCredentialUnavailableException(LOGGER, options, |
| 544 | + new CredentialUnavailableException( |
| 545 | + "Run Connect-AzAccount to login to Azure account in PowerShell."))); |
| 546 | + } |
519 | 547 |
|
520 | | - return powershellManager.runCommand(command).flatMap(out -> { |
521 | | - if (out.contains("Run Connect-AzAccount to login")) { |
522 | | - return Mono.error(LoggingUtil.logCredentialUnavailableException(LOGGER, options, |
523 | | - new CredentialUnavailableException( |
524 | | - "Run Connect-AzAccount to login to Azure account in PowerShell."))); |
525 | | - } |
526 | 548 |
|
527 | | - try { |
528 | | - LOGGER.verbose("Azure Powershell Authentication => Attempting to deserialize the " |
529 | | - + "received response from Azure Powershell."); |
530 | | - Map<String, String> objectMap = SERIALIZER_ADAPTER.deserialize(out, Map.class, |
531 | | - SerializerEncoding.JSON); |
532 | | - String accessToken = objectMap.get("Token"); |
533 | | - String time = objectMap.get("ExpiresOn"); |
534 | | - OffsetDateTime expiresOn = OffsetDateTime.parse(time).withOffsetSameInstant(ZoneOffset.UTC); |
535 | | - return Mono.just(new AccessToken(accessToken, expiresOn)); |
536 | | - } catch (IOException e) { |
537 | | - return Mono.error(LoggingUtil.logCredentialUnavailableException(LOGGER, options, |
538 | | - new CredentialUnavailableException( |
539 | | - "Encountered error when deserializing response from Azure Power Shell.", e))); |
540 | | - } |
541 | | - }); |
| 549 | + try { |
| 550 | + Map<String, String> objectMap = SERIALIZER_ADAPTER.deserialize(output, Map.class, |
| 551 | + SerializerEncoding.JSON); |
| 552 | + String accessToken = objectMap.get("Token"); |
| 553 | + String time = objectMap.get("ExpiresOn"); |
| 554 | + OffsetDateTime expiresOn = OffsetDateTime.parse(time).withOffsetSameInstant(ZoneOffset.UTC); |
| 555 | + return Mono.just(new AccessToken(accessToken, expiresOn)); |
| 556 | + } catch (IOException e) { |
| 557 | + return Mono.error(LoggingUtil.logCredentialUnavailableException(LOGGER, options, |
| 558 | + new CredentialUnavailableException( |
| 559 | + "Encountered error when deserializing response from Azure Power Shell.", e))); |
| 560 | + } |
542 | 561 | }); |
543 | 562 | }); |
544 | 563 | } |
|
0 commit comments