@@ -47,6 +47,10 @@ internal class ContainerRegistryServerAPICalls : ServerApiCall
47
47
const string containerRegistryStartUploadTemplate = "https://{0}/v2/{1}/blobs/uploads/" ; // 0 - registry, 1 - packagename
48
48
const string containerRegistryEndUploadTemplate = "https://{0}{1}&digest=sha256:{2}" ; // 0 - registry, 1 - location, 2 - digest
49
49
const string defaultScope = "&scope=repository:*:*&scope=registry:catalog:*" ;
50
+ const string catalogScope = "&scope=registry:catalog:*" ;
51
+ const string grantTypeTemplate = "grant_type=access_token&service={0}{1}" ; // 0 - registry, 1 - scope
52
+ const string authUrlTemplate = "{0}?service={1}{2}" ; // 0 - realm, 1 - service, 2 - scope
53
+
50
54
const string containerRegistryRepositoryListTemplate = "https://{0}/v2/_catalog" ; // 0 - registry
51
55
52
56
#endregion
@@ -323,7 +327,7 @@ private Stream InstallVersion(
323
327
return null ;
324
328
}
325
329
326
- string containerRegistryAccessToken = GetContainerRegistryAccessToken ( out errRecord ) ;
330
+ string containerRegistryAccessToken = GetContainerRegistryAccessToken ( needCatalogAccess : false , out errRecord ) ;
327
331
if ( errRecord != null )
328
332
{
329
333
return null ;
@@ -371,7 +375,7 @@ private Stream InstallVersion(
371
375
/// 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.
372
376
/// Note: Access token can be empty if the repository is unauthenticated
373
377
/// </summary>
374
- internal string GetContainerRegistryAccessToken ( out ErrorRecord errRecord )
378
+ internal string GetContainerRegistryAccessToken ( bool needCatalogAccess , out ErrorRecord errRecord )
375
379
{
376
380
_cmdletPassedIn . WriteDebug ( "In ContainerRegistryServerAPICalls::GetContainerRegistryAccessToken()" ) ;
377
381
string accessToken = string . Empty ;
@@ -393,7 +397,7 @@ internal string GetContainerRegistryAccessToken(out ErrorRecord errRecord)
393
397
}
394
398
else
395
399
{
396
- bool isRepositoryUnauthenticated = IsContainerRegistryUnauthenticated ( Repository . Uri . ToString ( ) , out errRecord , out accessToken ) ;
400
+ bool isRepositoryUnauthenticated = IsContainerRegistryUnauthenticated ( Repository . Uri . ToString ( ) , needCatalogAccess , out errRecord , out accessToken ) ;
397
401
_cmdletPassedIn . WriteDebug ( $ "Is repository unauthenticated: { isRepositoryUnauthenticated } ") ;
398
402
399
403
if ( errRecord != null )
@@ -446,7 +450,7 @@ internal string GetContainerRegistryAccessToken(out ErrorRecord errRecord)
446
450
/// <summary>
447
451
/// Checks if container registry repository is unauthenticated.
448
452
/// </summary>
449
- internal bool IsContainerRegistryUnauthenticated ( string containerRegistyUrl , out ErrorRecord errRecord , out string anonymousAccessToken )
453
+ internal bool IsContainerRegistryUnauthenticated ( string containerRegistyUrl , bool needCatalogAccess , out ErrorRecord errRecord , out string anonymousAccessToken )
450
454
{
451
455
_cmdletPassedIn . WriteDebug ( "In ContainerRegistryServerAPICalls::IsContainerRegistryUnauthenticated()" ) ;
452
456
errRecord = null ;
@@ -484,11 +488,11 @@ internal bool IsContainerRegistryUnauthenticated(string containerRegistyUrl, out
484
488
return false ;
485
489
}
486
490
487
- string content = "grant_type=access_token&service=" + service + defaultScope ;
491
+ string content = needCatalogAccess ? String . Format ( grantTypeTemplate , service , catalogScope ) : String . Format ( grantTypeTemplate , service , defaultScope ) ;
492
+
488
493
var contentHeaders = new Collection < KeyValuePair < string , string > > { new KeyValuePair < string , string > ( "Content-Type" , "application/x-www-form-urlencoded" ) } ;
489
494
490
- // get the anonymous access token
491
- var url = $ "{ realm } ?service={ service } { defaultScope } ";
495
+ string url = needCatalogAccess ? String . Format ( authUrlTemplate , realm , service , catalogScope ) : String . Format ( authUrlTemplate , realm , service , defaultScope ) ;
492
496
493
497
_cmdletPassedIn . WriteDebug ( $ "Getting anonymous access token from the realm: { url } ") ;
494
498
@@ -508,6 +512,7 @@ internal bool IsContainerRegistryUnauthenticated(string containerRegistyUrl, out
508
512
}
509
513
510
514
anonymousAccessToken = results [ "access_token" ] . ToString ( ) ;
515
+
511
516
_cmdletPassedIn . WriteDebug ( "Anonymous access token retrieved" ) ;
512
517
return true ;
513
518
}
@@ -1234,7 +1239,7 @@ internal bool PushNupkgContainerRegistry(
1234
1239
1235
1240
// Get access token (includes refresh tokens)
1236
1241
_cmdletPassedIn . WriteVerbose ( $ "Get access token for container registry server.") ;
1237
- var containerRegistryAccessToken = GetContainerRegistryAccessToken ( out errRecord ) ;
1242
+ var containerRegistryAccessToken = GetContainerRegistryAccessToken ( needCatalogAccess : false , out errRecord ) ;
1238
1243
if ( errRecord != null )
1239
1244
{
1240
1245
return false ;
@@ -1699,7 +1704,7 @@ private Hashtable[] FindPackagesWithVersionHelper(string packageName, VersionTyp
1699
1704
string packageNameLowercase = packageName . ToLower ( ) ;
1700
1705
1701
1706
string packageNameForFind = PrependMARPrefix ( packageNameLowercase ) ;
1702
- string containerRegistryAccessToken = GetContainerRegistryAccessToken ( out errRecord ) ;
1707
+ string containerRegistryAccessToken = GetContainerRegistryAccessToken ( needCatalogAccess : false , out errRecord ) ;
1703
1708
if ( errRecord != null )
1704
1709
{
1705
1710
return emptyHashResponses ;
@@ -1808,7 +1813,7 @@ private FindResults FindPackages(string packageName, bool includePrerelease, out
1808
1813
{
1809
1814
_cmdletPassedIn . WriteDebug ( "In ContainerRegistryServerAPICalls::FindPackages()" ) ;
1810
1815
errRecord = null ;
1811
- string containerRegistryAccessToken = GetContainerRegistryAccessToken ( out errRecord ) ;
1816
+ string containerRegistryAccessToken = GetContainerRegistryAccessToken ( needCatalogAccess : true , out errRecord ) ;
1812
1817
if ( errRecord != null )
1813
1818
{
1814
1819
return emptyResponseResults ;
0 commit comments