Skip to content

Commit

Permalink
Hot fix for azure cli authentication in cloud shell
Browse files Browse the repository at this point in the history
  • Loading branch information
Flanker32 committed Nov 24, 2021
1 parent ad5e33e commit 5e74f23
Showing 1 changed file with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import com.azure.core.credential.TokenRequestContext;
import com.azure.core.management.AzureEnvironment;
import com.azure.identity.implementation.util.ScopeUtil;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.microsoft.azure.toolkit.lib.auth.TokenCredentialManagerWithCache;
import com.microsoft.azure.toolkit.lib.auth.exception.AzureToolkitAuthenticationException;
import com.microsoft.azure.toolkit.lib.auth.util.AzureCliUtils;
import com.microsoft.azure.toolkit.lib.common.utils.JsonUtils;
Expand All @@ -23,8 +25,7 @@
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

import com.microsoft.azure.toolkit.lib.auth.TokenCredentialManagerWithCache;
import java.util.Optional;

class AzureCliTokenCredentialManager extends TokenCredentialManagerWithCache {
public AzureCliTokenCredentialManager(AzureEnvironment env) {
Expand Down Expand Up @@ -56,13 +57,14 @@ public Mono<AccessToken> getToken(TokenRequestContext request) {
// copied from https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/identity/azure-identity
// /src/main/java/com/azure/identity/implementation/IdentityClient.java#L487
String accessToken = result.get("accessToken").getAsString();
String time = result.get("expiresOn").getAsString();
String timeToSecond = time.substring(0, time.indexOf("."));
String timeJoinedWithT = String.join("T", timeToSecond.split(" "));
OffsetDateTime expiresOn = LocalDateTime.parse(timeJoinedWithT, DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.atZone(ZoneId.systemDefault())
.toOffsetDateTime().withOffsetSameInstant(ZoneOffset.UTC);
return Mono.just(new AccessToken(accessToken, expiresOn));
final OffsetDateTime expiresDateTime = Optional.ofNullable(result.get("expiresOn"))
.filter(jsonElement -> !jsonElement.isJsonNull())
.map(JsonElement::getAsString)
.map(value -> value.substring(0, value.indexOf(".")))
.map(value -> String.join("T", value.split(" "))).map(value -> LocalDateTime.parse(value, DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.atZone(ZoneId.systemDefault()).toOffsetDateTime().withOffsetSameInstant(ZoneOffset.UTC))
.orElse(OffsetDateTime.MAX);
return Mono.just(new AccessToken(accessToken, expiresDateTime));
}

boolean isInCloudShell() {
Expand Down

0 comments on commit 5e74f23

Please sign in to comment.