Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into MCRAuthChallenge
  • Loading branch information
adityapatwardhan committed Feb 19, 2025
2 parents 415c9f9 + 9aa8538 commit 54d9cf0
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 14 deletions.
92 changes: 79 additions & 13 deletions src/code/ContainerRegistryServerAPICalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ internal class ContainerRegistryServerAPICalls : ServerApiCall
const string containerRegistryStartUploadTemplate = "https://{0}/v2/{1}/blobs/uploads/"; // 0 - registry, 1 - packagename
const string containerRegistryEndUploadTemplate = "https://{0}{1}&digest=sha256:{2}"; // 0 - registry, 1 - location, 2 - digest
const string defaultScope = "repository:*:*";
const string containerRegistryRepositoryListTemplate = "https://{0}/v2/_catalog"; // 0 - registry

#endregion

Expand Down Expand Up @@ -77,13 +78,13 @@ public ContainerRegistryServerAPICalls(PSRepositoryInfo repository, PSCmdlet cmd
public override FindResults FindAll(bool includePrerelease, ResourceType type, out ErrorRecord errRecord)
{
_cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::FindAll()");
errRecord = new ErrorRecord(
new InvalidOperationException($"Find all is not supported for the ContainerRegistry server protocol repository '{Repository.Name}'"),
"FindAllFailure",
ErrorCategory.InvalidOperation,
this);
var findResult = FindPackages("*", includePrerelease, out errRecord);
if (errRecord != null)
{
return emptyResponseResults;
}

return emptyResponseResults;
return findResult;
}

/// <summary>
Expand Down Expand Up @@ -162,13 +163,13 @@ public override FindResults FindNameWithTag(string packageName, string[] tags, b
public override FindResults FindNameGlobbing(string packageName, bool includePrerelease, ResourceType type, out ErrorRecord errRecord)
{
_cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::FindNameGlobbing()");
errRecord = new ErrorRecord(
new InvalidOperationException($"FindNameGlobbing all is not supported for the ContainerRegistry server protocol repository '{Repository.Name}'"),
"FindNameGlobbingFailure",
ErrorCategory.InvalidOperation,
this);
var findResult = FindPackages(packageName, includePrerelease, out errRecord);
if (errRecord != null)
{
return emptyResponseResults;
}

return emptyResponseResults;
return findResult;
}

/// <summary>
Expand Down Expand Up @@ -669,6 +670,20 @@ internal JObject FindContainerRegistryImageTags(string packageName, string versi
return GetHttpResponseJObjectUsingDefaultHeaders(findImageUrl, HttpMethod.Get, defaultHeaders, out errRecord);
}

/// <summary>
/// Helper method to find all packages on container registry
/// </summary>
/// <param name="containerRegistryAccessToken"></param>
/// <param name="errRecord"></param>
/// <returns></returns>
internal JObject FindAllRepositories(string containerRegistryAccessToken, out ErrorRecord errRecord)
{
_cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::FindAllRepositories()");
string repositoryListUrl = string.Format(containerRegistryRepositoryListTemplate, Registry);
var defaultHeaders = GetDefaultHeaders(containerRegistryAccessToken);
return GetHttpResponseJObjectUsingDefaultHeaders(repositoryListUrl, HttpMethod.Get, defaultHeaders, out errRecord);
}

/// <summary>
/// Get metadata for a package version.
/// </summary>
Expand Down Expand Up @@ -1783,12 +1798,63 @@ private string PrependMARPrefix(string packageName)

// If the repostitory is MAR and its not a wildcard search, we need to prefix the package name with MAR prefix.
string updatedPackageName = Repository.IsMARRepository() && packageName.Trim() != "*"
? string.Concat(prefix, packageName)
? packageName.StartsWith(prefix) ? packageName : string.Concat(prefix, packageName)
: packageName;

return updatedPackageName;
}

private FindResults FindPackages(string packageName, bool includePrerelease, out ErrorRecord errRecord)
{
_cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::FindPackages()");
errRecord = null;
string containerRegistryAccessToken = GetContainerRegistryAccessToken(out errRecord);
if (errRecord != null)
{
return emptyResponseResults;
}

var pkgResult = FindAllRepositories(containerRegistryAccessToken, out errRecord);
if (errRecord != null)
{
return emptyResponseResults;
}

List<Hashtable> repositoriesList = new List<Hashtable>();
var isMAR = Repository.IsMARRepository();

// Convert the list of repositories to a list of hashtables
foreach (var repository in pkgResult["repositories"].ToList())
{
string repositoryName = repository.ToString();

if (isMAR && !repositoryName.StartsWith(PSRepositoryInfo.MARPrefix))
{
continue;
}

// This remove the 'psresource/' prefix from the repository name for comparison with wildcard.
string moduleName = repositoryName.Substring(11);

WildcardPattern wildcardPattern = new WildcardPattern(packageName, WildcardOptions.IgnoreCase);

if (!wildcardPattern.IsMatch(moduleName))
{
continue;
}

_cmdletPassedIn.WriteDebug($"Found repository: {repositoryName}");

repositoriesList.AddRange(FindPackagesWithVersionHelper(repositoryName, VersionType.VersionRange, versionRange: VersionRange.All, requiredVersion: null, includePrerelease, getOnlyLatest: true, out errRecord));
if (errRecord != null)
{
return emptyResponseResults;
}
}

return new FindResults(stringResponse: new string[] { }, hashtableResponse: repositoriesList.ToArray(), responseType: containerRegistryFindResponseType);
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,26 @@ Describe 'Test Find-PSResource for MAR Repository' -tags 'CI' {
It "Should find resource given specific Name, Version null" {
$res = Find-PSResource -Name "Az.Accounts" -Repository "MAR"
$res.Name | Should -Be "Az.Accounts"
$res.Version | Should -Be "4.0.0"
$res.Version | Should -BeGreaterThan ([Version]"4.0.0")
}

It "Should find resource and its dependency given specific Name and Version" {
$res = Find-PSResource -Name "Az.Storage" -Version "8.0.0" -Repository "MAR"
$res.Dependencies.Length | Should -Be 1
$res.Dependencies[0].Name | Should -Be "Az.Accounts"
}

It "Should find resource with wildcard in Name" {
$res = Find-PSResource -Name "Az.App*" -Repository "MAR"
$res | Should -Not -BeNullOrEmpty
$res.Count | Should -BeGreaterThan 1
}

It "Should find all resource with wildcard in Name" {
$res = Find-PSResource -Name "*" -Repository "MAR"
$res | Should -Not -BeNullOrEmpty
$res.Count | Should -BeGreaterThan 1
}
}

Describe 'Test Find-PSResource for unauthenticated ACR repository' -tags 'CI' {
Expand Down

0 comments on commit 54d9cf0

Please # to comment.