Skip to content

Commit 5da84fa

Browse files
authored
For Publish operation to ACR, non-anonymous access token should be retrieved (#1918)
1 parent 06b4384 commit 5da84fa

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

src/code/ContainerRegistryServerAPICalls.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ private Stream InstallVersion(
332332
return null;
333333
}
334334

335-
string containerRegistryAccessToken = GetContainerRegistryAccessToken(needCatalogAccess: false, out errRecord);
335+
string containerRegistryAccessToken = GetContainerRegistryAccessToken(needCatalogAccess: false, isPushOperation: false, out errRecord);
336336
if (errRecord != null)
337337
{
338338
return null;
@@ -380,7 +380,7 @@ private Stream InstallVersion(
380380
/// If no credential provided at registration then, check if the ACR endpoint can be accessed without a token. If not, try using Azure.Identity to get the az access token, then ACR refresh token and then ACR access token.
381381
/// Note: Access token can be empty if the repository is unauthenticated
382382
/// </summary>
383-
internal string GetContainerRegistryAccessToken(bool needCatalogAccess, out ErrorRecord errRecord)
383+
internal string GetContainerRegistryAccessToken(bool needCatalogAccess, bool isPushOperation, out ErrorRecord errRecord)
384384
{
385385
_cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::GetContainerRegistryAccessToken()");
386386
string accessToken = string.Empty;
@@ -408,7 +408,10 @@ internal string GetContainerRegistryAccessToken(bool needCatalogAccess, out Erro
408408
}
409409
else
410410
{
411-
bool isRepositoryUnauthenticated = IsContainerRegistryUnauthenticated(Repository.Uri.ToString(), needCatalogAccess, out errRecord, out accessToken);
411+
// A container registry repository is determined to be unauthenticated if it allows anonymous pull access. However, push operations always require authentication.
412+
bool isRepositoryUnauthenticated = isPushOperation ? false : IsContainerRegistryUnauthenticated(Repository.Uri.ToString(), needCatalogAccess, out errRecord, out accessToken);
413+
_cmdletPassedIn.WriteInformation($"Value of isRepositoryUnauthenticated: {isRepositoryUnauthenticated}", new string[] { "PSRGContainerRegistryUnauthenticatedCheck" });
414+
412415
_cmdletPassedIn.WriteDebug($"Is repository unauthenticated: {isRepositoryUnauthenticated}");
413416

414417
if (errRecord != null)
@@ -1330,7 +1333,7 @@ internal bool PushNupkgContainerRegistry(
13301333

13311334
// Get access token (includes refresh tokens)
13321335
_cmdletPassedIn.WriteVerbose($"Get access token for container registry server.");
1333-
var containerRegistryAccessToken = GetContainerRegistryAccessToken(needCatalogAccess: false, out errRecord);
1336+
var containerRegistryAccessToken = GetContainerRegistryAccessToken(needCatalogAccess: false, isPushOperation: true, out errRecord);
13341337
if (errRecord != null)
13351338
{
13361339
return false;
@@ -1795,7 +1798,7 @@ private Hashtable[] FindPackagesWithVersionHelper(string packageName, VersionTyp
17951798
string packageNameLowercase = packageName.ToLower();
17961799

17971800
string packageNameForFind = PrependMARPrefix(packageNameLowercase);
1798-
string containerRegistryAccessToken = GetContainerRegistryAccessToken(needCatalogAccess: false, out errRecord);
1801+
string containerRegistryAccessToken = GetContainerRegistryAccessToken(needCatalogAccess: false, isPushOperation: false,out errRecord);
17991802
if (errRecord != null)
18001803
{
18011804
return emptyHashResponses;
@@ -1907,7 +1910,7 @@ private FindResults FindPackages(string packageName, bool includePrerelease, out
19071910
{
19081911
_cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::FindPackages()");
19091912
errRecord = null;
1910-
string containerRegistryAccessToken = GetContainerRegistryAccessToken(needCatalogAccess: true, out errRecord);
1913+
string containerRegistryAccessToken = GetContainerRegistryAccessToken(needCatalogAccess: true, isPushOperation: false, out errRecord);
19111914
if (errRecord != null)
19121915
{
19131916
return emptyResponseResults;

test/PublishPSResourceTests/PublishPSResourceContainerRegistryServer.Tests.ps1

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,25 @@ Describe "Test Publish-PSResource" -tags 'CI' {
346346
$results[0].Version | Should -Be $correctVersion
347347
}
348348

349+
It "Publish a package should always require authentication" {
350+
$version = "15.0.0"
351+
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module"
352+
353+
Publish-PSResource -Path $script:PublishModuleBase -Repository $ACRRepoName -InformationVariable RegistryUnauthenticated
354+
355+
$results = Find-PSResource -Name $script:PublishModuleName -Repository $ACRRepoName
356+
$results | Should -Not -BeNullOrEmpty
357+
$results[0].Name | Should -Be $script:PublishModuleName
358+
$results[0].Version | Should -Be $version
359+
360+
if ($usingAzAuth)
361+
{
362+
$RegistryUnauthenticated | Should -Not -BeNullOrEmpty
363+
$RegistryUnauthenticated[0].Tags | Should -Be "PSRGContainerRegistryUnauthenticatedCheck"
364+
$RegistryUnauthenticated[0].MessageData | Should -Be "Value of isRepositoryUnauthenticated: False"
365+
}
366+
}
367+
349368
It "Publish a script"{
350369
$scriptVersion = "1.0.0"
351370
$params = @{

0 commit comments

Comments
 (0)