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