Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Part 2 of ACR work: rough prototype of find name #1497

Merged
merged 1 commit into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 88 additions & 11 deletions src/code/ACRServerAPICalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,53 @@ public override FindResults FindCommandOrDscResource(string[] tags, bool include
/// </summary>
public override FindResults FindName(string packageName, bool includePrerelease, ResourceType type, out ErrorRecord errRecord)
{
errRecord = null;
_cmdletPassedIn.WriteDebug("In ACRServerAPICalls::FindName()");
errRecord = new ErrorRecord(
new InvalidOperationException($"Find name is not supported for the ACR server protocol repository '{Repository.Name}'"),
"FindNameFailure",
ErrorCategory.InvalidOperation,
this);
string accessToken = string.Empty;
string tenantID = string.Empty;

// Need to set up secret management vault before hand
var repositoryCredentialInfo = Repository.CredentialInfo;
if (repositoryCredentialInfo != null)
{
accessToken = Utils.GetACRAccessTokenFromSecretManagement(
Repository.Name,
repositoryCredentialInfo,
_cmdletPassedIn);

_cmdletPassedIn.WriteVerbose("Access token retrieved.");

tenantID = Utils.GetSecretInfoFromSecretManagement(
Repository.Name,
repositoryCredentialInfo,
_cmdletPassedIn);
}

// Call asynchronous network methods in a try/catch block to handle exceptions.
string registry = Repository.Uri.Host;

_cmdletPassedIn.WriteVerbose("Getting acr refresh token");
var acrRefreshToken = GetAcrRefreshTokenAsync(registry, tenantID, accessToken).Result;
_cmdletPassedIn.WriteVerbose("Getting acr access token");
var acrAccessToken = GetAcrAccessTokenAsync(registry, acrRefreshToken).Result;

_cmdletPassedIn.WriteVerbose("Getting tags");
var foundTags = FindAcrImageTags(registry, packageName, "*", acrAccessToken).Result;
Console.WriteLine(foundTags.ToString(Formatting.None));

if (foundTags != null)
{
foreach (var item in foundTags["tags"])
{
// digest: {item["digest"]";
string tagVersion = item["name"].ToString();
Console.WriteLine("tag version: " + tagVersion);

/*
foundPkgs.Add(new PSResourceInfo(name: pkgName, version: tagVersion, repository: repo.Name));
*/
}
}

return new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: emptyHashResponses, responseType: v3FindResponseType);
}
Expand Down Expand Up @@ -227,13 +268,49 @@ public override FindResults FindVersionGlobbing(string packageName, VersionRange
/// </summary>
public override FindResults FindVersion(string packageName, string version, ResourceType type, out ErrorRecord errRecord)
{

errRecord = null;
_cmdletPassedIn.WriteDebug("In ACRServerAPICalls::FindVersion()");
errRecord = new ErrorRecord(
new InvalidOperationException($"Find version is not supported for the ACR server protocol repository '{Repository.Name}'"),
"FindVersionFailure",
ErrorCategory.InvalidOperation,
this);
string accessToken = string.Empty;
string tenantID = string.Empty;

// Need to set up secret management vault before hand
var repositoryCredentialInfo = Repository.CredentialInfo;
if (repositoryCredentialInfo != null)
{
accessToken = Utils.GetACRAccessTokenFromSecretManagement(
Repository.Name,
repositoryCredentialInfo,
_cmdletPassedIn);

_cmdletPassedIn.WriteVerbose("Access token retrieved.");

tenantID = Utils.GetSecretInfoFromSecretManagement(
Repository.Name,
repositoryCredentialInfo,
_cmdletPassedIn);
}

// Call asynchronous network methods in a try/catch block to handle exceptions.
string registry = Repository.Uri.Host;

_cmdletPassedIn.WriteVerbose("Getting acr refresh token");
var acrRefreshToken = GetAcrRefreshTokenAsync(registry, tenantID, accessToken).Result;
_cmdletPassedIn.WriteVerbose("Getting acr access token");
var acrAccessToken = GetAcrAccessTokenAsync(registry, acrRefreshToken).Result;

_cmdletPassedIn.WriteVerbose("Getting tags");
var foundTags = FindAcrImageTags(registry, packageName, version, acrAccessToken).Result;

if (foundTags != null)
{
var digest = foundTags["tag"]["digest"];
Console.WriteLine("digest: " + digest);
// pkgVersion was used in the API call (same as foundTags["name"])
// digest: foundTags["tag"]["digest"]";
/*
foundPkgs.Add(new PSResourceInfo(name: pkgName, version: pkgVersion, repository: repo.Name));
*/
}

return new FindResults(stringResponse: Utils.EmptyStrArray, hashtableResponse: emptyHashResponses, responseType: v3FindResponseType);
}
Expand Down
3 changes: 2 additions & 1 deletion src/code/PSRepositoryInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public enum APIVersion
v2,
v3,
local,
nugetServer
nugetServer,
acr
}

#endregion
Expand Down
4 changes: 4 additions & 0 deletions src/code/RepositorySettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,10 @@ private static PSRepositoryInfo.APIVersion GetRepoAPIVersion(Uri repoUri)
// repositories with Uri Scheme "temp" may have PSPath Uri's like: "Temp:\repo" and we should consider them as local repositories.
return PSRepositoryInfo.APIVersion.local;
}
else if (repoUri.AbsoluteUri.EndsWith(".azurecr.io") || repoUri.AbsoluteUri.EndsWith(".azurecr.io/"))
{
return PSRepositoryInfo.APIVersion.acr;
}
else
{
return PSRepositoryInfo.APIVersion.unknown;
Expand Down
4 changes: 4 additions & 0 deletions src/code/ResponseUtilFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public static ResponseUtil GetResponseUtil(PSRepositoryInfo repository)
currentResponseUtil = new NuGetServerResponseUtil(repository);
break;

case PSRepositoryInfo.APIVersion.acr:
currentResponseUtil = new ACRResponseUtil(repository);
break;

case PSRepositoryInfo.APIVersion.unknown:
break;
}
Expand Down
4 changes: 4 additions & 0 deletions src/code/ServerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public static ServerApiCall GetServer(PSRepositoryInfo repository, PSCmdlet cmdl
currentServer = new NuGetServerAPICalls(repository, cmdletPassedIn, networkCredential, userAgentString);
break;

case PSRepositoryInfo.APIVersion.acr:
currentServer = new ACRServerAPICalls(repository, cmdletPassedIn, networkCredential, userAgentString);
break;

case PSRepositoryInfo.APIVersion.unknown:
break;
}
Expand Down
5 changes: 5 additions & 0 deletions src/code/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,11 @@ public static string GetACRAccessTokenFromSecretManagement(
string password = new NetworkCredential(string.Empty, secretSecureString).Password;
return password;
}
else if(secretValue is PSCredential psCredSecret)
{
string password = new NetworkCredential(string.Empty, psCredSecret.Password).Password;
return password;
}

cmdletPassedIn.ThrowTerminatingError(
new ErrorRecord(
Expand Down
Loading