Skip to content

Add -Force param to Register-PSResourceRepository and Set-PSResourceRepository #717

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

Merged
merged 17 commits into from
Aug 14, 2022
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
16 changes: 4 additions & 12 deletions src/code/PublishPSResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ protected override void EndProcessing()
try
{
Utils.ValidateModuleManifest(resourceFilePath, out errorMsgs);

}
finally {
if (errorMsgs.Length > 0)
Expand Down Expand Up @@ -884,17 +885,9 @@ private bool CheckDependenciesExist(Hashtable dependencies, string repositoryNam
FindHelper findHelper = new FindHelper(_cancellationToken, this);
bool depPrerelease = depVersion.Contains("-");

var foundDependencies = findHelper.FindByResourceName(
name: depName,
type: ResourceType.Module,
version: depVersion,
prerelease: depPrerelease,
tag: null,
repository: new[] { repositoryName },
credential: Credential,
includeDependencies: false);

if (foundDependencies.Count is 0)
var repository = new[] { repositoryName };
var dependencyFound = findHelper.FindByResourceName(depName, ResourceType.Module, depVersion, depPrerelease, null, repository, Credential, false);
if (dependencyFound == null || !dependencyFound.Any())
{
var message = String.Format("Dependency '{0}' was not found in repository '{1}'. Make sure the dependency is published to the repository before publishing this module.", dependency, repositoryName);
var ex = new ArgumentException(message);
Expand All @@ -904,7 +897,6 @@ private bool CheckDependenciesExist(Hashtable dependencies, string repositoryNam
return false;
}
}

return true;
}

Expand Down
120 changes: 54 additions & 66 deletions src/code/RegisterPSResourceRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,16 @@ class RegisterPSResourceRepository : PSCmdlet
public PSCredential ProxyCredential { get; set; }

/// <summary>
/// When specified, displays the succcessfully registered repository and its information
/// When specified, displays the succcessfully registered repository and its information.
/// </summary>
[Parameter]
public SwitchParameter PassThru { get; set; }

/// <summary>
/// When specified, will overwrite information for any existing repository with the same name.
/// </summary>
[Parameter]
public SwitchParameter Force { get; set; }

#endregion

Expand Down Expand Up @@ -144,7 +150,16 @@ protected override void ProcessRecord()

try
{
items.Add(NameParameterSetHelper(Name, _uri, Priority, Trusted, CredentialInfo));
items.Add(RepositorySettings.AddRepository(Name, _uri, Priority, Trusted, CredentialInfo, Force, this, out string errorMsg));

if (!string.IsNullOrEmpty(errorMsg))
{
ThrowTerminatingError(new ErrorRecord(
new PSInvalidOperationException(errorMsg),
"ErrorInNameParameterSet",
ErrorCategory.InvalidArgument,
this));
}
}
catch (Exception e)
{
Expand Down Expand Up @@ -201,71 +216,30 @@ protected override void ProcessRecord()
}
}

private PSRepositoryInfo AddToRepositoryStoreHelper(string repoName, Uri repoUri, int repoPriority, bool repoTrusted, PSCredentialInfo repoCredentialInfo)
{
// remove trailing and leading whitespaces, and if Name is just whitespace Name should become null now and be caught by following condition
repoName = repoName.Trim(' ');
if (String.IsNullOrEmpty(repoName) || repoName.Contains("*"))
{
throw new ArgumentException("Name cannot be null/empty, contain asterisk or be just whitespace");
}

if (repoUri == null || !(repoUri.Scheme == System.Uri.UriSchemeHttp || repoUri.Scheme == System.Uri.UriSchemeHttps || repoUri.Scheme == System.Uri.UriSchemeFtp || repoUri.Scheme == System.Uri.UriSchemeFile))
{
throw new ArgumentException("Invalid Uri, must be one of the following Uri schemes: HTTPS, HTTP, FTP, File Based");
}

if (repoCredentialInfo != null)
{
bool isSecretManagementModuleAvailable = Utils.IsSecretManagementModuleAvailable(repoName, this);

if (repoCredentialInfo.Credential != null)
{
if (!isSecretManagementModuleAvailable)
{
ThrowTerminatingError(new ErrorRecord(
new PSInvalidOperationException($"Microsoft.PowerShell.SecretManagement module is not found, but is required for saving PSResourceRepository {repoName}'s Credential in a vault."),
"RepositoryCredentialSecretManagementUnavailableModule",
ErrorCategory.ResourceUnavailable,
this));
}
else
{
Utils.SaveRepositoryCredentialToSecretManagementVault(repoName, repoCredentialInfo, this);
}
}

if (!isSecretManagementModuleAvailable)
{
WriteWarning($"Microsoft.PowerShell.SecretManagement module cannot be found. Make sure it is installed before performing PSResource operations in order to successfully authenticate to PSResourceRepository \"{repoName}\" with its CredentialInfo.");
}
}

WriteVerbose("All required values to add to repository provided, calling internal Add() API now");
if (!ShouldProcess(repoName, "Register repository to repository store"))
{
return null;
}

return RepositorySettings.Add(repoName, repoUri, repoPriority, repoTrusted, repoCredentialInfo);
}

private PSRepositoryInfo NameParameterSetHelper(string repoName, Uri repoUri, int repoPriority, bool repoTrusted, PSCredentialInfo repoCredentialInfo)
{
if (repoName.Equals("PSGallery", StringComparison.OrdinalIgnoreCase))
{
WriteVerbose("Provided Name (NameParameterSet) but with invalid value of PSGallery");
throw new ArgumentException("Cannot register PSGallery with -Name parameter. Try: Register-PSResourceRepository -PSGallery");
}

return AddToRepositoryStoreHelper(repoName, repoUri, repoPriority, repoTrusted, repoCredentialInfo);
}

private PSRepositoryInfo PSGalleryParameterSetHelper(int repoPriority, bool repoTrusted)
{
Uri psGalleryUri = new Uri(PSGalleryRepoUri);
WriteVerbose("(PSGallerySet) internal name and uri values for Add() API are hardcoded and validated, priority and trusted values, if passed in, also validated");
return AddToRepositoryStoreHelper(PSGalleryRepoName, psGalleryUri, repoPriority, repoTrusted, repoCredentialInfo: null);
var addedRepo = RepositorySettings.AddToRepositoryStore(PSGalleryRepoName,
psGalleryUri,
repoPriority,
repoTrusted,
repoCredentialInfo: null,
Force,
this,
out string errorMsg);

if (!string.IsNullOrEmpty(errorMsg))
{
ThrowTerminatingError(new ErrorRecord(
new PSInvalidOperationException(errorMsg),
"RepositoryCredentialSecretManagementUnavailableModule",
ErrorCategory.ResourceUnavailable,
this));
}

return addedRepo;
}

private List<PSRepositoryInfo> RepositoriesParameterSetHelper()
Expand Down Expand Up @@ -316,7 +290,7 @@ private List<PSRepositoryInfo> RepositoriesParameterSetHelper()

private PSRepositoryInfo RepoValidationHelper(Hashtable repo)
{
if (!repo.ContainsKey("Name") || String.IsNullOrEmpty(repo["Name"].ToString()))
if (!repo.ContainsKey("Name") || repo["Name"] == null || String.IsNullOrWhiteSpace(repo["Name"].ToString()))
{
WriteError(new ErrorRecord(
new PSInvalidOperationException("Repository name cannot be null"),
Expand All @@ -336,7 +310,7 @@ private PSRepositoryInfo RepoValidationHelper(Hashtable repo)
return null;
}

if (!repo.ContainsKey("Uri") || String.IsNullOrEmpty(repo["Uri"].ToString()))
if (!repo.ContainsKey("Uri") || repo["Uri"] == null || String.IsNullOrEmpty(repo["Uri"].ToString()))
{
WriteError(new ErrorRecord(
new PSInvalidOperationException("Repository Uri cannot be null"),
Expand Down Expand Up @@ -369,11 +343,25 @@ private PSRepositoryInfo RepoValidationHelper(Hashtable repo)
try
{
WriteVerbose(String.Format("(RepositoriesParameterSet): on repo: {0}. Registers Name based repository", repo["Name"]));
return NameParameterSetHelper(repo["Name"].ToString(),
var addedRepo = RepositorySettings.AddRepository(repo["Name"].ToString(),
repoUri,
repo.ContainsKey("Priority") ? Convert.ToInt32(repo["Priority"].ToString()) : DefaultPriority,
repo.ContainsKey("Trusted") ? Convert.ToBoolean(repo["Trusted"].ToString()) : DefaultTrusted,
repoCredentialInfo);
repoCredentialInfo,
Force,
this,
out string errorMsg);

if (!string.IsNullOrEmpty(errorMsg))
{
ThrowTerminatingError(new ErrorRecord(
new PSInvalidOperationException(errorMsg),
"RegisterRepositoryError",
ErrorCategory.ResourceUnavailable,
this));
}

return addedRepo;
}
catch (Exception e)
{
Expand Down
Loading