Skip to content

Commit 8d089b2

Browse files
author
anamnavi
committed
set internal hook to check when registry may be unauthenticated and conditionally check for it
1 parent d53f564 commit 8d089b2

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

src/code/ContainerRegistryServerAPICalls.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,8 @@ internal string GetContainerRegistryAccessToken(bool needCatalogAccess, bool isP
409409
else
410410
{
411411
bool isRepositoryUnauthenticated = isPushOperation ? false : IsContainerRegistryUnauthenticated(Repository.Uri.ToString(), needCatalogAccess, out errRecord, out accessToken);
412+
InternalHooks.SetTestHook("IsRegistryUnauthenticatedCheckRequired", true);
413+
InternalHooks.SetTestHook("IsRegistryUnauthenticated", isRepositoryUnauthenticated);
412414
_cmdletPassedIn.WriteInformation($"Value of isRepositoryUnauthenticated: {isRepositoryUnauthenticated}", new string[] { "PSRGContainerRegistryUnauthenticatedCheck" });
413415

414416
_cmdletPassedIn.WriteDebug($"Is repository unauthenticated: {isRepositoryUnauthenticated}");

src/code/InternalHooks.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ public class InternalHooks
1717

1818
internal static string MARPrefix;
1919

20+
internal static bool IsRegistryUnauthenticatedCheckRequired;
21+
22+
internal static bool IsRegistryUnauthenticated;
23+
2024
public static void SetTestHook(string property, object value)
2125
{
2226
var fieldInfo = typeof(InternalHooks).GetField(property, BindingFlags.Static | BindingFlags.NonPublic);
@@ -27,5 +31,15 @@ public static string GetUserString()
2731
{
2832
return Microsoft.PowerShell.PSResourceGet.Cmdlets.UserAgentInfo.UserAgentString();
2933
}
34+
35+
public static bool? GetRegistryAuthenticationStatus()
36+
{
37+
if (IsRegistryUnauthenticatedCheckRequired)
38+
{
39+
return IsRegistryUnauthenticated;
40+
}
41+
42+
return null;
43+
}
3044
}
3145
}

test/PublishPSResourceTests/PublishPSResourceContainerRegistryServer.Tests.ps1

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,16 +351,23 @@ Describe "Test Publish-PSResource" -tags 'CI' {
351351
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module"
352352

353353
$infoRecord = & { Publish-PSResource -Path $script:PublishModuleBase -Repository $ACRRepoName 6>&1 } | Where-Object { $_ -is [System.Management.Automation.InformationRecord] }
354-
$infoRecord | Should -Not -BeNullOrEmpty
355-
356354

357355
$results = Find-PSResource -Name $script:PublishModuleName -Repository $ACRRepoName
358356
$results | Should -Not -BeNullOrEmpty
359357
$results[0].Name | Should -Be $script:PublishModuleName
360358
$results[0].Version | Should -Be $version
361359

362-
$infoRecord[0].Tags | Should -Be "PSRGContainerRegistryUnauthenticatedCheck"
363-
$infoRecord[0].MessageData | Should -Be "Value of isRepositoryUnauthenticated: False"
360+
$isRegistryUnauthenticatedCheckRequired = [Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::GetRegistryAuthenticationStatus()
361+
if ($isRegistryUnauthenticatedCheckRequired -ne $null)
362+
{
363+
$infoRecord | Should -Not -BeNullOrEmpty
364+
$infoRecord[0].Tags | Should -Be "PSRGContainerRegistryUnauthenticatedCheck"
365+
$infoRecord[0].MessageData | Should -Be "Value of isRepositoryUnauthenticated: $isRegistryUnauthenticatedCheckRequired"
366+
367+
# Reset the test hook after use
368+
[Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::SetTestHook("IsRegistryUnauthenticatedCheckRequired", $false)
369+
[Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::SetTestHook("IsRegistryUnauthenticated", $false)
370+
}
364371
}
365372

366373
It "Publish a script"{

0 commit comments

Comments
 (0)