Skip to content

Commit ed32181

Browse files
Add timeout for GetTokenAsync
1 parent dabfd95 commit ed32181

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

src/code/ContainerRegistryServerAPICalls.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,10 @@ internal string GetContainerRegistryAccessToken(out ErrorRecord errRecord)
394394
else
395395
{
396396
bool isRepositoryUnauthenticated = IsContainerRegistryUnauthenticated(Repository.Uri.ToString(), out errRecord, out accessToken);
397+
_cmdletPassedIn.WriteDebug($"Is repository unauthenticated: {isRepositoryUnauthenticated}");
398+
_cmdletPassedIn.WriteDebug($"Access token: {accessToken}");
399+
_cmdletPassedIn.WriteDebug($"Error Record: {errRecord}");
400+
397401
if (errRecord != null)
398402
{
399403
return null;
@@ -407,7 +411,7 @@ internal string GetContainerRegistryAccessToken(out ErrorRecord errRecord)
407411

408412
if (!isRepositoryUnauthenticated)
409413
{
410-
accessToken = Utils.GetAzAccessToken();
414+
accessToken = Utils.GetAzAccessToken(_cmdletPassedIn);
411415
if (string.IsNullOrEmpty(accessToken))
412416
{
413417
errRecord = new ErrorRecord(
@@ -488,6 +492,8 @@ internal bool IsContainerRegistryUnauthenticated(string containerRegistyUrl, out
488492
// get the anonymous access token
489493
var url = $"{realm}?service={service}{defaultScope}";
490494

495+
_cmdletPassedIn.WriteDebug($"Getting anonymous access token from the realm: {url}");
496+
491497
// we dont check the errorrecord here because we want to return false if we get a 401 and not throw an error
492498
var results = GetHttpResponseJObjectUsingContentHeaders(url, HttpMethod.Get, content, contentHeaders, out _);
493499

src/code/Utils.cs

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -650,25 +650,44 @@ public static PSCredential GetRepositoryCredentialFromSecretManagement(
650650
}
651651
}
652652

653-
public static string GetAzAccessToken()
653+
public static string GetAzAccessToken(PSCmdlet cmdletPassedIn)
654654
{
655+
cmdletPassedIn.WriteVerbose("Getting Azure access token using DefaultAzureCredential");
656+
655657
var credOptions = new DefaultAzureCredentialOptions
656658
{
657-
ExcludeEnvironmentCredential = true,
658-
ExcludeVisualStudioCodeCredential = true,
659-
ExcludeVisualStudioCredential = true,
660-
ExcludeWorkloadIdentityCredential = true,
661-
ExcludeManagedIdentityCredential = true, // ManagedIdentityCredential makes the experience slow
662-
ExcludeSharedTokenCacheCredential = true, // SharedTokenCacheCredential is not supported on macOS
663-
ExcludeAzureCliCredential = false,
664-
ExcludeAzurePowerShellCredential = false,
665-
ExcludeInteractiveBrowserCredential = false
659+
ExcludeEnvironmentCredential = true,
660+
ExcludeVisualStudioCodeCredential = true,
661+
ExcludeVisualStudioCredential = true,
662+
ExcludeWorkloadIdentityCredential = true,
663+
ExcludeManagedIdentityCredential = true, // ManagedIdentityCredential makes the experience slow
664+
ExcludeSharedTokenCacheCredential = true, // SharedTokenCacheCredential is not supported on macOS
665+
ExcludeAzureCliCredential = false,
666+
ExcludeAzurePowerShellCredential = false,
667+
ExcludeInteractiveBrowserCredential = false
666668
};
667669

668670
var dCred = new DefaultAzureCredential(credOptions);
669671
var tokenRequestContext = new TokenRequestContext(new string[] { "https://management.azure.com/.default" });
670-
var token = dCred.GetTokenAsync(tokenRequestContext).Result;
671-
return token.Token;
672+
673+
try
674+
{
675+
using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30)))
676+
{
677+
var token = dCred.GetTokenAsync(tokenRequestContext, cts.Token).GetAwaiter().GetResult();
678+
return token.Token;
679+
}
680+
}
681+
catch (OperationCanceledException)
682+
{
683+
cmdletPassedIn.WriteWarning("Timeout occurred while acquiring Azure access token.");
684+
throw;
685+
}
686+
catch (Exception ex)
687+
{
688+
cmdletPassedIn.WriteWarning($"Failed to acquire Azure access token: {ex.Message}");
689+
throw;
690+
}
672691
}
673692

674693
public static string GetContainerRegistryAccessTokenFromSecretManagement(

0 commit comments

Comments
 (0)