Skip to content

Commit fa5435b

Browse files
Request for catalog listing access for finding packages
1 parent 3c437c4 commit fa5435b

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/code/ContainerRegistryServerAPICalls.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ internal class ContainerRegistryServerAPICalls : ServerApiCall
4747
const string containerRegistryStartUploadTemplate = "https://{0}/v2/{1}/blobs/uploads/"; // 0 - registry, 1 - packagename
4848
const string containerRegistryEndUploadTemplate = "https://{0}{1}&digest=sha256:{2}"; // 0 - registry, 1 - location, 2 - digest
4949
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+
5054
const string containerRegistryRepositoryListTemplate = "https://{0}/v2/_catalog"; // 0 - registry
5155

5256
#endregion
@@ -323,7 +327,7 @@ private Stream InstallVersion(
323327
return null;
324328
}
325329

326-
string containerRegistryAccessToken = GetContainerRegistryAccessToken(out errRecord);
330+
string containerRegistryAccessToken = GetContainerRegistryAccessToken(needCatalogAccess: false, out errRecord);
327331
if (errRecord != null)
328332
{
329333
return null;
@@ -371,7 +375,7 @@ private Stream InstallVersion(
371375
/// 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.
372376
/// Note: Access token can be empty if the repository is unauthenticated
373377
/// </summary>
374-
internal string GetContainerRegistryAccessToken(out ErrorRecord errRecord)
378+
internal string GetContainerRegistryAccessToken(bool needCatalogAccess, out ErrorRecord errRecord)
375379
{
376380
_cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::GetContainerRegistryAccessToken()");
377381
string accessToken = string.Empty;
@@ -393,7 +397,7 @@ internal string GetContainerRegistryAccessToken(out ErrorRecord errRecord)
393397
}
394398
else
395399
{
396-
bool isRepositoryUnauthenticated = IsContainerRegistryUnauthenticated(Repository.Uri.ToString(), out errRecord, out accessToken);
400+
bool isRepositoryUnauthenticated = IsContainerRegistryUnauthenticated(Repository.Uri.ToString(), needCatalogAccess, out errRecord, out accessToken);
397401
if (errRecord != null)
398402
{
399403
return null;
@@ -444,7 +448,7 @@ internal string GetContainerRegistryAccessToken(out ErrorRecord errRecord)
444448
/// <summary>
445449
/// Checks if container registry repository is unauthenticated.
446450
/// </summary>
447-
internal bool IsContainerRegistryUnauthenticated(string containerRegistyUrl, out ErrorRecord errRecord, out string anonymousAccessToken)
451+
internal bool IsContainerRegistryUnauthenticated(string containerRegistyUrl, bool needCatalogAccess, out ErrorRecord errRecord, out string anonymousAccessToken)
448452
{
449453
_cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::IsContainerRegistryUnauthenticated()");
450454
errRecord = null;
@@ -482,11 +486,11 @@ internal bool IsContainerRegistryUnauthenticated(string containerRegistyUrl, out
482486
return false;
483487
}
484488

485-
string content = "grant_type=access_token&service=" + service + defaultScope;
489+
string content = needCatalogAccess ? String.Format(grantTypeTemplate, service, catalogScope) : String.Format(grantTypeTemplate, service, defaultScope);
490+
486491
var contentHeaders = new Collection<KeyValuePair<string, string>> { new KeyValuePair<string, string>("Content-Type", "application/x-www-form-urlencoded") };
487492

488-
// get the anonymous access token
489-
var url = $"{realm}?service={service}{defaultScope}";
493+
string url = needCatalogAccess ? String.Format(authUrlTemplate, realm, service, catalogScope) : String.Format(authUrlTemplate, realm, service, defaultScope);
490494

491495
// we dont check the errorrecord here because we want to return false if we get a 401 and not throw an error
492496
var results = GetHttpResponseJObjectUsingContentHeaders(url, HttpMethod.Get, content, contentHeaders, out _);
@@ -504,6 +508,7 @@ internal bool IsContainerRegistryUnauthenticated(string containerRegistyUrl, out
504508
}
505509

506510
anonymousAccessToken = results["access_token"].ToString();
511+
507512
_cmdletPassedIn.WriteDebug("Anonymous access token retrieved");
508513
return true;
509514
}
@@ -1230,7 +1235,7 @@ internal bool PushNupkgContainerRegistry(
12301235

12311236
// Get access token (includes refresh tokens)
12321237
_cmdletPassedIn.WriteVerbose($"Get access token for container registry server.");
1233-
var containerRegistryAccessToken = GetContainerRegistryAccessToken(out errRecord);
1238+
var containerRegistryAccessToken = GetContainerRegistryAccessToken(needCatalogAccess: false, out errRecord);
12341239
if (errRecord != null)
12351240
{
12361241
return false;
@@ -1695,7 +1700,7 @@ private Hashtable[] FindPackagesWithVersionHelper(string packageName, VersionTyp
16951700
string packageNameLowercase = packageName.ToLower();
16961701

16971702
string packageNameForFind = PrependMARPrefix(packageNameLowercase);
1698-
string containerRegistryAccessToken = GetContainerRegistryAccessToken(out errRecord);
1703+
string containerRegistryAccessToken = GetContainerRegistryAccessToken(needCatalogAccess: false, out errRecord);
16991704
if (errRecord != null)
17001705
{
17011706
return emptyHashResponses;
@@ -1804,7 +1809,7 @@ private FindResults FindPackages(string packageName, bool includePrerelease, out
18041809
{
18051810
_cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::FindPackages()");
18061811
errRecord = null;
1807-
string containerRegistryAccessToken = GetContainerRegistryAccessToken(out errRecord);
1812+
string containerRegistryAccessToken = GetContainerRegistryAccessToken(needCatalogAccess: true, out errRecord);
18081813
if (errRecord != null)
18091814
{
18101815
return emptyResponseResults;

0 commit comments

Comments
 (0)