Skip to content

Commit 55340f9

Browse files
Fix threading issues
1 parent a683bf7 commit 55340f9

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/code/PSResourceGetCredentialProvider.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public async Task<Credential> ResolveCredentialAsync(string hostname, Cancellati
5353
// Return cached credential if still valid
5454
if (!string.IsNullOrEmpty(_cachedCredential.RefreshToken) && DateTimeOffset.UtcNow < _tokenExpiry)
5555
{
56-
_cmdletPassedIn.WriteVerbose("Using cached ORAS credential.");
56+
Utils.WriteVerboseOnCmdlet(_cmdletPassedIn, "Using cached ORAS credential.");
5757
return _cachedCredential;
5858
}
5959

@@ -64,15 +64,15 @@ public async Task<Credential> ResolveCredentialAsync(string hostname, Cancellati
6464
if (repositoryCredentialInfo != null)
6565
{
6666
// Path 1: Credential from SecretsManagement vault
67-
_cmdletPassedIn.WriteVerbose("Retrieving access token from SecretManagement vault.");
67+
Utils.WriteVerboseOnCmdlet(_cmdletPassedIn, "Retrieving access token from SecretManagement vault.");
6868
aadAccessToken = Utils.GetContainerRegistryAccessTokenFromSecretManagement(
6969
_repository.Name,
7070
repositoryCredentialInfo,
7171
_cmdletPassedIn);
7272

7373
if (string.IsNullOrEmpty(aadAccessToken))
7474
{
75-
_cmdletPassedIn.WriteWarning("Failed to retrieve access token from SecretManagement vault.");
75+
Utils.WriteWarningOnCmdlet(_cmdletPassedIn, "Failed to retrieve access token from SecretManagement vault.");
7676
return new Credential();
7777
}
7878

@@ -81,28 +81,28 @@ public async Task<Credential> ResolveCredentialAsync(string hostname, Cancellati
8181
else
8282
{
8383
// Path 2: Azure Identity via existing Utils helper
84-
_cmdletPassedIn.WriteVerbose("Acquiring AAD access token via Utils.GetAzAccessToken.");
84+
Utils.WriteVerboseOnCmdlet(_cmdletPassedIn, "Acquiring AAD access token via Utils.GetAzAccessToken.");
8585
aadAccessToken = Utils.GetAzAccessToken(_cmdletPassedIn);
8686

8787
if (string.IsNullOrEmpty(aadAccessToken))
8888
{
8989
// If Azure Identity fails, return empty credential for anonymous access
90-
_cmdletPassedIn.WriteVerbose("No AAD token available; attempting anonymous access.");
90+
Utils.WriteVerboseOnCmdlet(_cmdletPassedIn, "No AAD token available; attempting anonymous access.");
9191
return new Credential();
9292
}
9393

9494
tenantId = null;
9595
}
9696

9797
// Exchange AAD access token for ACR refresh token via OAuth2 exchange endpoint
98-
_cmdletPassedIn.WriteVerbose("Exchanging AAD access token for ACR refresh token.");
98+
Utils.WriteVerboseOnCmdlet(_cmdletPassedIn, "Exchanging AAD access token for ACR refresh token.");
9999
try
100100
{
101101
string refreshToken = await ExchangeForAcrRefreshTokenAsync(aadAccessToken, tenantId, cancellationToken).ConfigureAwait(false);
102102

103103
if (string.IsNullOrEmpty(refreshToken))
104104
{
105-
_cmdletPassedIn.WriteWarning("Failed to obtain ACR refresh token from exchange.");
105+
Utils.WriteWarningOnCmdlet(_cmdletPassedIn, "Failed to obtain ACR refresh token from exchange.");
106106
return new Credential();
107107
}
108108

@@ -112,7 +112,7 @@ public async Task<Credential> ResolveCredentialAsync(string hostname, Cancellati
112112
}
113113
catch (Exception ex)
114114
{
115-
_cmdletPassedIn.WriteWarning($"Failed to exchange AAD token for ACR refresh token: {ex.Message}");
115+
Utils.WriteWarningOnCmdlet(_cmdletPassedIn, $"Failed to exchange AAD token for ACR refresh token: {ex.Message}");
116116
return new Credential();
117117
}
118118
}

src/code/Utils.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,22 @@ public static void WriteVerboseOnCmdlet(
14591459
catch { }
14601460
}
14611461

1462+
public static void WriteWarningOnCmdlet(
1463+
PSCmdlet cmdlet,
1464+
string message)
1465+
{
1466+
try
1467+
{
1468+
cmdlet.InvokeCommand.InvokeScript(
1469+
script: $"param ([string] $message) Write-Warning -Message $message",
1470+
useNewScope: true,
1471+
writeToPipeline: System.Management.Automation.Runspaces.PipelineResultTypes.None,
1472+
input: null,
1473+
args: new object[] { message });
1474+
}
1475+
catch { }
1476+
}
1477+
14621478
/// <summary>
14631479
/// Convert a json string into a hashtable object.
14641480
/// This uses custom script to perform the PSObject -> Hashtable

0 commit comments

Comments
 (0)